@swapkit/toolbox-evm
Advanced tools
Comparing version 1.5.7 to 1.5.8
@@ -1,3 +0,3 @@ | ||
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}; | ||
import{AbstractSigner as so,BrowserProvider as Oo,HDNodeWallet as ny,JsonRpcProvider as ty,Signature as oy,Transaction as yy}from"ethers";import{ChainIdToChain as Dn,RequestClient as Hn,formatBigIntToSafeValue as Gn}from"@swapkit/helpers";var J=({apiKey:n,chainId:t})=>({getBalance:async(y)=>{let{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)=>{let{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})=>{let l=(await n.getBalance(t)).find(({symbol:R,chain:$})=>p?R===p.symbol:R===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;let 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");let M=h*(c?(B.maxFeePerGas||1n)+(B.maxPriorityFeePerGas||1n):B.gasPrice),b=P.fromBigInt(l.getBaseValue("bigint")).sub(M.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})=>{let 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)=>{let 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";var _t=(n,t)=>{return new Xn(t||Qn[n])};import{Chain as q,ContractAddress as x,FeeOption as U,SwapKitError as a,SwapKitNumber as yn,erc20ABI as H,isGasAsset as Yn}from"@swapkit/helpers";import{BrowserProvider as Zn,Contract as Pn,Interface as Fn,MaxInt256 as _n,getAddress as S,hexlify as F,toUtf8Bytes as _}from"ethers";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)=>{let y=n.find((o)=>o.name===t);if(!y)throw new a("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})=>{let l=y||n;if(!p)throw new Error("contractAddress must be provided");let B=Kn(T,e),h={...i,from:i?.from||await o?.getAddress()};if(B&&k(l)&&o){let M=await I(l,{contractAddress:p,abi:T,funcName:e,funcParams:u,txOverrides:h});return Q(l,M)}let c=L(p,T,l);if(B){if(!o)throw new a("toolbox_evm_no_signer");let M=i?.from||await o.getAddress();if(!M)throw new a("toolbox_evm_no_signer_address");let b=c.connect(o),{maxFeePerGas:R,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:R,maxPriorityFeePerGas:$,gasPrice:G,nonce:i?.nonce||await l.getTransactionCount(M)});return typeof Z?.hash==="string"?Z?.hash:Z}let r=await c[e]?.(...u);return typeof r?.hash==="string"?r?.hash:r},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)=>{let 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})},sn=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)=>{let c=t.getBaseValue("bigint"),r=t.chain,M=e||await B?.getAddress();if(!M)throw new a("toolbox_evm_no_from_address");if(!t.isGasAsset){let R=X(t,r);if(!R)throw new a("toolbox_evm_no_contract_address");return W(n,h,{signer:B,contractAddress:R,abi:H,funcName:"transfer",funcParams:[o,c],txOverrides:{from:M,maxFeePerGas:u,maxPriorityFeePerGas:i,gasPrice:m},feeOption:p})}let b={...l,from:M,to:o,value:c,data:T||F(_(y||""))};return un(n,b,p,B,h)},z=async(n,t=!0)=>{try{let{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{[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 a("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 a("toolbox_evm_no_contract_address");let u=L(y,o,n);return t?u.connect(t).getFunction(p).estimateGas(...T,e):u.getFunction(p).estimateGas(...T,e)},On=(n,{assetValue:t,recipient:y,memo:o,from:p,funcName:T,funcParams:e,txOverrides:u,signer:i})=>{let 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 a("toolbox_evm_no_signer");let{from:T,to:e,data:u,value:i,...m}=t;if(!e)throw new a("toolbox_evm_no_to_address");let l={...m,data:u||"0x",to:e,from:T,value:BigInt(i||0)};if(k(n))return Q(n,l);let B=T||await o.getAddress(),h=t.nonce||await n.getTransactionCount(B),c=(await n.getNetwork()).chainId,r=An(l)||p,M=r&&!(l.maxFeePerGas&&l.maxPriorityFeePerGas)||!l.gasPrice?Object.entries((await z(n,p))[y]).reduce((R,[$,G])=>({...R,[$]:E(BigInt(G))}),{}):{},b;try{b=E(l.gasLimit||await n.estimateGas(l)*11n/10n)}catch(R){throw new a("toolbox_evm_error_estimating_gas_limit",{error:R})}try{let R={...l,chainId:c,type:r?2:0,gasLimit:b,nonce:h,...M};try{return(await o.sendTransaction(R)).hash}catch($){let G=await o.signTransaction({...R,from:B});return(await n.broadcastTransaction(G)).hash}}catch(R){throw new a("toolbox_evm_error_sending_transaction",{error:R})}},Vt=(n)=>S(n),Q=(n,{from:t,to:y,data:o,value:p})=>{if(!k(n))throw new a("toolbox_evm_provider_not_eip1193_compatible");return n.send("eth_sendTransaction",[{value:E(BigInt(p||0)),from:t,to:y,data:o}])},vt=(n,t)=>{let y=X(n,t);if(y)return S(y.toLowerCase());throw new a("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)=>{let h=t.getBaseValue("bigint"),c=t.chain,r=e||await B?.getAddress();if(!r)throw new a("toolbox_evm_no_from_address");if(!Yn(t)){let b=X(t,c);if(!b)throw new a("toolbox_evm_no_contract_address");return I(n,{contractAddress:b,abi:H,funcName:"transfer",funcParams:[o,h],txOverrides:{from:r,maxFeePerGas:u,maxPriorityFeePerGas:i,gasPrice:m}})}return{...l,from:r,to:o,value:h,data:T||F(_(y||""))}},nt=async(n,{assetAddress:t,spenderAddress:y,amount:o,from:p},T)=>{let e=[y,BigInt(o||N)];return await I(n,{contractAddress:t,abi:H,funcName:"approve",funcParams:e,signer:T,txOverrides:{from:p}})};function tt(n){if(!n)throw new a("toolbox_evm_no_signer");return n.signMessage}var 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})=>On(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)=>sn(n,o,t,y),validateAddress:pn,createTransferTx:(o)=>dn(n,o,t),createApprovalTx:(o)=>nt(n,o,t),signMessage:tt(t)}),st=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{let{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})=>{let 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]]}),an=({api:n,provider:t,signer:y,covalentApiKey:o})=>{let 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 at,Chain as Rn,ChainId as wn,ChainToExplorerUrl as Rt}from"@swapkit/helpers";var wt=()=>({chainId:wn.BinanceSmartChainHex,chainName:"BNB Chain",nativeCurrency:{name:"Binance Coin",symbol:"BNB",decimals:at.BSC},rpcUrls:["https://bsc-dataseed.binance.org"],blockExplorerUrls:[Rt[Rn.BinanceSmartChain]]}),hn=({api:n,provider:t,signer:y,covalentApiKey:o})=>{let p=n||J({apiKey:o,chainId:wn.BinanceSmartChain}),T=w({provider:t,signer:y,isEIP1559Compatible:!1}),e=Rn.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})=>{let 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 rt}from"@swapkit/helpers";var Mt=()=>({chainId:cn.BaseHex,chainName:"Base Mainnet",nativeCurrency:{name:"Ethereum",symbol:K.Ethereum,decimals:ft.ETH},rpcUrls:[rt.Base],blockExplorerUrls:[ct[K.Base]]}),rn=({api:n,provider:t,signer:y,covalentApiKey:o})=>{let p=w({provider:t,signer:y}),T=K.Base;return{...p,getNetworkParams:Mt,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 Mn,ChainToExplorerUrl as gt,RPCUrl as bt}from"@swapkit/helpers";var Ut=()=>({chainId:Mn.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})=>{let p=n||J({apiKey:o,chainId:Mn.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 s,ChainId as bn,ChainToExplorerUrl as $t,FeeOption as v,RPCUrl as xt}from"@swapkit/helpers";import{Contract as Ct,Transaction as Dt}from"ethers";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"}];var Ht="0x420000000000000000000000000000000000000f",O=(n)=>{return new Ct(Ht,gn,n)},Un=(n)=>{let t=O(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)=>{let y=await O(n),o=await qn(n,t);if(y&&"getL1Fee"in y)return y.getL1Fee(o)},xn=async(n,t)=>{let y=await n.send("eth_gasPrice",[]),o=await n.estimateGas(t);return y.mul(o)},Gt=async(n,t)=>{let y=await $n(n,t),o=await xn(n,t);return y.add(o)},zt=async(n,t)=>{let y=O(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:s.Ethereum,decimals:qt.ETH},rpcUrls:[xt.Optimism],blockExplorerUrls:[$t[s.Optimism]]}),Et=async(n)=>{try{let{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})=>{let 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:s.Optimism,potentialScamFilter:u})}};var jo=(n)=>{switch(n){case C.Avalanche:return an;case C.Arbitrum:return mn;case C.Base:return rn;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,Vt as toChecksumAddress,Kn as isStateChangingCall,k as isBrowserProvider,jo as getToolboxByChain,X as getTokenAddress,_t as getProvider,Un as getL1GasPrice,vt 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,O as connectGasPriceOracle,yy as Transaction,oy as Signature,Cn as OPToolbox,N as MAX_APPROVAL,Jn as MATICToolbox,ty as JsonRpcProvider,ny as HDNodeWallet,yt as EthNetwork,w as EVMToolbox,fn as ETHToolbox,Q as EIP1193SendTransaction,Oo as BrowserProvider,st as BaseEVMToolbox,hn as BSCToolbox,rn as BASEToolbox,so as AbstractSigner,an as AVAXToolbox,mn as ARBToolbox}; | ||
//# debugId=774474ED5A4D809064756E2164756E21 | ||
//# debugId=F573A9DE98394A9E64756E2164756E21 |
{ | ||
"author": "swapkit-oss", | ||
"dependencies": { | ||
"@swapkit/helpers": "1.13.2", | ||
"@swapkit/helpers": "1.13.3", | ||
"ethers": "6.11.1" | ||
@@ -35,3 +35,3 @@ }, | ||
"types": "./src/index.ts", | ||
"version": "1.5.7" | ||
"version": "1.5.8" | ||
} |
@@ -5,4 +5,3 @@ import { afterEach, beforeAll, beforeEach, describe, expect, test } from "bun:test"; | ||
import { AssetValue, Chain, FeeOption, erc20ABI } from "@swapkit/helpers"; | ||
import type { JsonRpcProvider } from "ethers"; | ||
import type { JsonRpcSigner } from "ethers"; | ||
import type { JsonRpcProvider, JsonRpcSigner } from "ethers"; | ||
import hre from "hardhat"; | ||
@@ -9,0 +8,0 @@ |
import { ChainToRPC, type EVMChain } from "@swapkit/helpers"; | ||
import { JsonRpcProvider } from "ethers/providers"; | ||
import { JsonRpcProvider } from "ethers"; | ||
@@ -4,0 +4,0 @@ export const getProvider = (chain: EVMChain, customUrl?: string) => { |
@@ -14,14 +14,18 @@ import { | ||
} from "@swapkit/helpers"; | ||
import type { | ||
ContractTransaction, | ||
Fragment, | ||
HDNodeWallet, | ||
JsonFragment, | ||
JsonRpcSigner, | ||
Provider, | ||
Signer, | ||
import { | ||
BrowserProvider, | ||
Contract, | ||
type ContractTransaction, | ||
type Fragment, | ||
type HDNodeWallet, | ||
Interface, | ||
type JsonFragment, | ||
type JsonRpcSigner, | ||
MaxInt256, | ||
type Provider, | ||
type Signer, | ||
getAddress, | ||
hexlify, | ||
toUtf8Bytes, | ||
} from "ethers"; | ||
import { BrowserProvider, Contract, Interface, hexlify, toUtf8Bytes } from "ethers"; | ||
import { getAddress } from "ethers/address"; | ||
import { MaxInt256 } from "ethers/constants"; | ||
@@ -28,0 +32,0 @@ import { |
@@ -10,2 +10,3 @@ import { | ||
import type { BrowserProvider, JsonRpcProvider, Signer, TransactionRequest } from "ethers"; | ||
import { Contract, Transaction } from "ethers"; | ||
@@ -17,3 +18,2 @@ import type { CovalentApiType } from "../api/covalentApi"; | ||
import { Contract, Transaction } from "ethers"; | ||
import { EVMToolbox } from "./EVMToolbox"; | ||
@@ -20,0 +20,0 @@ |
Sorry, the diff of this file is not supported yet
1861
148679
+ Added@swapkit/helpers@1.13.3(transitive)
- Removed@swapkit/helpers@1.13.2(transitive)
Updated@swapkit/helpers@1.13.3