UNPKG

@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.32 kB
"use strict";var N=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var V=Object.prototype.hasOwnProperty;var X=(r,s)=>{for(var l in s)N(r,l,{get:s[l],enumerable:true})};var W=(r,s,l,o)=>{if(s&&typeof s==="object"||typeof s==="function"){for(let u of Y(s))if(!V.call(r,u)&&u!==l)N(r,u,{get:()=>s[u],enumerable:!(o=F(s,u))||o.enumerable})}return r};var $=r=>W(N({},"__esModule",{value:true}),r);var j={};X(j,{parseSwap:()=>K});module.exports=$(j);var f=require("viem");var e=require("viem/chains");var R=[{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 k={EXECUTE_META_TXN:"0xfd3ad6d4"};var D=[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 A={[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 g=`0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE`;var O=`0xcA11bde05977b3631167028862bE2a173976CA11`;var U=`0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789`;var b=require("viem");var n=require("viem/chains");function H(r){const s=[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 s.includes(r)}function _(r,s){const{recipient:l,direction:o="to"}=s;let u=0n;const a=l.toLowerCase();function d(t){if(!t.value)return;const c=o==="from"?t.from:t.to;if(c.toLowerCase()===a){u+=(0,b.fromHex)(t.value,"bigint")}}function C(t){for(const c of t){d(c);if(c.calls&&c.calls.length>0){C(c.calls)}}}C(r.calls);return(0,b.formatEther)(u)}async function P({publicClient:r,transactionReceipt:s}){const l={Transfer:`0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef`};const o=s.logs.filter(t=>t.topics[0]===l.Transfer).map(t=>({...t,address:(0,b.getAddress)(t.address)}));const u=[...o.map(t=>({abi:b.erc20Abi,address:t.address,functionName:"symbol"})),...o.map(t=>({abi:b.erc20Abi,address:t.address,functionName:"decimals"}))];const a=await r.multicall({contracts:u});const d=Math.floor(a.length/2);const C=o.map((t,c)=>{const T=a[c].result;const L=a[d+c].result;const I=t.data==="0x"?"0":(0,b.formatUnits)(BigInt(t.data),L);const w=t.data==="0x"?0n:BigInt(t.data);const{address:p,topics:v}=t;const{1:S,2:E}=v;const i=(0,b.getAddress)(B(S));const m=(0,b.getAddress)(B(E));return{to:m,from:i,symbol:T,amount:I,amountRaw:w,address:p,decimals:L}}).filter(t=>t.amount!=="0");return C}function B(r){return`0x${r.slice(-40)}`}function M({logs:r,trace:s,chainId:l,smartContractWallet:o}){const u=r.reduce((c,T)=>{if(T.to===o)return{...c,output:T};if(T.from===o)return{...c,input:T};return c},{});let{input:a,output:d}=u;const C=_(s,{recipient:o});const t=_(s,{recipient:o,direction:"from"});if(!d&&C!=="0"){if(a){return{tokenIn:{address:a.address,amount:a.amount,symbol:a.symbol},tokenOut:{address:g,amount:C,symbol:A[l]}}}else{return null}}if(!a&&t!=="0"){const c=l===56?"WBNB":l===137?"WPOL":"WETH";const T=r.find(L=>L.symbol===c);if(T&&d){return{tokenIn:{address:g,amount:T.amount,symbol:A[l]},tokenOut:{address:d.address,amount:d.amount,symbol:d.symbol}}}else{return null}}if(a&&d){return{tokenIn:{address:a.address,amount:a.amount,symbol:a.symbol},tokenOut:{address:d.address,amount:d.amount,symbol:d.symbol}}}return null}async function K({publicClient:r,transactionHash:s,smartContractWallet:l}){const o=r.chain.id;if(!H(o)){throw new Error(`chainId ${o} is unsupported\u2026`)}const u=r.extend(i=>({async traceCall(m){return i.request({method:"debug_traceTransaction",params:[m.hash,{tracer:"callTracer"}]})}}));const[a,d,C]=await Promise.all([u.traceCall({hash:s}),r.getTransaction({hash:s}),r.getTransactionReceipt({hash:s})]);const{from:t,value:c,to:T}=d;const L=T===U.toLowerCase();const I=_(a,{recipient:t});if(C.status==="reverted"){const i=D.find(h=>h.id===o);const m=`Unable to parse. Transaction ${s} on ${i?.name} has reverted.`;console.warn(m);return null}const w=c>0n;const p=await P({publicClient:r,transactionReceipt:C});if(L){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:o,smartContractWallet:l})}const v=p.filter(i=>i.from.toLowerCase()===t.toLowerCase());let S=v.length?v.reduce((i,m)=>({...i,amount:(0,f.formatUnits)(i.amountRaw+m.amountRaw,m.decimals),amountRaw:i.amountRaw+m.amountRaw})):p[0];let E=I==="0"?p.find(i=>{return i.to.toLowerCase()===t.toLowerCase()}):{symbol:A[o],amount:I,address:g};if(T?.toLowerCase()===O.toLowerCase()){const{args:i}=(0,f.decodeFunctionData)({abi:f.multicall3Abi,data:d.input});const{args:m}=(0,f.decodeFunctionData)({abi:R,data:i[0][1].callData});const h=m[0].recipient.toLowerCase();const y=_(a,{recipient:h});if(y==="0"){E=E=p[p.length-1]}else{E={symbol:A[o],amount:y,address:g}}}if(d.input.startsWith(k.EXECUTE_META_TXN)){const{args:i}=(0,f.decodeFunctionData)({abi:R,data:d.input});const{3:m}=i;const h=_(a,{recipient:m});if(h==="0"){E=p[p.length-1];const y=p.filter(x=>x.to.toLowerCase()===m.toLowerCase());if(y.length===1){E={symbol:y[0].symbol,amount:y[0].amount,address:y[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(x=>x.from.toLowerCase()===m.toLowerCase())[0]}else{E={symbol:A[o],amount:h,address:g}}}if(w){const i=(0,f.formatEther)(c);const m=p.filter(h=>h.to.toLowerCase()===t).reduce((h,y)=>({symbol:y.symbol,amount:(0,f.formatUnits)(BigInt(h.amount)+(0,f.parseUnits)(y.amount,y.decimals),y.decimals),address:y.address}),{symbol:"",amount:"",address:""});return{tokenIn:{symbol:A[o],address:g,amount:i},tokenOut:m}}if(!E){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:E.symbol,amount:E.amount,address:E.address}}}0&&(module.exports={parseSwap});