@swapkit/toolbox-evm
Advanced tools
Comparing version 0.0.0-nightly-20241007090400 to 0.0.0-nightly-20241011172732
@@ -1,3 +0,3 @@ | ||
import{AbstractSigner as Oo,BrowserProvider as ny,HDNodeWallet as ty,JsonRpcProvider as oy,Signature as yy,Transaction as py}from"ethers";import{ChainIdToChain as Cn,RequestClient as Dn,formatBigIntToSafeValue as Hn}from"@swapkit/helpers";var M=({apiKey:n,chainId:t})=>({getBalance:async(y)=>{const{data:o}=await Dn.get(`https://api.covalenthq.com/v1/${t}/address/${y}/balances_v2/`,{searchParams:{key:n}});return(o?.items||[]).filter(({is_spam:p})=>!p).map(({balance:p,contract_decimals:T,contract_ticker_symbol:e,contract_address:u,native_token:i})=>({value:Hn({value:BigInt(p),decimal:T,bigIntDecimal:T}),decimal:T,chain:Cn[t],symbol:`${e||"Unknown"}${i?"":`-${u}`}`}))}});import{Chain as Gn,RequestClient as zn,formatBigIntToSafeValue as In}from"@swapkit/helpers";var En="https://api.ethplorer.io",d=(n="freekey")=>({getBalance:async(t)=>{const{tokens:y=[]}=await zn.get(`${En}/getAddressInfo/${t}`,{searchParams:{apiKey:n}});return y.filter(({tokenInfo:{symbol:o},rawBalance:p})=>o&&p!=="0").map(({tokenInfo:{symbol:o,decimals:p,address:T},rawBalance:e})=>({chain:Gn.Ethereum,symbol:T?`${o}-${T}`:o,value:In({value:BigInt(e),decimal:Number.parseInt(p),bigIntDecimal:Number.parseInt(p)}),decimal:Number.parseInt(p)}))}});import{AssetValue as D,BaseDecimal as nn,FeeOption as tn,SwapKitNumber as P,filterAssets as Sn,formatBigIntToSafeValue as kn,isGasAsset as Ln}from"@swapkit/helpers";var Xt=async({toolbox:n,from:t,memo:y="",feeOptionKey:o=tn.Fastest,assetValue:p,abi:T,funcName:e,funcParams:u,contractAddress:i,txOverrides:m})=>{const l=(await n.getBalance(t)).find(({symbol:w,chain:x})=>p?w===p.symbol:w===D.from({chain:x})?.symbol),B=(await n.estimateGasPrices())[o];if(!l)return D.from({chain:p.chain});if(p&&(l.chain!==p.chain||l.symbol!==p?.symbol))return l;const R=T&&e&&u&&i?await n.estimateCall({contractAddress:i,abi:T,funcName:e,funcParams:u,txOverrides:m}):await n.estimateGasLimit({from:t,recipient:t,memo:y,assetValue:p}),c="maxFeePerGas"in B;if(!(("gasPrice"in B)||c))throw new Error("Could not fetch fee data");const U=R*(c?(B.maxFeePerGas||1n)+(B.maxPriorityFeePerGas||1n):B.gasPrice),g=P.fromBigInt(l.getBaseValue("bigint")).sub(U.toString());return D.from({chain:l.chain,value:g.getValue("string")})},S=(n)=>n>0n?`0x${n.toString(16)}`:"0x0",r=async({provider:n,api:t,address:y,chain:o,potentialScamFilter:p})=>{const T=await t.getBalance(y),e=await n.getBalance(y),u=[{chain:o,symbol:D.from({chain:o}).symbol,value:kn({value:BigInt(e),decimal:18,bigIntDecimal:18}),decimal:nn[o]},...T.filter((m)=>!Ln(m))];return(p?Sn(u):u).map(({symbol:m,value:l,decimal:B})=>new D({decimal:B||nn[o],value:l,identifier:`${o}.${m}`}))},J=async(n,t=tn.Fast,y,o,p=!0)=>{const T=(await z(o,p))[t],e=await o.estimateGas(n),u=D.from({chain:y});if(!p&&T.gasPrice)return u.set(P.fromBigInt(T.gasPrice*e,u.decimal));if(T.maxFeePerGas&&T.maxPriorityFeePerGas)return u.set(P.fromBigInt((T.maxFeePerGas+T.maxPriorityFeePerGas)*e,u.decimal));throw new Error("No gas price found")};import{ChainToRPC as Wn}from"@swapkit/helpers";import{JsonRpcProvider as Qn}from"ethers/providers";var Ft=(n,t)=>{return new Qn(t||Wn[n])};import{Chain as q,ContractAddress as $,FeeOption as b,SwapKitError as a,SwapKitNumber as on,erc20ABI as H,isGasAsset as Xn}from"@swapkit/helpers";import{BrowserProvider as Yn,Contract as Zn,Interface as Pn,hexlify as F,toUtf8Bytes as _}from"ethers";import{getAddress as k}from"ethers/address";import{MaxInt256 as Fn}from"ethers/constants";function nt(n){if(!n)throw new a("toolbox_evm_no_signer");return n.signMessage}var N=Fn,_n={[q.Arbitrum]:$.ARB,[q.Avalanche]:$.AVAX,[q.Base]:$.BASE,[q.BinanceSmartChain]:$.BSC,[q.Ethereum]:$.ETH,[q.Optimism]:$.OP,[q.Polygon]:$.MATIC},Nn=["payable","nonpayable"],jn=(n)=>n.type===2||!!n.maxFeePerGas||!!n.maxPriorityFeePerGas,L=(n)=>n instanceof Yn,W=(n,t,y)=>{return new Zn(n,Pn.from(t),y)},yn=(n)=>{try{return k(n),!0}catch(t){return!1}},An=(n,t)=>{const y=n.find((o)=>o.name===t);if(!y)throw new a("toolbox_evm_no_abi_fragment",{functionName:t});return y.stateMutability&&Nn.includes(y.stateMutability)},Q=async(n,t,{callProvider:y,signer:o,contractAddress:p,abi:T,funcName:e,funcParams:u=[],txOverrides:i,feeOption:m=b.Fast})=>{const l=y||n;if(!p)throw new Error("contractAddress must be provided");const B=An(T,e);if(B&&L(l)&&o){const f=await I(l,{contractAddress:p,abi:T,funcName:e,funcParams:u,txOverrides:i});return X(l,f)}const R=W(p,T,l);if(B){if(!o)throw new a("toolbox_evm_no_signer");const f=i?.from||await o.getAddress();if(!f)throw new a("toolbox_evm_no_signer_address");const U=R.connect(o),{maxFeePerGas:g,maxPriorityFeePerGas:w,gasPrice:x}=(await z(n,t))[m],G=await R.getFunction(e).estimateGas(...u,i),E=await U[e](...u,{...i,gasLimit:G,maxFeePerGas:g,maxPriorityFeePerGas:w,gasPrice:x,nonce:i?.nonce||await l.getTransactionCount(f)});return typeof E?.hash==="string"?E?.hash:E}const c=await R[e]?.(...u);return typeof c?.hash==="string"?c?.hash:c},I=async(n,{contractAddress:t,abi:y,funcName:o,funcParams:p=[],txOverrides:T})=>W(t,y,n).getFunction(o).populateTransaction(...p.concat(T).filter((e)=>typeof e!=="undefined")),pn=async(n,{assetAddress:t,spenderAddress:y,from:o})=>await Q(n,!0,{contractAddress:t,abi:H,funcName:"allowance",funcParams:[o,y]}),Kn=async(n,{assetAddress:t,spenderAddress:y,from:o,amount:p=N})=>{return on.fromBigInt(await pn(n,{assetAddress:t,spenderAddress:y,from:o})).gte(on.fromBigInt(BigInt(p)))},Vn=async(n,{assetAddress:t,spenderAddress:y,feeOptionKey:o=b.Fast,amount:p,gasLimitFallback:T,from:e,nonce:u},i,m=!0)=>{const l=[y,BigInt(p||N)],R={contractAddress:t,abi:H,funcName:"approve",funcParams:l,signer:i,txOverrides:{from:e}};if(L(n))return X(n,await I(n,R));return Q(n,m,{...R,funcParams:l,txOverrides:{from:e,nonce:u,gasLimit:T?BigInt(T.toString()):void 0},feeOption:o})},vn=async(n,{assetValue:t,memo:y,recipient:o,feeOptionKey:p=b.Fast,data:T,from:e,maxFeePerGas:u,maxPriorityFeePerGas:i,gasPrice:m,...l},B,R=!0)=>{const c=t.getBaseValue("bigint"),f=t.chain,U=e||await B?.getAddress();if(!U)throw new a("toolbox_evm_no_from_address");if(!t.isGasAsset){const w=Y(t,f);if(!w)throw new a("toolbox_evm_no_contract_address");return Q(n,R,{signer:B,contractAddress:w,abi:H,funcName:"transfer",funcParams:[o,c],txOverrides:{from:U,maxFeePerGas:u,maxPriorityFeePerGas:i,gasPrice:m},feeOption:p})}const g={...l,from:U,to:o,value:c,data:T||F(_(y||""))};return Tn(n,g,p,B,R)},z=async(n,t=!0)=>{try{const{maxFeePerGas:y,maxPriorityFeePerGas:o,gasPrice:p}=await n.getFeeData();if(t){if(y===null||o===null)throw new a("toolbox_evm_no_fee_data");return{[b.Average]:{maxFeePerGas:y,maxPriorityFeePerGas:o},[b.Fast]:{maxFeePerGas:y*15n/10n,maxPriorityFeePerGas:o*15n/10n},[b.Fastest]:{maxFeePerGas:y*2n,maxPriorityFeePerGas:o*2n}}}if(!p)throw new a("toolbox_evm_no_gas_price");return{[b.Average]:{gasPrice:p},[b.Fast]:{gasPrice:p*15n/10n},[b.Fastest]:{gasPrice:p*2n}}}catch(y){throw new Error(`Failed to estimate gas price: ${y.msg??y.toString()}`)}},en=(n,{signer:t,contractAddress:y,abi:o,funcName:p,funcParams:T=[],txOverrides:e})=>{if(!y)throw new a("toolbox_evm_no_contract_address");const u=W(y,o,n);return t?u.connect(t).getFunction(p).estimateGas(...T,e):u.getFunction(p).estimateGas(...T,e)},sn=(n,{assetValue:t,recipient:y,memo:o,from:p,funcName:T,funcParams:e,txOverrides:u,signer:i})=>{const m=t.bigIntValue,l=t.isGasAsset?null:Y(t,t.chain);if(l&&T)return en(n,{contractAddress:l,abi:H,funcName:T,funcParams:e,txOverrides:u,signer:i});return n.estimateGas({from:p,to:y,value:m,data:o?F(_(o)):void 0})},Tn=async(n,t,y=b.Fast,o,p=!0)=>{if(!o)throw new a("toolbox_evm_no_signer");const{from:T,to:e,data:u,value:i,...m}=t;if(!e)throw new a("toolbox_evm_no_to_address");const l={...m,data:u||"0x",to:e,from:T,value:BigInt(i||0)};if(L(n))return X(n,l);const B=T||await o.getAddress(),R=t.nonce||await n.getTransactionCount(B),c=(await n.getNetwork()).chainId,f=jn(l)||p,U=f&&!(l.maxFeePerGas&&l.maxPriorityFeePerGas)||!l.gasPrice?Object.entries((await z(n,p))[y]).reduce((w,[x,G])=>({...w,[x]:S(BigInt(G))}),{}):{};let g;try{g=S(l.gasLimit||await n.estimateGas(l)*11n/10n)}catch(w){throw new a("toolbox_evm_error_estimating_gas_limit",{error:w})}try{const w={...l,chainId:c,type:f?2:0,gasLimit:g,nonce:R,...U};try{return(await o.sendTransaction(w)).hash}catch(x){const G=await o.signTransaction({...w,from:B});return(await n.broadcastTransaction(G)).hash}}catch(w){throw new a("toolbox_evm_error_sending_transaction",{error:w})}},vt=(n)=>k(n),X=(n,{from:t,to:y,data:o,value:p})=>{if(!L(n))throw new a("toolbox_evm_provider_not_eip1193_compatible");return n.send("eth_sendTransaction",[{value:S(BigInt(p||0)),from:t,to:y,data:o}])},st=(n,t)=>{const y=Y(n,t);if(y)return k(y.toLowerCase());throw new a("toolbox_evm_invalid_gas_asset_address")},Y=({chain:n,symbol:t,ticker:y},o)=>{try{if(n===o&&t===o&&y===o||n===q.BinanceSmartChain&&t==="BNB"&&y==="BNB"||n===q.Arbitrum&&t==="ETH"&&y==="ETH")return _n[o];return k(t.slice(y.length+1).replace(/^0X/,""))}catch(p){return null}},On=async(n,{assetValue:t,memo:y,recipient:o,feeOptionKey:p=b.Fast,data:T,from:e,maxFeePerGas:u,maxPriorityFeePerGas:i,gasPrice:m,...l},B)=>{const R=t.getBaseValue("bigint"),c=t.chain,f=e||await B?.getAddress();if(!f)throw new a("toolbox_evm_no_from_address");if(!Xn(t)){const g=Y(t,c);if(!g)throw new a("toolbox_evm_no_contract_address");return I(n,{contractAddress:g,abi:H,funcName:"transfer",funcParams:[o,R],txOverrides:{from:f,maxFeePerGas:u,maxPriorityFeePerGas:i,gasPrice:m}})}return{...l,from:f,to:o,value:R,data:T||F(_(y||""))}},dn=async(n,{assetAddress:t,spenderAddress:y,amount:o,from:p},T)=>{const e=[y,BigInt(o||N)];return await I(n,{contractAddress:t,abi:H,funcName:"approve",funcParams:e,signer:T,txOverrides:{from:p}})},h=({provider:n,signer:t,isEIP1559Compatible:y=!0})=>({approve:(o)=>Vn(n,o,t,y),approvedAmount:(o)=>pn(n,o),broadcastTransaction:n.broadcastTransaction,call:(o)=>Q(n,y,{...o,signer:t}),createContract:(o,p,T)=>W(o,p,T||n),createContractTxObject:(o)=>I(n,o),EIP1193SendTransaction:(o)=>X(n,o),estimateCall:(o)=>en(n,{...o,signer:t}),estimateGasLimit:({assetValue:o,recipient:p,memo:T})=>sn(n,{assetValue:o,recipient:p,memo:T,signer:t}),estimateGasPrices:()=>z(n,y),isApproved:(o)=>Kn(n,o),sendTransaction:(o,p)=>Tn(n,o,p,t,y),transfer:(o)=>vn(n,o,t,y),validateAddress:yn,createTransferTx:(o)=>On(n,o,t),createApprovalTx:(o)=>dn(n,o,t),signMessage:nt(t)}),Ot=h,tt=({address:n})=>yn(n);var ot;((y)=>{y.Test="goerli";y.Main="homestead"})(ot||={});import{BaseDecimal as yt,Chain as j,ChainId as un,ChainToExplorerUrl as pt,FeeOption as Z,RPCUrl as et}from"@swapkit/helpers";var Tt=()=>({chainId:un.ArbitrumHex,chainName:"Arbitrum One",nativeCurrency:{name:"Ethereum",symbol:j.Ethereum,decimals:yt.ETH},rpcUrls:[et.Arbitrum],blockExplorerUrls:[pt[j.Arbitrum]]}),ut=async(n)=>{try{const{gasPrice:t}=await n.getFeeData();if(!t)throw new Error("No fee data available");return{[Z.Average]:{gasPrice:t},[Z.Fast]:{gasPrice:t},[Z.Fastest]:{gasPrice:t}}}catch(t){throw new Error(`Failed to estimate gas price: ${t.msg??t.toString()}`)}},ln=({api:n,provider:t,signer:y,covalentApiKey:o})=>{const p=n||M({apiKey:o,chainId:un.Arbitrum}),T=h({provider:t,signer:y,isEIP1559Compatible:!1}),e=j.Arbitrum;return{...T,getNetworkParams:Tt,estimateGasPrices:()=>ut(t),estimateTransactionFee:(u)=>J(u,Z.Average,e,t,!1),getBalance:(u,i=!0,m)=>r({provider:m||t,api:p,address:u,chain:e,potentialScamFilter:i})}};import{BaseDecimal as it,Chain as A,ChainId as mn,ChainToExplorerUrl as lt}from"@swapkit/helpers";var mt=()=>({chainId:mn.AvalancheHex,chainName:"Avalanche Network",nativeCurrency:{name:"Avalanche",symbol:A.Avalanche,decimals:it.AVAX},rpcUrls:["https://api.avax.network/ext/bc/C/rpc"],blockExplorerUrls:[lt[A.Avalanche]]}),Bn=({api:n,provider:t,signer:y,covalentApiKey:o})=>{const p=n||M({apiKey:o,chainId:mn.Avalanche}),T=h({provider:t,signer:y}),e=A.Avalanche;return{...T,getNetworkParams:mt,estimateTransactionFee:(u,i)=>J(u,i,e,t),getBalance:(u,i=!0,m)=>r({provider:m||t,api:p,address:u,chain:e,potentialScamFilter:i})}};import{BaseDecimal as Bt,Chain as an,ChainId as Rn,ChainToExplorerUrl as at}from"@swapkit/helpers";var Rt=()=>({chainId:Rn.BinanceSmartChainHex,chainName:"BNB Chain",nativeCurrency:{name:"Binance Coin",symbol:"BNB",decimals:Bt.BSC},rpcUrls:["https://bsc-dataseed.binance.org"],blockExplorerUrls:[at[an.BinanceSmartChain]]}),wn=({api:n,provider:t,signer:y,covalentApiKey:o})=>{const p=n||M({apiKey:o,chainId:Rn.BinanceSmartChain}),T=h({provider:t,signer:y,isEIP1559Compatible:!1}),e=an.BinanceSmartChain;return{...T,getNetworkParams:Rt,estimateTransactionFee:(u,i)=>J(u,i,e,t,!1),getBalance:(u,i=!0,m)=>r({provider:m||t,api:p,address:u,chain:e,potentialScamFilter:i})}};import{Chain as wt}from"@swapkit/helpers";var hn=({api:n,ethplorerApiKey:t,signer:y,provider:o})=>{const p=n||d(t),T=h({provider:o,signer:y}),e=wt.Ethereum;return{...T,estimateTransactionFee:(u,i)=>J(u,i,e,o),getBalance:(u,i=!0,m)=>r({provider:m||o,api:p,address:u,chain:e,potentialScamFilter:i})}};import{Chain as C}from"@swapkit/helpers";import{BaseDecimal as ht,Chain as K,ChainId as fn,ChainToExplorerUrl as ft,RPCUrl as rt}from"@swapkit/helpers";var ct=()=>({chainId:fn.BaseHex,chainName:"Base Mainnet",nativeCurrency:{name:"Ethereum",symbol:K.Ethereum,decimals:ht.ETH},rpcUrls:[rt.Base],blockExplorerUrls:[ft[K.Base]]}),rn=({api:n,provider:t,signer:y,covalentApiKey:o})=>{const p=h({provider:t,signer:y}),T=K.Base;return{...p,getNetworkParams:ct,estimateTransactionFee:(e,u)=>J(e,u,T,t),getBalance:async(e,u=!0,i)=>{return await r({provider:i||t,api:n||M({apiKey:o,chainId:fn.Base}),address:e,chain:T,potentialScamFilter:u})}}};import{BaseDecimal as Mt,Chain as V,ChainId as cn,ChainToExplorerUrl as Jt,RPCUrl as gt}from"@swapkit/helpers";var bt=()=>({chainId:cn.PolygonHex,chainName:"Polygon Mainnet",nativeCurrency:{name:"Polygon",symbol:V.Polygon,decimals:Mt.MATIC},rpcUrls:[gt.Polygon],blockExplorerUrls:[Jt[V.Polygon]]}),Mn=({api:n,provider:t,signer:y,covalentApiKey:o})=>{const p=n||M({apiKey:o,chainId:cn.Polygon}),T=h({provider:t,signer:y}),e=V.Polygon;return{...T,getNetworkParams:bt,estimateTransactionFee:(u,i)=>J(u,i,e,t),getBalance:(u,i=!0,m)=>r({provider:m||t,api:p,address:u,chain:e,potentialScamFilter:i})}};import{BaseDecimal as Ut,Chain as s,ChainId as gn,ChainToExplorerUrl as qt,FeeOption as v,RPCUrl as xt}from"@swapkit/helpers";var Jn=[{inputs:[{internalType:"address",name:"_owner",type:"address"}],stateMutability:"nonpayable",type:"constructor"},{anonymous:!1,inputs:[{indexed:!1,internalType:"uint256",name:"",type:"uint256"}],name:"DecimalsUpdated",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"uint256",name:"",type:"uint256"}],name:"GasPriceUpdated",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"uint256",name:"",type:"uint256"}],name:"L1BaseFeeUpdated",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"uint256",name:"",type:"uint256"}],name:"OverheadUpdated",type:"event"},{anonymous:!1,inputs:[{indexed:!0,internalType:"address",name:"previousOwner",type:"address"},{indexed:!0,internalType:"address",name:"newOwner",type:"address"}],name:"OwnershipTransferred",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"uint256",name:"",type:"uint256"}],name:"ScalarUpdated",type:"event"},{inputs:[],name:"decimals",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"gasPrice",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"bytes",name:"_data",type:"bytes"}],name:"getL1Fee",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"bytes",name:"_data",type:"bytes"}],name:"getL1GasUsed",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"l1BaseFee",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"overhead",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"owner",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function"},{inputs:[],name:"renounceOwnership",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[],name:"scalar",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"uint256",name:"_decimals",type:"uint256"}],name:"setDecimals",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint256",name:"_gasPrice",type:"uint256"}],name:"setGasPrice",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint256",name:"_baseFee",type:"uint256"}],name:"setL1BaseFee",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint256",name:"_overhead",type:"uint256"}],name:"setOverhead",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint256",name:"_scalar",type:"uint256"}],name:"setScalar",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"newOwner",type:"address"}],name:"transferOwnership",outputs:[],stateMutability:"nonpayable",type:"function"}];import{Contract as $t,Transaction as Ct}from"ethers";var Dt="0x420000000000000000000000000000000000000f",O=(n)=>{return new $t(Dt,Jn,n)},bn=(n)=>{const t=O(n);if(t&&"l1BaseFee"in t)return t?.l1BaseFee();return},Un=async(n,{data:t,from:y,to:o,gasPrice:p,type:T,gasLimit:e,nonce:u})=>{if(!o)throw new Error("Missing to address");return Ct.from({data:t,to:o,gasPrice:p,type:T,gasLimit:e,nonce:u?u:y?await n.getTransactionCount(y):0}).serialized},qn=async(n,t)=>{const y=await O(n),o=await Un(n,t);if(y&&"getL1Fee"in y)return y.getL1Fee(o)},xn=async(n,t)=>{const y=await n.send("eth_gasPrice",[]),o=await n.estimateGas(t);return y.mul(o)},Ht=async(n,t)=>{const y=await qn(n,t),o=await xn(n,t);return y.add(o)},Gt=async(n,t)=>{const y=O(n),o=await Un(n,t);if(y&&"getL1GasUsed"in y)return y.getL1GasUsed(o)},zt=()=>({chainId:gn.OptimismHex,chainName:"Optimism",nativeCurrency:{name:"Ethereum",symbol:s.Ethereum,decimals:Ut.ETH},rpcUrls:[xt.Optimism],blockExplorerUrls:[qt[s.Optimism]]}),It=async(n)=>{try{const{maxFeePerGas:t,maxPriorityFeePerGas:y,gasPrice:o}=await n.getFeeData(),p=await bn(n),T=o;if(!(t&&y))throw new Error("No fee data available");return{[v.Average]:{l1GasPrice:p,gasPrice:o,maxFeePerGas:t,maxPriorityFeePerGas:y},[v.Fast]:{l1GasPrice:(p||0n)*15n/10n,gasPrice:T*15n/10n,maxFeePerGas:t,maxPriorityFeePerGas:y*15n/10n},[v.Fastest]:{l1GasPrice:(p||0n)*2n,gasPrice:T*2n,maxFeePerGas:t,maxPriorityFeePerGas:y*2n}}}catch(t){throw new Error(`Failed to estimate gas price: ${t.msg??t.toString()}`)}},$n=({api:n,provider:t,signer:y,covalentApiKey:o})=>{const p=n||M({apiKey:o,chainId:gn.Optimism});return{...h({provider:t,signer:y}),estimateTotalGasCost:(e)=>Ht(t,e),estimateL1GasCost:(e)=>qn(t,e),estimateL2GasCost:(e)=>xn(t,e),getL1GasPrice:()=>bn(t),estimateL1Gas:(e)=>Gt(t,e),getNetworkParams:zt,estimateGasPrices:()=>It(t),getBalance:(e,u=!0,i)=>r({provider:i||t,api:p,address:e,chain:s.Optimism,potentialScamFilter:u})}};var Ao=(n)=>{switch(n){case C.Avalanche:return Bn;case C.Arbitrum:return ln;case C.Base:return rn;case C.Optimism:return $n;case C.Polygon:return Mn;case C.BinanceSmartChain:return wn;case C.Ethereum:return hn;default:throw new Error(`Chain ${n} is not supported`)}};export{S as toHexString,vt as toChecksumAddress,An as isStateChangingCall,L as isBrowserProvider,Ao as getToolboxByChain,Y as getTokenAddress,Ft as getProvider,bn as getL1GasPrice,st as getChecksumAddressFromAsset,r as getBalance,tt as evmValidateAddress,d as ethplorerApi,J as estimateTransactionFee,Ht as estimateTotalGasCost,Xt as estimateMaxSendableAmount,xn as estimateL2GasCost,qn as estimateL1GasCost,Gt as estimateL1Gas,z as estimateGasPrices,I as createContractTxObject,W as createContract,M as covalentApi,O as connectGasPriceOracle,py as Transaction,yy as Signature,$n as OPToolbox,N as MAX_APPROVAL,Mn as MATICToolbox,oy as JsonRpcProvider,ty as HDNodeWallet,ot as EthNetwork,h as EVMToolbox,hn as ETHToolbox,X as EIP1193SendTransaction,ny as BrowserProvider,Ot as BaseEVMToolbox,wn as BSCToolbox,rn as BASEToolbox,Oo as AbstractSigner,Bn as AVAXToolbox,ln as ARBToolbox}; | ||
import{AbstractSigner as ny,BrowserProvider as ty,HDNodeWallet as oy,JsonRpcProvider as yy,Signature as py,Transaction as ey}from"ethers";import{ChainIdToChain as Dn,RequestClient as Hn,formatBigIntToSafeValue as Gn}from"@swapkit/helpers";var J=({apiKey:n,chainId:t})=>({getBalance:async(y)=>{const{data:o}=await Hn.get(`https://api.covalenthq.com/v1/${t}/address/${y}/balances_v2/`,{searchParams:{key:n}});return(o?.items||[]).filter(({is_spam:p})=>!p).map(({balance:p,contract_decimals:T,contract_ticker_symbol:e,contract_address:u,native_token:i})=>({value:Gn({value:BigInt(p),decimal:T,bigIntDecimal:T}),decimal:T,chain:Dn[t],symbol:`${e||"Unknown"}${i?"":`-${u}`}`}))}});import{Chain as zn,RequestClient as In,formatBigIntToSafeValue as En}from"@swapkit/helpers";var Sn="https://api.ethplorer.io",nn=(n="freekey")=>({getBalance:async(t)=>{const{tokens:y=[]}=await In.get(`${Sn}/getAddressInfo/${t}`,{searchParams:{apiKey:n}});return y.filter(({tokenInfo:{symbol:o},rawBalance:p})=>o&&p!=="0").map(({tokenInfo:{symbol:o,decimals:p,address:T},rawBalance:e})=>({chain:zn.Ethereum,symbol:T?`${o}-${T}`:o,value:En({value:BigInt(e),decimal:Number.parseInt(p),bigIntDecimal:Number.parseInt(p)}),decimal:Number.parseInt(p)}))}});import{AssetValue as D,BaseDecimal as tn,FeeOption as on,SwapKitNumber as P,filterAssets as kn,formatBigIntToSafeValue as Ln,isGasAsset as Wn}from"@swapkit/helpers";var Yt=async({toolbox:n,from:t,memo:y="",feeOptionKey:o=on.Fastest,assetValue:p,abi:T,funcName:e,funcParams:u,contractAddress:i,txOverrides:m})=>{const l=(await n.getBalance(t)).find(({symbol:a,chain:$})=>p?a===p.symbol:a===D.from({chain:$})?.symbol),B=(await n.estimateGasPrices())[o];if(!l)return D.from({chain:p.chain});if(p&&(l.chain!==p.chain||l.symbol!==p?.symbol))return l;const h=T&&e&&u&&i?await n.estimateCall({contractAddress:i,abi:T,funcName:e,funcParams:u,txOverrides:m}):await n.estimateGasLimit({from:t,recipient:t,memo:y,assetValue:p}),c="maxFeePerGas"in B;if(!(("gasPrice"in B)||c))throw new Error("Could not fetch fee data");const r=h*(c?(B.maxFeePerGas||1n)+(B.maxPriorityFeePerGas||1n):B.gasPrice),b=P.fromBigInt(l.getBaseValue("bigint")).sub(r.toString());return D.from({chain:l.chain,value:b.getValue("string")})},E=(n)=>n>0n?`0x${n.toString(16)}`:"0x0",f=async({provider:n,api:t,address:y,chain:o,potentialScamFilter:p})=>{const T=await t.getBalance(y),e=await n.getBalance(y),u=[{chain:o,symbol:D.from({chain:o}).symbol,value:Ln({value:BigInt(e),decimal:18,bigIntDecimal:18}),decimal:tn[o]},...T.filter((m)=>!Wn(m))];return(p?kn(u):u).map(({symbol:m,value:l,decimal:B})=>new D({decimal:B||tn[o],value:l,identifier:`${o}.${m}`}))},g=async(n,t=on.Fast,y,o,p=!0)=>{const T=(await z(o,p))[t],e=await o.estimateGas(n),u=D.from({chain:y});if(!p&&T.gasPrice)return u.set(P.fromBigInt(T.gasPrice*e,u.decimal));if(T.maxFeePerGas&&T.maxPriorityFeePerGas)return u.set(P.fromBigInt((T.maxFeePerGas+T.maxPriorityFeePerGas)*e,u.decimal));throw new Error("No gas price found")};import{ChainToRPC as Qn}from"@swapkit/helpers";import{JsonRpcProvider as Xn}from"ethers/providers";var _t=(n,t)=>{return new Xn(t||Qn[n])};import{Chain as q,ContractAddress as x,FeeOption as U,SwapKitError as R,SwapKitNumber as yn,erc20ABI as H,isGasAsset as Yn}from"@swapkit/helpers";import{BrowserProvider as Zn,Contract as Pn,Interface as Fn,hexlify as F,toUtf8Bytes as _}from"ethers";import{getAddress as S}from"ethers/address";import{MaxInt256 as _n}from"ethers/constants";function tt(n){if(!n)throw new R("toolbox_evm_no_signer");return n.signMessage}var N=_n,Nn={[q.Arbitrum]:x.ARB,[q.Avalanche]:x.AVAX,[q.Base]:x.BASE,[q.BinanceSmartChain]:x.BSC,[q.Ethereum]:x.ETH,[q.Optimism]:x.OP,[q.Polygon]:x.MATIC},jn=["payable","nonpayable"],An=(n)=>n.type===2||!!n.maxFeePerGas||!!n.maxPriorityFeePerGas,k=(n)=>n instanceof Zn,L=(n,t,y)=>{return new Pn(n,Fn.from(t),y)},pn=(n)=>{try{return S(n),!0}catch(t){return!1}},Kn=(n,t)=>{const y=n.find((o)=>o.name===t);if(!y)throw new R("toolbox_evm_no_abi_fragment",{functionName:t});return y.stateMutability&&jn.includes(y.stateMutability)},W=async(n,t,{callProvider:y,signer:o,contractAddress:p,abi:T,funcName:e,funcParams:u=[],txOverrides:i={},feeOption:m=U.Fast})=>{const l=y||n;if(!p)throw new Error("contractAddress must be provided");const B=Kn(T,e),h={...i,from:i?.from||await o?.getAddress()};if(B&&k(l)&&o){const r=await I(l,{contractAddress:p,abi:T,funcName:e,funcParams:u,txOverrides:h});return Q(l,r)}const c=L(p,T,l);if(B){if(!o)throw new R("toolbox_evm_no_signer");const r=i?.from||await o.getAddress();if(!r)throw new R("toolbox_evm_no_signer_address");const b=c.connect(o),{maxFeePerGas:a,maxPriorityFeePerGas:$,gasPrice:G}=(await z(n,t))[m],d=await c.getFunction(e).estimateGas(...u,i),Z=await b[e](...u,{...i,gasLimit:d,maxFeePerGas:a,maxPriorityFeePerGas:$,gasPrice:G,nonce:i?.nonce||await l.getTransactionCount(r)});return typeof Z?.hash==="string"?Z?.hash:Z}const M=await c[e]?.(...u);return typeof M?.hash==="string"?M?.hash:M},I=async(n,{contractAddress:t,abi:y,funcName:o,funcParams:p=[],txOverrides:T})=>L(t,y,n).getFunction(o).populateTransaction(...p.concat(T).filter((e)=>typeof e!=="undefined")),en=async(n,{assetAddress:t,spenderAddress:y,from:o})=>await W(n,!0,{contractAddress:t,abi:H,funcName:"allowance",funcParams:[o,y]}),Vn=async(n,{assetAddress:t,spenderAddress:y,from:o,amount:p=N})=>{return yn.fromBigInt(await en(n,{assetAddress:t,spenderAddress:y,from:o})).gte(yn.fromBigInt(BigInt(p)))},vn=async(n,{assetAddress:t,spenderAddress:y,feeOptionKey:o=U.Fast,amount:p,gasLimitFallback:T,from:e,nonce:u},i,m=!0)=>{const l=[y,BigInt(p||N)],h={contractAddress:t,abi:H,funcName:"approve",funcParams:l,signer:i,txOverrides:{from:e}};if(k(n))return Q(n,await I(n,h));return W(n,m,{...h,funcParams:l,txOverrides:{from:e,nonce:u,gasLimit:T?BigInt(T.toString()):void 0},feeOption:o})},On=async(n,{assetValue:t,memo:y,recipient:o,feeOptionKey:p=U.Fast,data:T,from:e,maxFeePerGas:u,maxPriorityFeePerGas:i,gasPrice:m,...l},B,h=!0)=>{const c=t.getBaseValue("bigint"),M=t.chain,r=e||await B?.getAddress();if(!r)throw new R("toolbox_evm_no_from_address");if(!t.isGasAsset){const a=X(t,M);if(!a)throw new R("toolbox_evm_no_contract_address");return W(n,h,{signer:B,contractAddress:a,abi:H,funcName:"transfer",funcParams:[o,c],txOverrides:{from:r,maxFeePerGas:u,maxPriorityFeePerGas:i,gasPrice:m},feeOption:p})}const b={...l,from:r,to:o,value:c,data:T||F(_(y||""))};return un(n,b,p,B,h)},z=async(n,t=!0)=>{try{const{maxFeePerGas:y,maxPriorityFeePerGas:o,gasPrice:p}=await n.getFeeData();if(t){if(y===null||o===null)throw new R("toolbox_evm_no_fee_data");return{[U.Average]:{maxFeePerGas:y,maxPriorityFeePerGas:o},[U.Fast]:{maxFeePerGas:y*15n/10n,maxPriorityFeePerGas:o*15n/10n},[U.Fastest]:{maxFeePerGas:y*2n,maxPriorityFeePerGas:o*2n}}}if(!p)throw new R("toolbox_evm_no_gas_price");return{[U.Average]:{gasPrice:p},[U.Fast]:{gasPrice:p*15n/10n},[U.Fastest]:{gasPrice:p*2n}}}catch(y){throw new Error(`Failed to estimate gas price: ${y.msg??y.toString()}`)}},Tn=(n,{signer:t,contractAddress:y,abi:o,funcName:p,funcParams:T=[],txOverrides:e})=>{if(!y)throw new R("toolbox_evm_no_contract_address");const u=L(y,o,n);return t?u.connect(t).getFunction(p).estimateGas(...T,e):u.getFunction(p).estimateGas(...T,e)},sn=(n,{assetValue:t,recipient:y,memo:o,from:p,funcName:T,funcParams:e,txOverrides:u,signer:i})=>{const m=t.bigIntValue,l=t.isGasAsset?null:X(t,t.chain);if(l&&T)return Tn(n,{contractAddress:l,abi:H,funcName:T,funcParams:e,txOverrides:u,signer:i});return n.estimateGas({from:p,to:y,value:m,data:o?F(_(o)):void 0})},un=async(n,t,y=U.Fast,o,p=!0)=>{if(!o)throw new R("toolbox_evm_no_signer");const{from:T,to:e,data:u,value:i,...m}=t;if(!e)throw new R("toolbox_evm_no_to_address");const l={...m,data:u||"0x",to:e,from:T,value:BigInt(i||0)};if(k(n))return Q(n,l);const B=T||await o.getAddress(),h=t.nonce||await n.getTransactionCount(B),c=(await n.getNetwork()).chainId,M=An(l)||p,r=M&&!(l.maxFeePerGas&&l.maxPriorityFeePerGas)||!l.gasPrice?Object.entries((await z(n,p))[y]).reduce((a,[$,G])=>({...a,[$]:E(BigInt(G))}),{}):{};let b;try{b=E(l.gasLimit||await n.estimateGas(l)*11n/10n)}catch(a){throw new R("toolbox_evm_error_estimating_gas_limit",{error:a})}try{const a={...l,chainId:c,type:M?2:0,gasLimit:b,nonce:h,...r};try{return(await o.sendTransaction(a)).hash}catch($){const G=await o.signTransaction({...a,from:B});return(await n.broadcastTransaction(G)).hash}}catch(a){throw new R("toolbox_evm_error_sending_transaction",{error:a})}},Ot=(n)=>S(n),Q=(n,{from:t,to:y,data:o,value:p})=>{if(!k(n))throw new R("toolbox_evm_provider_not_eip1193_compatible");return n.send("eth_sendTransaction",[{value:E(BigInt(p||0)),from:t,to:y,data:o}])},st=(n,t)=>{const y=X(n,t);if(y)return S(y.toLowerCase());throw new R("toolbox_evm_invalid_gas_asset_address")},X=({chain:n,symbol:t,ticker:y},o)=>{try{if(n===o&&t===o&&y===o||n===q.BinanceSmartChain&&t==="BNB"&&y==="BNB"||n===q.Arbitrum&&t==="ETH"&&y==="ETH")return Nn[o];return S(t.slice(y.length+1).replace(/^0X/,""))}catch(p){return null}},dn=async(n,{assetValue:t,memo:y,recipient:o,feeOptionKey:p=U.Fast,data:T,from:e,maxFeePerGas:u,maxPriorityFeePerGas:i,gasPrice:m,...l},B)=>{const h=t.getBaseValue("bigint"),c=t.chain,M=e||await B?.getAddress();if(!M)throw new R("toolbox_evm_no_from_address");if(!Yn(t)){const b=X(t,c);if(!b)throw new R("toolbox_evm_no_contract_address");return I(n,{contractAddress:b,abi:H,funcName:"transfer",funcParams:[o,h],txOverrides:{from:M,maxFeePerGas:u,maxPriorityFeePerGas:i,gasPrice:m}})}return{...l,from:M,to:o,value:h,data:T||F(_(y||""))}},nt=async(n,{assetAddress:t,spenderAddress:y,amount:o,from:p},T)=>{const e=[y,BigInt(o||N)];return await I(n,{contractAddress:t,abi:H,funcName:"approve",funcParams:e,signer:T,txOverrides:{from:p}})},w=({provider:n,signer:t,isEIP1559Compatible:y=!0})=>({approve:(o)=>vn(n,o,t,y),approvedAmount:(o)=>en(n,o),broadcastTransaction:n.broadcastTransaction,call:(o)=>W(n,y,{...o,signer:t}),createContract:(o,p,T)=>L(o,p,T||n),createContractTxObject:(o)=>I(n,o),EIP1193SendTransaction:(o)=>Q(n,o),estimateCall:(o)=>Tn(n,{...o,signer:t}),estimateGasLimit:({assetValue:o,recipient:p,memo:T})=>sn(n,{assetValue:o,recipient:p,memo:T,signer:t}),estimateGasPrices:()=>z(n,y),isApproved:(o)=>Vn(n,o),sendTransaction:(o,p)=>un(n,o,p,t,y),transfer:(o)=>On(n,o,t,y),validateAddress:pn,createTransferTx:(o)=>dn(n,o,t),createApprovalTx:(o)=>nt(n,o,t),signMessage:tt(t)}),dt=w,ot=({address:n})=>pn(n);var yt;((y)=>{y.Test="goerli";y.Main="homestead"})(yt||={});import{BaseDecimal as pt,Chain as j,ChainId as ln,ChainToExplorerUrl as et,FeeOption as Y,RPCUrl as Tt}from"@swapkit/helpers";var ut=()=>({chainId:ln.ArbitrumHex,chainName:"Arbitrum One",nativeCurrency:{name:"Ethereum",symbol:j.Ethereum,decimals:pt.ETH},rpcUrls:[Tt.Arbitrum],blockExplorerUrls:[et[j.Arbitrum]]}),it=async(n)=>{try{const{gasPrice:t}=await n.getFeeData();if(!t)throw new Error("No fee data available");return{[Y.Average]:{gasPrice:t},[Y.Fast]:{gasPrice:t},[Y.Fastest]:{gasPrice:t}}}catch(t){throw new Error(`Failed to estimate gas price: ${t.msg??t.toString()}`)}},mn=({api:n,provider:t,signer:y,covalentApiKey:o})=>{const p=n||J({apiKey:o,chainId:ln.Arbitrum}),T=w({provider:t,signer:y,isEIP1559Compatible:!1}),e=j.Arbitrum;return{...T,getNetworkParams:ut,estimateGasPrices:()=>it(t),estimateTransactionFee:(u)=>g(u,Y.Average,e,t,!1),getBalance:(u,i=!0,m)=>f({provider:m||t,api:p,address:u,chain:e,potentialScamFilter:i})}};import{BaseDecimal as lt,Chain as A,ChainId as Bn,ChainToExplorerUrl as mt}from"@swapkit/helpers";var Bt=()=>({chainId:Bn.AvalancheHex,chainName:"Avalanche Network",nativeCurrency:{name:"Avalanche",symbol:A.Avalanche,decimals:lt.AVAX},rpcUrls:["https://api.avax.network/ext/bc/C/rpc"],blockExplorerUrls:[mt[A.Avalanche]]}),Rn=({api:n,provider:t,signer:y,covalentApiKey:o})=>{const p=n||J({apiKey:o,chainId:Bn.Avalanche}),T=w({provider:t,signer:y}),e=A.Avalanche;return{...T,getNetworkParams:Bt,estimateTransactionFee:(u,i)=>g(u,i,e,t),getBalance:(u,i=!0,m)=>f({provider:m||t,api:p,address:u,chain:e,potentialScamFilter:i})}};import{BaseDecimal as Rt,Chain as an,ChainId as wn,ChainToExplorerUrl as at}from"@swapkit/helpers";var wt=()=>({chainId:wn.BinanceSmartChainHex,chainName:"BNB Chain",nativeCurrency:{name:"Binance Coin",symbol:"BNB",decimals:Rt.BSC},rpcUrls:["https://bsc-dataseed.binance.org"],blockExplorerUrls:[at[an.BinanceSmartChain]]}),hn=({api:n,provider:t,signer:y,covalentApiKey:o})=>{const p=n||J({apiKey:o,chainId:wn.BinanceSmartChain}),T=w({provider:t,signer:y,isEIP1559Compatible:!1}),e=an.BinanceSmartChain;return{...T,getNetworkParams:wt,estimateTransactionFee:(u,i)=>g(u,i,e,t,!1),getBalance:(u,i=!0,m)=>f({provider:m||t,api:p,address:u,chain:e,potentialScamFilter:i})}};import{Chain as ht}from"@swapkit/helpers";var fn=({api:n,ethplorerApiKey:t,signer:y,provider:o})=>{const p=n||nn(t),T=w({provider:o,signer:y}),e=ht.Ethereum;return{...T,estimateTransactionFee:(u,i)=>g(u,i,e,o),getBalance:(u,i=!0,m)=>f({provider:m||o,api:p,address:u,chain:e,potentialScamFilter:i})}};import{Chain as C}from"@swapkit/helpers";import{BaseDecimal as ft,Chain as K,ChainId as cn,ChainToExplorerUrl as ct,RPCUrl as Mt}from"@swapkit/helpers";var rt=()=>({chainId:cn.BaseHex,chainName:"Base Mainnet",nativeCurrency:{name:"Ethereum",symbol:K.Ethereum,decimals:ft.ETH},rpcUrls:[Mt.Base],blockExplorerUrls:[ct[K.Base]]}),Mn=({api:n,provider:t,signer:y,covalentApiKey:o})=>{const p=w({provider:t,signer:y}),T=K.Base;return{...p,getNetworkParams:rt,estimateTransactionFee:(e,u)=>g(e,u,T,t),getBalance:async(e,u=!0,i)=>{return await f({provider:i||t,api:n||J({apiKey:o,chainId:cn.Base}),address:e,chain:T,potentialScamFilter:u})}}};import{BaseDecimal as Jt,Chain as V,ChainId as rn,ChainToExplorerUrl as gt,RPCUrl as bt}from"@swapkit/helpers";var Ut=()=>({chainId:rn.PolygonHex,chainName:"Polygon Mainnet",nativeCurrency:{name:"Polygon",symbol:V.Polygon,decimals:Jt.MATIC},rpcUrls:[bt.Polygon],blockExplorerUrls:[gt[V.Polygon]]}),Jn=({api:n,provider:t,signer:y,covalentApiKey:o})=>{const p=n||J({apiKey:o,chainId:rn.Polygon}),T=w({provider:t,signer:y}),e=V.Polygon;return{...T,getNetworkParams:Ut,estimateTransactionFee:(u,i)=>g(u,i,e,t),getBalance:(u,i=!0,m)=>f({provider:m||t,api:p,address:u,chain:e,potentialScamFilter:i})}};import{BaseDecimal as qt,Chain as O,ChainId as bn,ChainToExplorerUrl as $t,FeeOption as v,RPCUrl as xt}from"@swapkit/helpers";var gn=[{inputs:[{internalType:"address",name:"_owner",type:"address"}],stateMutability:"nonpayable",type:"constructor"},{anonymous:!1,inputs:[{indexed:!1,internalType:"uint256",name:"",type:"uint256"}],name:"DecimalsUpdated",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"uint256",name:"",type:"uint256"}],name:"GasPriceUpdated",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"uint256",name:"",type:"uint256"}],name:"L1BaseFeeUpdated",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"uint256",name:"",type:"uint256"}],name:"OverheadUpdated",type:"event"},{anonymous:!1,inputs:[{indexed:!0,internalType:"address",name:"previousOwner",type:"address"},{indexed:!0,internalType:"address",name:"newOwner",type:"address"}],name:"OwnershipTransferred",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"uint256",name:"",type:"uint256"}],name:"ScalarUpdated",type:"event"},{inputs:[],name:"decimals",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"gasPrice",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"bytes",name:"_data",type:"bytes"}],name:"getL1Fee",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"bytes",name:"_data",type:"bytes"}],name:"getL1GasUsed",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"l1BaseFee",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"overhead",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"owner",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function"},{inputs:[],name:"renounceOwnership",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[],name:"scalar",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"uint256",name:"_decimals",type:"uint256"}],name:"setDecimals",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint256",name:"_gasPrice",type:"uint256"}],name:"setGasPrice",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint256",name:"_baseFee",type:"uint256"}],name:"setL1BaseFee",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint256",name:"_overhead",type:"uint256"}],name:"setOverhead",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint256",name:"_scalar",type:"uint256"}],name:"setScalar",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"newOwner",type:"address"}],name:"transferOwnership",outputs:[],stateMutability:"nonpayable",type:"function"}];import{Contract as Ct,Transaction as Dt}from"ethers";var Ht="0x420000000000000000000000000000000000000f",s=(n)=>{return new Ct(Ht,gn,n)},Un=(n)=>{const t=s(n);if(t&&"l1BaseFee"in t)return t?.l1BaseFee();return},qn=async(n,{data:t,from:y,to:o,gasPrice:p,type:T,gasLimit:e,nonce:u})=>{if(!o)throw new Error("Missing to address");return Dt.from({data:t,to:o,gasPrice:p,type:T,gasLimit:e,nonce:u?u:y?await n.getTransactionCount(y):0}).serialized},$n=async(n,t)=>{const y=await s(n),o=await qn(n,t);if(y&&"getL1Fee"in y)return y.getL1Fee(o)},xn=async(n,t)=>{const y=await n.send("eth_gasPrice",[]),o=await n.estimateGas(t);return y.mul(o)},Gt=async(n,t)=>{const y=await $n(n,t),o=await xn(n,t);return y.add(o)},zt=async(n,t)=>{const y=s(n),o=await qn(n,t);if(y&&"getL1GasUsed"in y)return y.getL1GasUsed(o)},It=()=>({chainId:bn.OptimismHex,chainName:"Optimism",nativeCurrency:{name:"Ethereum",symbol:O.Ethereum,decimals:qt.ETH},rpcUrls:[xt.Optimism],blockExplorerUrls:[$t[O.Optimism]]}),Et=async(n)=>{try{const{maxFeePerGas:t,maxPriorityFeePerGas:y,gasPrice:o}=await n.getFeeData(),p=await Un(n),T=o;if(!(t&&y))throw new Error("No fee data available");return{[v.Average]:{l1GasPrice:p,gasPrice:o,maxFeePerGas:t,maxPriorityFeePerGas:y},[v.Fast]:{l1GasPrice:(p||0n)*15n/10n,gasPrice:T*15n/10n,maxFeePerGas:t,maxPriorityFeePerGas:y*15n/10n},[v.Fastest]:{l1GasPrice:(p||0n)*2n,gasPrice:T*2n,maxFeePerGas:t,maxPriorityFeePerGas:y*2n}}}catch(t){throw new Error(`Failed to estimate gas price: ${t.msg??t.toString()}`)}},Cn=({api:n,provider:t,signer:y,covalentApiKey:o})=>{const p=n||J({apiKey:o,chainId:bn.Optimism});return{...w({provider:t,signer:y}),estimateTotalGasCost:(e)=>Gt(t,e),estimateL1GasCost:(e)=>$n(t,e),estimateL2GasCost:(e)=>xn(t,e),getL1GasPrice:()=>Un(t),estimateL1Gas:(e)=>zt(t,e),getNetworkParams:It,estimateGasPrices:()=>Et(t),getBalance:(e,u=!0,i)=>f({provider:i||t,api:p,address:e,chain:O.Optimism,potentialScamFilter:u})}};var Ko=(n)=>{switch(n){case C.Avalanche:return Rn;case C.Arbitrum:return mn;case C.Base:return Mn;case C.Optimism:return Cn;case C.Polygon:return Jn;case C.BinanceSmartChain:return hn;case C.Ethereum:return fn;default:throw new Error(`Chain ${n} is not supported`)}};export{E as toHexString,Ot as toChecksumAddress,Kn as isStateChangingCall,k as isBrowserProvider,Ko as getToolboxByChain,X as getTokenAddress,_t as getProvider,Un as getL1GasPrice,st as getChecksumAddressFromAsset,f as getBalance,ot as evmValidateAddress,nn as ethplorerApi,g as estimateTransactionFee,Gt as estimateTotalGasCost,Yt as estimateMaxSendableAmount,xn as estimateL2GasCost,$n as estimateL1GasCost,zt as estimateL1Gas,z as estimateGasPrices,I as createContractTxObject,L as createContract,J as covalentApi,s as connectGasPriceOracle,ey as Transaction,py as Signature,Cn as OPToolbox,N as MAX_APPROVAL,Jn as MATICToolbox,yy as JsonRpcProvider,oy as HDNodeWallet,yt as EthNetwork,w as EVMToolbox,fn as ETHToolbox,Q as EIP1193SendTransaction,ty as BrowserProvider,dt as BaseEVMToolbox,hn as BSCToolbox,Mn as BASEToolbox,ny as AbstractSigner,Rn as AVAXToolbox,mn as ARBToolbox}; | ||
//# debugId=1EF5EE00D9445C2764756E2164756E21 | ||
//# debugId=774474ED5A4D809064756E2164756E21 |
{ | ||
"author": "swapkit-oss", | ||
"dependencies": { | ||
"@swapkit/helpers": "0.0.0-nightly-20241007090400", | ||
"@swapkit/helpers": "0.0.0-nightly-20241011172732", | ||
"ethers": "6.11.1" | ||
@@ -21,3 +21,2 @@ }, | ||
"name": "@swapkit/toolbox-evm", | ||
"react-native": "./src/index.ts", | ||
"repository": { | ||
@@ -37,3 +36,3 @@ "type": "git", | ||
"types": "./src/index.ts", | ||
"version": "0.0.0-nightly-20241007090400" | ||
"version": "0.0.0-nightly-20241011172732" | ||
} |
@@ -110,3 +110,3 @@ import { | ||
funcParams = [], | ||
txOverrides, | ||
txOverrides = {}, | ||
feeOption = FeeOption.Fast, | ||
@@ -119,2 +119,6 @@ }: WithSigner<CallParams>, | ||
const isStateChanging = isStateChangingCall(abi, funcName); | ||
const overridesParams = { | ||
...txOverrides, | ||
from: txOverrides?.from || (await signer?.getAddress()), | ||
}; | ||
@@ -127,3 +131,3 @@ if (isStateChanging && isBrowserProvider(contractProvider) && signer) { | ||
funcParams, | ||
txOverrides, | ||
txOverrides: overridesParams, | ||
}); | ||
@@ -130,0 +134,0 @@ |
Sorry, the diff of this file is not supported yet
149050
1858
+ Added@swapkit/contracts@0.0.0-nightly-20241011172732(transitive)
+ Added@swapkit/helpers@0.0.0-nightly-20241011172732(transitive)
+ Added@swapkit/tokens@0.0.0-nightly-20241011172732(transitive)
- Removed@swapkit/contracts@0.0.0-nightly-20241007090400(transitive)
- Removed@swapkit/helpers@0.0.0-nightly-20241007090400(transitive)
- Removed@swapkit/tokens@0.0.0-nightly-20241007090400(transitive)