@0x/0x-parser
Version:
🧾 Designed for 0x integrators: This library parses 0x transactions on EVM blockchains into a format that is both user-friendly and easy to understand.
2 lines (1 loc) • 7.42 kB
JavaScript
;var R=Object.defineProperty;var Y=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var X=Object.prototype.hasOwnProperty;var W=(r,o)=>{for(var l in o)R(r,l,{get:o[l],enumerable:true})};var $=(r,o,l,s)=>{if(o&&typeof o==="object"||typeof o==="function"){for(let u of V(o))if(!X.call(r,u)&&u!==l)R(r,u,{get:()=>o[u],enumerable:!(s=Y(o,u))||s.enumerable})}return r};var K=r=>$(R({},"__esModule",{value:true}),r);var G={};W(G,{parseSwap:()=>j});module.exports=K(G);var f=require("viem");var e=require("viem/chains");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 D={EXECUTE_META_TXN:"0xfd3ad6d4"};var O=[e.bsc,e.base,e.mode,e.blast,e.linea,e.scroll,e.mantle,e.mainnet,e.polygon,e.arbitrum,e.unichain,e.optimism,e.avalanche,e.berachain,e.worldchain];var g={[e.bsc.id]:e.bsc.nativeCurrency.symbol,[e.base.id]:e.base.nativeCurrency.symbol,[e.mode.id]:e.mode.nativeCurrency.symbol,[e.blast.id]:e.blast.nativeCurrency.symbol,[e.linea.id]:e.linea.nativeCurrency.symbol,[e.scroll.id]:e.scroll.nativeCurrency.symbol,[e.mantle.id]:e.mantle.nativeCurrency.symbol,[e.mainnet.id]:e.mainnet.nativeCurrency.symbol,[e.polygon.id]:e.polygon.nativeCurrency.symbol,[e.unichain.id]:e.unichain.nativeCurrency.symbol,[e.optimism.id]:e.optimism.nativeCurrency.symbol,[e.arbitrum.id]:e.arbitrum.nativeCurrency.symbol,[e.avalanche.id]:e.avalanche.nativeCurrency.symbol,[e.berachain.id]:e.berachain.nativeCurrency.symbol,[e.worldchain.id]:e.worldchain.nativeCurrency.symbol};var L=`0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE`;var U=`0xcA11bde05977b3631167028862bE2a173976CA11`;var B=`0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789`;var y=require("viem");var n=require("viem/chains");function P(r){const o=[n.bsc.id,n.base.id,n.mode.id,n.blast.id,n.linea.id,n.scroll.id,n.mantle.id,n.polygon.id,n.mainnet.id,n.arbitrum.id,n.optimism.id,n.unichain.id,n.avalanche.id,n.berachain.id,n.worldchain.id];return o.includes(r)}function w(r,o){const{recipient:l,direction:s="to"}=o;let u=0n;const a=l.toLowerCase();function c(t){if(!t.value)return;const m=s==="from"?t.from:t.to;if(m.toLowerCase()===a){u+=(0,y.fromHex)(t.value,"bigint")}}function E(t){for(const m of t){c(m);if(m.calls&&m.calls.length>0){E(m.calls)}}}E(r.calls);return(0,y.formatEther)(u)}async function F({publicClient:r,transactionReceipt:o}){const l={Transfer:`0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef`};const s=o.logs.filter(t=>t.topics[0]===l.Transfer).map(t=>({...t,address:(0,y.getAddress)(t.address)}));const u=[...s.map(t=>({abi:y.erc20Abi,address:t.address,functionName:"symbol"})),...s.map(t=>({abi:y.erc20Abi,address:t.address,functionName:"decimals"}))];const a=await r.multicall({contracts:u});const c=Math.floor(a.length/2);const E=s.map((t,m)=>{const b=a[m].result;const _=a[c+m].result;const v=t.data==="0x"?"0":(0,y.formatUnits)(BigInt(t.data),_);const N=t.data==="0x"?0n:BigInt(t.data);const{address:p,topics:x}=t;const{1:S,2:T}=x;const i=(0,y.getAddress)(H(S));const d=(0,y.getAddress)(H(T));return{to:d,from:i,symbol:b,amount:v,amountRaw:N,address:p,decimals:_}}).filter(t=>t.amount!=="0");return E}function H(r){return`0x${r.slice(-40)}`}function M({logs:r,trace:o,chainId:l,smartContractWallet:s}){const u=r.reduce((m,b)=>{if(b.to===s)return{...m,output:b};if(b.from===s)return{...m,input:b};return m},{});let{input:a,output:c}=u;const E=w(o,{recipient:s});const t=w(o,{recipient:s,direction:"from"});if(!c&&E!=="0"){if(a){return{tokenIn:{address:a.address,amount:a.amount,symbol:a.symbol},tokenOut:{address:L,amount:E,symbol:g[l]}}}else{return null}}if(!a&&t!=="0"){const m=l===56?"WBNB":l===137?"WPOL":"WETH";const b=r.find(_=>_.symbol===m);if(b&&c){return{tokenIn:{address:L,amount:b.amount,symbol:g[l]},tokenOut:{address:c.address,amount:c.amount,symbol:c.symbol}}}else{return null}}if(a&&c){return{tokenIn:{address:a.address,amount:a.amount,symbol:a.symbol},tokenOut:{address:c.address,amount:c.amount,symbol:c.symbol}}}return null}async function j({publicClient:r,transactionHash:o,smartContractWallet:l}){const s=r.chain.id;if(!P(s)){throw new Error(`chainId ${s} is unsupported\u2026`)}const u=r.extend(i=>({async traceCall(d){return i.request({method:"debug_traceTransaction",params:[d.hash,{tracer:"callTracer"}]})}}));const[a,c,E]=await Promise.all([u.traceCall({hash:o}),r.getTransaction({hash:o}),r.getTransactionReceipt({hash:o})]);const{from:t,value:m,to:b}=c;const _=b===B.toLowerCase();const v=w(a,{recipient:t});if(E.status==="reverted"){const i=O.find(C=>C.id===s);const d=`Unable to parse. Transaction ${o} on ${i?.name} has reverted.`;console.warn(d);return null}const N=m>0n;const p=await F({publicClient:r,transactionReceipt:E});if(_){if(!l){throw new Error("This is an ERC-4337 transaction. You must provide a smart contract wallet address to 0x-parser.")}return M({logs:p,trace:a,chainId:s,smartContractWallet:l})}const x=p.filter(i=>i.from.toLowerCase()===t.toLowerCase());let S=x.length?x.reduce((i,d)=>({...i,amount:(0,f.formatUnits)(i.amountRaw+d.amountRaw,d.decimals),amountRaw:i.amountRaw+d.amountRaw})):p[0];let T=v==="0"?p.find(i=>{return i.to.toLowerCase()===t.toLowerCase()}):{symbol:g[s],amount:v,address:L};if(b?.toLowerCase()===U.toLowerCase()){const{args:i}=(0,f.decodeFunctionData)({abi:f.multicall3Abi,data:c.input});const{args:d}=(0,f.decodeFunctionData)({abi:k,data:i[0][1].callData});const C=d[0].recipient.toLowerCase();const h=d[3];const A=w(a,{recipient:C});if(A==="0"){[T]=p.filter(I=>I.to.toLowerCase()===h.toLowerCase())}else{T={symbol:g[s],amount:A,address:L}}}if(c.input.startsWith(D.EXECUTE_META_TXN)){const{args:i}=(0,f.decodeFunctionData)({abi:k,data:c.input});const{3:d}=i;const C=p.filter(A=>A.from===d);const h=w(a,{recipient:d});if(C.length){S=C[0]}if(h==="0"){T=p[p.length-1];const A=p.filter(I=>I.to.toLowerCase()===d.toLowerCase());if(A.length===1){T={symbol:A[0].symbol,amount:A[0].amount,address:A[0].address}}else{console.error("File a bug report here, including the expected results (URL to a block explorer) and the unexpected results: https://github.com/0xProject/0x-parser/issues/new/choose.")}S=p.filter(I=>I.from.toLowerCase()===d.toLowerCase())[0]}else{T={symbol:g[s],amount:h,address:L}}}if(N){const i=(0,f.formatEther)(m);const d=p.filter(C=>C.to.toLowerCase()===t).reduce((C,h)=>({symbol:h.symbol,amount:(0,f.formatUnits)(BigInt(C.amount)+(0,f.parseUnits)(h.amount,h.decimals),h.decimals),address:h.address}),{symbol:"",amount:"",address:""});return{tokenIn:{symbol:g[s],address:L,amount:i},tokenOut:d}}if(!T){console.error("File a bug report here, including the expected results (URL to a block explorer) and the unexpected results: https://github.com/0xProject/0x-parser/issues/new/choose.");return null}return{tokenIn:{symbol:S.symbol,amount:S.amount,address:S.address},tokenOut:{symbol:T.symbol,amount:T.amount,address:T.address}}}0&&(module.exports={parseSwap});