@swapkit/toolboxes 5.2.1 → 5.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +0,0 @@
1
- import{fa as Ny}from"./chunk-nvfkjbyq.js";import{ga as Uy}from"./chunk-xbdjxeqd.js";import{Chain as H}from"@swapkit/helpers";import{match as Q5}from"ts-pattern";import{Chain as J,getChainConfig as p,getRPCUrl as Wu,SKConfig as Qu,SwapKitError as h}from"@swapkit/helpers";import{FallbackProvider as Xu,getAddress as Hy,Interface as Yu,JsonRpcProvider as ty,Network as Zu}from"ethers";import{match as Ry}from"ts-pattern";function $u(y){return new ty(y)}async function B5(y,u){return $u(u||await Wu(y))}function Dy(y){let u=Qu.get("rpcUrls")[y]?.filter(Boolean)??[];if(u.length===0)throw new h("helpers_chain_no_public_or_set_rpc_url",{chain:y});let{chainId:n}=p(y),t=Zu.from(Number(n)),[T]=u;if(u.length===1)return new ty(T,t,{staticNetwork:t});return new Xu(u.map((M,b)=>({priority:b+1,provider:new ty(M,t,{staticNetwork:t}),stallTimeout:2000})),t,{quorum:1})}function l(y){return y>0n?`0x${y.toString(16)}`:"0x0"}function ky(y){let{explorerUrl:u,chainIdHex:n,rpcUrls:t}=p(y);return function(){return y!==J.Ethereum?{...Bu({chain:y}),blockExplorerUrls:[u],chainId:n,rpcUrls:t}:void 0}}function Ly(y){return![J.Adi,J.Arbitrum,J.BinanceSmartChain,J.Robinhood].includes(y)}function Bu({chain:y}){let{name:u,nativeCurrency:n,baseDecimal:t}=p(y);return Ry(y).with(J.Arbitrum,()=>({chainName:"Arbitrum One",nativeCurrency:{decimals:t,name:"Ethereum",symbol:J.Ethereum}})).with(J.Aurora,()=>({chainName:"Aurora Mainnet",nativeCurrency:{decimals:t,name:"Ethereum",symbol:J.Ethereum}})).with(J.Avalanche,()=>({chainName:"Avalanche Network",nativeCurrency:{decimals:t,name:"Avalanche",symbol:y}})).with(J.Base,()=>({chainName:"Base Mainnet",nativeCurrency:{decimals:t,name:"Ethereum",symbol:J.Ethereum}})).with(J.Berachain,()=>({chainName:"Berachain",nativeCurrency:{decimals:t,name:"Berachain",symbol:"BERA"}})).with(J.XLayer,()=>({chainName:"X Layer",nativeCurrency:{decimals:t,name:"OKB",symbol:"OKB"}})).with(J.BinanceSmartChain,()=>({chainName:"BNB Smart Chain Mainnet",nativeCurrency:{decimals:t,name:"Binance Coin",symbol:"BNB"}})).with(J.Gnosis,()=>({chainName:"Gnosis",nativeCurrency:{decimals:t,name:"xDAI",symbol:"XDAI"}})).with(J.Optimism,()=>({chainName:"OP Mainnet",nativeCurrency:{decimals:t,name:"Ethereum",symbol:J.Ethereum}})).with(J.Polygon,()=>({chainName:"Polygon Mainnet",nativeCurrency:{decimals:t,name:"Polygon",symbol:J.Polygon}})).with(J.Robinhood,()=>({chainName:"Robinhood Chain",nativeCurrency:{decimals:t,name:"Ethereum",symbol:J.Ethereum}})).otherwise(()=>({chainName:u,nativeCurrency:{decimals:t,name:n,symbol:n}}))}async function zy(y,u){if(!("authorize"in y))throw Error("Signer does not support EIP-7702 authorization. Requires ethers.js v6.14+");let n={...u,nonce:u.nonce!==void 0?BigInt(u.nonce):void 0};return await y.authorize(n)}var Jy=(y)=>typeof y==="string"&&/^0x[a-fA-F0-9]{64}$/.test(y),Ju=(y)=>{try{return typeof y==="string"&&!!Hy(y)}catch{return!1}};function Hu(y,u){let n=u!==void 0?`index ${u}: `:"",t=(T,M)=>{throw new h({errorKey:"toolbox_evm_invalid_eip7702_authorization",info:{field:T,message:`${n}${M}`}})};Ry(y).when(({chainId:T})=>typeof T!=="bigint",()=>t("chainId","chainId must be a bigint")).when(({address:T})=>!Ju(T),()=>t("address","address must be a valid checksummed address")).when(({nonce:T})=>typeof T!=="bigint"||T<0n,()=>t("nonce","nonce must be a non-negative bigint")).when(({signature:T})=>!T,()=>t("signature","signature is required")).when(({signature:T})=>typeof T?.yParity!=="number"||![0,1].includes(T.yParity),()=>t("signature.yParity","yParity must be 0 or 1")).when(({signature:T})=>!Jy(T?.r),()=>t("signature.r","r must be a 32-byte hex string")).when(({signature:T})=>!Jy(T?.s),()=>t("signature.s","s must be a 32-byte hex string")).otherwise(()=>{})}function Ru(y){return[{key:"name",type:"string"},{key:"version",type:"string"},{key:"chainId",type:"uint256"},{key:"verifyingContract",type:"address"},{key:"salt",type:"bytes32"}].filter(({key:u})=>y[u]!=null).map(({key:u,type:n})=>({name:u,type:n}))}function Ty(y){return y.charAt(0).toUpperCase()+y.slice(1)}function My(y,u){if(y.isTuple()&&y.components){let n=Ty(y.name);return u[n]=y.components.map((t)=>({name:t.name,type:My(t,u)})),n}if(y.isArray()){let n=y.arrayChildren;if(n?.isTuple()&&n.components){let t=Ty(y.name.replace(/s$/,""));return u[t]=n.components.map((T)=>({name:T.name,type:My(T,u)})),`${t}[]`}return`${n?.type??"bytes"}[]`}return y.type}function by(y,u){if(typeof y==="bigint")return y;if(u?.isTuple()&&typeof y?.toObject==="function"){let n=y.toObject(),t={};for(let[T,M]of(u.components??[]).entries())t[M.name]=by(n[M.name]??y[T],M);return t}if(Array.isArray(y)){let n=u?.arrayChildren;return y.map((t)=>by(t,n??void 0))}return y}function J5({chain:y,abi:u,data:n,to:t}){let M=new Yu(u).parseTransaction({data:n});if(!M)throw new h({errorKey:"toolbox_evm_invalid_params",info:{error:"Could not decode calldata"}});let b={},q=Ty(M.name);b[q]=M.fragment.inputs.map((X)=>({name:X.name,type:My(X,b)}));let W={};for(let[X,$]of M.fragment.inputs.entries())W[$.name]=by(M.args[X],$);let{chainId:Q}=p(y),U={chainId:Number(Q),verifyingContract:Hy(t)},D=Ru(U);return{domain:U,message:W,primaryType:q,types:{EIP712Domain:D,...b}}}function jy(y,u){if(!Array.isArray(y)||y.length===0)throw new h({errorKey:"toolbox_evm_invalid_eip7702_authorization",info:{message:"EIP-7702 transactions require at least one authorization"}});for(let[n,t]of y.entries())if(Hu(t,n),u!==void 0&&t.chainId!==u)throw new h({errorKey:"toolbox_evm_invalid_eip7702_authorization",info:{field:"chainId",message:`index ${n}: authorization chainId (${t.chainId}) does not match transaction chainId (${u})`}})}import{Chain as R,derivationPathToString as T5,FeeOption as M5,resolveDerivationPath as b5}from"@swapkit/helpers";import{HDNodeWallet as q5}from"ethers";import{match as U5,P as Cy}from"ts-pattern";var Gy=[{inputs:[{components:[{internalType:"address",name:"target",type:"address"},{internalType:"bytes",name:"callData",type:"bytes"}],internalType:"struct Multicall2.Call[]",name:"calls",type:"tuple[]"}],name:"aggregate",outputs:[{internalType:"uint256",name:"blockNumber",type:"uint256"},{internalType:"bytes[]",name:"returnData",type:"bytes[]"}],stateMutability:"nonpayable",type:"function"},{inputs:[{components:[{internalType:"address",name:"target",type:"address"},{internalType:"bytes",name:"callData",type:"bytes"}],internalType:"struct Multicall2.Call[]",name:"calls",type:"tuple[]"}],name:"blockAndAggregate",outputs:[{internalType:"uint256",name:"blockNumber",type:"uint256"},{internalType:"bytes32",name:"blockHash",type:"bytes32"},{components:[{internalType:"bool",name:"success",type:"bool"},{internalType:"bytes",name:"returnData",type:"bytes"}],internalType:"struct Multicall2.Result[]",name:"returnData",type:"tuple[]"}],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint256",name:"blockNumber",type:"uint256"}],name:"getBlockHash",outputs:[{internalType:"bytes32",name:"blockHash",type:"bytes32"}],stateMutability:"view",type:"function"},{inputs:[],name:"getBlockNumber",outputs:[{internalType:"uint256",name:"blockNumber",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"getCurrentBlockCoinbase",outputs:[{internalType:"address",name:"coinbase",type:"address"}],stateMutability:"view",type:"function"},{inputs:[],name:"getCurrentBlockDifficulty",outputs:[{internalType:"uint256",name:"difficulty",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"getCurrentBlockGasLimit",outputs:[{internalType:"uint256",name:"gaslimit",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"getCurrentBlockTimestamp",outputs:[{internalType:"uint256",name:"timestamp",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"addr",type:"address"}],name:"getEthBalance",outputs:[{internalType:"uint256",name:"balance",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"getLastBlockHash",outputs:[{internalType:"bytes32",name:"blockHash",type:"bytes32"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"bool",name:"requireSuccess",type:"bool"},{components:[{internalType:"address",name:"target",type:"address"},{internalType:"bytes",name:"callData",type:"bytes"}],internalType:"struct Multicall2.Call[]",name:"calls",type:"tuple[]"}],name:"tryAggregate",outputs:[{components:[{internalType:"bool",name:"success",type:"bool"},{internalType:"bytes",name:"returnData",type:"bytes"}],internalType:"struct Multicall2.Result[]",name:"returnData",type:"tuple[]"}],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"bool",name:"requireSuccess",type:"bool"},{components:[{internalType:"address",name:"target",type:"address"},{internalType:"bytes",name:"callData",type:"bytes"}],internalType:"struct Multicall2.Call[]",name:"calls",type:"tuple[]"}],name:"tryBlockAndAggregate",outputs:[{internalType:"uint256",name:"blockNumber",type:"uint256"},{internalType:"bytes32",name:"blockHash",type:"bytes32"},{components:[{internalType:"bool",name:"success",type:"bool"},{internalType:"bytes",name:"returnData",type:"bytes"}],internalType:"struct Multicall2.Result[]",name:"returnData",type:"tuple[]"}],stateMutability:"nonpayable",type:"function"}];import{AssetValue as Du,applyFeeMultiplierToBigInt as j,Chain as K,EVMChains as ku,FeeOption as Y,isGasAsset as Lu,SwapKitError as Z,SwapKitNumber as s}from"@swapkit/helpers";import{erc20ABI as o}from"@swapkit/helpers/contracts";import{BrowserProvider as zu,Contract as _y,getAddress as i,getBytes as ju,Interface as Vy}from"ethers";import{match as wy}from"ts-pattern";function d(y){return{getBalance:Ny(y)}}function k5(y){return y}var Wy=BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");function a({chain:y=K.Ethereum,provider:u,signer:n,isEIP1559Compatible:t=!0,estimateGasPrices:T,estimateAdditionalFee:M}){let b=T??m({chain:y,isEIP1559Compatible:t,provider:u}),q=fy({chain:y,estimateAdditionalFee:M,estimateGasPrices:b,isEIP1559Compatible:t,provider:u,signer:n});return{approve:Ou({chain:y,isEIP1559Compatible:t,provider:u,signer:n}),approvedAmount:ly({chain:y,provider:u}),broadcastTransaction:u.broadcastTransaction,call:yy({chain:y,isEIP1559Compatible:t,provider:u,signer:n}),createApprovalTx:cu({chain:y,provider:u,signer:n}),createContract:Nu({chain:y,provider:u}),createContractTxObject:c({chain:y,provider:u}),createTransaction:q,createTransferTx:q,EIP1193SendTransaction:r(u),estimateCall:Sy({provider:u,signer:n}),estimateGasLimit:xu({chain:y,provider:u,signer:n}),estimateGasPrices:b,estimateTransactionFee:mu({chain:y,isEIP1559Compatible:t,provider:u}),getAddress:()=>{return n?n.getAddress():void 0},getBalance:d(y).getBalance,getNetworkParams:ky(y),isApproved:Ku({chain:y,provider:u}),sendTransaction:Oy({chain:y,estimateGasPrices:b,isEIP1559Compatible:t,provider:u,signer:n}),signAuthorization:Fu({signer:n}),signMessage:n?(W)=>n.signMessage(W):void 0,signTypedData:n?({domain:W,types:Q,value:U})=>n.signTypedData(W,Q,U):void 0,transfer:fu({chain:y,estimateAdditionalFee:M,estimateGasPrices:b,isEIP1559Compatible:t,provider:u,signer:n}),validateAddress:Gu}}function Gu(y){try{return i(y),!0}catch{return!1}}function e(y){return y instanceof zu}function Qy(y,u,n){return new _y(y,Vy.from(u),n)}function Nu({provider:y}){return function(n,t){return new _y(n,Vy.from(t),y)}}var _u=["payable","nonpayable"];function Vu({abi:y,funcName:u}){let n=y.find((t)=>t.name===u);if(!n)throw new Z("toolbox_evm_no_abi_fragment",{funcName:u});return n.stateMutability&&_u.includes(n.stateMutability)}function w5(y){return i(y)}function r(y,u=!1){return function({value:t,...T}){if(!e(y))throw new Z("toolbox_evm_provider_not_eip1193_compatible");let M={...T,gasPrice:"gasPrice"in T?T.gasPrice:void 0,maxFeePerGas:"maxFeePerGas"in T?T.maxFeePerGas:void 0,maxPriorityFeePerGas:"maxPriorityFeePerGas"in T?T.maxPriorityFeePerGas:void 0},{gasLimit:b,gasPrice:q,maxFeePerGas:W,maxPriorityFeePerGas:Q,...U}=M,D={...U,...u&&b!==void 0&&{gas:l(BigInt(b))},...u&&q!==void 0&&{gasPrice:l(BigInt(q))},...u&&W!==void 0&&{maxFeePerGas:l(BigInt(W))},...u&&Q!==void 0&&{maxPriorityFeePerGas:l(BigInt(Q))},value:l(BigInt(t||0))};return y.send("eth_sendTransaction",[D])}}function l5(y,u){let n=Xy(y,u);if(n)return i(n.toLowerCase());throw new Z("toolbox_evm_invalid_gas_asset_address")}var wu=ku.reduce((y,u)=>{return y[u]="0x0000000000000000000000000000000000000000",y},{}),lu={...wu,[K.Optimism]:"0x4200000000000000000000000000000000000042",[K.Polygon]:"0x0000000000000000000000000000000000001010"},Su=[K.Arbitrum,K.Aurora,K.Base,K.Optimism,K.Robinhood];function Xy({chain:y,symbol:u,ticker:n},t){try{let T=y===K.BinanceSmartChain&&u==="BNB"&&n==="BNB",M=y===K.Hyperevm&&u==="HYPE"&&n==="HYPE",b=y===t&&u===t&&n===t,q=Su.includes(y)&&u==="ETH"&&n==="ETH";if(b||T||M||q)return lu[t];return i(u.slice(n.length+1).replace(/^0X/,""))}catch{return null}}function c({provider:y}){return async({contractAddress:u,abi:n,funcName:t,funcParams:T=[],txOverrides:M})=>{let b=await Qy(u,n,y).getFunction(t).populateTransaction(...T.concat(M).filter((q)=>typeof q<"u"));return{...b,value:l(BigInt(b.value||0))}}}function m({chain:y,provider:u,isEIP1559Compatible:n=!0}){return wy(y).with(K.Gnosis,()=>{return async function(){try{let{gasPrice:T,maxPriorityFeePerGas:M}=await u.getFeeData();if(!T||M===null)throw new Z("toolbox_evm_no_fee_data");return{[Y.Average]:{maxFeePerGas:j(T,Y.Average),maxPriorityFeePerGas:j(M,Y.Average)},[Y.Fast]:{maxFeePerGas:j(T,Y.Fast),maxPriorityFeePerGas:j(M,Y.Fast)},[Y.Fastest]:{maxFeePerGas:j(T,Y.Fastest),maxPriorityFeePerGas:j(M,Y.Fastest)}}}catch(T){throw new Z("toolbox_evm_gas_estimation_error",{error:T.msg??T.toString()})}}}).with(K.Arbitrum,()=>{return async function(){try{let{gasPrice:T}=await u.getFeeData();if(!T)throw new Z("toolbox_evm_no_fee_data");let M=T*12n/10n;return{[Y.Average]:{gasPrice:M},[Y.Fast]:{gasPrice:j(M,Y.Fast)},[Y.Fastest]:{gasPrice:j(M,Y.Fastest)}}}catch(T){throw new Z("toolbox_evm_gas_estimation_error",{error:T.msg??T.toString()})}}}).otherwise(()=>{return async function(){try{let{maxFeePerGas:T,maxPriorityFeePerGas:M,gasPrice:b}=await u.getFeeData();if(n){if(T===null||M===null)throw new Z("toolbox_evm_no_fee_data");return{[Y.Average]:{maxFeePerGas:T,maxPriorityFeePerGas:M},[Y.Fast]:{maxFeePerGas:j(T,Y.Fast),maxPriorityFeePerGas:j(M,Y.Fast)},[Y.Fastest]:{maxFeePerGas:j(T,Y.Fastest),maxPriorityFeePerGas:j(M,Y.Fastest)}}}if(!b)throw new Z("toolbox_evm_no_gas_price");return{[Y.Average]:{gasPrice:b},[Y.Fast]:{gasPrice:j(b,Y.Fast)},[Y.Fastest]:{gasPrice:j(b,Y.Fastest)}}}catch(T){throw new Z("toolbox_evm_gas_estimation_error",{error:T.msg??T.toString()})}}})}function yy({provider:y,isEIP1559Compatible:u,signer:n,chain:t}){return async function({callProvider:M,contractAddress:b,abi:q,funcName:W,funcParams:Q=[],txOverrides:U={},feeOption:D=Y.Fast}){let X=M||y;if(!b)throw new Z("toolbox_evm_invalid_params",{error:"contractAddress must be provided"});let $=Vu({abi:q,funcName:W});if($&&e(X)&&n){let z=c({chain:t,provider:X}),O=U?.from||await n?.getAddress(),G=await z({abi:q,contractAddress:b,funcName:W,funcParams:Q,txOverrides:{...U,from:O}});return r(X)(G)}let B=Qy(b,q,X);if($){if(!n)throw new Z("toolbox_evm_no_signer");let z=U?.from||await n.getAddress();if(!z)throw new Z("toolbox_evm_no_signer_address");let O=B.connect(n),G=m({chain:t,isEIP1559Compatible:u,provider:y}),[{maxFeePerGas:N,maxPriorityFeePerGas:I,gasPrice:x},_,F]=await Promise.all([G().then((V)=>V[D]),B.getFunction(W).estimateGas(...Q,U),U?.nonce!==void 0?Promise.resolve(U.nonce):X.getTransactionCount(z)]),S=await O[W](...Q,{...U,gasLimit:_,gasPrice:x,maxFeePerGas:N,maxPriorityFeePerGas:I,nonce:F});return typeof S?.hash==="string"?S?.hash:S}let k=await B[W]?.(...Q);return typeof k?.hash==="string"?k?.hash:k}}function ly({provider:y,chain:u}){return function({assetAddress:t,spenderAddress:T,from:M}){return yy({chain:u,isEIP1559Compatible:!0,provider:y})({abi:o,contractAddress:t,funcName:"allowance",funcParams:[M,T]})}}function Ku({provider:y,chain:u}){return async function({assetAddress:t,spenderAddress:T,from:M,amount:b=Wy}){let q=await ly({chain:u,provider:y})({assetAddress:t,from:M,spenderAddress:T});return s.fromBigInt(q).gte(s.fromBigInt(BigInt(b)))}}function Ou({signer:y,isEIP1559Compatible:u=!0,provider:n,chain:t}){return async function({assetAddress:M,spenderAddress:b,feeOptionKey:q=Y.Fast,amount:W,gasLimitFallback:Q,from:U,nonce:D}){let X=[b,BigInt(W||Wy)],$=await y?.getAddress()||U,B={abi:o,contractAddress:M,funcName:"approve",funcParams:X,signer:y,txOverrides:{from:$}};if(e(n)){let z=c({chain:t,provider:n}),O=r(n),G=await z(B);return O(G)}return yy({chain:t,isEIP1559Compatible:u,provider:n,signer:y})({...B,feeOption:q,funcParams:X,txOverrides:{from:$,gasLimit:Q?BigInt(Q.toString()):void 0,nonce:D}})}}function fu({chain:y,estimateGasPrices:u,estimateAdditionalFee:n,signer:t,isEIP1559Compatible:T=!0,provider:M}){return async function({assetValue:q,memo:W,recipient:Q,feeOptionKey:U=Y.Fast,sender:D,sweep:X,...$}){let B=D||await t?.getAddress(),k=Oy({chain:y,estimateGasPrices:u,isEIP1559Compatible:T,provider:M,signer:t});if(!B)throw new Z("toolbox_evm_no_from_address");let z=await fy({chain:y,estimateAdditionalFee:n,estimateGasPrices:u,isEIP1559Compatible:T,provider:M,signer:t})({assetValue:q,feeOptionKey:U,memo:W,recipient:Q,sender:B,sweep:X,...$});return k({...z,feeOptionKey:U,sweep:X})}}function Sy({provider:y,signer:u}){return function({contractAddress:t,abi:T,funcName:M,funcParams:b=[],txOverrides:q}){if(!t)throw new Z("toolbox_evm_no_contract_address");let W=Qy(t,T,y);return u?W.connect(u).getFunction(M).estimateGas(...b,q):W.getFunction(M).estimateGas(...b,q)}}function xu({provider:y,signer:u}){return async function({assetValue:t,recipient:T,memo:M,data:b,sender:q,funcName:W,funcParams:Q,txOverrides:U}){let D=t.bigIntValue,X=t.isGasAsset?null:Xy(t,t.chain);if(X&&W)return Sy({provider:y,signer:u})({abi:o,contractAddress:X,funcName:W,funcParams:Q,txOverrides:U});let{hexlify:$,toUtf8Bytes:B}=await import("ethers");return y.estimateGas({data:b?b:M?$(B(M)):void 0,from:q,to:T,value:D})}}function Fu({signer:y}){if(!y)return;return(u)=>zy(y,u)}var Ky=(y)=>y.type===4||(y.authorizations?.length??0)>0,Iu=(y)=>!Ky(y)&&(y.type===2||!!y.maxFeePerGas||!!y.maxPriorityFeePerGas),Eu=(y,u=!0)=>{if(Ky(y))return"eip7702";if(Iu(y)||u)return"eip1559";return"legacy"};function Au(y,u){if(y==="legacy"){if(u.gasPrice===void 0)throw new Z("toolbox_evm_no_fee_data");return{gasPrice:l(u.gasPrice)}}if(u.maxFeePerGas===void 0||u.maxPriorityFeePerGas===void 0)throw new Z("toolbox_evm_no_fee_data");return{maxFeePerGas:l(u.maxFeePerGas),maxPriorityFeePerGas:l(u.maxPriorityFeePerGas)}}function Oy({chain:y,estimateGasPrices:u,provider:n,signer:t,isEIP1559Compatible:T=!0}){return async function({feeOptionKey:b=Y.Fast,sweep:q,...W}){let{from:Q,to:U,data:D,value:X,...$}=W;if(!t)throw new Z("toolbox_evm_no_signer");if(!U)throw new Z("toolbox_evm_no_to_address");let B={...$,data:D||"0x",from:Q,to:U,value:BigInt(X||0)};if(e(n))return r(n,q)(B);let k=Q||await t.getAddress(),[z,O]=await Promise.all([W.nonce!==void 0?Promise.resolve(W.nonce):n.getTransactionCount(k),n.getNetwork()]),G=O.chainId,N=Eu(B,T);if(N==="eip7702")jy(B.authorizations||[],G);let I=wy(N).with("eip7702",()=>4).with("eip1559",()=>2).otherwise(()=>0),x={...B,chainId:G,nonce:z,type:I,...N==="eip7702"&&{authorizationList:B.authorizations}},_=u??m({chain:y,isEIP1559Compatible:T,provider:n}),S=(N==="legacy"?B.gasPrice!==void 0:B.maxFeePerGas!==void 0&&B.maxPriorityFeePerGas!==void 0)?{}:Au(N,(await _())[b]),V;try{V=l(B.gasLimit?BigInt(B.gasLimit):await n.estimateGas(x)*11n/10n)}catch(w){throw new Z("toolbox_evm_error_estimating_gas_limit",{error:w})}try{let w={...x,gasLimit:V,...S};try{return(await t.sendTransaction(w)).hash}catch{let E=await t.signTransaction({...w,from:k});return(await n.broadcastTransaction(E)).hash}}catch(w){throw new Z("toolbox_evm_error_sending_transaction",{error:w})}}}function A(y,u){throw new Z({errorKey:"toolbox_sweep_amount_too_low",info:{balance:y.toString(),fee:u.toString()}})}function Pu(y,u){if(u){if(y.maxFeePerGas===void 0||y.maxPriorityFeePerGas===void 0)throw new Z("toolbox_evm_no_fee_data");return{feeFields:{maxFeePerGas:y.maxFeePerGas,maxPriorityFeePerGas:y.maxPriorityFeePerGas},feePerGas:y.maxFeePerGas}}if(y.gasPrice===void 0)throw new Z("toolbox_evm_no_fee_data");return{feeFields:{gasPrice:y.gasPrice},feePerGas:y.gasPrice}}function ou(y,u){if(y>u)return y;return u}function Cu(y){return ju(y).reduce((u,n)=>u+(n===0?4n:16n),21000n)}function qy(y){let n=(y instanceof Error?y.message:String(y)).toLowerCase();return n.includes("insufficient funds")||n.includes("insufficient balance")||n.includes("gas required exceeds allowance")}async function gu({balance:y,estimateAdditionalFee:u,feeOptionKey:n,l2Reserve:t,sweepTx:T,valueBeforeL1Fee:M}){if(!u)return 0n;let b=await u({...T,value:M})??0n,q=j(b,n),W=M-q;if(W<=0n)A(y,t+q);let Q=await u({...T,value:W})??b;return j(ou(b,Q),n)}async function vu({chain:y,data:u,estimateGasPrices:n,estimateAdditionalFee:t,feeOptionKey:T,from:M,isEIP1559Compatible:b,provider:q,recipient:W,transaction:Q}){let U=await q.getBalance(M),D=n??m({chain:y,isEIP1559Compatible:b,provider:q}),{feeFields:X,feePerGas:$}=Pu((await D())[T],b),k={...t&&Q.nonce===void 0?{...Q,nonce:await q.getTransactionCount(M)}:Q,...X,data:u,from:M,to:W},z=Cu(u)*$;if(U-z<=0n)A(U,z);let O=(U-z)/2n,G;try{G=await q.estimateGas({...k,value:O})}catch(v){if(!qy(v))throw v;try{G=await q.estimateGas({...k,value:0n})}catch(By){if(qy(By))A(U,z);throw By}}let N=G*$;if(U-N<=0n)A(U,N);let _=G*11n/10n*$,F=U>_?U-_:0n,S;try{S=await q.estimateGas({...k,value:F})}catch(v){if(!qy(v))throw v;A(U,_)}let V=S*$,w=U-V;if(w<=0n)A(U,V);let E=await gu({balance:U,estimateAdditionalFee:t,feeOptionKey:T,l2Reserve:V,sweepTx:{...k,gasLimit:S},valueBeforeL1Fee:w}),P=w-E;if(P<=0n)A(U,V+E);return{...k,gasLimit:S,value:l(P)}}async function hu({chain:y,contractAddress:u,from:n,provider:t}){let T=await yy({chain:y,provider:t})({abi:o,contractAddress:u,funcName:"balanceOf",funcParams:[n]});if(typeof T!=="bigint")throw new Z({errorKey:"toolbox_evm_invalid_transaction",info:{error:"Invalid ERC20 balance"}});return T}function fy({chain:y,estimateAdditionalFee:u,estimateGasPrices:n,isEIP1559Compatible:t=!0,provider:T,signer:M}){return async function({assetValue:q,authorizations:W,memo:Q,recipient:U,data:D,feeOptionKey:X=Y.Fast,gasLimit:$,sender:B,sweep:k,type:z,maxFeePerGas:O,maxPriorityFeePerGas:G,gasPrice:N,...I}){let x=q.getBaseValue("bigint"),_=B||await M?.getAddress();if(!_)throw new Z("toolbox_evm_no_from_address");if(k&&(z===4||(W?.length??0)>0))throw new Z({errorKey:"toolbox_not_supported",info:{chain:y,reason:"EIP-7702 sweep transactions are not supported"}});if(Lu(q)){let{hexlify:w,toUtf8Bytes:E}=await import("ethers"),P=D||w(E(Q||""));if(!k)return{...I,...W!==void 0&&{authorizations:W},data:P,from:_,...$!==void 0&&{gasLimit:$},to:U,...z!==void 0&&{type:z},value:l(x)};return vu({chain:y,data:P,estimateAdditionalFee:u,estimateGasPrices:n,feeOptionKey:X,from:_,isEIP1559Compatible:t,provider:T,recipient:U,transaction:{nonce:I.nonce,type:z}})}let F=Xy(q,y);if(!F)throw new Z("toolbox_evm_no_contract_address");let S=c({chain:y,provider:T}),V=k?await hu({chain:y,contractAddress:F,from:_,provider:T}):x;if(k&&V<=0n)throw new Z({errorKey:"toolbox_sweep_amount_too_low",info:{asset:q.toString(),balance:V.toString()}});return S({abi:o,contractAddress:F,funcName:"transfer",funcParams:[U,V],txOverrides:{from:_,gasLimit:$,gasPrice:N,maxFeePerGas:O,maxPriorityFeePerGas:G}})}}function cu({provider:y,signer:u,chain:n}){return async function({assetAddress:T,spenderAddress:M,amount:b,from:q}){let W=await u?.getAddress()||q,Q=c({chain:n,provider:y}),U=["bigint","number"].includes(typeof b)?b:b||Wy;return await Q({abi:o,contractAddress:T,funcName:"approve",funcParams:[M,BigInt(U)],txOverrides:{from:W}})}}function mu({provider:y,isEIP1559Compatible:u=!0,chain:n}){return async function({feeOption:T=Y.Fast,...M}){let q=await m({chain:n,isEIP1559Compatible:u,provider:y})(),W=await y.estimateGas(M),Q=Du.from({chain:n}),{gasPrice:U,maxFeePerGas:D,maxPriorityFeePerGas:X}=q[T];if(!u&&U)return Q.set(s.fromBigInt(U*W,Q.decimal));if(D&&X){let $=(D+X)*W;return Q.set(s.fromBigInt($,Q.decimal))}throw new Z("toolbox_evm_no_gas_price")}}import{applyFeeMultiplierToBigInt as C,Chain as Iy,derivationPathToString as pu,FeeOption as f,getChainConfig as du,resolveDerivationPath as su,SwapKitError as g}from"@swapkit/helpers";import{Contract as iu,HDNodeWallet as au}from"ethers";import{match as eu,P as Fy}from"ts-pattern";var xy=[{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:[{internalType:"uint256",name:"_gasUsed",type:"uint256"}],name:"getOperatorFee",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 ru="0x420000000000000000000000000000000000000f";function uy(y){return new iu(ru,xy,y)}function Ey(y){return async function(){let n=uy(y);if(n&&"l1BaseFee"in n)return await n?.l1BaseFee();return}}function Ay(y){return async function({from:n,to:t,nonce:T,...M}){let{Transaction:b}=await import("ethers");if(!t)throw new g("toolbox_evm_invalid_transaction",{error:"Missing to address"});return b.from({...M,authorizationList:M.authorizationList,nonce:T??(n?await y.getTransactionCount(n):0),to:t}).unsignedSerialized}}function Yy(y,u){return async function(t){let T=uy(y),M=t.chainId??u??(await y.getNetwork()).chainId,b=await Ay(y)({...t,chainId:M});if(T&&"getL1Fee"in T){let q=await T.getL1Fee(b);if(typeof q!=="bigint")throw new g({errorKey:"toolbox_evm_invalid_transaction",info:{error:"Invalid OP-stack L1 fee"}});return q}return}}function Zy(y){return async function(n){let t=uy(y);if(t&&"getOperatorFee"in t){let T=await t.getOperatorFee(n);if(typeof T!=="bigint")throw new g({errorKey:"toolbox_evm_invalid_transaction",info:{error:"Invalid OP-stack operator fee"}});return T}return 0n}}function y5(y,u){let n=Yy(y,u),t=Zy(y);return async function(M){if(M.gasLimit===void 0||M.gasLimit===null)throw new g({errorKey:"toolbox_evm_invalid_transaction",info:{error:"Missing gas limit for OP-stack fee estimation"}});let[b,q]=await Promise.all([n(M),t(BigInt(M.gasLimit.toString()))]);return(b??0n)+q}}function Py(y){return async function(n){let T=(await y.getFeeData()).gasPrice??0n,M=await y.estimateGas(n);return T*M}}function u5(y){return async function(n){let t=await Yy(y)(n)??0n,T=await Py(y)(n),M=n.gasLimit??await y.estimateGas(n),b=await Zy(y)(BigInt(M.toString()));return t+T+b}}function n5(y){return async function(n){let t=uy(y),T=await Ay(y)(n);if(t&&"getL1GasUsed"in t)return t.getL1GasUsed(T)}}function t5(y){return async function(){try{let{maxFeePerGas:n,maxPriorityFeePerGas:t,gasPrice:T}=await y.getFeeData(),M=await Ey(y)(),b=T;if(!(n&&t))throw new g("toolbox_evm_no_fee_data");return{[f.Average]:{gasPrice:b,l1GasPrice:M,maxFeePerGas:n,maxPriorityFeePerGas:t},[f.Fast]:{gasPrice:C(b,f.Fast),l1GasPrice:C(M||0n,f.Fast),maxFeePerGas:n,maxPriorityFeePerGas:C(t,f.Fast)},[f.Fastest]:{gasPrice:C(b,f.Fastest),l1GasPrice:C(M||0n,f.Fastest),maxFeePerGas:n,maxPriorityFeePerGas:C(t,f.Fastest)}}}catch(n){throw new g("toolbox_evm_gas_estimation_error",{error:n.msg??n.toString()})}}}function ny(y){return function({provider:n,...t}){let T=eu(t).with({phrase:Fy.string},({phrase:X,derivationPath:$,index:B=0})=>au.fromPhrase(X,void 0,pu(su({chain:y,derivationPath:$,index:B}))).connect(n)).with({signer:Fy.any},({signer:X})=>X).otherwise(()=>{return}),M=Ey(n),b=BigInt(du(y).chainId),q=Yy(n,b),W=y5(n,b),Q=Zy(n),U=y===Iy.Optimism?t5(n):void 0;return{...a({chain:y,estimateAdditionalFee:W,estimateGasPrices:U,provider:n,signer:T}),estimateL1Gas:n5(n),estimateL1GasCost:q,estimateL2GasCost:Py(n),estimateOperatorFee:Q,estimateTotalGasCost:u5(n),getBalance:d(y).getBalance,getL1GasPrice:M}}}var oy=ny(Iy.Optimism);function gy(y){let u=L(R.Ethereum)(y);async function n(t,T="0x5ba1e12693dc8f9c48aad8770482f4739beed696",M="aggregate",b=M5.Fast){let q=await u.createContractTxObject({abi:Gy,contractAddress:T,funcName:M,funcParams:[t]});return u.sendTransaction({...q,feeOptionKey:b})}return{...u,multicall:n}}var vy=L(R.Adi),hy=L(R.Arbitrum),cy=L(R.Aurora),my=L(R.Avalanche),py=ny(R.Base),dy=L(R.Berachain),sy=L(R.BinanceSmartChain),m5=L(R.Botanix),iy=L(R.Core),ay=L(R.Corn),ey=L(R.Cronos),ry=L(R.Gnosis),yu=L(R.Robinhood),uu=L(R.Hyperevm),nu=L(R.Linea),tu=L(R.Polygon),Tu=L(R.MegaETH),Mu=L(R.Monad),bu=L(R.Plasma),$y=L(R.Sonic),qu=ny(R.Unichain),Uu=L(R.XLayer);function L(y){return function({provider:n,...t}){let T=Ly(y),M=U5(t).with({phrase:Cy.string},({phrase:q,derivationPath:W,index:Q=0})=>q5.fromPhrase(q,void 0,T5(b5({chain:y,derivationPath:W,index:Q}))).connect(n)).with({signer:Cy.any},({signer:q})=>q).otherwise(()=>{return});return a({chain:y,isEIP1559Compatible:T,provider:n,signer:M})}}function X5(y,u){return Q5(y).with(H.Adi,()=>vy(u)).with(H.Arbitrum,()=>hy(u)).with(H.Aurora,()=>cy(u)).with(H.Avalanche,()=>my(u)).with(H.Base,()=>py(u)).with(H.Berachain,()=>dy(u)).with(H.BinanceSmartChain,()=>sy(u)).with(H.Botanix,()=>$y(u)).with(H.Core,()=>iy(u)).with(H.Corn,()=>ay(u)).with(H.Cronos,()=>ey(u)).with(H.Ethereum,()=>gy(u)).with(H.Gnosis,()=>ry(u)).with(H.Hyperevm,()=>uu(u)).with(H.Linea,()=>nu(u)).with(H.MegaETH,()=>Tu(u)).with(H.Monad,()=>Mu(u)).with(H.Optimism,()=>oy(u)).with(H.Plasma,()=>bu(u)).with(H.Polygon,()=>tu(u)).with(H.Robinhood,()=>yu(u)).with(H.Sonic,()=>$y(u)).with(H.Unichain,()=>qu(u)).with(H.XLayer,()=>Uu(u)).exhaustive()}async function a5(y,u){let n={...u,provider:u?.provider||Dy(y)};return X5(y,n)}
2
- export{d as b,k5 as c,$u as d,B5 as e,Dy as f,l as g,ky as h,Ly as i,zy as j,Hu as k,Ru as l,J5 as m,jy as n,Wy as o,a as p,Gu as q,e as r,Qy as s,Nu as t,Vu as u,w5 as v,r as w,l5 as x,lu as y,Xy as z,c as A,m as B,Ky as C,Iu as D,Eu as E,ny as F,oy as G,gy as H,vy as I,hy as J,cy as K,my as L,py as M,dy as N,sy as O,m5 as P,iy as Q,ay as R,ey as S,ry as T,yu as U,uu as V,nu as W,tu as X,Tu as Y,Mu as Z,bu as _,$y as $,qu as aa,Uu as ba,X5 as ca,a5 as da};
@@ -1 +0,0 @@
1
- import{$ as N,A as m,B as n,C as o,D as p,E as q,F as r,G as s,H as t,I as u,J as v,K as w,L as x,M as y,N as z,O as A,P as B,Q as C,R as D,S as E,T as F,U as G,V as H,W as I,X as J,Y as K,Z as L,_ as M,aa as O,ba as P,ca as Q,da as R,o as a,p as b,q as c,r as d,s as e,t as f,u as g,v as h,w as i,x as j,y as k,z as l}from"./chunk-ye1j5pz2.js";import"./chunk-nvfkjbyq.js";import"./chunk-xbdjxeqd.js";export{c as validateEVMAddress,h as toChecksumAddress,g as isStateChangingCall,o as isEIP7702Transaction,p as isEIP1559Transaction,d as isBrowserProvider,l as getTokenAddress,R as getEvmToolboxAsync,Q as getEvmToolbox,n as getEstimateGasPrices,q as getEVMTxType,i as getEIP1193SendTransaction,m as getCreateContractTxObject,f as getCreateContract,j as getChecksumAddressFromAsset,r as createOPStackToolbox,e as createContract,P as XLayerToolbox,O as UNIToolbox,N as SONICToolbox,M as PLASMAToolbox,s as OPToolbox,L as MONADToolbox,K as MEGAETHToolbox,a as MAX_APPROVAL,J as MATICToolbox,I as LINEAToolbox,H as HYPEREVMToolbox,G as HOODToolbox,F as GNOToolbox,t as ETHToolbox,k as ContractAddress,E as CROToolbox,D as CORNToolbox,C as COREToolbox,B as BotanixToolbox,b as BaseEVMToolbox,A as BSCToolbox,z as BERAToolbox,y as BASEToolbox,x as AVAXToolbox,w as AURORAToolbox,v as ARBToolbox,u as ADIToolbox};
File without changes