@0x/0x-parser
Advanced tools
Comparing version 1.2.4 to 1.2.5
@@ -1,1 +0,1 @@ | ||
"use strict";var M=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var Q=Object.prototype.hasOwnProperty;var tt=(a,e)=>{for(var n in e)M(a,n,{get:e[n],enumerable:true})};var et=(a,e,n,r)=>{if(e&&typeof e==="object"||typeof e==="function"){for(let s of J(e))if(!Q.call(a,s)&&s!==n)M(a,s,{get:()=>e[s],enumerable:!(r=z(e,s))||r.enumerable})}return a};var nt=a=>et(M({},"__esModule",{value:true}),a);var Pt={};tt(Pt,{parseSwap:()=>Ft,parseSwapV2:()=>q});module.exports=nt(Pt);var L=require("viem");var K=require("viem/chains");var m=require("viem");var N={LimitOrderFilled:"0xab614d2b738543c0ea21f56347cf696a3a0c42a7cbec3212a5ca22a4dcff2124",LiquidityProviderSwap:"0x40a6ba9513d09e3488135e0e0d10e2d4382b792720155b144cbea89ac9db6d34",OtcOrderFilled:"0xac75f773e3a92f1a02b12134d65e1f47f8a14eabe4eaf1e24624918e6a8b269f",MetaTransactionExecuted:"0x7f4fe3ff8ae440e1570c558da08440b26f89fb1c1f2910cd91ca6452955f121a",Transfer:"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",TransformedERC20:"0x0f6672f78a59ba8e5e5b5d38df3ebc67f3c792e2c9259b8d97d7f00dd78ba1b3"};var v={permitAndCall:{IERC2612:"0x34b4d153",IERC20PermitAllowed:"0x7f9cb4d7",IERC20MetaTransaction:"0x9d50b5e4"}};var V="https://raw.githubusercontent.com/0xProject/protocol/development/packages/contract-artifacts/artifacts/IZeroEx.json";var S="0xDef1C0ded9bec7F1a1670819833240f027b25EfF";var U={1:"0x1291C02D288de3De7dC25353459489073D11E1Ae",137:"0x2Ddd30fE5c12FC4CD497526F14Bf3d1fcd3D5Db4",8453:"0x3CA53031Ad0B86a304845e83644983Be3340895f"};var O={1:S,5:"0xF91bB752490473B8342a3E964E855b9f9a2A668e",10:"0xDEF1ABE32c034e558Cdd535791643C58a13aCC10",56:S,137:S,250:"0xDEF189DeAEF76E379df891899eb5A00a94cBC250",8453:S,42161:S,42220:S,43114:S};var w={weth:"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"};var g={symbol:"ETH",decimals:18,address:"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"};var Y={1:"ETH",5:"ETH",10:"ETH",56:"BNB",137:"MATIC",250:"FTM",8453:"ETH",42161:"ETH",42220:"CELO",43114:"AVAX"};var F=[{type:"function",name:"decimals",stateMutability:"view",inputs:[],outputs:[{name:"",type:"uint8"}]},{type:"function",name:"symbol",stateMutability:"view",inputs:[],outputs:[{name:"",type:"string"}]}];var I=require("viem");function X(a){return`0x${a.slice(-40)}`}function G(a){return[1,5,10,56,137,250,8453,42220,43114,42161].includes(a)}function $(a){return[1,137,8453].includes(a)}async function T({publicClient:a,transactionReceipt:e}){const{logs:n}=e;const r=n.filter(d=>d.topics[0]===N.Transfer).map(d=>({...d,address:(0,I.getAddress)(d.address)}));const s=[...r.map(d=>({abi:F,address:d.address,functionName:"symbol"})),...r.map(d=>({abi:F,address:d.address,functionName:"decimals"}))];const t=await a.multicall({contracts:s});const o=Math.floor(t.length/2);const c=r.map((d,i)=>{const l=t[i].result;const u=t[o+i].result;const p=(0,I.formatUnits)(BigInt(d.data),u);const{address:x,topics:y}=d;const{1:h,2:f}=y;const A=(0,I.getAddress)(X(h));const k=(0,I.getAddress)(X(f));return{to:k,from:A,symbol:l,amount:p,address:x,decimals:u}}).filter(d=>d.amount!=="0");return c}function C(a,e){return{tokenIn:{symbol:a.symbol,amount:a.amount,address:(0,I.getAddress)(a.address)},tokenOut:{symbol:e.symbol,amount:e.amount,address:(0,I.getAddress)(e.address)}}}var P=[{inputs:[{internalType:"contract IERC2612",name:"token",type:"address"},{internalType:"address",name:"owner",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{internalType:"uint256",name:"deadline",type:"uint256"},{internalType:"uint8",name:"v",type:"uint8"},{internalType:"bytes32",name:"r",type:"bytes32"},{internalType:"bytes32",name:"s",type:"bytes32"},{internalType:"bytes",name:"data",type:"bytes"}],name:"permitAndCall",outputs:[{internalType:"bytes",name:"",type:"bytes"}],stateMutability:"payable",type:"function"},{inputs:[{internalType:"contract IERC20PermitAllowed",name:"token",type:"address"},{internalType:"address",name:"owner",type:"address"},{internalType:"uint256",name:"nonce",type:"uint256"},{internalType:"uint256",name:"deadline",type:"uint256"},{internalType:"bool",name:"allowed",type:"bool"},{internalType:"uint8",name:"v",type:"uint8"},{internalType:"bytes32",name:"r",type:"bytes32"},{internalType:"bytes32",name:"s",type:"bytes32"},{internalType:"bytes",name:"data",type:"bytes"}],name:"permitAndCall",outputs:[{internalType:"bytes",name:"",type:"bytes"}],stateMutability:"payable",type:"function"},{inputs:[{internalType:"contract IERC20MetaTransaction",name:"token",type:"address"},{internalType:"address",name:"owner",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{internalType:"uint8",name:"v",type:"uint8"},{internalType:"bytes32",name:"r",type:"bytes32"},{internalType:"bytes32",name:"s",type:"bytes32"},{internalType:"bytes",name:"data",type:"bytes"}],name:"permitAndCall",outputs:[{internalType:"bytes",name:"",type:"bytes"}],stateMutability:"payable",type:"function"},{inputs:[],name:"target",outputs:[{internalType:"address payable",name:"",type:"address"}],stateMutability:"view",type:"function"}];async function st({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=(0,m.getAddress)(e.from);const s=n.find(o=>r===o.from);const t=n.find(o=>r===o.to);if(s&&t){return C(s,t)}}async function ot({publicClient:a,transactionReceipt:e}){const n=(0,m.getAddress)(e.from);const r=await T({publicClient:a,transactionReceipt:e});const s=r.find(o=>n===o.from);const t=r.find(o=>o.address===w.weth);if(s&&t){t.symbol=g.symbol;t.address=g.address;return C(s,t)}}async function at({publicClient:a,transactionReceipt:e}){const n=(0,m.getAddress)(e.from);const r=await T({publicClient:a,transactionReceipt:e});const s=r.find(o=>o.address===w.weth);const t=r.find(o=>o.to===n);if(s&&t){s.symbol=g.symbol;s.address=g.address;return C(s,t)}}async function rt({callData:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const{args:s}=(0,m.decodeFunctionData)({abi:n,data:a});const[t]=s;const o=await T({publicClient:e,transactionReceipt:r});const{maker:c,taker:d}=t;const i=o.find(u=>u.from===d);const l=o.find(u=>u.from===c);if(i&&l){return C(i,l)}}async function it({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=(0,m.getAddress)(e.from);const s=n.find(o=>o.from===r);const t=n.find(o=>o.to===r);if(s&&t){return C(s,t)}}async function ct({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=n.find(t=>t.address!==w.weth);const s=n.find(t=>t.address===w.weth);if(r&&s){s.symbol=g.symbol;s.address=g.address;return C(r,s)}}async function dt({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=(0,m.getAddress)(e.from);const s=n.find(o=>o.from===r);const t=n.find(o=>o.address===w.weth);if(s&&t){t.symbol=g.symbol;t.address=g.address;return C(s,t)}}async function lt({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=(0,m.getAddress)(e.from);const s=n.find(o=>r===o.from);const t=n.find(o=>r===o.to);if(s&&t){return C(s,t)}}async function ut({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=(0,m.getAddress)(e.from);const s=n.find(o=>o.address===w.weth);const t=n.find(o=>r===o.to);if(s&&t){s.symbol=g.symbol;s.address=g.address;return C(s,t)}}async function mt({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=n[0];const s=n[n.length-1];return C(r,s)}async function pt({chainId:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const s=Y[a];for(const t of r.logs){const{topics:o,data:c}=t;const[d]=o;if(d===N.TransformedERC20){let i;let l;let u;let p;const x=(0,m.decodeEventLog)({data:c,topics:o,abi:n});const{inputToken:y,outputToken:h,inputTokenAmount:f,outputTokenAmount:A}=x.args;const k={address:y,abi:F};const E={address:h,abi:F};if(y===g.address){i=s;l=18}else{[{result:i},{result:l}]=await e.multicall({contracts:[{...k,functionName:"symbol"},{...k,functionName:"decimals"}]})}if(h===g.address){u=s;p=18}else{[{result:u},{result:p}]=await e.multicall({contracts:[{...E,functionName:"symbol"},{...E,functionName:"decimals"}]})}const H=(0,m.formatUnits)(f,l);const Z=(0,m.formatUnits)(A,p);return{tokenIn:{address:y,amount:H,symbol:i},tokenOut:{address:h,amount:Z,symbol:u}}}}}async function ft({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=(0,m.getAddress)(e.from);const s=n.find(o=>r===o.from);const t=n.find(o=>r===o.to);if(s&&t){return C(s,t)}}async function bt({callData:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const s=await T({publicClient:e,transactionReceipt:r});const{args:t}=(0,m.decodeFunctionData)({abi:n,data:a});const[o]=t;return s.reduce((c,d)=>{const{symbol:i,amount:l,address:u}=d;if(u===w.weth){return{...c,tokenOut:{...c.tokenOut,amount:(Number(c.tokenOut.amount)+Number(l)).toString()}}}else if(u===o){return{...c,tokenIn:{...c.tokenIn,symbol:i,address:u,amount:(Number(c.tokenIn.amount)+Number(l)).toString()}}}else{return c}},{tokenIn:{address:"",amount:"",symbol:""},tokenOut:{amount:"",symbol:g.symbol,address:g.address}})}async function yt({callData:a,transaction:e,publicClient:n,exchangeProxyAbi:r,transactionReceipt:s}){const{value:t}=e;const{args:o}=(0,m.decodeFunctionData)({abi:r,data:a});const c=await T({publicClient:n,transactionReceipt:s});const[d]=o;const i=1000000000000000000n;const l=t/i;const u=t%i;const p=Number(l)+Number(u)/Number(i);const x=c.reduce((f,A)=>{return A.address===d?f+Number(A.amount):f},0);const y=c.find(f=>f.address===w.weth);const h=c.find(f=>f.address===d);if(y&&h){return{tokenIn:{symbol:g.symbol,amount:p.toString(),address:g.address},tokenOut:{symbol:h.symbol,amount:x.toString(),address:h.address}}}}async function gt({callData:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const s=await T({publicClient:e,transactionReceipt:r});const{args:t}=(0,m.decodeFunctionData)({abi:n,data:a});const[o,c]=t;const d={[o]:{amount:"0",symbol:"",address:""},[c]:{amount:"0",symbol:"",address:""}};s.forEach(({address:i,symbol:l,amount:u})=>{if((0,m.getAddress)(i)in d){d[(0,m.getAddress)(i)].address=(0,m.getAddress)(i);d[(0,m.getAddress)(i)].symbol=l;d[(0,m.getAddress)(i)].amount=(Number(d[(0,m.getAddress)(i)].amount)+Number(u)).toString()}});return{tokenIn:d[o],tokenOut:d[c]}}async function Tt({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=(0,m.getAddress)(O[56]);const s=(0,m.getAddress)(e.from);let t=n.find(c=>c.from===s);let o=n.find(c=>c.from!==s);if(t===void 0){t=n.find(c=>c.from===r);o=n.find(c=>c.from!==r)}if(!n.some(c=>c.to===s)){o=n.find(c=>c.to===r)}if(t&&o){return C(t,o)}}async function Et({callData:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const{args:s}=(0,m.decodeFunctionData)({abi:n,data:a});const t=await T({publicClient:e,transactionReceipt:r});const[o]=s;const{makerToken:c,takerToken:d}=o;const i=t.find(u=>u.address===d);const l=t.find(u=>u.address===c);if(i&&l){l.symbol=g.symbol;l.address=g.address;return C(i,l)}}async function At({callData:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const{args:s}=(0,m.decodeFunctionData)({abi:n,data:a});const t=await T({publicClient:e,transactionReceipt:r});const[o]=s;const{makerToken:c,takerToken:d}=o;const i=t.find(u=>u.address===d);const l=t.find(u=>u.address===c);if(i&&l){i.symbol=g.symbol;i.address=g.address;return C(i,l)}}async function Ct({callData:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const{args:s}=(0,m.decodeFunctionData)({abi:n,data:a});const[t]=s;const{maker:o,taker:c}=t;const d=await T({publicClient:e,transactionReceipt:r});if(typeof o==="string"&&typeof c==="string"){const i=d.find(u=>u.from===c);const l=d.find(u=>u.from===o);if(i&&l){return C(i,l)}}}async function xt({callData:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const{args:s}=(0,m.decodeFunctionData)({abi:n,data:a});const[t]=s;const{signer:o}=t;const c=await T({publicClient:e,transactionReceipt:r});if(typeof o==="string"){const d=c.find(l=>l.from===o);const i=c.find(l=>l.to===o);if(d&&i){return C(d,i)}}}async function ht({chainId:a,transaction:e,publicClient:n,exchangeProxyAbi:r,transactionReceipt:s,callData:t}){const{args:o}=(0,m.decodeFunctionData)({abi:r,data:t});const[c]=o;const{callData:d}=c;const{functionName:i}=(0,m.decodeFunctionData)({data:d,abi:r});const l=_[i];return l({chainId:a,callData:d,transaction:e,publicClient:n,exchangeProxyAbi:r,transactionReceipt:s})}function kt({chainId:a,transaction:e,publicClient:n,exchangeProxyAbi:r,transactionReceipt:s}){const{args:t}=(0,m.decodeFunctionData)({abi:P,data:e.input});let o;const c=(0,m.encodeFunctionData)({abi:P,functionName:"permitAndCall",args:[...t]});const d=c.slice(0,10);const{IERC2612:i,IERC20PermitAllowed:l,IERC20MetaTransaction:u}=v.permitAndCall;switch(d){case i:o=t[7];break;case l:o=t[8];break;case u:o=t[6];break}if(o){const{functionName:p}=(0,m.decodeFunctionData)({abi:r,data:o});const x=_[p];return x({chainId:a,callData:o,transaction:e,publicClient:n,exchangeProxyAbi:r,transactionReceipt:s})}}var _={fillLimitOrder:Ct,fillOtcOrder:it,fillOtcOrderForEth:Et,fillOtcOrderWithEth:At,fillTakerSignedOtcOrder:rt,fillTakerSignedOtcOrderForEth:ct,executeMetaTransaction:xt,executeMetaTransactionV2:ht,multiplexBatchSellTokenForToken:gt,multiplexBatchSellTokenForEth:bt,multiplexBatchSellEthForToken:yt,multiplexMultiHopSellTokenForToken:ft,multiplexMultiHopSellEthForToken:at,multiplexMultiHopSellTokenForEth:ot,permitAndCall:kt,sellToUniswap:mt,sellTokenForEthToUniswapV3:dt,sellEthForTokenToUniswapV3:ut,sellTokenForTokenToUniswapV3:lt,sellToLiquidityProvider:st,sellToPancakeSwap:Tt,transformERC20:pt};var b=require("viem");var j=[{inputs:[{components:[{internalType:"address",name:"recipient",type:"address"},{internalType:"contract IERC20",name:"buyToken",type:"address"},{internalType:"uint256",name:"minAmountOut",type:"uint256"}],internalType:"struct SettlerBase.AllowedSlippage",name:"slippage",type:"tuple"},{internalType:"bytes[]",name:"actions",type:"bytes[]"},{internalType:"bytes32",name:"",type:"bytes32"},{internalType:"address",name:"msgSender",type:"address"},{internalType:"bytes",name:"sig",type:"bytes"}],name:"executeMetaTxn",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"nonpayable",type:"function"}];var B="0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";var D={1:"ETH",10:"ETH",56:"BNB",137:"MATIC",8453:"ETH",42161:"ETH",43114:"AVAX"};var wt={1:"0x7C39a136EA20B3483e402EA031c1f3C019bAb24b",10:"0x4069560a180EbD76bB1aF947f5119Fe555BB4eA0",56:"0x73C25Ef091Ce3F2451946Be3f982549776bFED31",137:"0xF9332450385291b6dcE301917aF6905e28E8f35f",8453:"0x5CE929DDB01804bfF35B2F5c77b735bdB094AAc8",42161:"0x1aa84EB5cb62f686FC0D908AFd85864f4A05d5Ee",43114:"0x2adb2cE26848B94E13d2f7fE0fF7E945050D741c"};var Lt="0xcA11bde05977b3631167028862bE2a173976CA11";function It(a){return[1,10,56,137,8453,42161,43114].includes(a)}function R(a,e){let n=0n;function r(s){s.forEach(t=>{if(t.to.toLowerCase()===e.toLowerCase()&&(0,b.fromHex)(t.value,"bigint")>0n){n=n+(0,b.fromHex)(t.value,"bigint")}if(t.calls&&t.calls.length>0){r(t.calls)}})}r(a.calls);return(0,b.formatEther)(n)}async function St({publicClient:a,transactionReceipt:e}){const n={Transfer:"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"};const{logs:r}=e;const s=r.filter(i=>i.topics[0]===n.Transfer).map(i=>({...i,address:(0,b.getAddress)(i.address)}));const t=[...s.map(i=>({abi:b.erc20Abi,address:i.address,functionName:"symbol"})),...s.map(i=>({abi:b.erc20Abi,address:i.address,functionName:"decimals"}))];const o=await a.multicall({contracts:t});const c=Math.floor(o.length/2);const d=s.map((i,l)=>{const u=o[l].result;const p=o[c+l].result;const x=i.data==="0x"?"0":(0,b.formatUnits)(BigInt(i.data),p);const{address:y,topics:h}=i;const{1:f,2:A}=h;const k=(0,b.getAddress)(W(f));const E=(0,b.getAddress)(W(A));return{to:E,from:k,symbol:u,amount:x,address:y,decimals:p}}).filter(i=>i.amount!=="0");return d}function W(a){return`0x${a.slice(-40)}`}async function q({publicClient:a,transactionHash:e}){const n=await a.getChainId();if(!It(n)){throw new Error(`chainId ${n} is unsupported\u2026`)}const r=a.extend(f=>({async traceCall(A){return f.request({method:"debug_traceTransaction",params:[A.hash,{tracer:"callTracer"}]})}}));const s=await r.traceCall({hash:e});const t=await a.getTransaction({hash:e});const{from:o,value:c,to:d}=t;const i=R(s,o);const l=await a.getTransactionReceipt({hash:e});const u=c>0n;const p=await St({publicClient:a,transactionReceipt:l});let x=p[0];let y=i==="0"?p.find(f=>{return f.to.toLowerCase()===o.toLowerCase()}):{symbol:D[n],amount:i,address:B};if(d?.toLowerCase()===Lt.toLowerCase()){const{args:f}=(0,b.decodeFunctionData)({abi:b.multicall3Abi,data:t.input});const{args:A}=(0,b.decodeFunctionData)({abi:j,data:f[0][1].callData});const k=A[0].recipient.toLowerCase();const E=R(s,k);if(E==="0"){y=y=p[p.length-1]}else{y={symbol:D[n],amount:E,address:B}}}const h=d?.toLowerCase()===wt[n].toLowerCase();if(h){const{args:f}=(0,b.decodeFunctionData)({abi:j,data:t.input});const{3:A}=f;const k=R(s,A);if(k==="0"){y=p[p.length-1];const E=p.filter(H=>H.to.toLowerCase()===A.toLowerCase());if(E.length===1){y={symbol:E[0].symbol,amount:E[0].amount,address:E[0].address}}else{y={symbol:"",amount:"",address:""};console.error("More than one `takerReceived` log. File a bug report here: https://github.com/0xProject/0x-parser/issues/new/choose")}}else{y={symbol:D[n],amount:k,address:B}}}if(u){const f=(0,b.formatEther)(c);const A=p.filter(k=>k.to.toLowerCase()===o).reduce((k,E)=>({symbol:E.symbol,amount:(0,b.formatUnits)(BigInt(k.amount)+(0,b.parseUnits)(E.amount,E.decimals),E.decimals),address:E.address}),{symbol:"",amount:"",address:""});return{tokenIn:{symbol:D[n],address:B,amount:f},tokenOut:A}}if(!y){console.error("An error has occurred. File a bug report here: https://github.com/0xProject/0x-parser/issues/new/choose");return null}return{tokenIn:{symbol:x.symbol,amount:x.amount,address:x.address},tokenOut:{symbol:y.symbol,amount:y.amount,address:y.address}}}async function Ft({rpcUrl:a,exchangeProxyAbi:e,transactionHash:n}){if(!a)throw new Error("Missing rpcUrl\u2026");if(!n)throw new Error("Missing transaction hash\u2026");if(!e){throw new Error(`Missing 0x Exchange Proxy ABI: ${V}\u2026`)}const r=(0,L.createPublicClient)({chain:K.mainnet,transport:(0,L.http)(a)});const{getChainId:s,getTransaction:t,getTransactionReceipt:o}=r;const[c,d,i]=await Promise.all([s(),t({hash:n}),o({hash:n})]);if(i.status==="reverted"){return null}if(!G(c)){throw new Error(`chainId ${c} is unsupported\u2026`)}const l=O[c];const u=$(c)?U[c]:null;const p=(0,L.getAddress)(d.to);const x=p===l;const y=p===u;if(!x&&!y){return null}const{functionName:h}=x?(0,L.decodeFunctionData)({abi:e,data:d.input}):(0,L.decodeFunctionData)({abi:P,data:d.input});const f=_[h];return f({chainId:c,transaction:d,publicClient:r,exchangeProxyAbi:e,transactionReceipt:i,callData:d.input})}0&&(module.exports={parseSwap,parseSwapV2}); | ||
"use strict";var R=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var Q=Object.prototype.hasOwnProperty;var tt=(a,e)=>{for(var n in e)R(a,n,{get:e[n],enumerable:true})};var et=(a,e,n,r)=>{if(e&&typeof e==="object"||typeof e==="function"){for(let o of J(e))if(!Q.call(a,o)&&o!==n)R(a,o,{get:()=>e[o],enumerable:!(r=z(e,o))||r.enumerable})}return a};var nt=a=>et(R({},"__esModule",{value:true}),a);var Pt={};tt(Pt,{parseSwap:()=>Ft,parseSwapV2:()=>K});module.exports=nt(Pt);var I=require("viem");var Z=require("viem/chains");var m=require("viem");var O={LimitOrderFilled:"0xab614d2b738543c0ea21f56347cf696a3a0c42a7cbec3212a5ca22a4dcff2124",LiquidityProviderSwap:"0x40a6ba9513d09e3488135e0e0d10e2d4382b792720155b144cbea89ac9db6d34",OtcOrderFilled:"0xac75f773e3a92f1a02b12134d65e1f47f8a14eabe4eaf1e24624918e6a8b269f",MetaTransactionExecuted:"0x7f4fe3ff8ae440e1570c558da08440b26f89fb1c1f2910cd91ca6452955f121a",Transfer:"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",TransformedERC20:"0x0f6672f78a59ba8e5e5b5d38df3ebc67f3c792e2c9259b8d97d7f00dd78ba1b3"};var V={permitAndCall:{IERC2612:"0x34b4d153",IERC20PermitAllowed:"0x7f9cb4d7",IERC20MetaTransaction:"0x9d50b5e4"}};var U="https://raw.githubusercontent.com/0xProject/protocol/development/packages/contract-artifacts/artifacts/IZeroEx.json";var F="0xDef1C0ded9bec7F1a1670819833240f027b25EfF";var Y={1:"0x1291C02D288de3De7dC25353459489073D11E1Ae",137:"0x2Ddd30fE5c12FC4CD497526F14Bf3d1fcd3D5Db4",8453:"0x3CA53031Ad0B86a304845e83644983Be3340895f"};var _={1:F,5:"0xF91bB752490473B8342a3E964E855b9f9a2A668e",10:"0xDEF1ABE32c034e558Cdd535791643C58a13aCC10",56:F,137:F,250:"0xDEF189DeAEF76E379df891899eb5A00a94cBC250",8453:F,42161:F,42220:F,43114:F};var L={weth:"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"};var y={symbol:"ETH",decimals:18,address:"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"};var X={1:"ETH",5:"ETH",10:"ETH",56:"BNB",137:"MATIC",250:"FTM",8453:"ETH",42161:"ETH",42220:"CELO",43114:"AVAX"};var P=[{type:"function",name:"decimals",stateMutability:"view",inputs:[],outputs:[{name:"",type:"uint8"}]},{type:"function",name:"symbol",stateMutability:"view",inputs:[],outputs:[{name:"",type:"string"}]}];var S=require("viem");function G(a){return`0x${a.slice(-40)}`}function $(a){return[1,5,10,56,137,250,8453,42220,43114,42161].includes(a)}function j(a){return[1,137,8453].includes(a)}async function T({publicClient:a,transactionReceipt:e}){const{logs:n}=e;const r=n.filter(d=>d.topics[0]===O.Transfer).map(d=>({...d,address:(0,S.getAddress)(d.address)}));const o=[...r.map(d=>({abi:P,address:d.address,functionName:"symbol"})),...r.map(d=>({abi:P,address:d.address,functionName:"decimals"}))];const t=await a.multicall({contracts:o});const s=Math.floor(t.length/2);const c=r.map((d,i)=>{const l=t[i].result;const u=t[s+i].result;const p=(0,S.formatUnits)(BigInt(d.data),u);const{address:C,topics:x}=d;const{1:f,2:A}=x;const g=(0,S.getAddress)(G(f));const h=(0,S.getAddress)(G(A));return{to:h,from:g,symbol:l,amount:p,address:C,decimals:u}}).filter(d=>d.amount!=="0");return c}function E(a,e){return{tokenIn:{symbol:a.symbol,amount:a.amount,address:(0,S.getAddress)(a.address)},tokenOut:{symbol:e.symbol,amount:e.amount,address:(0,S.getAddress)(e.address)}}}var N=[{inputs:[{internalType:"contract IERC2612",name:"token",type:"address"},{internalType:"address",name:"owner",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{internalType:"uint256",name:"deadline",type:"uint256"},{internalType:"uint8",name:"v",type:"uint8"},{internalType:"bytes32",name:"r",type:"bytes32"},{internalType:"bytes32",name:"s",type:"bytes32"},{internalType:"bytes",name:"data",type:"bytes"}],name:"permitAndCall",outputs:[{internalType:"bytes",name:"",type:"bytes"}],stateMutability:"payable",type:"function"},{inputs:[{internalType:"contract IERC20PermitAllowed",name:"token",type:"address"},{internalType:"address",name:"owner",type:"address"},{internalType:"uint256",name:"nonce",type:"uint256"},{internalType:"uint256",name:"deadline",type:"uint256"},{internalType:"bool",name:"allowed",type:"bool"},{internalType:"uint8",name:"v",type:"uint8"},{internalType:"bytes32",name:"r",type:"bytes32"},{internalType:"bytes32",name:"s",type:"bytes32"},{internalType:"bytes",name:"data",type:"bytes"}],name:"permitAndCall",outputs:[{internalType:"bytes",name:"",type:"bytes"}],stateMutability:"payable",type:"function"},{inputs:[{internalType:"contract IERC20MetaTransaction",name:"token",type:"address"},{internalType:"address",name:"owner",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{internalType:"uint8",name:"v",type:"uint8"},{internalType:"bytes32",name:"r",type:"bytes32"},{internalType:"bytes32",name:"s",type:"bytes32"},{internalType:"bytes",name:"data",type:"bytes"}],name:"permitAndCall",outputs:[{internalType:"bytes",name:"",type:"bytes"}],stateMutability:"payable",type:"function"},{inputs:[],name:"target",outputs:[{internalType:"address payable",name:"",type:"address"}],stateMutability:"view",type:"function"}];async function st({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=(0,m.getAddress)(e.from);const o=n.find(s=>r===s.from);const t=n.find(s=>r===s.to);if(o&&t){return E(o,t)}}async function ot({publicClient:a,transactionReceipt:e}){const n=(0,m.getAddress)(e.from);const r=await T({publicClient:a,transactionReceipt:e});const o=r.find(s=>n===s.from);const t=r.find(s=>s.address===L.weth);if(o&&t){t.symbol=y.symbol;t.address=y.address;return E(o,t)}}async function at({publicClient:a,transactionReceipt:e}){const n=(0,m.getAddress)(e.from);const r=await T({publicClient:a,transactionReceipt:e});const o=r.find(s=>s.address===L.weth);const t=r.find(s=>s.to===n);if(o&&t){o.symbol=y.symbol;o.address=y.address;return E(o,t)}}async function rt({callData:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const{args:o}=(0,m.decodeFunctionData)({abi:n,data:a});const[t]=o;const s=await T({publicClient:e,transactionReceipt:r});const{maker:c,taker:d}=t;const i=s.find(u=>u.from===d);const l=s.find(u=>u.from===c);if(i&&l){return E(i,l)}}async function it({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=(0,m.getAddress)(e.from);const o=n.find(s=>s.from===r);const t=n.find(s=>s.to===r);if(o&&t){return E(o,t)}}async function ct({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=n.find(t=>t.address!==L.weth);const o=n.find(t=>t.address===L.weth);if(r&&o){o.symbol=y.symbol;o.address=y.address;return E(r,o)}}async function dt({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=(0,m.getAddress)(e.from);const o=n.find(s=>s.from===r);const t=n.find(s=>s.address===L.weth);if(o&&t){t.symbol=y.symbol;t.address=y.address;return E(o,t)}}async function lt({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=(0,m.getAddress)(e.from);const o=n.find(s=>r===s.from);const t=n.find(s=>r===s.to);if(o&&t){return E(o,t)}}async function ut({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=(0,m.getAddress)(e.from);const o=n.find(s=>s.address===L.weth);const t=n.find(s=>r===s.to);if(o&&t){o.symbol=y.symbol;o.address=y.address;return E(o,t)}}async function mt({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=n[0];const o=n[n.length-1];return E(r,o)}async function pt({chainId:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const o=X[a];for(const t of r.logs){const{topics:s,data:c}=t;const[d]=s;if(d===O.TransformedERC20){let i;let l;let u;let p;const C=(0,m.decodeEventLog)({data:c,topics:s,abi:n});const{inputToken:x,outputToken:f,inputTokenAmount:A,outputTokenAmount:g}=C.args;const h={address:x,abi:P};const w={address:f,abi:P};if(x===y.address){i=o;l=18}else{[{result:i},{result:l}]=await e.multicall({contracts:[{...h,functionName:"symbol"},{...h,functionName:"decimals"}]})}if(f===y.address){u=o;p=18}else{[{result:u},{result:p}]=await e.multicall({contracts:[{...w,functionName:"symbol"},{...w,functionName:"decimals"}]})}const k=(0,m.formatUnits)(A,l);const M=(0,m.formatUnits)(g,p);return{tokenIn:{address:x,amount:k,symbol:i},tokenOut:{address:f,amount:M,symbol:u}}}}}async function ft({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=(0,m.getAddress)(e.from);const o=n.find(s=>r===s.from);const t=n.find(s=>r===s.to);if(o&&t){return E(o,t)}}async function bt({callData:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const o=await T({publicClient:e,transactionReceipt:r});const{args:t}=(0,m.decodeFunctionData)({abi:n,data:a});const[s]=t;return o.reduce((c,d)=>{const{symbol:i,amount:l,address:u}=d;if(u===L.weth){return{...c,tokenOut:{...c.tokenOut,amount:(Number(c.tokenOut.amount)+Number(l)).toString()}}}else if(u===s){return{...c,tokenIn:{...c.tokenIn,symbol:i,address:u,amount:(Number(c.tokenIn.amount)+Number(l)).toString()}}}else{return c}},{tokenIn:{address:"",amount:"",symbol:""},tokenOut:{amount:"",symbol:y.symbol,address:y.address}})}async function yt({callData:a,transaction:e,publicClient:n,exchangeProxyAbi:r,transactionReceipt:o}){const{value:t}=e;const{args:s}=(0,m.decodeFunctionData)({abi:r,data:a});const c=await T({publicClient:n,transactionReceipt:o});const[d]=s;const i=1000000000000000000n;const l=t/i;const u=t%i;const p=Number(l)+Number(u)/Number(i);const C=c.reduce((A,g)=>{return g.address===d?A+Number(g.amount):A},0);const x=c.find(A=>A.address===L.weth);const f=c.find(A=>A.address===d);if(x&&f){return{tokenIn:{symbol:y.symbol,amount:p.toString(),address:y.address},tokenOut:{symbol:f.symbol,amount:C.toString(),address:f.address}}}}async function gt({callData:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const o=await T({publicClient:e,transactionReceipt:r});const{args:t}=(0,m.decodeFunctionData)({abi:n,data:a});const[s,c]=t;const d={[s]:{amount:"0",symbol:"",address:""},[c]:{amount:"0",symbol:"",address:""}};o.forEach(({address:i,symbol:l,amount:u})=>{if((0,m.getAddress)(i)in d){d[(0,m.getAddress)(i)].address=(0,m.getAddress)(i);d[(0,m.getAddress)(i)].symbol=l;d[(0,m.getAddress)(i)].amount=(Number(d[(0,m.getAddress)(i)].amount)+Number(u)).toString()}});return{tokenIn:d[s],tokenOut:d[c]}}async function Tt({publicClient:a,transactionReceipt:e}){const n=await T({publicClient:a,transactionReceipt:e});const r=(0,m.getAddress)(_[56]);const o=(0,m.getAddress)(e.from);let t=n.find(c=>c.from===o);let s=n.find(c=>c.from!==o);if(t===void 0){t=n.find(c=>c.from===r);s=n.find(c=>c.from!==r)}if(!n.some(c=>c.to===o)){s=n.find(c=>c.to===r)}if(t&&s){return E(t,s)}}async function Et({callData:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const{args:o}=(0,m.decodeFunctionData)({abi:n,data:a});const t=await T({publicClient:e,transactionReceipt:r});const[s]=o;const{makerToken:c,takerToken:d}=s;const i=t.find(u=>u.address===d);const l=t.find(u=>u.address===c);if(i&&l){l.symbol=y.symbol;l.address=y.address;return E(i,l)}}async function At({callData:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const{args:o}=(0,m.decodeFunctionData)({abi:n,data:a});const t=await T({publicClient:e,transactionReceipt:r});const[s]=o;const{makerToken:c,takerToken:d}=s;const i=t.find(u=>u.address===d);const l=t.find(u=>u.address===c);if(i&&l){i.symbol=y.symbol;i.address=y.address;return E(i,l)}}async function Ct({callData:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const{args:o}=(0,m.decodeFunctionData)({abi:n,data:a});const[t]=o;const{maker:s,taker:c}=t;const d=await T({publicClient:e,transactionReceipt:r});if(typeof s==="string"&&typeof c==="string"){const i=d.find(u=>u.from===c);const l=d.find(u=>u.from===s);if(i&&l){return E(i,l)}}}async function xt({callData:a,publicClient:e,exchangeProxyAbi:n,transactionReceipt:r}){const{args:o}=(0,m.decodeFunctionData)({abi:n,data:a});const[t]=o;const{signer:s}=t;const c=await T({publicClient:e,transactionReceipt:r});if(typeof s==="string"){const d=c.find(l=>l.from===s);const i=c.find(l=>l.to===s);if(d&&i){return E(d,i)}}}async function ht({chainId:a,transaction:e,publicClient:n,exchangeProxyAbi:r,transactionReceipt:o,callData:t}){const{args:s}=(0,m.decodeFunctionData)({abi:r,data:t});const[c]=s;const{callData:d}=c;const{functionName:i}=(0,m.decodeFunctionData)({data:d,abi:r});const l=B[i];return l({chainId:a,callData:d,transaction:e,publicClient:n,exchangeProxyAbi:r,transactionReceipt:o})}function kt({chainId:a,transaction:e,publicClient:n,exchangeProxyAbi:r,transactionReceipt:o}){const{args:t}=(0,m.decodeFunctionData)({abi:N,data:e.input});let s;const c=(0,m.encodeFunctionData)({abi:N,functionName:"permitAndCall",args:[...t]});const d=c.slice(0,10);const{IERC2612:i,IERC20PermitAllowed:l,IERC20MetaTransaction:u}=V.permitAndCall;switch(d){case i:s=t[7];break;case l:s=t[8];break;case u:s=t[6];break}if(s){const{functionName:p}=(0,m.decodeFunctionData)({abi:r,data:s});const C=B[p];return C({chainId:a,callData:s,transaction:e,publicClient:n,exchangeProxyAbi:r,transactionReceipt:o})}}var B={fillLimitOrder:Ct,fillOtcOrder:it,fillOtcOrderForEth:Et,fillOtcOrderWithEth:At,fillTakerSignedOtcOrder:rt,fillTakerSignedOtcOrderForEth:ct,executeMetaTransaction:xt,executeMetaTransactionV2:ht,multiplexBatchSellTokenForToken:gt,multiplexBatchSellTokenForEth:bt,multiplexBatchSellEthForToken:yt,multiplexMultiHopSellTokenForToken:ft,multiplexMultiHopSellEthForToken:at,multiplexMultiHopSellTokenForEth:ot,permitAndCall:kt,sellToUniswap:mt,sellTokenForEthToUniswapV3:dt,sellEthForTokenToUniswapV3:ut,sellTokenForTokenToUniswapV3:lt,sellToLiquidityProvider:st,sellToPancakeSwap:Tt,transformERC20:pt};var b=require("viem");var W=[{inputs:[{components:[{internalType:"address",name:"recipient",type:"address"},{internalType:"contract IERC20",name:"buyToken",type:"address"},{internalType:"uint256",name:"minAmountOut",type:"uint256"}],internalType:"struct SettlerBase.AllowedSlippage",name:"slippage",type:"tuple"},{internalType:"bytes[]",name:"actions",type:"bytes[]"},{internalType:"bytes32",name:"",type:"bytes32"},{internalType:"address",name:"msgSender",type:"address"},{internalType:"bytes",name:"sig",type:"bytes"}],name:"executeMetaTxn",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"nonpayable",type:"function"}];var D="0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";var H={1:"ETH",10:"ETH",56:"BNB",137:"MATIC",8453:"ETH",42161:"ETH",43114:"AVAX"};var wt={1:"0x7C39a136EA20B3483e402EA031c1f3C019bAb24b",10:"0x4069560a180EbD76bB1aF947f5119Fe555BB4eA0",56:"0x73C25Ef091Ce3F2451946Be3f982549776bFED31",137:"0xF9332450385291b6dcE301917aF6905e28E8f35f",8453:"0x5CE929DDB01804bfF35B2F5c77b735bdB094AAc8",42161:"0x1aa84EB5cb62f686FC0D908AFd85864f4A05d5Ee",43114:"0x2adb2cE26848B94E13d2f7fE0fF7E945050D741c"};var Lt="0xcA11bde05977b3631167028862bE2a173976CA11";function It(a){return[1,10,56,137,8453,42161,43114].includes(a)}function v(a,e){let n=0n;function r(o){o.forEach(t=>{if(t.to.toLowerCase()===e.toLowerCase()&&(0,b.fromHex)(t.value,"bigint")>0n){n=n+(0,b.fromHex)(t.value,"bigint")}if(t.calls&&t.calls.length>0){r(t.calls)}})}r(a.calls);return(0,b.formatEther)(n)}async function St({publicClient:a,transactionReceipt:e}){const n={Transfer:"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"};const{logs:r}=e;const o=r.filter(i=>i.topics[0]===n.Transfer).map(i=>({...i,address:(0,b.getAddress)(i.address)}));const t=[...o.map(i=>({abi:b.erc20Abi,address:i.address,functionName:"symbol"})),...o.map(i=>({abi:b.erc20Abi,address:i.address,functionName:"decimals"}))];const s=await a.multicall({contracts:t});const c=Math.floor(s.length/2);const d=o.map((i,l)=>{const u=s[l].result;const p=s[c+l].result;const C=i.data==="0x"?"0":(0,b.formatUnits)(BigInt(i.data),p);const{address:x,topics:f}=i;const{1:A,2:g}=f;const h=(0,b.getAddress)(q(A));const w=(0,b.getAddress)(q(g));return{to:w,from:h,symbol:u,amount:C,address:x,decimals:p}}).filter(i=>i.amount!=="0");return d}function q(a){return`0x${a.slice(-40)}`}async function K({publicClient:a,transactionHash:e}){const n=await a.getChainId();if(!It(n)){throw new Error(`chainId ${n} is unsupported\u2026`)}const r=a.extend(g=>({async traceCall(h){return g.request({method:"debug_traceTransaction",params:[h.hash,{tracer:"callTracer"}]})}}));const o=await r.traceCall({hash:e});const t=await a.getTransaction({hash:e});const{from:s,value:c,to:d}=t;const i=v(o,s);const l=await a.getTransactionReceipt({hash:e});const u=c>0n;const p=await St({publicClient:a,transactionReceipt:l});const C=p.filter(g=>g.from.toLowerCase()===s.toLowerCase());let x=C.length?C[0]:p[0];let f=i==="0"?p.find(g=>{return g.to.toLowerCase()===s.toLowerCase()}):{symbol:H[n],amount:i,address:D};if(d?.toLowerCase()===Lt.toLowerCase()){const{args:g}=(0,b.decodeFunctionData)({abi:b.multicall3Abi,data:t.input});const{args:h}=(0,b.decodeFunctionData)({abi:W,data:g[0][1].callData});const w=h[0].recipient.toLowerCase();const k=v(o,w);if(k==="0"){f=f=p[p.length-1]}else{f={symbol:H[n],amount:k,address:D}}}const A=d?.toLowerCase()===wt[n].toLowerCase();if(A){const{args:g}=(0,b.decodeFunctionData)({abi:W,data:t.input});const{3:h}=g;const w=v(o,h);if(w==="0"){f=p[p.length-1];const k=p.filter(M=>M.to.toLowerCase()===h.toLowerCase());if(k.length===1){f={symbol:k[0].symbol,amount:k[0].amount,address:k[0].address}}else{f={symbol:"",amount:"",address:""};console.error("More than one `takerReceived` log. File a bug report here: https://github.com/0xProject/0x-parser/issues/new/choose")}}else{f={symbol:H[n],amount:w,address:D}}}if(u){const g=(0,b.formatEther)(c);const h=p.filter(w=>w.to.toLowerCase()===s).reduce((w,k)=>({symbol:k.symbol,amount:(0,b.formatUnits)(BigInt(w.amount)+(0,b.parseUnits)(k.amount,k.decimals),k.decimals),address:k.address}),{symbol:"",amount:"",address:""});return{tokenIn:{symbol:H[n],address:D,amount:g},tokenOut:h}}if(!f){console.error("An error has occurred. File a bug report here: https://github.com/0xProject/0x-parser/issues/new/choose");return null}return{tokenIn:{symbol:x.symbol,amount:x.amount,address:x.address},tokenOut:{symbol:f.symbol,amount:f.amount,address:f.address}}}async function Ft({rpcUrl:a,exchangeProxyAbi:e,transactionHash:n}){if(!a)throw new Error("Missing rpcUrl\u2026");if(!n)throw new Error("Missing transaction hash\u2026");if(!e){throw new Error(`Missing 0x Exchange Proxy ABI: ${U}\u2026`)}const r=(0,I.createPublicClient)({chain:Z.mainnet,transport:(0,I.http)(a)});const{getChainId:o,getTransaction:t,getTransactionReceipt:s}=r;const[c,d,i]=await Promise.all([o(),t({hash:n}),s({hash:n})]);if(i.status==="reverted"){return null}if(!$(c)){throw new Error(`chainId ${c} is unsupported\u2026`)}const l=_[c];const u=j(c)?Y[c]:null;const p=(0,I.getAddress)(d.to);const C=p===l;const x=p===u;if(!C&&!x){return null}const{functionName:f}=C?(0,I.decodeFunctionData)({abi:e,data:d.input}):(0,I.decodeFunctionData)({abi:N,data:d.input});const A=B[f];return A({chainId:c,transaction:d,publicClient:r,exchangeProxyAbi:e,transactionReceipt:i,callData:d.input})}0&&(module.exports={parseSwap,parseSwapV2}); |
@@ -1,1 +0,1 @@ | ||
import{http as _t,getAddress as Bt,decodeFunctionData as Q,createPublicClient as Dt}from"viem";import{mainnet as Ht}from"viem/chains";import{getAddress as C,formatUnits as j,decodeEventLog as nt,decodeFunctionData as k,encodeFunctionData as st}from"viem";var P={LimitOrderFilled:"0xab614d2b738543c0ea21f56347cf696a3a0c42a7cbec3212a5ca22a4dcff2124",LiquidityProviderSwap:"0x40a6ba9513d09e3488135e0e0d10e2d4382b792720155b144cbea89ac9db6d34",OtcOrderFilled:"0xac75f773e3a92f1a02b12134d65e1f47f8a14eabe4eaf1e24624918e6a8b269f",MetaTransactionExecuted:"0x7f4fe3ff8ae440e1570c558da08440b26f89fb1c1f2910cd91ca6452955f121a",Transfer:"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",TransformedERC20:"0x0f6672f78a59ba8e5e5b5d38df3ebc67f3c792e2c9259b8d97d7f00dd78ba1b3"};var v={permitAndCall:{IERC2612:"0x34b4d153",IERC20PermitAllowed:"0x7f9cb4d7",IERC20MetaTransaction:"0x9d50b5e4"}};var V="https://raw.githubusercontent.com/0xProject/protocol/development/packages/contract-artifacts/artifacts/IZeroEx.json";var L="0xDef1C0ded9bec7F1a1670819833240f027b25EfF";var U={1:"0x1291C02D288de3De7dC25353459489073D11E1Ae",137:"0x2Ddd30fE5c12FC4CD497526F14Bf3d1fcd3D5Db4",8453:"0x3CA53031Ad0B86a304845e83644983Be3340895f"};var N={1:L,5:"0xF91bB752490473B8342a3E964E855b9f9a2A668e",10:"0xDEF1ABE32c034e558Cdd535791643C58a13aCC10",56:L,137:L,250:"0xDEF189DeAEF76E379df891899eb5A00a94cBC250",8453:L,42161:L,42220:L,43114:L};var w={weth:"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"};var b={symbol:"ETH",decimals:18,address:"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"};var Y={1:"ETH",5:"ETH",10:"ETH",56:"BNB",137:"MATIC",250:"FTM",8453:"ETH",42161:"ETH",42220:"CELO",43114:"AVAX"};var I=[{type:"function",name:"decimals",stateMutability:"view",inputs:[],outputs:[{name:"",type:"uint8"}]},{type:"function",name:"symbol",stateMutability:"view",inputs:[],outputs:[{name:"",type:"string"}]}];import{getAddress as S,formatUnits as et}from"viem";function X(o){return`0x${o.slice(-40)}`}function G(o){return[1,5,10,56,137,250,8453,42220,43114,42161].includes(o)}function $(o){return[1,137,8453].includes(o)}async function y({publicClient:o,transactionReceipt:a}){const{logs:n}=a;const r=n.filter(d=>d.topics[0]===P.Transfer).map(d=>({...d,address:S(d.address)}));const s=[...r.map(d=>({abi:I,address:d.address,functionName:"symbol"})),...r.map(d=>({abi:I,address:d.address,functionName:"decimals"}))];const t=await o.multicall({contracts:s});const e=Math.floor(t.length/2);const c=r.map((d,i)=>{const l=t[i].result;const u=t[e+i].result;const m=et(BigInt(d.data),u);const{address:A,topics:f}=d;const{1:x,2:p}=f;const T=S(X(x));const h=S(X(p));return{to:h,from:T,symbol:l,amount:m,address:A,decimals:u}}).filter(d=>d.amount!=="0");return c}function E(o,a){return{tokenIn:{symbol:o.symbol,amount:o.amount,address:S(o.address)},tokenOut:{symbol:a.symbol,amount:a.amount,address:S(a.address)}}}var F=[{inputs:[{internalType:"contract IERC2612",name:"token",type:"address"},{internalType:"address",name:"owner",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{internalType:"uint256",name:"deadline",type:"uint256"},{internalType:"uint8",name:"v",type:"uint8"},{internalType:"bytes32",name:"r",type:"bytes32"},{internalType:"bytes32",name:"s",type:"bytes32"},{internalType:"bytes",name:"data",type:"bytes"}],name:"permitAndCall",outputs:[{internalType:"bytes",name:"",type:"bytes"}],stateMutability:"payable",type:"function"},{inputs:[{internalType:"contract IERC20PermitAllowed",name:"token",type:"address"},{internalType:"address",name:"owner",type:"address"},{internalType:"uint256",name:"nonce",type:"uint256"},{internalType:"uint256",name:"deadline",type:"uint256"},{internalType:"bool",name:"allowed",type:"bool"},{internalType:"uint8",name:"v",type:"uint8"},{internalType:"bytes32",name:"r",type:"bytes32"},{internalType:"bytes32",name:"s",type:"bytes32"},{internalType:"bytes",name:"data",type:"bytes"}],name:"permitAndCall",outputs:[{internalType:"bytes",name:"",type:"bytes"}],stateMutability:"payable",type:"function"},{inputs:[{internalType:"contract IERC20MetaTransaction",name:"token",type:"address"},{internalType:"address",name:"owner",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{internalType:"uint8",name:"v",type:"uint8"},{internalType:"bytes32",name:"r",type:"bytes32"},{internalType:"bytes32",name:"s",type:"bytes32"},{internalType:"bytes",name:"data",type:"bytes"}],name:"permitAndCall",outputs:[{internalType:"bytes",name:"",type:"bytes"}],stateMutability:"payable",type:"function"},{inputs:[],name:"target",outputs:[{internalType:"address payable",name:"",type:"address"}],stateMutability:"view",type:"function"}];async function ot({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=C(a.from);const s=n.find(e=>r===e.from);const t=n.find(e=>r===e.to);if(s&&t){return E(s,t)}}async function at({publicClient:o,transactionReceipt:a}){const n=C(a.from);const r=await y({publicClient:o,transactionReceipt:a});const s=r.find(e=>n===e.from);const t=r.find(e=>e.address===w.weth);if(s&&t){t.symbol=b.symbol;t.address=b.address;return E(s,t)}}async function rt({publicClient:o,transactionReceipt:a}){const n=C(a.from);const r=await y({publicClient:o,transactionReceipt:a});const s=r.find(e=>e.address===w.weth);const t=r.find(e=>e.to===n);if(s&&t){s.symbol=b.symbol;s.address=b.address;return E(s,t)}}async function it({callData:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const{args:s}=k({abi:n,data:o});const[t]=s;const e=await y({publicClient:a,transactionReceipt:r});const{maker:c,taker:d}=t;const i=e.find(u=>u.from===d);const l=e.find(u=>u.from===c);if(i&&l){return E(i,l)}}async function ct({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=C(a.from);const s=n.find(e=>e.from===r);const t=n.find(e=>e.to===r);if(s&&t){return E(s,t)}}async function dt({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=n.find(t=>t.address!==w.weth);const s=n.find(t=>t.address===w.weth);if(r&&s){s.symbol=b.symbol;s.address=b.address;return E(r,s)}}async function lt({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=C(a.from);const s=n.find(e=>e.from===r);const t=n.find(e=>e.address===w.weth);if(s&&t){t.symbol=b.symbol;t.address=b.address;return E(s,t)}}async function ut({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=C(a.from);const s=n.find(e=>r===e.from);const t=n.find(e=>r===e.to);if(s&&t){return E(s,t)}}async function mt({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=C(a.from);const s=n.find(e=>e.address===w.weth);const t=n.find(e=>r===e.to);if(s&&t){s.symbol=b.symbol;s.address=b.address;return E(s,t)}}async function pt({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=n[0];const s=n[n.length-1];return E(r,s)}async function ft({chainId:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const s=Y[o];for(const t of r.logs){const{topics:e,data:c}=t;const[d]=e;if(d===P.TransformedERC20){let i;let l;let u;let m;const A=nt({data:c,topics:e,abi:n});const{inputToken:f,outputToken:x,inputTokenAmount:p,outputTokenAmount:T}=A.args;const h={address:f,abi:I};const g={address:x,abi:I};if(f===b.address){i=s;l=18}else{[{result:i},{result:l}]=await a.multicall({contracts:[{...h,functionName:"symbol"},{...h,functionName:"decimals"}]})}if(x===b.address){u=s;m=18}else{[{result:u},{result:m}]=await a.multicall({contracts:[{...g,functionName:"symbol"},{...g,functionName:"decimals"}]})}const D=j(p,l);const tt=j(T,m);return{tokenIn:{address:f,amount:D,symbol:i},tokenOut:{address:x,amount:tt,symbol:u}}}}}async function bt({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=C(a.from);const s=n.find(e=>r===e.from);const t=n.find(e=>r===e.to);if(s&&t){return E(s,t)}}async function yt({callData:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const s=await y({publicClient:a,transactionReceipt:r});const{args:t}=k({abi:n,data:o});const[e]=t;return s.reduce((c,d)=>{const{symbol:i,amount:l,address:u}=d;if(u===w.weth){return{...c,tokenOut:{...c.tokenOut,amount:(Number(c.tokenOut.amount)+Number(l)).toString()}}}else if(u===e){return{...c,tokenIn:{...c.tokenIn,symbol:i,address:u,amount:(Number(c.tokenIn.amount)+Number(l)).toString()}}}else{return c}},{tokenIn:{address:"",amount:"",symbol:""},tokenOut:{amount:"",symbol:b.symbol,address:b.address}})}async function gt({callData:o,transaction:a,publicClient:n,exchangeProxyAbi:r,transactionReceipt:s}){const{value:t}=a;const{args:e}=k({abi:r,data:o});const c=await y({publicClient:n,transactionReceipt:s});const[d]=e;const i=1000000000000000000n;const l=t/i;const u=t%i;const m=Number(l)+Number(u)/Number(i);const A=c.reduce((p,T)=>{return T.address===d?p+Number(T.amount):p},0);const f=c.find(p=>p.address===w.weth);const x=c.find(p=>p.address===d);if(f&&x){return{tokenIn:{symbol:b.symbol,amount:m.toString(),address:b.address},tokenOut:{symbol:x.symbol,amount:A.toString(),address:x.address}}}}async function Tt({callData:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const s=await y({publicClient:a,transactionReceipt:r});const{args:t}=k({abi:n,data:o});const[e,c]=t;const d={[e]:{amount:"0",symbol:"",address:""},[c]:{amount:"0",symbol:"",address:""}};s.forEach(({address:i,symbol:l,amount:u})=>{if(C(i)in d){d[C(i)].address=C(i);d[C(i)].symbol=l;d[C(i)].amount=(Number(d[C(i)].amount)+Number(u)).toString()}});return{tokenIn:d[e],tokenOut:d[c]}}async function Et({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=C(N[56]);const s=C(a.from);let t=n.find(c=>c.from===s);let e=n.find(c=>c.from!==s);if(t===void 0){t=n.find(c=>c.from===r);e=n.find(c=>c.from!==r)}if(!n.some(c=>c.to===s)){e=n.find(c=>c.to===r)}if(t&&e){return E(t,e)}}async function At({callData:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const{args:s}=k({abi:n,data:o});const t=await y({publicClient:a,transactionReceipt:r});const[e]=s;const{makerToken:c,takerToken:d}=e;const i=t.find(u=>u.address===d);const l=t.find(u=>u.address===c);if(i&&l){l.symbol=b.symbol;l.address=b.address;return E(i,l)}}async function Ct({callData:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const{args:s}=k({abi:n,data:o});const t=await y({publicClient:a,transactionReceipt:r});const[e]=s;const{makerToken:c,takerToken:d}=e;const i=t.find(u=>u.address===d);const l=t.find(u=>u.address===c);if(i&&l){i.symbol=b.symbol;i.address=b.address;return E(i,l)}}async function xt({callData:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const{args:s}=k({abi:n,data:o});const[t]=s;const{maker:e,taker:c}=t;const d=await y({publicClient:a,transactionReceipt:r});if(typeof e==="string"&&typeof c==="string"){const i=d.find(u=>u.from===c);const l=d.find(u=>u.from===e);if(i&&l){return E(i,l)}}}async function ht({callData:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const{args:s}=k({abi:n,data:o});const[t]=s;const{signer:e}=t;const c=await y({publicClient:a,transactionReceipt:r});if(typeof e==="string"){const d=c.find(l=>l.from===e);const i=c.find(l=>l.to===e);if(d&&i){return E(d,i)}}}async function kt({chainId:o,transaction:a,publicClient:n,exchangeProxyAbi:r,transactionReceipt:s,callData:t}){const{args:e}=k({abi:r,data:t});const[c]=e;const{callData:d}=c;const{functionName:i}=k({data:d,abi:r});const l=O[i];return l({chainId:o,callData:d,transaction:a,publicClient:n,exchangeProxyAbi:r,transactionReceipt:s})}function wt({chainId:o,transaction:a,publicClient:n,exchangeProxyAbi:r,transactionReceipt:s}){const{args:t}=k({abi:F,data:a.input});let e;const c=st({abi:F,functionName:"permitAndCall",args:[...t]});const d=c.slice(0,10);const{IERC2612:i,IERC20PermitAllowed:l,IERC20MetaTransaction:u}=v.permitAndCall;switch(d){case i:e=t[7];break;case l:e=t[8];break;case u:e=t[6];break}if(e){const{functionName:m}=k({abi:r,data:e});const A=O[m];return A({chainId:o,callData:e,transaction:a,publicClient:n,exchangeProxyAbi:r,transactionReceipt:s})}}var O={fillLimitOrder:xt,fillOtcOrder:ct,fillOtcOrderForEth:At,fillOtcOrderWithEth:Ct,fillTakerSignedOtcOrder:it,fillTakerSignedOtcOrderForEth:dt,executeMetaTransaction:ht,executeMetaTransactionV2:kt,multiplexBatchSellTokenForToken:Tt,multiplexBatchSellTokenForEth:yt,multiplexBatchSellEthForToken:gt,multiplexMultiHopSellTokenForToken:bt,multiplexMultiHopSellEthForToken:rt,multiplexMultiHopSellTokenForEth:at,permitAndCall:wt,sellToUniswap:pt,sellTokenForEthToUniswapV3:lt,sellEthForTokenToUniswapV3:mt,sellTokenForTokenToUniswapV3:ut,sellToLiquidityProvider:ot,sellToPancakeSwap:Et,transformERC20:ft};import{fromHex as W,erc20Abi as q,parseUnits as Lt,getAddress as H,formatUnits as z,formatEther as J,multicall3Abi as It,decodeFunctionData as M}from"viem";var K=[{inputs:[{components:[{internalType:"address",name:"recipient",type:"address"},{internalType:"contract IERC20",name:"buyToken",type:"address"},{internalType:"uint256",name:"minAmountOut",type:"uint256"}],internalType:"struct SettlerBase.AllowedSlippage",name:"slippage",type:"tuple"},{internalType:"bytes[]",name:"actions",type:"bytes[]"},{internalType:"bytes32",name:"",type:"bytes32"},{internalType:"address",name:"msgSender",type:"address"},{internalType:"bytes",name:"sig",type:"bytes"}],name:"executeMetaTxn",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"nonpayable",type:"function"}];var _="0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";var B={1:"ETH",10:"ETH",56:"BNB",137:"MATIC",8453:"ETH",42161:"ETH",43114:"AVAX"};var St={1:"0x7C39a136EA20B3483e402EA031c1f3C019bAb24b",10:"0x4069560a180EbD76bB1aF947f5119Fe555BB4eA0",56:"0x73C25Ef091Ce3F2451946Be3f982549776bFED31",137:"0xF9332450385291b6dcE301917aF6905e28E8f35f",8453:"0x5CE929DDB01804bfF35B2F5c77b735bdB094AAc8",42161:"0x1aa84EB5cb62f686FC0D908AFd85864f4A05d5Ee",43114:"0x2adb2cE26848B94E13d2f7fE0fF7E945050D741c"};var Ft="0xcA11bde05977b3631167028862bE2a173976CA11";function Pt(o){return[1,10,56,137,8453,42161,43114].includes(o)}function R(o,a){let n=0n;function r(s){s.forEach(t=>{if(t.to.toLowerCase()===a.toLowerCase()&&W(t.value,"bigint")>0n){n=n+W(t.value,"bigint")}if(t.calls&&t.calls.length>0){r(t.calls)}})}r(o.calls);return J(n)}async function Nt({publicClient:o,transactionReceipt:a}){const n={Transfer:"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"};const{logs:r}=a;const s=r.filter(i=>i.topics[0]===n.Transfer).map(i=>({...i,address:H(i.address)}));const t=[...s.map(i=>({abi:q,address:i.address,functionName:"symbol"})),...s.map(i=>({abi:q,address:i.address,functionName:"decimals"}))];const e=await o.multicall({contracts:t});const c=Math.floor(e.length/2);const d=s.map((i,l)=>{const u=e[l].result;const m=e[c+l].result;const A=i.data==="0x"?"0":z(BigInt(i.data),m);const{address:f,topics:x}=i;const{1:p,2:T}=x;const h=H(Z(p));const g=H(Z(T));return{to:g,from:h,symbol:u,amount:A,address:f,decimals:m}}).filter(i=>i.amount!=="0");return d}function Z(o){return`0x${o.slice(-40)}`}async function Ot({publicClient:o,transactionHash:a}){const n=await o.getChainId();if(!Pt(n)){throw new Error(`chainId ${n} is unsupported\u2026`)}const r=o.extend(p=>({async traceCall(T){return p.request({method:"debug_traceTransaction",params:[T.hash,{tracer:"callTracer"}]})}}));const s=await r.traceCall({hash:a});const t=await o.getTransaction({hash:a});const{from:e,value:c,to:d}=t;const i=R(s,e);const l=await o.getTransactionReceipt({hash:a});const u=c>0n;const m=await Nt({publicClient:o,transactionReceipt:l});let A=m[0];let f=i==="0"?m.find(p=>{return p.to.toLowerCase()===e.toLowerCase()}):{symbol:B[n],amount:i,address:_};if(d?.toLowerCase()===Ft.toLowerCase()){const{args:p}=M({abi:It,data:t.input});const{args:T}=M({abi:K,data:p[0][1].callData});const h=T[0].recipient.toLowerCase();const g=R(s,h);if(g==="0"){f=f=m[m.length-1]}else{f={symbol:B[n],amount:g,address:_}}}const x=d?.toLowerCase()===St[n].toLowerCase();if(x){const{args:p}=M({abi:K,data:t.input});const{3:T}=p;const h=R(s,T);if(h==="0"){f=m[m.length-1];const g=m.filter(D=>D.to.toLowerCase()===T.toLowerCase());if(g.length===1){f={symbol:g[0].symbol,amount:g[0].amount,address:g[0].address}}else{f={symbol:"",amount:"",address:""};console.error("More than one `takerReceived` log. File a bug report here: https://github.com/0xProject/0x-parser/issues/new/choose")}}else{f={symbol:B[n],amount:h,address:_}}}if(u){const p=J(c);const T=m.filter(h=>h.to.toLowerCase()===e).reduce((h,g)=>({symbol:g.symbol,amount:z(BigInt(h.amount)+Lt(g.amount,g.decimals),g.decimals),address:g.address}),{symbol:"",amount:"",address:""});return{tokenIn:{symbol:B[n],address:_,amount:p},tokenOut:T}}if(!f){console.error("An error has occurred. File a bug report here: https://github.com/0xProject/0x-parser/issues/new/choose");return null}return{tokenIn:{symbol:A.symbol,amount:A.amount,address:A.address},tokenOut:{symbol:f.symbol,amount:f.amount,address:f.address}}}async function ae({rpcUrl:o,exchangeProxyAbi:a,transactionHash:n}){if(!o)throw new Error("Missing rpcUrl\u2026");if(!n)throw new Error("Missing transaction hash\u2026");if(!a){throw new Error(`Missing 0x Exchange Proxy ABI: ${V}\u2026`)}const r=Dt({chain:Ht,transport:_t(o)});const{getChainId:s,getTransaction:t,getTransactionReceipt:e}=r;const[c,d,i]=await Promise.all([s(),t({hash:n}),e({hash:n})]);if(i.status==="reverted"){return null}if(!G(c)){throw new Error(`chainId ${c} is unsupported\u2026`)}const l=N[c];const u=$(c)?U[c]:null;const m=Bt(d.to);const A=m===l;const f=m===u;if(!A&&!f){return null}const{functionName:x}=A?Q({abi:a,data:d.input}):Q({abi:F,data:d.input});const p=O[x];return p({chainId:c,transaction:d,publicClient:r,exchangeProxyAbi:a,transactionReceipt:i,callData:d.input})}export{ae as parseSwap,Ot as parseSwapV2}; | ||
import{http as _t,getAddress as Bt,decodeFunctionData as tt,createPublicClient as Dt}from"viem";import{mainnet as Ht}from"viem/chains";import{getAddress as E,formatUnits as W,decodeEventLog as nt,decodeFunctionData as w,encodeFunctionData as st}from"viem";var N={LimitOrderFilled:"0xab614d2b738543c0ea21f56347cf696a3a0c42a7cbec3212a5ca22a4dcff2124",LiquidityProviderSwap:"0x40a6ba9513d09e3488135e0e0d10e2d4382b792720155b144cbea89ac9db6d34",OtcOrderFilled:"0xac75f773e3a92f1a02b12134d65e1f47f8a14eabe4eaf1e24624918e6a8b269f",MetaTransactionExecuted:"0x7f4fe3ff8ae440e1570c558da08440b26f89fb1c1f2910cd91ca6452955f121a",Transfer:"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",TransformedERC20:"0x0f6672f78a59ba8e5e5b5d38df3ebc67f3c792e2c9259b8d97d7f00dd78ba1b3"};var V={permitAndCall:{IERC2612:"0x34b4d153",IERC20PermitAllowed:"0x7f9cb4d7",IERC20MetaTransaction:"0x9d50b5e4"}};var U="https://raw.githubusercontent.com/0xProject/protocol/development/packages/contract-artifacts/artifacts/IZeroEx.json";var I="0xDef1C0ded9bec7F1a1670819833240f027b25EfF";var Y={1:"0x1291C02D288de3De7dC25353459489073D11E1Ae",137:"0x2Ddd30fE5c12FC4CD497526F14Bf3d1fcd3D5Db4",8453:"0x3CA53031Ad0B86a304845e83644983Be3340895f"};var O={1:I,5:"0xF91bB752490473B8342a3E964E855b9f9a2A668e",10:"0xDEF1ABE32c034e558Cdd535791643C58a13aCC10",56:I,137:I,250:"0xDEF189DeAEF76E379df891899eb5A00a94cBC250",8453:I,42161:I,42220:I,43114:I};var L={weth:"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"};var f={symbol:"ETH",decimals:18,address:"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"};var X={1:"ETH",5:"ETH",10:"ETH",56:"BNB",137:"MATIC",250:"FTM",8453:"ETH",42161:"ETH",42220:"CELO",43114:"AVAX"};var S=[{type:"function",name:"decimals",stateMutability:"view",inputs:[],outputs:[{name:"",type:"uint8"}]},{type:"function",name:"symbol",stateMutability:"view",inputs:[],outputs:[{name:"",type:"string"}]}];import{getAddress as F,formatUnits as et}from"viem";function G(o){return`0x${o.slice(-40)}`}function $(o){return[1,5,10,56,137,250,8453,42220,43114,42161].includes(o)}function j(o){return[1,137,8453].includes(o)}async function y({publicClient:o,transactionReceipt:a}){const{logs:n}=a;const r=n.filter(d=>d.topics[0]===N.Transfer).map(d=>({...d,address:F(d.address)}));const s=[...r.map(d=>({abi:S,address:d.address,functionName:"symbol"})),...r.map(d=>({abi:S,address:d.address,functionName:"decimals"}))];const t=await o.multicall({contracts:s});const e=Math.floor(t.length/2);const c=r.map((d,i)=>{const l=t[i].result;const u=t[e+i].result;const m=et(BigInt(d.data),u);const{address:A,topics:C}=d;const{1:p,2:T}=C;const b=F(G(p));const x=F(G(T));return{to:x,from:b,symbol:l,amount:m,address:A,decimals:u}}).filter(d=>d.amount!=="0");return c}function g(o,a){return{tokenIn:{symbol:o.symbol,amount:o.amount,address:F(o.address)},tokenOut:{symbol:a.symbol,amount:a.amount,address:F(a.address)}}}var P=[{inputs:[{internalType:"contract IERC2612",name:"token",type:"address"},{internalType:"address",name:"owner",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{internalType:"uint256",name:"deadline",type:"uint256"},{internalType:"uint8",name:"v",type:"uint8"},{internalType:"bytes32",name:"r",type:"bytes32"},{internalType:"bytes32",name:"s",type:"bytes32"},{internalType:"bytes",name:"data",type:"bytes"}],name:"permitAndCall",outputs:[{internalType:"bytes",name:"",type:"bytes"}],stateMutability:"payable",type:"function"},{inputs:[{internalType:"contract IERC20PermitAllowed",name:"token",type:"address"},{internalType:"address",name:"owner",type:"address"},{internalType:"uint256",name:"nonce",type:"uint256"},{internalType:"uint256",name:"deadline",type:"uint256"},{internalType:"bool",name:"allowed",type:"bool"},{internalType:"uint8",name:"v",type:"uint8"},{internalType:"bytes32",name:"r",type:"bytes32"},{internalType:"bytes32",name:"s",type:"bytes32"},{internalType:"bytes",name:"data",type:"bytes"}],name:"permitAndCall",outputs:[{internalType:"bytes",name:"",type:"bytes"}],stateMutability:"payable",type:"function"},{inputs:[{internalType:"contract IERC20MetaTransaction",name:"token",type:"address"},{internalType:"address",name:"owner",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{internalType:"uint8",name:"v",type:"uint8"},{internalType:"bytes32",name:"r",type:"bytes32"},{internalType:"bytes32",name:"s",type:"bytes32"},{internalType:"bytes",name:"data",type:"bytes"}],name:"permitAndCall",outputs:[{internalType:"bytes",name:"",type:"bytes"}],stateMutability:"payable",type:"function"},{inputs:[],name:"target",outputs:[{internalType:"address payable",name:"",type:"address"}],stateMutability:"view",type:"function"}];async function ot({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=E(a.from);const s=n.find(e=>r===e.from);const t=n.find(e=>r===e.to);if(s&&t){return g(s,t)}}async function at({publicClient:o,transactionReceipt:a}){const n=E(a.from);const r=await y({publicClient:o,transactionReceipt:a});const s=r.find(e=>n===e.from);const t=r.find(e=>e.address===L.weth);if(s&&t){t.symbol=f.symbol;t.address=f.address;return g(s,t)}}async function rt({publicClient:o,transactionReceipt:a}){const n=E(a.from);const r=await y({publicClient:o,transactionReceipt:a});const s=r.find(e=>e.address===L.weth);const t=r.find(e=>e.to===n);if(s&&t){s.symbol=f.symbol;s.address=f.address;return g(s,t)}}async function it({callData:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const{args:s}=w({abi:n,data:o});const[t]=s;const e=await y({publicClient:a,transactionReceipt:r});const{maker:c,taker:d}=t;const i=e.find(u=>u.from===d);const l=e.find(u=>u.from===c);if(i&&l){return g(i,l)}}async function ct({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=E(a.from);const s=n.find(e=>e.from===r);const t=n.find(e=>e.to===r);if(s&&t){return g(s,t)}}async function dt({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=n.find(t=>t.address!==L.weth);const s=n.find(t=>t.address===L.weth);if(r&&s){s.symbol=f.symbol;s.address=f.address;return g(r,s)}}async function lt({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=E(a.from);const s=n.find(e=>e.from===r);const t=n.find(e=>e.address===L.weth);if(s&&t){t.symbol=f.symbol;t.address=f.address;return g(s,t)}}async function ut({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=E(a.from);const s=n.find(e=>r===e.from);const t=n.find(e=>r===e.to);if(s&&t){return g(s,t)}}async function mt({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=E(a.from);const s=n.find(e=>e.address===L.weth);const t=n.find(e=>r===e.to);if(s&&t){s.symbol=f.symbol;s.address=f.address;return g(s,t)}}async function pt({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=n[0];const s=n[n.length-1];return g(r,s)}async function ft({chainId:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const s=X[o];for(const t of r.logs){const{topics:e,data:c}=t;const[d]=e;if(d===N.TransformedERC20){let i;let l;let u;let m;const A=nt({data:c,topics:e,abi:n});const{inputToken:C,outputToken:p,inputTokenAmount:T,outputTokenAmount:b}=A.args;const x={address:C,abi:S};const k={address:p,abi:S};if(C===f.address){i=s;l=18}else{[{result:i},{result:l}]=await a.multicall({contracts:[{...x,functionName:"symbol"},{...x,functionName:"decimals"}]})}if(p===f.address){u=s;m=18}else{[{result:u},{result:m}]=await a.multicall({contracts:[{...k,functionName:"symbol"},{...k,functionName:"decimals"}]})}const h=W(T,l);const H=W(b,m);return{tokenIn:{address:C,amount:h,symbol:i},tokenOut:{address:p,amount:H,symbol:u}}}}}async function bt({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=E(a.from);const s=n.find(e=>r===e.from);const t=n.find(e=>r===e.to);if(s&&t){return g(s,t)}}async function yt({callData:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const s=await y({publicClient:a,transactionReceipt:r});const{args:t}=w({abi:n,data:o});const[e]=t;return s.reduce((c,d)=>{const{symbol:i,amount:l,address:u}=d;if(u===L.weth){return{...c,tokenOut:{...c.tokenOut,amount:(Number(c.tokenOut.amount)+Number(l)).toString()}}}else if(u===e){return{...c,tokenIn:{...c.tokenIn,symbol:i,address:u,amount:(Number(c.tokenIn.amount)+Number(l)).toString()}}}else{return c}},{tokenIn:{address:"",amount:"",symbol:""},tokenOut:{amount:"",symbol:f.symbol,address:f.address}})}async function gt({callData:o,transaction:a,publicClient:n,exchangeProxyAbi:r,transactionReceipt:s}){const{value:t}=a;const{args:e}=w({abi:r,data:o});const c=await y({publicClient:n,transactionReceipt:s});const[d]=e;const i=1000000000000000000n;const l=t/i;const u=t%i;const m=Number(l)+Number(u)/Number(i);const A=c.reduce((T,b)=>{return b.address===d?T+Number(b.amount):T},0);const C=c.find(T=>T.address===L.weth);const p=c.find(T=>T.address===d);if(C&&p){return{tokenIn:{symbol:f.symbol,amount:m.toString(),address:f.address},tokenOut:{symbol:p.symbol,amount:A.toString(),address:p.address}}}}async function Tt({callData:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const s=await y({publicClient:a,transactionReceipt:r});const{args:t}=w({abi:n,data:o});const[e,c]=t;const d={[e]:{amount:"0",symbol:"",address:""},[c]:{amount:"0",symbol:"",address:""}};s.forEach(({address:i,symbol:l,amount:u})=>{if(E(i)in d){d[E(i)].address=E(i);d[E(i)].symbol=l;d[E(i)].amount=(Number(d[E(i)].amount)+Number(u)).toString()}});return{tokenIn:d[e],tokenOut:d[c]}}async function Et({publicClient:o,transactionReceipt:a}){const n=await y({publicClient:o,transactionReceipt:a});const r=E(O[56]);const s=E(a.from);let t=n.find(c=>c.from===s);let e=n.find(c=>c.from!==s);if(t===void 0){t=n.find(c=>c.from===r);e=n.find(c=>c.from!==r)}if(!n.some(c=>c.to===s)){e=n.find(c=>c.to===r)}if(t&&e){return g(t,e)}}async function At({callData:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const{args:s}=w({abi:n,data:o});const t=await y({publicClient:a,transactionReceipt:r});const[e]=s;const{makerToken:c,takerToken:d}=e;const i=t.find(u=>u.address===d);const l=t.find(u=>u.address===c);if(i&&l){l.symbol=f.symbol;l.address=f.address;return g(i,l)}}async function Ct({callData:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const{args:s}=w({abi:n,data:o});const t=await y({publicClient:a,transactionReceipt:r});const[e]=s;const{makerToken:c,takerToken:d}=e;const i=t.find(u=>u.address===d);const l=t.find(u=>u.address===c);if(i&&l){i.symbol=f.symbol;i.address=f.address;return g(i,l)}}async function xt({callData:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const{args:s}=w({abi:n,data:o});const[t]=s;const{maker:e,taker:c}=t;const d=await y({publicClient:a,transactionReceipt:r});if(typeof e==="string"&&typeof c==="string"){const i=d.find(u=>u.from===c);const l=d.find(u=>u.from===e);if(i&&l){return g(i,l)}}}async function ht({callData:o,publicClient:a,exchangeProxyAbi:n,transactionReceipt:r}){const{args:s}=w({abi:n,data:o});const[t]=s;const{signer:e}=t;const c=await y({publicClient:a,transactionReceipt:r});if(typeof e==="string"){const d=c.find(l=>l.from===e);const i=c.find(l=>l.to===e);if(d&&i){return g(d,i)}}}async function kt({chainId:o,transaction:a,publicClient:n,exchangeProxyAbi:r,transactionReceipt:s,callData:t}){const{args:e}=w({abi:r,data:t});const[c]=e;const{callData:d}=c;const{functionName:i}=w({data:d,abi:r});const l=_[i];return l({chainId:o,callData:d,transaction:a,publicClient:n,exchangeProxyAbi:r,transactionReceipt:s})}function wt({chainId:o,transaction:a,publicClient:n,exchangeProxyAbi:r,transactionReceipt:s}){const{args:t}=w({abi:P,data:a.input});let e;const c=st({abi:P,functionName:"permitAndCall",args:[...t]});const d=c.slice(0,10);const{IERC2612:i,IERC20PermitAllowed:l,IERC20MetaTransaction:u}=V.permitAndCall;switch(d){case i:e=t[7];break;case l:e=t[8];break;case u:e=t[6];break}if(e){const{functionName:m}=w({abi:r,data:e});const A=_[m];return A({chainId:o,callData:e,transaction:a,publicClient:n,exchangeProxyAbi:r,transactionReceipt:s})}}var _={fillLimitOrder:xt,fillOtcOrder:ct,fillOtcOrderForEth:At,fillOtcOrderWithEth:Ct,fillTakerSignedOtcOrder:it,fillTakerSignedOtcOrderForEth:dt,executeMetaTransaction:ht,executeMetaTransactionV2:kt,multiplexBatchSellTokenForToken:Tt,multiplexBatchSellTokenForEth:yt,multiplexBatchSellEthForToken:gt,multiplexMultiHopSellTokenForToken:bt,multiplexMultiHopSellEthForToken:rt,multiplexMultiHopSellTokenForEth:at,permitAndCall:wt,sellToUniswap:pt,sellTokenForEthToUniswapV3:lt,sellEthForTokenToUniswapV3:mt,sellTokenForTokenToUniswapV3:ut,sellToLiquidityProvider:ot,sellToPancakeSwap:Et,transformERC20:ft};import{fromHex as q,erc20Abi as K,parseUnits as Lt,getAddress as M,formatUnits as J,formatEther as Q,multicall3Abi as It,decodeFunctionData as R}from"viem";var Z=[{inputs:[{components:[{internalType:"address",name:"recipient",type:"address"},{internalType:"contract IERC20",name:"buyToken",type:"address"},{internalType:"uint256",name:"minAmountOut",type:"uint256"}],internalType:"struct SettlerBase.AllowedSlippage",name:"slippage",type:"tuple"},{internalType:"bytes[]",name:"actions",type:"bytes[]"},{internalType:"bytes32",name:"",type:"bytes32"},{internalType:"address",name:"msgSender",type:"address"},{internalType:"bytes",name:"sig",type:"bytes"}],name:"executeMetaTxn",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"nonpayable",type:"function"}];var B="0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";var D={1:"ETH",10:"ETH",56:"BNB",137:"MATIC",8453:"ETH",42161:"ETH",43114:"AVAX"};var St={1:"0x7C39a136EA20B3483e402EA031c1f3C019bAb24b",10:"0x4069560a180EbD76bB1aF947f5119Fe555BB4eA0",56:"0x73C25Ef091Ce3F2451946Be3f982549776bFED31",137:"0xF9332450385291b6dcE301917aF6905e28E8f35f",8453:"0x5CE929DDB01804bfF35B2F5c77b735bdB094AAc8",42161:"0x1aa84EB5cb62f686FC0D908AFd85864f4A05d5Ee",43114:"0x2adb2cE26848B94E13d2f7fE0fF7E945050D741c"};var Ft="0xcA11bde05977b3631167028862bE2a173976CA11";function Pt(o){return[1,10,56,137,8453,42161,43114].includes(o)}function v(o,a){let n=0n;function r(s){s.forEach(t=>{if(t.to.toLowerCase()===a.toLowerCase()&&q(t.value,"bigint")>0n){n=n+q(t.value,"bigint")}if(t.calls&&t.calls.length>0){r(t.calls)}})}r(o.calls);return Q(n)}async function Nt({publicClient:o,transactionReceipt:a}){const n={Transfer:"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"};const{logs:r}=a;const s=r.filter(i=>i.topics[0]===n.Transfer).map(i=>({...i,address:M(i.address)}));const t=[...s.map(i=>({abi:K,address:i.address,functionName:"symbol"})),...s.map(i=>({abi:K,address:i.address,functionName:"decimals"}))];const e=await o.multicall({contracts:t});const c=Math.floor(e.length/2);const d=s.map((i,l)=>{const u=e[l].result;const m=e[c+l].result;const A=i.data==="0x"?"0":J(BigInt(i.data),m);const{address:C,topics:p}=i;const{1:T,2:b}=p;const x=M(z(T));const k=M(z(b));return{to:k,from:x,symbol:u,amount:A,address:C,decimals:m}}).filter(i=>i.amount!=="0");return d}function z(o){return`0x${o.slice(-40)}`}async function Ot({publicClient:o,transactionHash:a}){const n=await o.getChainId();if(!Pt(n)){throw new Error(`chainId ${n} is unsupported\u2026`)}const r=o.extend(b=>({async traceCall(x){return b.request({method:"debug_traceTransaction",params:[x.hash,{tracer:"callTracer"}]})}}));const s=await r.traceCall({hash:a});const t=await o.getTransaction({hash:a});const{from:e,value:c,to:d}=t;const i=v(s,e);const l=await o.getTransactionReceipt({hash:a});const u=c>0n;const m=await Nt({publicClient:o,transactionReceipt:l});const A=m.filter(b=>b.from.toLowerCase()===e.toLowerCase());let C=A.length?A[0]:m[0];let p=i==="0"?m.find(b=>{return b.to.toLowerCase()===e.toLowerCase()}):{symbol:D[n],amount:i,address:B};if(d?.toLowerCase()===Ft.toLowerCase()){const{args:b}=R({abi:It,data:t.input});const{args:x}=R({abi:Z,data:b[0][1].callData});const k=x[0].recipient.toLowerCase();const h=v(s,k);if(h==="0"){p=p=m[m.length-1]}else{p={symbol:D[n],amount:h,address:B}}}const T=d?.toLowerCase()===St[n].toLowerCase();if(T){const{args:b}=R({abi:Z,data:t.input});const{3:x}=b;const k=v(s,x);if(k==="0"){p=m[m.length-1];const h=m.filter(H=>H.to.toLowerCase()===x.toLowerCase());if(h.length===1){p={symbol:h[0].symbol,amount:h[0].amount,address:h[0].address}}else{p={symbol:"",amount:"",address:""};console.error("More than one `takerReceived` log. File a bug report here: https://github.com/0xProject/0x-parser/issues/new/choose")}}else{p={symbol:D[n],amount:k,address:B}}}if(u){const b=Q(c);const x=m.filter(k=>k.to.toLowerCase()===e).reduce((k,h)=>({symbol:h.symbol,amount:J(BigInt(k.amount)+Lt(h.amount,h.decimals),h.decimals),address:h.address}),{symbol:"",amount:"",address:""});return{tokenIn:{symbol:D[n],address:B,amount:b},tokenOut:x}}if(!p){console.error("An error has occurred. File a bug report here: https://github.com/0xProject/0x-parser/issues/new/choose");return null}return{tokenIn:{symbol:C.symbol,amount:C.amount,address:C.address},tokenOut:{symbol:p.symbol,amount:p.amount,address:p.address}}}async function ae({rpcUrl:o,exchangeProxyAbi:a,transactionHash:n}){if(!o)throw new Error("Missing rpcUrl\u2026");if(!n)throw new Error("Missing transaction hash\u2026");if(!a){throw new Error(`Missing 0x Exchange Proxy ABI: ${U}\u2026`)}const r=Dt({chain:Ht,transport:_t(o)});const{getChainId:s,getTransaction:t,getTransactionReceipt:e}=r;const[c,d,i]=await Promise.all([s(),t({hash:n}),e({hash:n})]);if(i.status==="reverted"){return null}if(!$(c)){throw new Error(`chainId ${c} is unsupported\u2026`)}const l=O[c];const u=j(c)?Y[c]:null;const m=Bt(d.to);const A=m===l;const C=m===u;if(!A&&!C){return null}const{functionName:p}=A?tt({abi:a,data:d.input}):tt({abi:P,data:d.input});const T=_[p];return T({chainId:c,transaction:d,publicClient:r,exchangeProxyAbi:a,transactionReceipt:i,callData:d.input})}export{ae as parseSwap,Ot as parseSwapV2}; |
{ | ||
"name": "@0x/0x-parser", | ||
"version": "1.2.4", | ||
"version": "1.2.5", | ||
"description": "š§¾ Designed for 0x integrators: This library parses 0x transactions on EVM blockchains into a format that is both user-friendly and easy to understand.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -250,4 +250,8 @@ import { | ||
let input = logs[0]; | ||
const fromTaker = logs.filter( | ||
(log) => log.from.toLowerCase() === taker.toLowerCase() | ||
); | ||
let input = fromTaker.length ? fromTaker[0] : logs[0]; | ||
let output = | ||
@@ -254,0 +258,0 @@ nativeTransferAmount === "0" |
@@ -541,2 +541,33 @@ import { | ||
// https://optimistic.etherscan.io/tx/0x9abc33ffc67ff9348af9a29f109c4f27b36ee5bc80dd81ae3814e69309449a61 | ||
test("parse a gasless swap on Optimism (USDC for OP) for execute", async () => { | ||
const publicClient = createPublicClient({ | ||
chain: optimism, | ||
transport: http( | ||
`https://opt-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}` | ||
), | ||
}) as PublicClient<Transport, Chain>; | ||
const transactionHash = | ||
"0x9abc33ffc67ff9348af9a29f109c4f27b36ee5bc80dd81ae3814e69309449a61"; | ||
const result = await parseSwapV2({ | ||
publicClient, | ||
transactionHash, | ||
}); | ||
expect(result).toEqual({ | ||
tokenIn: { | ||
symbol: "USDC", | ||
amount: "5", | ||
address: "0x7F5c764cBc14f9669B88837ca1490cCa17c31607", | ||
}, | ||
tokenOut: { | ||
symbol: "OP", | ||
amount: "2.73214427938472425", | ||
address: "0x4200000000000000000000000000000000000042", | ||
}, | ||
}); | ||
}); | ||
// https://bscscan.com/tx/0xdda12da1e32c3320082355c985d6f2c6559169989de51e3cc83123395516c057 | ||
@@ -543,0 +574,0 @@ test("parse a swap on BNB Chain (ETH for USDC) for execute", async () => { |
576733
17453