@0x/0x-parser
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -9,5 +9,8 @@ export declare const EVENT_SIGNATURES: { | ||
}; | ||
export declare const ERC20_FUNCTION_HASHES: { | ||
readonly symbol: "0x95d89b41"; | ||
readonly decimals: "0x313ce567"; | ||
export declare const FUNCTION_SELECTORS: { | ||
readonly permitAndCall: { | ||
readonly IERC2612: "0x34b4d153"; | ||
readonly IERC20PermitAllowed: "0x7f9cb4d7"; | ||
readonly IERC20MetaTransaction: "0x9d50b5e4"; | ||
}; | ||
}; | ||
@@ -14,0 +17,0 @@ export declare const EXCHANGE_PROXY_ABI_URL = "https://raw.githubusercontent.com/0xProject/protocol/development/packages/contract-artifacts/artifacts/IZeroEx.json"; |
@@ -1,1 +0,1 @@ | ||
"use strict";var N=Object.defineProperty;var X=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var W=Object.prototype.hasOwnProperty;var $=(s,t)=>{for(var r in t)N(s,r,{get:t[r],enumerable:true})};var j=(s,t,r,a)=>{if(t&&typeof t==="object"||typeof t==="function"){for(let n of G(t))if(!W.call(s,n)&&n!==r)N(s,n,{get:()=>t[n],enumerable:!(a=X(t,n))||a.enumerable})}return s};var q=s=>j(N({},"__esModule",{value:true}),s);var Tt={};$(Tt,{parseSwap:()=>gt});module.exports=q(Tt);var A=require("viem");var v=require("viem/chains");var l=require("viem");var O={LimitOrderFilled:"0xab614d2b738543c0ea21f56347cf696a3a0c42a7cbec3212a5ca22a4dcff2124",LiquidityProviderSwap:"0x40a6ba9513d09e3488135e0e0d10e2d4382b792720155b144cbea89ac9db6d34",OtcOrderFilled:"0xac75f773e3a92f1a02b12134d65e1f47f8a14eabe4eaf1e24624918e6a8b269f",MetaTransactionExecuted:"0x7f4fe3ff8ae440e1570c558da08440b26f89fb1c1f2910cd91ca6452955f121a",Transfer:"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",TransformedERC20:"0x0f6672f78a59ba8e5e5b5d38df3ebc67f3c792e2c9259b8d97d7f00dd78ba1b3"};var M="https://raw.githubusercontent.com/0xProject/protocol/development/packages/contract-artifacts/artifacts/IZeroEx.json";var k="0xDef1C0ded9bec7F1a1670819833240f027b25EfF";var B={1:"0x1291C02D288de3De7dC25353459489073D11E1Ae",137:"0x2Ddd30fE5c12FC4CD497526F14Bf3d1fcd3D5Db4",8453:"0x3CA53031Ad0B86a304845e83644983Be3340895f"};var P={1:k,5:"0xF91bB752490473B8342a3E964E855b9f9a2A668e",10:"0xDEF1ABE32c034e558Cdd535791643C58a13aCC10",56:k,137:k,250:"0xDEF189DeAEF76E379df891899eb5A00a94cBC250",8453:k,42161:k,42220:k,43114:k};var C={weth:"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"};var p={symbol:"ETH",decimals:18,address:"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"};var D={1:"ETH",5:"ETH",10:"ETH",56:"BNB",137:"MATIC",250:"FTM",8453:"ETH",42161:"ETH",42220:"CELO",43114:"AVAX"};var L=[{type:"function",name:"decimals",stateMutability:"view",inputs:[],outputs:[{name:"",type:"uint8"}]},{type:"function",name:"symbol",stateMutability:"view",inputs:[],outputs:[{name:"",type:"string"}]}];var h=require("viem");function H(s){return`0x${s.slice(-40)}`}function R(s){return[1,5,10,56,137,250,8453,42220,43114,42161].includes(s)}function V(s){return[1,137,8453].includes(s)}async function f({publicClient:s,transactionReceipt:t}){const{logs:r}=t;const a=r.filter(i=>i.topics[0]===O.Transfer).map(i=>({...i,address:(0,h.getAddress)(i.address)}));const n=[...a.map(i=>({abi:L,address:i.address,functionName:"symbol"})),...a.map(i=>({abi:L,address:i.address,functionName:"decimals"}))];const o=await s.multicall({contracts:n});const e=Math.floor(o.length/2);const c=a.map((i,d)=>{const u=o[d].result;const m=o[e+d].result;const T=(0,h.formatUnits)(BigInt(i.data),m);const{address:x,topics:E}=i;const{1:g,2:b}=E;const w=(0,h.getAddress)(H(g));const I=(0,h.getAddress)(H(b));return{to:I,from:w,symbol:u,amount:T,address:x,decimals:m}}).filter(i=>i.amount!=="0");return c}function y(s,t){return{tokenIn:{symbol:s.symbol,amount:s.amount,address:(0,h.getAddress)(s.address)},tokenOut:{symbol:t.symbol,amount:t.amount,address:(0,h.getAddress)(t.address)}}}var S=[{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 Z({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=(0,l.getAddress)(t.from);const n=r.find(e=>a===e.from);const o=r.find(e=>a===e.to);if(n&&o){return y(n,o)}}async function z({publicClient:s,transactionReceipt:t}){const r=(0,l.getAddress)(t.from);const a=await f({publicClient:s,transactionReceipt:t});const n=a.find(e=>r===e.from);const o=a.find(e=>e.address===C.weth);if(n&&o){o.symbol=p.symbol;o.address=p.address;return y(n,o)}}async function J({publicClient:s,transactionReceipt:t}){const r=(0,l.getAddress)(t.from);const a=await f({publicClient:s,transactionReceipt:t});const n=a.find(e=>e.address===C.weth);const o=a.find(e=>e.to===r);if(n&&o){n.symbol=p.symbol;n.address=p.address;return y(n,o)}}async function K({callData:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const{args:n}=(0,l.decodeFunctionData)({abi:r,data:s});const[o]=n;const e=await f({publicClient:t,transactionReceipt:a});const{maker:c,taker:i}=o;const d=e.find(m=>m.from===i);const u=e.find(m=>m.from===c);if(d&&u){return y(d,u)}}async function Q({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=(0,l.getAddress)(t.from);const n=r.find(e=>e.from===a);const o=r.find(e=>e.to===a);if(n&&o){return y(n,o)}}async function tt({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=r.find(o=>o.address!==C.weth);const n=r.find(o=>o.address===C.weth);if(a&&n){n.symbol=p.symbol;n.address=p.address;return y(a,n)}}async function nt({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=(0,l.getAddress)(t.from);const n=r.find(e=>e.from===a);const o=r.find(e=>e.from!==a);if(n&&o){o.symbol=p.symbol;o.address=p.address;return y(n,o)}}async function et({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=(0,l.getAddress)(t.from);const n=r.find(e=>a===e.from);const o=r.find(e=>a===e.to);if(n&&o){return y(n,o)}}async function ot({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=(0,l.getAddress)(t.from);const n=r.find(e=>e.address===C.weth);const o=r.find(e=>a===e.to);if(n&&o){n.symbol=p.symbol;n.address=p.address;return y(n,o)}}async function st({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=r[0];const n=r[r.length-1];return y(a,n)}async function rt({chainId:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const n=D[s];for(const o of a.logs){const{topics:e,data:c}=o;const[i]=e;if(i===O.TransformedERC20){let d;let u;let m;let T;const x=(0,l.decodeEventLog)({data:c,topics:e,abi:r});const{inputToken:E,outputToken:g,inputTokenAmount:b,outputTokenAmount:w}=x.args;const I={address:E,abi:L};const _={address:g,abi:L};if(E===p.address){d=n;u=18}else{[{result:d},{result:u}]=await t.multicall({contracts:[{...I,functionName:"symbol"},{...I,functionName:"decimals"}]})}if(g===p.address){m=n;T=18}else{[{result:m},{result:T}]=await t.multicall({contracts:[{..._,functionName:"symbol"},{..._,functionName:"decimals"}]})}const Y=(0,l.formatUnits)(b,u);const U=(0,l.formatUnits)(w,T);return{tokenIn:{address:E,amount:Y,symbol:d},tokenOut:{address:g,amount:U,symbol:m}}}}}async function at({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=(0,l.getAddress)(t.from);const n=r.find(e=>a===e.from);const o=r.find(e=>a===e.to);if(n&&o){return y(n,o)}}async function it({callData:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const n=await f({publicClient:t,transactionReceipt:a});const{args:o}=(0,l.decodeFunctionData)({abi:r,data:s});const[e]=o;return n.reduce((c,i)=>{const{symbol:d,amount:u,address:m}=i;if(m===C.weth){return{...c,tokenOut:{...c.tokenOut,amount:(Number(c.tokenOut.amount)+Number(u)).toString()}}}else if(m===e){return{...c,tokenIn:{...c.tokenIn,symbol:d,address:m,amount:(Number(c.tokenIn.amount)+Number(u)).toString()}}}else{return c}},{tokenIn:{address:"",amount:"",symbol:""},tokenOut:{amount:"",symbol:p.symbol,address:p.address}})}async function ct({callData:s,transaction:t,publicClient:r,exchangeProxyAbi:a,transactionReceipt:n}){const{value:o}=t;const{args:e}=(0,l.decodeFunctionData)({abi:a,data:s});const c=await f({publicClient:r,transactionReceipt:n});const[i]=e;const d=1000000000000000000n;const u=o/d;const m=o%d;const T=Number(u)+Number(m)/Number(d);const x=c.reduce((b,w)=>{return w.address===i?b+Number(w.amount):b},0);const E=c.find(b=>b.address===C.weth);const g=c.find(b=>b.address===i);if(E&&g){return{tokenIn:{symbol:p.symbol,amount:T.toString(),address:p.address},tokenOut:{symbol:g.symbol,amount:x.toString(),address:g.address}}}}async function dt({callData:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const n=await f({publicClient:t,transactionReceipt:a});const{args:o}=(0,l.decodeFunctionData)({abi:r,data:s});const[e,c]=o;const i={[e]:{amount:"0",symbol:"",address:""},[c]:{amount:"0",symbol:"",address:""}};n.forEach(({address:d,symbol:u,amount:m})=>{if((0,l.getAddress)(d)in i){i[(0,l.getAddress)(d)].address=(0,l.getAddress)(d);i[(0,l.getAddress)(d)].symbol=u;i[(0,l.getAddress)(d)].amount=(Number(i[(0,l.getAddress)(d)].amount)+Number(m)).toString()}});return{tokenIn:i[e],tokenOut:i[c]}}async function lt({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=(0,l.getAddress)(P[56]);const n=(0,l.getAddress)(t.from);let o=r.find(c=>c.from===n);let e=r.find(c=>c.from!==n);if(o===void 0){o=r.find(c=>c.from===a);e=r.find(c=>c.from!==a)}if(o&&e){return y(o,e)}}async function ut({callData:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const{args:n}=(0,l.decodeFunctionData)({abi:r,data:s});const o=await f({publicClient:t,transactionReceipt:a});const[e]=n;const{makerToken:c,takerToken:i}=e;const d=o.find(m=>m.address===i);const u=o.find(m=>m.address===c);if(d&&u){u.symbol=p.symbol;u.address=p.address;return y(d,u)}}async function mt({callData:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const{args:n}=(0,l.decodeFunctionData)({abi:r,data:s});const o=await f({publicClient:t,transactionReceipt:a});const[e]=n;const{makerToken:c,takerToken:i}=e;const d=o.find(m=>m.address===i);const u=o.find(m=>m.address===c);if(d&&u){d.symbol=p.symbol;d.address=p.address;return y(d,u)}}async function pt({callData:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const{args:n}=(0,l.decodeFunctionData)({abi:r,data:s});const[o]=n;const{maker:e,taker:c}=o;const i=await f({publicClient:t,transactionReceipt:a});if(typeof e==="string"&&typeof c==="string"){const d=i.find(m=>m.from===c);const u=i.find(m=>m.from===e);if(d&&u){return y(d,u)}}}async function ft({callData:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const{args:n}=(0,l.decodeFunctionData)({abi:r,data:s});const[o]=n;const{signer:e}=o;const c=await f({publicClient:t,transactionReceipt:a});if(typeof e==="string"){const i=c.find(u=>u.from===e);const d=c.find(u=>u.to===e);if(i&&d){return y(i,d)}}}async function yt({chainId:s,transaction:t,publicClient:r,exchangeProxyAbi:a,transactionReceipt:n,callData:o}){const{args:e}=(0,l.decodeFunctionData)({abi:a,data:o});const[c]=e;const{callData:i}=c;const{functionName:d}=(0,l.decodeFunctionData)({data:i,abi:a});const u=F[d];return u({chainId:s,callData:i,transaction:t,publicClient:r,exchangeProxyAbi:a,transactionReceipt:n})}function bt({chainId:s,transaction:t,publicClient:r,exchangeProxyAbi:a,transactionReceipt:n}){const{args:o}=(0,l.decodeFunctionData)({abi:S,data:t.input});let{7:e}=o;if(!e){const{6:d}=o;e=d}const{functionName:c}=(0,l.decodeFunctionData)({abi:a,data:e});const i=F[c];return i({chainId:s,callData:e,transaction:t,publicClient:r,exchangeProxyAbi:a,transactionReceipt:n})}var F={fillLimitOrder:pt,fillOtcOrder:Q,fillOtcOrderForEth:ut,fillOtcOrderWithEth:mt,fillTakerSignedOtcOrder:K,fillTakerSignedOtcOrderForEth:tt,executeMetaTransaction:ft,executeMetaTransactionV2:yt,multiplexBatchSellTokenForToken:dt,multiplexBatchSellTokenForEth:it,multiplexBatchSellEthForToken:ct,multiplexMultiHopSellTokenForToken:at,multiplexMultiHopSellEthForToken:J,multiplexMultiHopSellTokenForEth:z,permitAndCall:bt,sellToUniswap:st,sellTokenForEthToUniswapV3:nt,sellEthForTokenToUniswapV3:ot,sellTokenForTokenToUniswapV3:et,sellToLiquidityProvider:Z,sellToPancakeSwap:lt,transformERC20:rt};async function gt({rpcUrl:s,exchangeProxyAbi:t,transactionHash:r}){if(!s)throw new Error("Missing rpcUrl\u2026");if(!r)throw new Error("Missing transaction hash\u2026");if(!t){throw new Error(`Missing 0x Exchange Proxy ABI: ${M}\u2026`)}const a=(0,A.createPublicClient)({chain:v.mainnet,transport:(0,A.http)(s)});const{getChainId:n,getTransaction:o,getTransactionReceipt:e}=a;const[c,i,d]=await Promise.all([n(),o({hash:r}),e({hash:r})]);if(d.status==="reverted"){return null}if(!R(c)){throw new Error(`chainId ${c} is unsupported\u2026`)}const u=P[c];const m=V(c)?B[c]:null;const T=(0,A.getAddress)(i.to);const x=T===u;const E=T===m;if(!x&&!E){return null}const{functionName:g}=x?(0,A.decodeFunctionData)({abi:t,data:i.input}):(0,A.decodeFunctionData)({abi:S,data:i.input});const b=F[g];return b({chainId:c,transaction:i,publicClient:a,exchangeProxyAbi:t,transactionReceipt:d,callData:i.input})}0&&(module.exports={parseSwap}); | ||
"use strict";var N=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var $=Object.prototype.hasOwnProperty;var j=(s,t)=>{for(var r in t)N(s,r,{get:t[r],enumerable:true})};var q=(s,t,r,a)=>{if(t&&typeof t==="object"||typeof t==="function"){for(let e of W(t))if(!$.call(s,e)&&e!==r)N(s,e,{get:()=>t[e],enumerable:!(a=G(t,e))||a.enumerable})}return s};var Z=s=>q(N({},"__esModule",{value:true}),s);var Et={};j(Et,{parseSwap:()=>Tt});module.exports=Z(Et);var x=require("viem");var Y=require("viem/chains");var l=require("viem");var P={LimitOrderFilled:"0xab614d2b738543c0ea21f56347cf696a3a0c42a7cbec3212a5ca22a4dcff2124",LiquidityProviderSwap:"0x40a6ba9513d09e3488135e0e0d10e2d4382b792720155b144cbea89ac9db6d34",OtcOrderFilled:"0xac75f773e3a92f1a02b12134d65e1f47f8a14eabe4eaf1e24624918e6a8b269f",MetaTransactionExecuted:"0x7f4fe3ff8ae440e1570c558da08440b26f89fb1c1f2910cd91ca6452955f121a",Transfer:"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",TransformedERC20:"0x0f6672f78a59ba8e5e5b5d38df3ebc67f3c792e2c9259b8d97d7f00dd78ba1b3"};var M={permitAndCall:{IERC2612:"0x34b4d153",IERC20PermitAllowed:"0x7f9cb4d7",IERC20MetaTransaction:"0x9d50b5e4"}};var B="https://raw.githubusercontent.com/0xProject/protocol/development/packages/contract-artifacts/artifacts/IZeroEx.json";var h="0xDef1C0ded9bec7F1a1670819833240f027b25EfF";var D={1:"0x1291C02D288de3De7dC25353459489073D11E1Ae",137:"0x2Ddd30fE5c12FC4CD497526F14Bf3d1fcd3D5Db4",8453:"0x3CA53031Ad0B86a304845e83644983Be3340895f"};var S={1:h,5:"0xF91bB752490473B8342a3E964E855b9f9a2A668e",10:"0xDEF1ABE32c034e558Cdd535791643C58a13aCC10",56:h,137:h,250:"0xDEF189DeAEF76E379df891899eb5A00a94cBC250",8453:h,42161:h,42220:h,43114:h};var C={weth:"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"};var p={symbol:"ETH",decimals:18,address:"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"};var R={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"}]}];var k=require("viem");function H(s){return`0x${s.slice(-40)}`}function V(s){return[1,5,10,56,137,250,8453,42220,43114,42161].includes(s)}function v(s){return[1,137,8453].includes(s)}async function f({publicClient:s,transactionReceipt:t}){const{logs:r}=t;const a=r.filter(i=>i.topics[0]===P.Transfer).map(i=>({...i,address:(0,k.getAddress)(i.address)}));const e=[...a.map(i=>({abi:I,address:i.address,functionName:"symbol"})),...a.map(i=>({abi:I,address:i.address,functionName:"decimals"}))];const o=await s.multicall({contracts:e});const n=Math.floor(o.length/2);const c=a.map((i,d)=>{const u=o[d].result;const m=o[n+d].result;const b=(0,k.formatUnits)(BigInt(i.data),m);const{address:E,topics:A}=i;const{1:T,2:g}=A;const w=(0,k.getAddress)(H(T));const O=(0,k.getAddress)(H(g));return{to:O,from:w,symbol:u,amount:b,address:E,decimals:m}}).filter(i=>i.amount!=="0");return c}function y(s,t){return{tokenIn:{symbol:s.symbol,amount:s.amount,address:(0,k.getAddress)(s.address)},tokenOut:{symbol:t.symbol,amount:t.amount,address:(0,k.getAddress)(t.address)}}}var L=[{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 z({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=(0,l.getAddress)(t.from);const e=r.find(n=>a===n.from);const o=r.find(n=>a===n.to);if(e&&o){return y(e,o)}}async function J({publicClient:s,transactionReceipt:t}){const r=(0,l.getAddress)(t.from);const a=await f({publicClient:s,transactionReceipt:t});const e=a.find(n=>r===n.from);const o=a.find(n=>n.address===C.weth);if(e&&o){o.symbol=p.symbol;o.address=p.address;return y(e,o)}}async function K({publicClient:s,transactionReceipt:t}){const r=(0,l.getAddress)(t.from);const a=await f({publicClient:s,transactionReceipt:t});const e=a.find(n=>n.address===C.weth);const o=a.find(n=>n.to===r);if(e&&o){e.symbol=p.symbol;e.address=p.address;return y(e,o)}}async function Q({callData:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const{args:e}=(0,l.decodeFunctionData)({abi:r,data:s});const[o]=e;const n=await f({publicClient:t,transactionReceipt:a});const{maker:c,taker:i}=o;const d=n.find(m=>m.from===i);const u=n.find(m=>m.from===c);if(d&&u){return y(d,u)}}async function tt({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=(0,l.getAddress)(t.from);const e=r.find(n=>n.from===a);const o=r.find(n=>n.to===a);if(e&&o){return y(e,o)}}async function nt({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=r.find(o=>o.address!==C.weth);const e=r.find(o=>o.address===C.weth);if(a&&e){e.symbol=p.symbol;e.address=p.address;return y(a,e)}}async function et({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=(0,l.getAddress)(t.from);const e=r.find(n=>n.from===a);const o=r.find(n=>n.from!==a);if(e&&o){o.symbol=p.symbol;o.address=p.address;return y(e,o)}}async function ot({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=(0,l.getAddress)(t.from);const e=r.find(n=>a===n.from);const o=r.find(n=>a===n.to);if(e&&o){return y(e,o)}}async function st({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=(0,l.getAddress)(t.from);const e=r.find(n=>n.address===C.weth);const o=r.find(n=>a===n.to);if(e&&o){e.symbol=p.symbol;e.address=p.address;return y(e,o)}}async function rt({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=r[0];const e=r[r.length-1];return y(a,e)}async function at({chainId:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const e=R[s];for(const o of a.logs){const{topics:n,data:c}=o;const[i]=n;if(i===P.TransformedERC20){let d;let u;let m;let b;const E=(0,l.decodeEventLog)({data:c,topics:n,abi:r});const{inputToken:A,outputToken:T,inputTokenAmount:g,outputTokenAmount:w}=E.args;const O={address:A,abi:I};const _={address:T,abi:I};if(A===p.address){d=e;u=18}else{[{result:d},{result:u}]=await t.multicall({contracts:[{...O,functionName:"symbol"},{...O,functionName:"decimals"}]})}if(T===p.address){m=e;b=18}else{[{result:m},{result:b}]=await t.multicall({contracts:[{..._,functionName:"symbol"},{..._,functionName:"decimals"}]})}const U=(0,l.formatUnits)(g,u);const X=(0,l.formatUnits)(w,b);return{tokenIn:{address:A,amount:U,symbol:d},tokenOut:{address:T,amount:X,symbol:m}}}}}async function it({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=(0,l.getAddress)(t.from);const e=r.find(n=>a===n.from);const o=r.find(n=>a===n.to);if(e&&o){return y(e,o)}}async function ct({callData:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const e=await f({publicClient:t,transactionReceipt:a});const{args:o}=(0,l.decodeFunctionData)({abi:r,data:s});const[n]=o;return e.reduce((c,i)=>{const{symbol:d,amount:u,address:m}=i;if(m===C.weth){return{...c,tokenOut:{...c.tokenOut,amount:(Number(c.tokenOut.amount)+Number(u)).toString()}}}else if(m===n){return{...c,tokenIn:{...c.tokenIn,symbol:d,address:m,amount:(Number(c.tokenIn.amount)+Number(u)).toString()}}}else{return c}},{tokenIn:{address:"",amount:"",symbol:""},tokenOut:{amount:"",symbol:p.symbol,address:p.address}})}async function dt({callData:s,transaction:t,publicClient:r,exchangeProxyAbi:a,transactionReceipt:e}){const{value:o}=t;const{args:n}=(0,l.decodeFunctionData)({abi:a,data:s});const c=await f({publicClient:r,transactionReceipt:e});const[i]=n;const d=1000000000000000000n;const u=o/d;const m=o%d;const b=Number(u)+Number(m)/Number(d);const E=c.reduce((g,w)=>{return w.address===i?g+Number(w.amount):g},0);const A=c.find(g=>g.address===C.weth);const T=c.find(g=>g.address===i);if(A&&T){return{tokenIn:{symbol:p.symbol,amount:b.toString(),address:p.address},tokenOut:{symbol:T.symbol,amount:E.toString(),address:T.address}}}}async function lt({callData:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const e=await f({publicClient:t,transactionReceipt:a});const{args:o}=(0,l.decodeFunctionData)({abi:r,data:s});const[n,c]=o;const i={[n]:{amount:"0",symbol:"",address:""},[c]:{amount:"0",symbol:"",address:""}};e.forEach(({address:d,symbol:u,amount:m})=>{if((0,l.getAddress)(d)in i){i[(0,l.getAddress)(d)].address=(0,l.getAddress)(d);i[(0,l.getAddress)(d)].symbol=u;i[(0,l.getAddress)(d)].amount=(Number(i[(0,l.getAddress)(d)].amount)+Number(m)).toString()}});return{tokenIn:i[n],tokenOut:i[c]}}async function ut({publicClient:s,transactionReceipt:t}){const r=await f({publicClient:s,transactionReceipt:t});const a=(0,l.getAddress)(S[56]);const e=(0,l.getAddress)(t.from);let o=r.find(c=>c.from===e);let n=r.find(c=>c.from!==e);if(o===void 0){o=r.find(c=>c.from===a);n=r.find(c=>c.from!==a)}if(o&&n){return y(o,n)}}async function mt({callData:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const{args:e}=(0,l.decodeFunctionData)({abi:r,data:s});const o=await f({publicClient:t,transactionReceipt:a});const[n]=e;const{makerToken:c,takerToken:i}=n;const d=o.find(m=>m.address===i);const u=o.find(m=>m.address===c);if(d&&u){u.symbol=p.symbol;u.address=p.address;return y(d,u)}}async function pt({callData:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const{args:e}=(0,l.decodeFunctionData)({abi:r,data:s});const o=await f({publicClient:t,transactionReceipt:a});const[n]=e;const{makerToken:c,takerToken:i}=n;const d=o.find(m=>m.address===i);const u=o.find(m=>m.address===c);if(d&&u){d.symbol=p.symbol;d.address=p.address;return y(d,u)}}async function ft({callData:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const{args:e}=(0,l.decodeFunctionData)({abi:r,data:s});const[o]=e;const{maker:n,taker:c}=o;const i=await f({publicClient:t,transactionReceipt:a});if(typeof n==="string"&&typeof c==="string"){const d=i.find(m=>m.from===c);const u=i.find(m=>m.from===n);if(d&&u){return y(d,u)}}}async function yt({callData:s,publicClient:t,exchangeProxyAbi:r,transactionReceipt:a}){const{args:e}=(0,l.decodeFunctionData)({abi:r,data:s});const[o]=e;const{signer:n}=o;const c=await f({publicClient:t,transactionReceipt:a});if(typeof n==="string"){const i=c.find(u=>u.from===n);const d=c.find(u=>u.to===n);if(i&&d){return y(i,d)}}}async function bt({chainId:s,transaction:t,publicClient:r,exchangeProxyAbi:a,transactionReceipt:e,callData:o}){const{args:n}=(0,l.decodeFunctionData)({abi:a,data:o});const[c]=n;const{callData:i}=c;const{functionName:d}=(0,l.decodeFunctionData)({data:i,abi:a});const u=F[d];return u({chainId:s,callData:i,transaction:t,publicClient:r,exchangeProxyAbi:a,transactionReceipt:e})}function gt({chainId:s,transaction:t,publicClient:r,exchangeProxyAbi:a,transactionReceipt:e}){const{args:o}=(0,l.decodeFunctionData)({abi:L,data:t.input});let n;const c=(0,l.encodeFunctionData)({abi:L,functionName:"permitAndCall",args:[...o]});const i=c.slice(0,10);const{IERC2612:d,IERC20PermitAllowed:u,IERC20MetaTransaction:m}=M.permitAndCall;switch(i){case d:n=o[7];break;case u:n=o[8];break;case m:n=o[6];break}if(n){const{functionName:b}=(0,l.decodeFunctionData)({abi:a,data:n});const E=F[b];return E({chainId:s,callData:n,transaction:t,publicClient:r,exchangeProxyAbi:a,transactionReceipt:e})}}var F={fillLimitOrder:ft,fillOtcOrder:tt,fillOtcOrderForEth:mt,fillOtcOrderWithEth:pt,fillTakerSignedOtcOrder:Q,fillTakerSignedOtcOrderForEth:nt,executeMetaTransaction:yt,executeMetaTransactionV2:bt,multiplexBatchSellTokenForToken:lt,multiplexBatchSellTokenForEth:ct,multiplexBatchSellEthForToken:dt,multiplexMultiHopSellTokenForToken:it,multiplexMultiHopSellEthForToken:K,multiplexMultiHopSellTokenForEth:J,permitAndCall:gt,sellToUniswap:rt,sellTokenForEthToUniswapV3:et,sellEthForTokenToUniswapV3:st,sellTokenForTokenToUniswapV3:ot,sellToLiquidityProvider:z,sellToPancakeSwap:ut,transformERC20:at};async function Tt({rpcUrl:s,exchangeProxyAbi:t,transactionHash:r}){if(!s)throw new Error("Missing rpcUrl\u2026");if(!r)throw new Error("Missing transaction hash\u2026");if(!t){throw new Error(`Missing 0x Exchange Proxy ABI: ${B}\u2026`)}const a=(0,x.createPublicClient)({chain:Y.mainnet,transport:(0,x.http)(s)});const{getChainId:e,getTransaction:o,getTransactionReceipt:n}=a;const[c,i,d]=await Promise.all([e(),o({hash:r}),n({hash:r})]);if(d.status==="reverted"){return null}if(!V(c)){throw new Error(`chainId ${c} is unsupported\u2026`)}const u=S[c];const m=v(c)?D[c]:null;const b=(0,x.getAddress)(i.to);const E=b===u;const A=b===m;if(!E&&!A){return null}const{functionName:T}=E?(0,x.decodeFunctionData)({abi:t,data:i.input}):(0,x.decodeFunctionData)({abi:L,data:i.input});const g=F[T];return g({chainId:c,transaction:i,publicClient:a,exchangeProxyAbi:t,transactionReceipt:d,callData:i.input})}0&&(module.exports={parseSwap}); |
@@ -1,1 +0,1 @@ | ||
import{http as pt,getAddress as ft,decodeFunctionData as v,createPublicClient as yt}from"viem";import{mainnet as bt}from"viem/chains";import{getAddress as y,formatUnits as V,decodeEventLog as G,decodeFunctionData as g}from"viem";var O={LimitOrderFilled:"0xab614d2b738543c0ea21f56347cf696a3a0c42a7cbec3212a5ca22a4dcff2124",LiquidityProviderSwap:"0x40a6ba9513d09e3488135e0e0d10e2d4382b792720155b144cbea89ac9db6d34",OtcOrderFilled:"0xac75f773e3a92f1a02b12134d65e1f47f8a14eabe4eaf1e24624918e6a8b269f",MetaTransactionExecuted:"0x7f4fe3ff8ae440e1570c558da08440b26f89fb1c1f2910cd91ca6452955f121a",Transfer:"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",TransformedERC20:"0x0f6672f78a59ba8e5e5b5d38df3ebc67f3c792e2c9259b8d97d7f00dd78ba1b3"};var _="https://raw.githubusercontent.com/0xProject/protocol/development/packages/contract-artifacts/artifacts/IZeroEx.json";var h="0xDef1C0ded9bec7F1a1670819833240f027b25EfF";var M={1:"0x1291C02D288de3De7dC25353459489073D11E1Ae",137:"0x2Ddd30fE5c12FC4CD497526F14Bf3d1fcd3D5Db4",8453:"0x3CA53031Ad0B86a304845e83644983Be3340895f"};var P={1:h,5:"0xF91bB752490473B8342a3E964E855b9f9a2A668e",10:"0xDEF1ABE32c034e558Cdd535791643C58a13aCC10",56:h,137:h,250:"0xDEF189DeAEF76E379df891899eb5A00a94cBC250",8453:h,42161:h,42220:h,43114:h};var C={weth:"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"};var m={symbol:"ETH",decimals:18,address:"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"};var B={1:"ETH",5:"ETH",10:"ETH",56:"BNB",137:"MATIC",250:"FTM",8453:"ETH",42161:"ETH",42220:"CELO",43114:"AVAX"};var w=[{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 L,formatUnits as X}from"viem";function D(a){return`0x${a.slice(-40)}`}function H(a){return[1,5,10,56,137,250,8453,42220,43114,42161].includes(a)}function R(a){return[1,137,8453].includes(a)}async function p({publicClient:a,transactionReceipt:e}){const{logs:r}=e;const s=r.filter(i=>i.topics[0]===O.Transfer).map(i=>({...i,address:L(i.address)}));const o=[...s.map(i=>({abi:w,address:i.address,functionName:"symbol"})),...s.map(i=>({abi:w,address:i.address,functionName:"decimals"}))];const n=await a.multicall({contracts:o});const t=Math.floor(n.length/2);const c=s.map((i,d)=>{const l=n[d].result;const u=n[t+d].result;const E=X(BigInt(i.data),u);const{address:x,topics:A}=i;const{1:T,2:b}=A;const k=L(D(T));const I=L(D(b));return{to:I,from:k,symbol:l,amount:E,address:x,decimals:u}}).filter(i=>i.amount!=="0");return c}function f(a,e){return{tokenIn:{symbol:a.symbol,amount:a.amount,address:L(a.address)},tokenOut:{symbol:e.symbol,amount:e.amount,address:L(e.address)}}}var S=[{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 W({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=y(e.from);const o=r.find(t=>s===t.from);const n=r.find(t=>s===t.to);if(o&&n){return f(o,n)}}async function $({publicClient:a,transactionReceipt:e}){const r=y(e.from);const s=await p({publicClient:a,transactionReceipt:e});const o=s.find(t=>r===t.from);const n=s.find(t=>t.address===C.weth);if(o&&n){n.symbol=m.symbol;n.address=m.address;return f(o,n)}}async function j({publicClient:a,transactionReceipt:e}){const r=y(e.from);const s=await p({publicClient:a,transactionReceipt:e});const o=s.find(t=>t.address===C.weth);const n=s.find(t=>t.to===r);if(o&&n){o.symbol=m.symbol;o.address=m.address;return f(o,n)}}async function q({callData:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const{args:o}=g({abi:r,data:a});const[n]=o;const t=await p({publicClient:e,transactionReceipt:s});const{maker:c,taker:i}=n;const d=t.find(u=>u.from===i);const l=t.find(u=>u.from===c);if(d&&l){return f(d,l)}}async function Z({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=y(e.from);const o=r.find(t=>t.from===s);const n=r.find(t=>t.to===s);if(o&&n){return f(o,n)}}async function z({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=r.find(n=>n.address!==C.weth);const o=r.find(n=>n.address===C.weth);if(s&&o){o.symbol=m.symbol;o.address=m.address;return f(s,o)}}async function J({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=y(e.from);const o=r.find(t=>t.from===s);const n=r.find(t=>t.from!==s);if(o&&n){n.symbol=m.symbol;n.address=m.address;return f(o,n)}}async function K({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=y(e.from);const o=r.find(t=>s===t.from);const n=r.find(t=>s===t.to);if(o&&n){return f(o,n)}}async function Q({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=y(e.from);const o=r.find(t=>t.address===C.weth);const n=r.find(t=>s===t.to);if(o&&n){o.symbol=m.symbol;o.address=m.address;return f(o,n)}}async function tt({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=r[0];const o=r[r.length-1];return f(s,o)}async function nt({chainId:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const o=B[a];for(const n of s.logs){const{topics:t,data:c}=n;const[i]=t;if(i===O.TransformedERC20){let d;let l;let u;let E;const x=G({data:c,topics:t,abi:r});const{inputToken:A,outputToken:T,inputTokenAmount:b,outputTokenAmount:k}=x.args;const I={address:A,abi:w};const N={address:T,abi:w};if(A===m.address){d=o;l=18}else{[{result:d},{result:l}]=await e.multicall({contracts:[{...I,functionName:"symbol"},{...I,functionName:"decimals"}]})}if(T===m.address){u=o;E=18}else{[{result:u},{result:E}]=await e.multicall({contracts:[{...N,functionName:"symbol"},{...N,functionName:"decimals"}]})}const Y=V(b,l);const U=V(k,E);return{tokenIn:{address:A,amount:Y,symbol:d},tokenOut:{address:T,amount:U,symbol:u}}}}}async function et({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=y(e.from);const o=r.find(t=>s===t.from);const n=r.find(t=>s===t.to);if(o&&n){return f(o,n)}}async function ot({callData:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const o=await p({publicClient:e,transactionReceipt:s});const{args:n}=g({abi:r,data:a});const[t]=n;return o.reduce((c,i)=>{const{symbol:d,amount:l,address:u}=i;if(u===C.weth){return{...c,tokenOut:{...c.tokenOut,amount:(Number(c.tokenOut.amount)+Number(l)).toString()}}}else if(u===t){return{...c,tokenIn:{...c.tokenIn,symbol:d,address:u,amount:(Number(c.tokenIn.amount)+Number(l)).toString()}}}else{return c}},{tokenIn:{address:"",amount:"",symbol:""},tokenOut:{amount:"",symbol:m.symbol,address:m.address}})}async function st({callData:a,transaction:e,publicClient:r,exchangeProxyAbi:s,transactionReceipt:o}){const{value:n}=e;const{args:t}=g({abi:s,data:a});const c=await p({publicClient:r,transactionReceipt:o});const[i]=t;const d=1000000000000000000n;const l=n/d;const u=n%d;const E=Number(l)+Number(u)/Number(d);const x=c.reduce((b,k)=>{return k.address===i?b+Number(k.amount):b},0);const A=c.find(b=>b.address===C.weth);const T=c.find(b=>b.address===i);if(A&&T){return{tokenIn:{symbol:m.symbol,amount:E.toString(),address:m.address},tokenOut:{symbol:T.symbol,amount:x.toString(),address:T.address}}}}async function rt({callData:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const o=await p({publicClient:e,transactionReceipt:s});const{args:n}=g({abi:r,data:a});const[t,c]=n;const i={[t]:{amount:"0",symbol:"",address:""},[c]:{amount:"0",symbol:"",address:""}};o.forEach(({address:d,symbol:l,amount:u})=>{if(y(d)in i){i[y(d)].address=y(d);i[y(d)].symbol=l;i[y(d)].amount=(Number(i[y(d)].amount)+Number(u)).toString()}});return{tokenIn:i[t],tokenOut:i[c]}}async function at({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=y(P[56]);const o=y(e.from);let n=r.find(c=>c.from===o);let t=r.find(c=>c.from!==o);if(n===void 0){n=r.find(c=>c.from===s);t=r.find(c=>c.from!==s)}if(n&&t){return f(n,t)}}async function it({callData:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const{args:o}=g({abi:r,data:a});const n=await p({publicClient:e,transactionReceipt:s});const[t]=o;const{makerToken:c,takerToken:i}=t;const d=n.find(u=>u.address===i);const l=n.find(u=>u.address===c);if(d&&l){l.symbol=m.symbol;l.address=m.address;return f(d,l)}}async function ct({callData:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const{args:o}=g({abi:r,data:a});const n=await p({publicClient:e,transactionReceipt:s});const[t]=o;const{makerToken:c,takerToken:i}=t;const d=n.find(u=>u.address===i);const l=n.find(u=>u.address===c);if(d&&l){d.symbol=m.symbol;d.address=m.address;return f(d,l)}}async function dt({callData:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const{args:o}=g({abi:r,data:a});const[n]=o;const{maker:t,taker:c}=n;const i=await p({publicClient:e,transactionReceipt:s});if(typeof t==="string"&&typeof c==="string"){const d=i.find(u=>u.from===c);const l=i.find(u=>u.from===t);if(d&&l){return f(d,l)}}}async function lt({callData:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const{args:o}=g({abi:r,data:a});const[n]=o;const{signer:t}=n;const c=await p({publicClient:e,transactionReceipt:s});if(typeof t==="string"){const i=c.find(l=>l.from===t);const d=c.find(l=>l.to===t);if(i&&d){return f(i,d)}}}async function ut({chainId:a,transaction:e,publicClient:r,exchangeProxyAbi:s,transactionReceipt:o,callData:n}){const{args:t}=g({abi:s,data:n});const[c]=t;const{callData:i}=c;const{functionName:d}=g({data:i,abi:s});const l=F[d];return l({chainId:a,callData:i,transaction:e,publicClient:r,exchangeProxyAbi:s,transactionReceipt:o})}function mt({chainId:a,transaction:e,publicClient:r,exchangeProxyAbi:s,transactionReceipt:o}){const{args:n}=g({abi:S,data:e.input});let{7:t}=n;if(!t){const{6:d}=n;t=d}const{functionName:c}=g({abi:s,data:t});const i=F[c];return i({chainId:a,callData:t,transaction:e,publicClient:r,exchangeProxyAbi:s,transactionReceipt:o})}var F={fillLimitOrder:dt,fillOtcOrder:Z,fillOtcOrderForEth:it,fillOtcOrderWithEth:ct,fillTakerSignedOtcOrder:q,fillTakerSignedOtcOrderForEth:z,executeMetaTransaction:lt,executeMetaTransactionV2:ut,multiplexBatchSellTokenForToken:rt,multiplexBatchSellTokenForEth:ot,multiplexBatchSellEthForToken:st,multiplexMultiHopSellTokenForToken:et,multiplexMultiHopSellEthForToken:j,multiplexMultiHopSellTokenForEth:$,permitAndCall:mt,sellToUniswap:tt,sellTokenForEthToUniswapV3:J,sellEthForTokenToUniswapV3:Q,sellTokenForTokenToUniswapV3:K,sellToLiquidityProvider:W,sellToPancakeSwap:at,transformERC20:nt};async function Ht({rpcUrl:a,exchangeProxyAbi:e,transactionHash:r}){if(!a)throw new Error("Missing rpcUrl\u2026");if(!r)throw new Error("Missing transaction hash\u2026");if(!e){throw new Error(`Missing 0x Exchange Proxy ABI: ${_}\u2026`)}const s=yt({chain:bt,transport:pt(a)});const{getChainId:o,getTransaction:n,getTransactionReceipt:t}=s;const[c,i,d]=await Promise.all([o(),n({hash:r}),t({hash:r})]);if(d.status==="reverted"){return null}if(!H(c)){throw new Error(`chainId ${c} is unsupported\u2026`)}const l=P[c];const u=R(c)?M[c]:null;const E=ft(i.to);const x=E===l;const A=E===u;if(!x&&!A){return null}const{functionName:T}=x?v({abi:e,data:i.input}):v({abi:S,data:i.input});const b=F[T];return b({chainId:c,transaction:i,publicClient:s,exchangeProxyAbi:e,transactionReceipt:d,callData:i.input})}export{Ht as parseSwap}; | ||
import{http as yt,getAddress as bt,decodeFunctionData as Y,createPublicClient as gt}from"viem";import{mainnet as Tt}from"viem/chains";import{getAddress as y,formatUnits as v,decodeEventLog as W,decodeFunctionData as T,encodeFunctionData as $}from"viem";var P={LimitOrderFilled:"0xab614d2b738543c0ea21f56347cf696a3a0c42a7cbec3212a5ca22a4dcff2124",LiquidityProviderSwap:"0x40a6ba9513d09e3488135e0e0d10e2d4382b792720155b144cbea89ac9db6d34",OtcOrderFilled:"0xac75f773e3a92f1a02b12134d65e1f47f8a14eabe4eaf1e24624918e6a8b269f",MetaTransactionExecuted:"0x7f4fe3ff8ae440e1570c558da08440b26f89fb1c1f2910cd91ca6452955f121a",Transfer:"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",TransformedERC20:"0x0f6672f78a59ba8e5e5b5d38df3ebc67f3c792e2c9259b8d97d7f00dd78ba1b3"};var _={permitAndCall:{IERC2612:"0x34b4d153",IERC20PermitAllowed:"0x7f9cb4d7",IERC20MetaTransaction:"0x9d50b5e4"}};var M="https://raw.githubusercontent.com/0xProject/protocol/development/packages/contract-artifacts/artifacts/IZeroEx.json";var k="0xDef1C0ded9bec7F1a1670819833240f027b25EfF";var B={1:"0x1291C02D288de3De7dC25353459489073D11E1Ae",137:"0x2Ddd30fE5c12FC4CD497526F14Bf3d1fcd3D5Db4",8453:"0x3CA53031Ad0B86a304845e83644983Be3340895f"};var S={1:k,5:"0xF91bB752490473B8342a3E964E855b9f9a2A668e",10:"0xDEF1ABE32c034e558Cdd535791643C58a13aCC10",56:k,137:k,250:"0xDEF189DeAEF76E379df891899eb5A00a94cBC250",8453:k,42161:k,42220:k,43114:k};var C={weth:"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"};var m={symbol:"ETH",decimals:18,address:"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"};var D={1:"ETH",5:"ETH",10:"ETH",56:"BNB",137:"MATIC",250:"FTM",8453:"ETH",42161:"ETH",42220:"CELO",43114:"AVAX"};var w=[{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 I,formatUnits as G}from"viem";function R(a){return`0x${a.slice(-40)}`}function H(a){return[1,5,10,56,137,250,8453,42220,43114,42161].includes(a)}function V(a){return[1,137,8453].includes(a)}async function p({publicClient:a,transactionReceipt:e}){const{logs:r}=e;const s=r.filter(i=>i.topics[0]===P.Transfer).map(i=>({...i,address:I(i.address)}));const o=[...s.map(i=>({abi:w,address:i.address,functionName:"symbol"})),...s.map(i=>({abi:w,address:i.address,functionName:"decimals"}))];const n=await a.multicall({contracts:o});const t=Math.floor(n.length/2);const c=s.map((i,d)=>{const l=n[d].result;const u=n[t+d].result;const b=G(BigInt(i.data),u);const{address:A,topics:x}=i;const{1:E,2:g}=x;const h=I(R(E));const O=I(R(g));return{to:O,from:h,symbol:l,amount:b,address:A,decimals:u}}).filter(i=>i.amount!=="0");return c}function f(a,e){return{tokenIn:{symbol:a.symbol,amount:a.amount,address:I(a.address)},tokenOut:{symbol:e.symbol,amount:e.amount,address:I(e.address)}}}var L=[{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 j({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=y(e.from);const o=r.find(t=>s===t.from);const n=r.find(t=>s===t.to);if(o&&n){return f(o,n)}}async function q({publicClient:a,transactionReceipt:e}){const r=y(e.from);const s=await p({publicClient:a,transactionReceipt:e});const o=s.find(t=>r===t.from);const n=s.find(t=>t.address===C.weth);if(o&&n){n.symbol=m.symbol;n.address=m.address;return f(o,n)}}async function Z({publicClient:a,transactionReceipt:e}){const r=y(e.from);const s=await p({publicClient:a,transactionReceipt:e});const o=s.find(t=>t.address===C.weth);const n=s.find(t=>t.to===r);if(o&&n){o.symbol=m.symbol;o.address=m.address;return f(o,n)}}async function z({callData:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const{args:o}=T({abi:r,data:a});const[n]=o;const t=await p({publicClient:e,transactionReceipt:s});const{maker:c,taker:i}=n;const d=t.find(u=>u.from===i);const l=t.find(u=>u.from===c);if(d&&l){return f(d,l)}}async function J({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=y(e.from);const o=r.find(t=>t.from===s);const n=r.find(t=>t.to===s);if(o&&n){return f(o,n)}}async function K({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=r.find(n=>n.address!==C.weth);const o=r.find(n=>n.address===C.weth);if(s&&o){o.symbol=m.symbol;o.address=m.address;return f(s,o)}}async function Q({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=y(e.from);const o=r.find(t=>t.from===s);const n=r.find(t=>t.from!==s);if(o&&n){n.symbol=m.symbol;n.address=m.address;return f(o,n)}}async function tt({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=y(e.from);const o=r.find(t=>s===t.from);const n=r.find(t=>s===t.to);if(o&&n){return f(o,n)}}async function nt({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=y(e.from);const o=r.find(t=>t.address===C.weth);const n=r.find(t=>s===t.to);if(o&&n){o.symbol=m.symbol;o.address=m.address;return f(o,n)}}async function et({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=r[0];const o=r[r.length-1];return f(s,o)}async function ot({chainId:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const o=D[a];for(const n of s.logs){const{topics:t,data:c}=n;const[i]=t;if(i===P.TransformedERC20){let d;let l;let u;let b;const A=W({data:c,topics:t,abi:r});const{inputToken:x,outputToken:E,inputTokenAmount:g,outputTokenAmount:h}=A.args;const O={address:x,abi:w};const N={address:E,abi:w};if(x===m.address){d=o;l=18}else{[{result:d},{result:l}]=await e.multicall({contracts:[{...O,functionName:"symbol"},{...O,functionName:"decimals"}]})}if(E===m.address){u=o;b=18}else{[{result:u},{result:b}]=await e.multicall({contracts:[{...N,functionName:"symbol"},{...N,functionName:"decimals"}]})}const U=v(g,l);const X=v(h,b);return{tokenIn:{address:x,amount:U,symbol:d},tokenOut:{address:E,amount:X,symbol:u}}}}}async function st({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=y(e.from);const o=r.find(t=>s===t.from);const n=r.find(t=>s===t.to);if(o&&n){return f(o,n)}}async function rt({callData:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const o=await p({publicClient:e,transactionReceipt:s});const{args:n}=T({abi:r,data:a});const[t]=n;return o.reduce((c,i)=>{const{symbol:d,amount:l,address:u}=i;if(u===C.weth){return{...c,tokenOut:{...c.tokenOut,amount:(Number(c.tokenOut.amount)+Number(l)).toString()}}}else if(u===t){return{...c,tokenIn:{...c.tokenIn,symbol:d,address:u,amount:(Number(c.tokenIn.amount)+Number(l)).toString()}}}else{return c}},{tokenIn:{address:"",amount:"",symbol:""},tokenOut:{amount:"",symbol:m.symbol,address:m.address}})}async function at({callData:a,transaction:e,publicClient:r,exchangeProxyAbi:s,transactionReceipt:o}){const{value:n}=e;const{args:t}=T({abi:s,data:a});const c=await p({publicClient:r,transactionReceipt:o});const[i]=t;const d=1000000000000000000n;const l=n/d;const u=n%d;const b=Number(l)+Number(u)/Number(d);const A=c.reduce((g,h)=>{return h.address===i?g+Number(h.amount):g},0);const x=c.find(g=>g.address===C.weth);const E=c.find(g=>g.address===i);if(x&&E){return{tokenIn:{symbol:m.symbol,amount:b.toString(),address:m.address},tokenOut:{symbol:E.symbol,amount:A.toString(),address:E.address}}}}async function it({callData:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const o=await p({publicClient:e,transactionReceipt:s});const{args:n}=T({abi:r,data:a});const[t,c]=n;const i={[t]:{amount:"0",symbol:"",address:""},[c]:{amount:"0",symbol:"",address:""}};o.forEach(({address:d,symbol:l,amount:u})=>{if(y(d)in i){i[y(d)].address=y(d);i[y(d)].symbol=l;i[y(d)].amount=(Number(i[y(d)].amount)+Number(u)).toString()}});return{tokenIn:i[t],tokenOut:i[c]}}async function ct({publicClient:a,transactionReceipt:e}){const r=await p({publicClient:a,transactionReceipt:e});const s=y(S[56]);const o=y(e.from);let n=r.find(c=>c.from===o);let t=r.find(c=>c.from!==o);if(n===void 0){n=r.find(c=>c.from===s);t=r.find(c=>c.from!==s)}if(n&&t){return f(n,t)}}async function dt({callData:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const{args:o}=T({abi:r,data:a});const n=await p({publicClient:e,transactionReceipt:s});const[t]=o;const{makerToken:c,takerToken:i}=t;const d=n.find(u=>u.address===i);const l=n.find(u=>u.address===c);if(d&&l){l.symbol=m.symbol;l.address=m.address;return f(d,l)}}async function lt({callData:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const{args:o}=T({abi:r,data:a});const n=await p({publicClient:e,transactionReceipt:s});const[t]=o;const{makerToken:c,takerToken:i}=t;const d=n.find(u=>u.address===i);const l=n.find(u=>u.address===c);if(d&&l){d.symbol=m.symbol;d.address=m.address;return f(d,l)}}async function ut({callData:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const{args:o}=T({abi:r,data:a});const[n]=o;const{maker:t,taker:c}=n;const i=await p({publicClient:e,transactionReceipt:s});if(typeof t==="string"&&typeof c==="string"){const d=i.find(u=>u.from===c);const l=i.find(u=>u.from===t);if(d&&l){return f(d,l)}}}async function mt({callData:a,publicClient:e,exchangeProxyAbi:r,transactionReceipt:s}){const{args:o}=T({abi:r,data:a});const[n]=o;const{signer:t}=n;const c=await p({publicClient:e,transactionReceipt:s});if(typeof t==="string"){const i=c.find(l=>l.from===t);const d=c.find(l=>l.to===t);if(i&&d){return f(i,d)}}}async function pt({chainId:a,transaction:e,publicClient:r,exchangeProxyAbi:s,transactionReceipt:o,callData:n}){const{args:t}=T({abi:s,data:n});const[c]=t;const{callData:i}=c;const{functionName:d}=T({data:i,abi:s});const l=F[d];return l({chainId:a,callData:i,transaction:e,publicClient:r,exchangeProxyAbi:s,transactionReceipt:o})}function ft({chainId:a,transaction:e,publicClient:r,exchangeProxyAbi:s,transactionReceipt:o}){const{args:n}=T({abi:L,data:e.input});let t;const c=$({abi:L,functionName:"permitAndCall",args:[...n]});const i=c.slice(0,10);const{IERC2612:d,IERC20PermitAllowed:l,IERC20MetaTransaction:u}=_.permitAndCall;switch(i){case d:t=n[7];break;case l:t=n[8];break;case u:t=n[6];break}if(t){const{functionName:b}=T({abi:s,data:t});const A=F[b];return A({chainId:a,callData:t,transaction:e,publicClient:r,exchangeProxyAbi:s,transactionReceipt:o})}}var F={fillLimitOrder:ut,fillOtcOrder:J,fillOtcOrderForEth:dt,fillOtcOrderWithEth:lt,fillTakerSignedOtcOrder:z,fillTakerSignedOtcOrderForEth:K,executeMetaTransaction:mt,executeMetaTransactionV2:pt,multiplexBatchSellTokenForToken:it,multiplexBatchSellTokenForEth:rt,multiplexBatchSellEthForToken:at,multiplexMultiHopSellTokenForToken:st,multiplexMultiHopSellEthForToken:Z,multiplexMultiHopSellTokenForEth:q,permitAndCall:ft,sellToUniswap:et,sellTokenForEthToUniswapV3:Q,sellEthForTokenToUniswapV3:nt,sellTokenForTokenToUniswapV3:tt,sellToLiquidityProvider:j,sellToPancakeSwap:ct,transformERC20:ot};async function Vt({rpcUrl:a,exchangeProxyAbi:e,transactionHash:r}){if(!a)throw new Error("Missing rpcUrl\u2026");if(!r)throw new Error("Missing transaction hash\u2026");if(!e){throw new Error(`Missing 0x Exchange Proxy ABI: ${M}\u2026`)}const s=gt({chain:Tt,transport:yt(a)});const{getChainId:o,getTransaction:n,getTransactionReceipt:t}=s;const[c,i,d]=await Promise.all([o(),n({hash:r}),t({hash:r})]);if(d.status==="reverted"){return null}if(!H(c)){throw new Error(`chainId ${c} is unsupported\u2026`)}const l=S[c];const u=V(c)?B[c]:null;const b=bt(i.to);const A=b===l;const x=b===u;if(!A&&!x){return null}const{functionName:E}=A?Y({abi:e,data:i.input}):Y({abi:L,data:i.input});const g=F[E];return g({chainId:c,transaction:i,publicClient:s,exchangeProxyAbi:e,transactionReceipt:d,callData:i.input})}export{Vt as parseSwap}; |
import { permitAndCallAbi } from "./abi/PermitAndCall"; | ||
import { exchangeProxyAbi } from "./abi/ExchangeProxyAbi"; | ||
import type { Hex, Chain, Address, Transport, Transaction, PublicClient, TransactionReceipt } from "viem"; | ||
import type { Hex, Hash, Chain, Address, Transport, Transaction, PublicClient, TransactionReceipt } from "viem"; | ||
export type PermitAndCallChainIds = 1 | 137 | 8453; | ||
@@ -13,12 +13,2 @@ export type SupportedChainId = 1 | 5 | 10 | 56 | 137 | 250 | 8453 | 42220 | 43114 | 42161; | ||
} | ||
export type PermitAndCallArgs = [ | ||
Address, | ||
Address, | ||
bigint, | ||
bigint, | ||
number, | ||
Hex, | ||
Hex, | ||
Hex | ||
]; | ||
export interface EnrichedLog { | ||
@@ -38,3 +28,3 @@ to: Address; | ||
rpcUrl: string; | ||
transactionHash: Hex; | ||
transactionHash: Hash; | ||
exchangeProxyAbi: typeof exchangeProxyAbi; | ||
@@ -59,3 +49,3 @@ } | ||
}; | ||
export type ParserFunction = (args: ParserArgs) => TokenTransaction | Promise<TokenTransaction | undefined>; | ||
type ParserFunction = (args: ParserArgs) => TokenTransaction | Promise<TokenTransaction | undefined>; | ||
export interface Parsers { | ||
@@ -62,0 +52,0 @@ [key: string]: ParserFunction; |
{ | ||
"name": "@0x/0x-parser", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"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": [ |
@@ -76,3 +76,3 @@ # 0x-parser | ||
This repository contains example code that you can run locally, which is useful for the development process. The code can be found in `/examples/web/index.html` and you can run the code by runnng `npm run web:example`. | ||
This repository contains example code that you can run locally, which is useful for the development process. The code can be found in `/examples/web/index.html` and you can run the code by running `npm run web:example`. | ||
@@ -79,0 +79,0 @@ ## Contributing |
@@ -16,6 +16,9 @@ export const EVENT_SIGNATURES = { | ||
export const ERC20_FUNCTION_HASHES = { | ||
symbol: "0x95d89b41", | ||
decimals: "0x313ce567", | ||
} as const; | ||
export const FUNCTION_SELECTORS = { | ||
permitAndCall: { | ||
IERC2612: "0x34b4d153", // Canonical permit | ||
IERC20PermitAllowed: "0x7f9cb4d7", // Dai permit | ||
IERC20MetaTransaction: "0x9d50b5e4", // Meta Transaction permit | ||
} | ||
} as const | ||
@@ -25,3 +28,3 @@ export const EXCHANGE_PROXY_ABI_URL = | ||
const CONONICAL_EXCHANGE_PROXY = "0xDef1C0ded9bec7F1a1670819833240f027b25EfF"; | ||
const CANONICAL_EXCHANGE_PROXY = "0xDef1C0ded9bec7F1a1670819833240f027b25EfF"; | ||
@@ -37,12 +40,12 @@ export const MULTICALL3 = "0xcA11bde05977b3631167028862bE2a173976CA11"; | ||
export const EXCHANGE_PROXY_BY_CHAIN_ID = { | ||
1: CONONICAL_EXCHANGE_PROXY, | ||
1: CANONICAL_EXCHANGE_PROXY, | ||
5: "0xF91bB752490473B8342a3E964E855b9f9a2A668e", | ||
10: "0xDEF1ABE32c034e558Cdd535791643C58a13aCC10", | ||
56: CONONICAL_EXCHANGE_PROXY, | ||
137: CONONICAL_EXCHANGE_PROXY, | ||
56: CANONICAL_EXCHANGE_PROXY, | ||
137: CANONICAL_EXCHANGE_PROXY, | ||
250: "0xDEF189DeAEF76E379df891899eb5A00a94cBC250", | ||
8453: CONONICAL_EXCHANGE_PROXY, | ||
42161: CONONICAL_EXCHANGE_PROXY, | ||
42220: CONONICAL_EXCHANGE_PROXY, | ||
43114: CONONICAL_EXCHANGE_PROXY, | ||
8453: CANONICAL_EXCHANGE_PROXY, | ||
42161: CANONICAL_EXCHANGE_PROXY, | ||
42220: CANONICAL_EXCHANGE_PROXY, | ||
43114: CANONICAL_EXCHANGE_PROXY, | ||
} as const; | ||
@@ -49,0 +52,0 @@ |
@@ -6,2 +6,3 @@ import { | ||
decodeFunctionData, | ||
encodeFunctionData, | ||
} from "viem"; | ||
@@ -12,2 +13,3 @@ import { | ||
EVENT_SIGNATURES, | ||
FUNCTION_SELECTORS, | ||
NATIVE_SYMBOL_BY_CHAIN_ID, | ||
@@ -277,3 +279,4 @@ EXCHANGE_PROXY_BY_CHAIN_ID, | ||
// 0x-parser expects decimals to be present. If you find a transaction where this is not the case, please open an issue. | ||
// 0x-parser expects decimals to be present. If you find a transaction | ||
// where this is not the case, please open a GitHub issue. | ||
const inputAmount = formatUnits(inputTokenAmount, inputDecimal!); | ||
@@ -588,23 +591,43 @@ const outputAmount = formatUnits(outputTokenAmount, outputDecimal!); | ||
}); | ||
let { 7: callData } = args; | ||
let callData: Hex | undefined; | ||
if (!callData) { | ||
const { 6: otherCallData } = args; | ||
callData = otherCallData; | ||
const encodedHexData = encodeFunctionData({ | ||
abi: permitAndCallAbi as any, | ||
functionName: "permitAndCall", | ||
args: [...args], | ||
}); | ||
const functionSelector = encodedHexData.slice(0, 10); | ||
const { IERC2612, IERC20PermitAllowed, IERC20MetaTransaction } = | ||
FUNCTION_SELECTORS.permitAndCall; | ||
switch (functionSelector) { | ||
case IERC2612: | ||
callData = args[7]; | ||
break; | ||
case IERC20PermitAllowed: | ||
callData = args[8]; | ||
break; | ||
case IERC20MetaTransaction: | ||
callData = args[6]; | ||
break; | ||
} | ||
const { functionName: exchangeProxyFn } = decodeFunctionData({ | ||
abi: exchangeProxyAbi, | ||
data: callData, | ||
}); | ||
const parser = parsers[exchangeProxyFn]; | ||
if (callData) { | ||
const { functionName: exchangeProxyFn } = decodeFunctionData({ | ||
abi: exchangeProxyAbi, | ||
data: callData, | ||
}); | ||
const parser = parsers[exchangeProxyFn]; | ||
return parser({ | ||
chainId, | ||
callData, | ||
transaction, | ||
publicClient, | ||
exchangeProxyAbi, | ||
transactionReceipt, | ||
}); | ||
return parser({ | ||
chainId, | ||
callData, | ||
transaction, | ||
publicClient, | ||
exchangeProxyAbi, | ||
transactionReceipt, | ||
}); | ||
} | ||
} | ||
@@ -611,0 +634,0 @@ |
@@ -10,2 +10,4 @@ import { describe, expect, it } from "vitest"; | ||
const POLYGON_MAINNET_RPC = "https://rpc.ankr.com/polygon"; | ||
if (!ETH_MAINNET_RPC) { | ||
@@ -15,5 +17,5 @@ throw new Error("Missing environment variable `ETH_MAINNET_RPC`"); | ||
describe("permitAndCall on Ethereum", () => { | ||
// https://etherscan.io/tx/0x5eac379185f24ddeba7fcd4414779df77ecfd1102da6ebf6dacf25b01a14b241 | ||
it("parses swap", async () => { | ||
describe("permitAndCall", () => { | ||
// https://etherscan.io/tx/0x142909f33e8b9882c73da6dc85193a81cd2bfa3bd39d485dd901d3b70e985ee4 | ||
it("IERC2612: parses swap with permit and call on Ethereum with canonical permit", async () => { | ||
const data = await parseSwap({ | ||
@@ -23,3 +25,3 @@ exchangeProxyAbi, | ||
transactionHash: | ||
"0x5eac379185f24ddeba7fcd4414779df77ecfd1102da6ebf6dacf25b01a14b241", | ||
"0x142909f33e8b9882c73da6dc85193a81cd2bfa3bd39d485dd901d3b70e985ee4", | ||
}); | ||
@@ -29,32 +31,9 @@ | ||
tokenIn: { | ||
symbol: "USDC", | ||
amount: "20", | ||
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", | ||
symbol: "BAL", | ||
amount: "50.883400023914484682", | ||
address: "0xba100000625a3754423978a60c9317c58a424e3D", | ||
}, | ||
tokenOut: { | ||
symbol: "USDT", | ||
amount: "8.735176", | ||
address: "0xdAC17F958D2ee523a2206206994597C13D831ec7", | ||
}, | ||
}); | ||
}); | ||
// https://etherscan.io/tx/0x96f00ee10fb5bc7a71865d0efef87e1105946b5a7a87d44ccc8a60fa852ba467 | ||
it("parses swap with a meta transaction that wraps multiplexBatchSellTokenForEth using permitAndCall with 7 arguments", async () => { | ||
const data = await parseSwap({ | ||
exchangeProxyAbi, | ||
rpcUrl: ETH_MAINNET_RPC, | ||
transactionHash: | ||
"0x96f00ee10fb5bc7a71865d0efef87e1105946b5a7a87d44ccc8a60fa852ba467", | ||
}); | ||
expect(data).toEqual({ | ||
tokenIn: { | ||
symbol: "cbETH", | ||
amount: "0.15941858187679941", | ||
address: "0xBe9895146f7AF43049ca1c1AE358B0541Ea49704", | ||
}, | ||
tokenOut: { | ||
symbol: NATIVE_ASSET.symbol, | ||
amount: "0.1540473741459534", | ||
amount: "0.144785082557763087", | ||
address: NATIVE_ASSET.address, | ||
@@ -65,9 +44,9 @@ }, | ||
// https://etherscan.io/tx/0x142909f33e8b9882c73da6dc85193a81cd2bfa3bd39d485dd901d3b70e985ee4 | ||
it("parses swap with a meta transaction that wraps transformERC20 using permitAndCall with 7 arguments", async () => { | ||
// https://polygonscan.com/tx/0xc020f1a4265c44474544c2b38bf086ab5f463c8bde4f5aa394db3d1429ad42b8 | ||
it("IERC2612: parses swap with permit and call on Polygon with canonical permit", async () => { | ||
const data = await parseSwap({ | ||
exchangeProxyAbi, | ||
rpcUrl: ETH_MAINNET_RPC, | ||
rpcUrl: POLYGON_MAINNET_RPC, | ||
transactionHash: | ||
"0x142909f33e8b9882c73da6dc85193a81cd2bfa3bd39d485dd901d3b70e985ee4", | ||
"0xc020f1a4265c44474544c2b38bf086ab5f463c8bde4f5aa394db3d1429ad42b8", | ||
}); | ||
@@ -77,10 +56,10 @@ | ||
tokenIn: { | ||
symbol: "BAL", | ||
amount: "50.883400023914484682", | ||
address: "0xba100000625a3754423978a60c9317c58a424e3D", | ||
address: "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174", | ||
amount: "3.598205", | ||
symbol: "USDC", | ||
}, | ||
tokenOut: { | ||
symbol: NATIVE_ASSET.symbol, | ||
amount: "0.144785082557763087", | ||
address: NATIVE_ASSET.address, | ||
address: "0x2F6F07CDcf3588944Bf4C42aC74ff24bF56e7590", | ||
amount: "6.876078336435440124", | ||
symbol: "STG", | ||
}, | ||
@@ -90,4 +69,4 @@ }); | ||
// https://etherscan.io/tx/0x911354147775d4460259809bf7606b6a0f06c70a788856bd7f9371411abf3a5e | ||
it("parses swap with a meta transaction that wraps multiplexBatchSellTokenForToken using permitAndCall with 7 arguments", async () => { | ||
// https://etherscan.io/tx/0x2e48f20033a45c7a7e1f926db51b1876c5a6a2042bf8e06b17ddbdd58f3b67ab | ||
it("IERC20PermitAllowed: parses swap with permit and call on Ethereum with dai permit ", async () => { | ||
const data = await parseSwap({ | ||
@@ -97,3 +76,3 @@ exchangeProxyAbi, | ||
transactionHash: | ||
"0x911354147775d4460259809bf7606b6a0f06c70a788856bd7f9371411abf3a5e", | ||
"0x2e48f20033a45c7a7e1f926db51b1876c5a6a2042bf8e06b17ddbdd58f3b67ab", | ||
}); | ||
@@ -103,20 +82,16 @@ | ||
tokenIn: { | ||
symbol: "USDC", | ||
amount: "7967.727535", | ||
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", | ||
symbol: "DAI", | ||
amount: "2502.47", | ||
address: "0x6B175474E89094C44Da98b954EedeAC495271d0F", | ||
}, | ||
tokenOut: { | ||
symbol: "HEX", | ||
amount: "545031.76796769", | ||
address: "0x2b591e99afE9f32eAA6214f7B7629768c40Eeb39", | ||
symbol: "USDT", | ||
amount: "2494.136763", | ||
address: "0xdAC17F958D2ee523a2206206994597C13D831ec7", | ||
}, | ||
}); | ||
}); | ||
}); | ||
describe("permitAndCall on Polygon", () => { | ||
const POLYGON_MAINNET_RPC = "https://rpc.ankr.com/polygon"; | ||
// https://polygonscan.com/tx/0x407f0ffe42963d2f68beeac39c71d6db1530a90c9716ed4643de116abefcc642 | ||
it("parses swap with a executeMetaTransactionV2 that wraps transformERC20 using permitAndCall with 6 arguments", async () => { | ||
it("IERC20MetaTransaction: parses swap with permit and call on Polygon with meta transaction", async () => { | ||
const data = await parseSwap({ | ||
@@ -142,25 +117,2 @@ exchangeProxyAbi, | ||
}); | ||
// https://polygonscan.com/tx/0xc020f1a4265c44474544c2b38bf086ab5f463c8bde4f5aa394db3d1429ad42b8 | ||
it("parses swap with a executeMetaTransactionV2 that wraps transformERC20 using permitAndCall with 7 arguments", async () => { | ||
const data = await parseSwap({ | ||
exchangeProxyAbi, | ||
rpcUrl: POLYGON_MAINNET_RPC, | ||
transactionHash: | ||
"0xc020f1a4265c44474544c2b38bf086ab5f463c8bde4f5aa394db3d1429ad42b8", | ||
}); | ||
expect(data).toEqual({ | ||
tokenIn: { | ||
address: "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174", | ||
amount: "3.598205", | ||
symbol: "USDC", | ||
}, | ||
tokenOut: { | ||
address: "0x2F6F07CDcf3588944Bf4C42aC74ff24bF56e7590", | ||
amount: "6.876078336435440124", | ||
symbol: "STG", | ||
}, | ||
}); | ||
}); | ||
}); |
@@ -5,2 +5,3 @@ import { permitAndCallAbi } from "./abi/PermitAndCall"; | ||
Hex, | ||
Hash, | ||
Chain, | ||
@@ -36,13 +37,2 @@ Address, | ||
export type PermitAndCallArgs = [ | ||
Address, | ||
Address, | ||
bigint, | ||
bigint, | ||
number, | ||
Hex, | ||
Hex, | ||
Hex | ||
]; | ||
export interface EnrichedLog { | ||
@@ -64,3 +54,3 @@ to: Address; | ||
rpcUrl: string; | ||
transactionHash: Hex; | ||
transactionHash: Hash; | ||
exchangeProxyAbi: typeof exchangeProxyAbi; | ||
@@ -91,3 +81,3 @@ } | ||
export type ParserFunction = ( | ||
type ParserFunction = ( | ||
args: ParserArgs | ||
@@ -94,0 +84,0 @@ ) => TokenTransaction | Promise<TokenTransaction | undefined>; |
537866
16463