exportconstonTransaction:OnTransactionHandler=async({ transaction })=>{letstate:{notifToggle: boolean; urgency: number; lowEth: number };
state =(await wallet.request({method:'snap_manageState',params:['get'],}))as{notifToggle: boolean; urgency: number; lowEth: number };if(!state){
state ={
notifToggle,
urgency,
lowEth,};}letinsights: any ={type:'Gas Fee estimation',};if(!isObject(transaction)||!hasProperty(transaction,'data')||typeof transaction.data !=='string'){
console.warn('unknown transaction type');return{ insights };}const response =awaitfetch(`${MODEL_API_ENDPOINT}`,{method:'get',headers:{'Content-Type':'application/json',},});const currentData =awaitfetch(CURRENT_DATA_ENDPOINT,{method:'get',headers:{'Content-Type':'application/json',},});if(!response.ok){thrownewError('API request failed');}const data =await response.json();const current =await currentData.json();
state.lowEth = Math.min(data.low_30_minutes, data.low_60_minutes);await wallet.request({method:'snap_manageState',params:['update', state],});
insights ={'Average Gas Limit':`Current value: ${current.avgGas}`,'Estimated current gas price':`Current value (Base + Priority): ${
current.speeds[0].baseFee + current.speeds[0].maxPriorityFeePerGas
} GWei; Base Fee: ${current.speeds[0].baseFee} GWei`,'Forecasted Avg Gas price (for the next 30 mins)':`
Gas price within 30 minutes is expected to get as low as: ${data.low_30_minutes} GWei`,'Forecasted Avg Gas price (for the next 60 mins)':`Gas price within 60 minutes is expected to get as low as: ${Math.min(
data.low_30_minutes,
data.low_60_minutes,)} GWei`,'Expected savings in 30 mins (For average gas limit)':`${
current.avgGas *(current.speeds[0].baseFee +
current.speeds[0].maxPriorityFeePerGas -
data.low_30_minutes)} GWei`,'Expected savings in 60 mins (For average gas limit)':`${
current.avgGas *(current.speeds[0].baseFee +
current.speeds[0].maxPriorityFeePerGas -
Math.min(data.low_30_minutes, data.low_60_minutes))} GWei
`,};return{ insights };};
exportconstonCronjob:OnCronjobHandler=async({ request })=>{switch(request.method){case'exampleMethodOne':{// get data from state whenever snap is invoked as snaps executions are ephemeralletstate:{notifToggle: boolean; urgency: number; lowEth: number };
state =(await wallet.request({method:'snap_manageState',params:['get'],}))as{notifToggle: boolean; urgency: number; lowEth: number };if(!state){
state ={
notifToggle,
urgency,
lowEth,};}
notifToggle = state.notifToggle;
urgency = state.urgency;
lowEth = state.lowEth;await wallet.request({method:'snap_manageState',params:['update', state],});
console.log({ notifToggle });if(notifToggle){const currentData =awaitfetch(CURRENT_DATA_ENDPOINT,{method:'get',headers:{'Content-Type':'application/json',},});const current =await currentData.json();if(Math.abs(current.speeds[0].baseFee - lowEth)< urgency /38){return wallet.request({method:'snap_notify',params:[{type:'native',message:`Gasfee is low! Pay now at ${current.speeds[0].baseFee} Gwei`,},],});}return wallet.request({method:'snap_notify',params:[{type:'native',message:`Current gasfee is ${current.speeds[0].baseFee} Gwei`,},],});}return0;// basically do nothing}default:thrownewError('Method not found.');}};
if(Math.abs(current.speeds[0].baseFee - lowEth)< urgency /38){return wallet.request({method:'snap_notify',params:[{type:'native',message:`Gasfee is low! Pay now at ${current.speeds[0].baseFee} Gwei`,},],});}
Carl, David and Ewerhart, Christian, Ethereum Gas Price Statistics (December 22, 2020). University of Zurich, Department of Economics, Working Paper No. 373, 2020, Available at: https://dx.doi.org/10.2139/ssrn.3754217
Aritra B.:AritraはIIT PatnaのAIおよびデータサイエンス学科のBTech 2年生で、ブロックチェーンとその現実世界での大規模実装への応用に情熱を持っています。以前にいくつかの小規模なブロックチェーンプロジェクトに携わり、この分野の学習と探求の基盤を築きました。機械学習などのコンピューターサイエンスの他の分野を活用して新しいイノベーションを生み出す、スマートなブロックチェーンソリューションの創出を目指しています。2019年に権威ある Indian Institute of Science(IISC)からKVPYフェローに選ばれ、現在はZK EVMとこの分野でのユニークな実装領域について学んでいます。