@cowprotocol/permit-utils 3.7.0 → 3.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +1 -1
- package/index.js +274 -262
- package/package.json +3 -3
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("viem"),q=require("viem/accounts"),T=require("wagmi/actions"),V=require("@cowprotocol/hook-dapp-lib"),B=require("@cowprotocol/cow-sdk"),K="0xa50dc0f7fc051309434deb3b1c71e927dbb711759231d8ecbf630c85d94a42fe",k=q.privateKeyToAccount(K),$=55000n,C=80000n,O=f.maxUint256,W=157788e6;async function z(a,n,e,r,t,i,{version:s,type:o,name:u}){const c=u||t;if(o==="unsupported")return!1;if(!c)throw new Error(`No token name for ${r}`);const l={chainId:n,tokenName:c,tokenAddress:r,callData:i,version:s};let m;if(i.startsWith(w)?m=e.recoverPermitOwnerFromCallData({...l,callData:i.replace(w,"0x")}):i.startsWith(E)&&(m=e.recoverDaiLikePermitOwnerFromCallData({...l,callData:i.replace(E,"0x")})),!!m)try{return(await m).toLowerCase()===a.toLowerCase()}catch(y){console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${a} with callData ${i}`,y);return}}async function x({eip2612Utils:a,callDataParams:n}){return(await a.buildDaiLikePermitCallData(...n)).replace("0x",E)}async function H({eip2612Utils:a,callDataParams:n}){const[e,r,t,...i]=n;return(await a.buildPermitCallData(e,r,t,...i)).replace("0x",w)}function _(){return Math.ceil((Date.now()+W)/1e3)}function F(a){return!!a&&a.type!=="unsupported"}const v={},J=[4001,-32e3],Q=["user denied","user rejected","rejected transaction","transaction was rejected"];async function Y(a){const n=ee(a),e=v[n];if(e)return await e;const r=Z(a).catch(t=>{if(te(t))throw t;console.debug("[generatePermitHook] cached request failed",t)}).finally(()=>{delete v[n]});return v[n]=r,r}async function X({data:a,from:n,to:e,config:r,isUserAccount:t}){try{const i=await T.estimateGas(r,{account:n,to:e,data:a}),s=t?i:i+i/10n;return s>C?s:C}catch(i){return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default",i),C}}async function Z(a){const{inputToken:n,spender:e,chainId:r,permitInfo:t,config:i,account:s,eip2612Utils:o,nonce:u}=a,c=n.address,l=t.name||n.name;if(!F(t))throw new Error(`Trying to generate permit hook for unsupported token: ${c}`);if(!l)throw new Error(`No token name for token: ${c}`);const m=s||k.address,y=u===void 0?await o.getTokenNonce(c,m):u,P=_(),g=a.amount||O,h=t.type==="eip-2612"?await H({eip2612Utils:o,callDataParams:[{owner:m,spender:e,value:g.toString(),nonce:y,deadline:P},r,l,c,t.version]}):await x({eip2612Utils:o,callDataParams:[{holder:m,spender:e,allowed:!0,value:g.toString(),nonce:y,expiry:P},r,l,c,t.version]}),p=await X({data:h,from:m,to:c,config:i,isUserAccount:!!s});return{target:c,callData:h,gasLimit:p.toString(),dappId:V.PERMIT_HOOK_DAPP_ID}}function ee(a){const{inputToken:n,chainId:e,account:r,amount:t}=a;return`${n.address.toLowerCase()}-${e}${r?`-${r.toLowerCase()}`:""}${t?`-${t.toString()}`:""}`}function te(a){if(!a)return!1;if(J.includes(a.code))return!0;const n=(typeof a=="string"?a:a.message)?.toLowerCase()||"";return Q.some(e=>n.includes(e))}class ne{constructor(n,e){this.publicClient=n,this.walletClient=e}contractEncodeABI(n,e,r,t){const i=t.map(s=>s instanceof Uint8Array?f.bytesToHex(s):s);return f.encodeFunctionData({abi:n,functionName:r,args:i})}signTypedData(n,e,r){const t=Object.fromEntries(Object.entries(e.types).filter(([s])=>s!=="EIP712Domain")),i=this.walletClient.account??n;return this.walletClient.signTypedData({account:i,domain:e.domain,types:t,primaryType:e.primaryType,message:e.message})}ethCall(n,e){return this.publicClient.call({to:n,data:e}).then(({data:r})=>r||"0x")}decodeABIParameter(n,e){const[r]=f.decodeAbiParameters([{type:n}],e);return r}decodeABIParameters(n,e){const r=f.decodeAbiParameters(n,e),t={};return n.forEach((i,s)=>{const o=r[s],u=typeof o=="bigint"?f.toHex(o):o;t[s]=u,i.name&&(t[i.name]=u)}),t}}const L=new Map,N=new Map;async function G({chainId:a,publicClient:n,account:e,walletClient:r}){const t=L.get(a);if(!e&&t)return t;const i=`${a}-${e}`,s=N.get(i);if(s)return s;let o;if(!e)o=f.createWalletClient({account:k,chain:n.chain,transport:f.http()});else if(r)o=r;else throw new Error("Permit signing needs an active wallet client. Reconnect your wallet or use an injected extension.");const u=new ne(n,o),c=await Promise.resolve().then(()=>require("./1inchPermitUtils-BDO_CqjL.cjs")).then(m=>m.Eip2612PermitUtils),l=new c(u,{enabledCheckSalt:!0});return e?N.set(i,l):L.set(a,l),l}const ae=[{constant:!0,inputs:[],name:"name",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{inputs:[],name:"eip712Domain",outputs:[{internalType:"bytes1",name:"fields",type:"bytes1"},{internalType:"string",name:"name",type:"string"},{internalType:"string",name:"version",type:"string"},{internalType:"uint256",name:"chainId",type:"uint256"},{internalType:"address",name:"verifyingContract",type:"address"},{internalType:"bytes32",name:"salt",type:"bytes32"},{internalType:"uint256[]",name:"extensions",type:"uint256[]"}],stateMutability:"view",type:"function"}];async function re(a,n,e){const[r,t,i,s,o,u,c]=await T.readContract(e,{abi:ae,address:a,functionName:"eip712Domain"}),l={fields:r,name:t,version:i,chainId:s.toString(),verifyingContract:o,salt:u,extensions:c.map(m=>String(m))};return ie(l,n,a)}async function ie(a,n,e){const{extensions:r,...t}=a;if(r.length>0)throw Error("Extensions not implemented");if(t.chainId&&n!==parseInt(t.chainId,16))throw Error(`ChainId mismatch. Received: '${t.chainId}', expected: '${n}'`);if(t.verifyingContract&&e.toLowerCase()!==t.verifyingContract.toLowerCase())throw Error(`Address mismatch. Received: '${t.verifyingContract}', expected: '${e}'`);return se(t)}const oe=["name","version","chainId","verifyingContract","salt"];function se(a){const n=parseInt(a.fields,16);return oe.reduce((e,r,t)=>(n&1<<t&&(e[r]=a[r]),e),{})}async function ce(a,n,e){return T.readContract(e,{abi:f.erc20Abi,address:a,functionName:"name"})}const le=[[{constant:!0,inputs:[],name:"version",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"}],[{constant:!0,inputs:[],name:"ERC712_VERSION",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"}]];async function j(a,n,e=0){const r=le[e];if(!r)return"1";try{const t=await T.readContract(n,{abi:r,address:a,functionName:r[0].name});if(t==="0x"||Number.isNaN(Number(t)))return"1";if(!t.startsWith("0x"))return t;const[i]=f.decodeAbiParameters([{type:"string"}],t);return i}catch{return j(a,n,e+1)}}const ue={nonce:0,deadline:_()},de={allowed:!0,nonce:0,expiry:_()},M={},b={type:"unsupported"};async function me(a){const{tokenAddress:n,chainId:e}=a,r=B.getTokenId({address:n,chainId:e}),t=M[r];if(t)return t;const i=pe(a);return M[r]=i,i}async function pe(a){const{amount:n,chainId:e,config:r,minGasLimit:t,publicClient:i,spender:s,tokenAddress:o}=a,u=await G({chainId:e,publicClient:i}),c=k.address;let l;try{l=await re(o,e,r)}catch{console.debug(`[checkTokenIsPermittable] Couldn't fetch eip712domain for token ${o}`)}const m=!l?.name,y=l?.version===void 0,[P,g,h]=await Promise.allSettled([m?ce(o,e,r):Promise.resolve(l?.name),u.getTokenNonce(o,c),y?j(o,r):Promise.resolve(l?.version)]);let p=l?.name;if(P.status==="fulfilled")p=P.value;else if(m){const d=P.reason;if(/ETIMEDOUT/.test(d)&&!p)return{error:"Failed to fetch token name from contract. RPC connection error"};console.debug(`[checkTokenIsPermittable] Couldn't fetch token name from the contract for token ${o}, using provided '${p}'`,d)}if(!p)return{error:`Token name could not be determined for ${o}`};let D;if(g.status==="fulfilled")D=g.value;else{const d=g.reason;return d==="nonce not supported"||d.message==="nonce is NaN"?(console.debug(`[checkTokenIsPermittable] Not a permittable token ${o} - ${p}`,d?.message||d),{...b,name:p}):(console.debug(`[checkTokenIsPermittable] Failed to get nonce for ${o} - ${p}`,d),{error:d.message||d.toString()})}let S=l?.version;h.status==="fulfilled"?S=h.value:y&&console.debug(`[checkTokenIsPermittable] Failed to get version for ${o} - ${p}`,h.reason);const R={chainId:e,eip2612PermitUtils:u,nonce:D,spender:s,tokenAddress:o,tokenName:p,walletAddress:c,version:S,minGasLimit:t,amount:n};try{return await U({...R,type:"eip-2612",config:r})}catch(d){try{return await Pe(o,u)?await U({...R,type:"dai-like",config:r}):/invalid signature/.test(d)||d?.code==="UNPREDICTABLE_GAS_LIMIT"?(console.debug(`[checkTokenIsPermittable] Token ${o} - ${p} might be permittable, but it's not supported for now. Reason:`,d?.reason),{...b,name:p}):(console.debug(`[checkTokenIsPermittable] Failed to estimate eip-2612 permit for ${o} - ${p}`,d),{error:d.message||d.toString()})}catch(I){return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${o} - ${p}`,I),{error:I.message||I.toString()}}}}async function U(a){const{config:n,walletAddress:e,tokenAddress:r,tokenName:t,type:i,version:s,minGasLimit:o=$}=a,c=await(i==="eip-2612"?ye:fe)(a);return c?await T.estimateGas(n,{to:r,account:e,data:c})>o?{type:i,version:s,name:t}:{...b,name:t}:{...b,name:t}}async function fe(a){const{eip2612PermitUtils:n,tokenAddress:e,walletAddress:r,spender:t,nonce:i,chainId:s,tokenName:o,version:u}=a;return await n.getPermitTypeHash(e)===A?x({eip2612Utils:n,callDataParams:[{...de,holder:r,spender:t,nonce:i},s,o,e,u]}):!1}async function ye(a){const{eip2612PermitUtils:n,walletAddress:e,spender:r,nonce:t,chainId:i,tokenName:s,tokenAddress:o,version:u,amount:c}=a;return H({eip2612Utils:n,callDataParams:[{...ue,owner:e,spender:r,nonce:t,value:c?c.toString():O.toString()},+i,s,o,u]})}async function Pe(a,n){return await n.getPermitTypeHash(a)===A}const ge=[{constant:!1,inputs:[{internalType:"address",name:"holder",type:"address"},{internalType:"address",name:"spender",type:"address"},{internalType:"uint256",name:"nonce",type:"uint256"},{internalType:"uint256",name:"expiry",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"}],name:"permit",outputs:[],payable:!1,stateMutability:"nonpayable",type:"function"}],he=[{type:"function",name:"permit",stateMutability:"nonpayable",inputs:[{name:"owner",type:"address"},{name:"spender",type:"address"},{name:"value",type:"uint256"},{name:"deadline",type:"uint256"},{name:"v",type:"uint8"},{name:"r",type:"bytes32"},{name:"s",type:"bytes32"}],outputs:[]}],w="0xd505accf",E="0x8fcbaf0c",A="0xea2aa0a1be11a07ed86d755c93467f4f82362b452371d1ba94d1715123511acb",Te=Object.freeze(Object.defineProperty({__proto__:null,DAI_EIP_2612_PERMIT_ABI:ge,DAI_LIKE_PERMIT_TYPEHASH:A,DAI_PERMIT_SELECTOR:E,EIP_2612_PERMIT_ABI:he,EIP_2612_PERMIT_SELECTOR:w},Symbol.toStringTag,{value:"Module"}));exports.DEFAULT_MIN_GAS_LIMIT=$;exports.PERMIT_ACCOUNT=k;exports.checkIsCallDataAValidPermit=z;exports.generatePermitHook=Y;exports.getPermitUtilsInstance=G;exports.getTokenPermitInfo=me;exports.isSupportedPermitInfo=F;exports.oneInchPermitUtilsConsts=Te;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("viem"),B=require("viem/accounts"),b=require("wagmi/actions"),V=require("@cowprotocol/hook-dapp-lib"),K=require("@cowprotocol/cow-sdk"),z="0xa50dc0f7fc051309434deb3b1c71e927dbb711759231d8ecbf630c85d94a42fe",v=B.privateKeyToAccount(z),$=55000n,k=80000n,x=f.maxUint256,W=157788e6;async function J(e,n,t,r,a,o,{version:s,type:i,name:u}){const c=u||a;if(i==="unsupported")return!1;if(!c)throw new Error(`No token name for ${r}`);const l={chainId:n,tokenName:c,tokenAddress:r,callData:o,version:s};let m;if(o.startsWith(I)?m=t.recoverPermitOwnerFromCallData({...l,callData:o.replace(I,"0x")}):o.startsWith(w)&&(m=t.recoverDaiLikePermitOwnerFromCallData({...l,callData:o.replace(w,"0x")})),!!m)try{return(await m).toLowerCase()===e.toLowerCase()}catch(y){console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${e} with callData ${o}`,y);return}}async function O({eip2612Utils:e,callDataParams:n}){return(await e.buildDaiLikePermitCallData(...n)).replace("0x",w)}async function H({eip2612Utils:e,callDataParams:n}){const[t,r,a,...o]=n;return(await e.buildPermitCallData(t,r,a,...o)).replace("0x",I)}function _(){return Math.ceil((Date.now()+W)/1e3)}function F(e){return!!e&&e.type!=="unsupported"}const C={},Q=[4001,-32e3],Y=["user denied","user rejected","rejected transaction","transaction was rejected"];async function X(e){const n=te(e),t=C[n];if(t)return await t;const r=ee(e).catch(a=>{const o=G(a);if(ae(o)||o instanceof f.ExecutionRevertedError)throw o;console.debug("[generatePermitHook] cached request failed",o)}).finally(()=>{delete C[n]});return C[n]=r,r}async function Z({data:e,from:n,to:t,config:r,isUserAccount:a}){try{const o=await b.estimateGas(r,{account:n,to:t,data:e}),s=a?o:o+o/10n;return s>k?s:k}catch(o){const s=G(o),i=a?ne(s):void 0;if(i)throw i;return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default",s),k}}async function ee(e){const{inputToken:n,spender:t,chainId:r,permitInfo:a,config:o,account:s,eip2612Utils:i,nonce:u}=e,c=n.address,l=a.name||n.name;if(!F(a))throw new Error(`Trying to generate permit hook for unsupported token: ${c}`);if(!l)throw new Error(`No token name for token: ${c}`);const m=s||v.address,y=u===void 0?await i.getTokenNonce(c,m):u,g=_(),P=e.amount||x,h=a.type==="eip-2612"?await H({eip2612Utils:i,callDataParams:[{owner:m,spender:t,value:P.toString(),nonce:y,deadline:g},r,l,c,a.version]}):await O({eip2612Utils:i,callDataParams:[{holder:m,spender:t,allowed:!0,value:P.toString(),nonce:y,expiry:g},r,l,c,a.version]}),p=await Z({data:h,from:m,to:c,config:o,isUserAccount:!!s});return{target:c,callData:h,gasLimit:p.toString(),dappId:V.PERMIT_HOOK_DAPP_ID}}function te(e){const{inputToken:n,chainId:t,account:r,amount:a}=e;return`${n.address.toLowerCase()}-${t}${r?`-${r.toLowerCase()}`:""}${a?`-${a.toString()}`:""}`}function ne(e){if(!(e instanceof f.BaseError))return;const n=e.walk(t=>t instanceof f.ExecutionRevertedError);return n instanceof f.ExecutionRevertedError?n:void 0}function ae(e){if(e.code!==void 0&&Q.includes(e.code))return!0;const n=e.message.toLowerCase();return Y.some(t=>n.includes(t))}function G(e){if(e instanceof Error)return e;const n=typeof e=="object"&&e!==null&&"message"in e&&typeof e.message=="string"?e.message:String(e),t=new Error(n);return typeof e=="object"&&e!==null&&"code"in e&&typeof e.code=="number"&&(t.code=e.code),t}class re{constructor(n,t){this.publicClient=n,this.walletClient=t}contractEncodeABI(n,t,r,a){const o=a.map(s=>s instanceof Uint8Array?f.bytesToHex(s):s);return f.encodeFunctionData({abi:n,functionName:r,args:o})}signTypedData(n,t,r){const a=Object.fromEntries(Object.entries(t.types).filter(([s])=>s!=="EIP712Domain")),o=this.walletClient.account??n;return this.walletClient.signTypedData({account:o,domain:t.domain,types:a,primaryType:t.primaryType,message:t.message})}ethCall(n,t){return this.publicClient.call({to:n,data:t}).then(({data:r})=>r||"0x")}decodeABIParameter(n,t){const[r]=f.decodeAbiParameters([{type:n}],t);return r}decodeABIParameters(n,t){const r=f.decodeAbiParameters(n,t),a={};return n.forEach((o,s)=>{const i=r[s],u=typeof i=="bigint"?f.toHex(i):i;a[s]=u,o.name&&(a[o.name]=u)}),a}}const L=new Map,N=new Map;async function j({chainId:e,publicClient:n,account:t,walletClient:r}){const a=L.get(e);if(!t&&a)return a;const o=`${e}-${t}`,s=N.get(o);if(s)return s;let i;if(!t)i=f.createWalletClient({account:v,chain:n.chain,transport:f.http()});else if(r)i=r;else throw new Error("Permit signing needs an active wallet client. Reconnect your wallet or use an injected extension.");const u=new re(n,i),c=await Promise.resolve().then(()=>require("./1inchPermitUtils-BDO_CqjL.cjs")).then(m=>m.Eip2612PermitUtils),l=new c(u,{enabledCheckSalt:!0});return t?N.set(o,l):L.set(e,l),l}const oe=[{constant:!0,inputs:[],name:"name",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{inputs:[],name:"eip712Domain",outputs:[{internalType:"bytes1",name:"fields",type:"bytes1"},{internalType:"string",name:"name",type:"string"},{internalType:"string",name:"version",type:"string"},{internalType:"uint256",name:"chainId",type:"uint256"},{internalType:"address",name:"verifyingContract",type:"address"},{internalType:"bytes32",name:"salt",type:"bytes32"},{internalType:"uint256[]",name:"extensions",type:"uint256[]"}],stateMutability:"view",type:"function"}];async function ie(e,n,t){const[r,a,o,s,i,u,c]=await b.readContract(t,{abi:oe,address:e,functionName:"eip712Domain"}),l={fields:r,name:a,version:o,chainId:s.toString(),verifyingContract:i,salt:u,extensions:c.map(m=>String(m))};return se(l,n,e)}async function se(e,n,t){const{extensions:r,...a}=e;if(r.length>0)throw Error("Extensions not implemented");if(a.chainId&&n!==parseInt(a.chainId,16))throw Error(`ChainId mismatch. Received: '${a.chainId}', expected: '${n}'`);if(a.verifyingContract&&t.toLowerCase()!==a.verifyingContract.toLowerCase())throw Error(`Address mismatch. Received: '${a.verifyingContract}', expected: '${t}'`);return le(a)}const ce=["name","version","chainId","verifyingContract","salt"];function le(e){const n=parseInt(e.fields,16);return ce.reduce((t,r,a)=>(n&1<<a&&(t[r]=e[r]),t),{})}async function ue(e,n,t){return b.readContract(t,{abi:f.erc20Abi,address:e,functionName:"name"})}const de=[[{constant:!0,inputs:[],name:"version",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"}],[{constant:!0,inputs:[],name:"ERC712_VERSION",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"}]];async function q(e,n,t=0){const r=de[t];if(!r)return"1";try{const a=await b.readContract(n,{abi:r,address:e,functionName:r[0].name});if(a==="0x"||Number.isNaN(Number(a)))return"1";if(!a.startsWith("0x"))return a;const[o]=f.decodeAbiParameters([{type:"string"}],a);return o}catch{return q(e,n,t+1)}}const me={nonce:0,deadline:_()},pe={allowed:!0,nonce:0,expiry:_()},M={},T={type:"unsupported"};async function fe(e){const{tokenAddress:n,chainId:t}=e,r=K.getTokenId({address:n,chainId:t}),a=M[r];if(a)return a;const o=ye(e);return M[r]=o,o}async function ye(e){const{amount:n,chainId:t,config:r,minGasLimit:a,publicClient:o,spender:s,tokenAddress:i}=e,u=await j({chainId:t,publicClient:o}),c=v.address;let l;try{l=await ie(i,t,r)}catch{console.debug(`[checkTokenIsPermittable] Couldn't fetch eip712domain for token ${i}`)}const m=!l?.name,y=l?.version===void 0,[g,P,h]=await Promise.allSettled([m?ue(i,t,r):Promise.resolve(l?.name),u.getTokenNonce(i,c),y?q(i,r):Promise.resolve(l?.version)]);let p=l?.name;if(g.status==="fulfilled")p=g.value;else if(m){const d=g.reason;if(/ETIMEDOUT/.test(d)&&!p)return{error:"Failed to fetch token name from contract. RPC connection error"};console.debug(`[checkTokenIsPermittable] Couldn't fetch token name from the contract for token ${i}, using provided '${p}'`,d)}if(!p)return{error:`Token name could not be determined for ${i}`};let A;if(P.status==="fulfilled")A=P.value;else{const d=P.reason;return d==="nonce not supported"||d.message==="nonce is NaN"?(console.debug(`[checkTokenIsPermittable] Not a permittable token ${i} - ${p}`,d?.message||d),{...T,name:p}):(console.debug(`[checkTokenIsPermittable] Failed to get nonce for ${i} - ${p}`,d),{error:d.message||d.toString()})}let R=l?.version;h.status==="fulfilled"?R=h.value:y&&console.debug(`[checkTokenIsPermittable] Failed to get version for ${i} - ${p}`,h.reason);const S={chainId:t,eip2612PermitUtils:u,nonce:A,spender:s,tokenAddress:i,tokenName:p,walletAddress:c,version:R,minGasLimit:a,amount:n};try{return await U({...S,type:"eip-2612",config:r})}catch(d){try{return await he(i,u)?await U({...S,type:"dai-like",config:r}):/invalid signature/.test(d)||d?.code==="UNPREDICTABLE_GAS_LIMIT"?(console.debug(`[checkTokenIsPermittable] Token ${i} - ${p} might be permittable, but it's not supported for now. Reason:`,d?.reason),{...T,name:p}):(console.debug(`[checkTokenIsPermittable] Failed to estimate eip-2612 permit for ${i} - ${p}`,d),{error:d.message||d.toString()})}catch(E){return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${i} - ${p}`,E),{error:E.message||E.toString()}}}}async function U(e){const{config:n,walletAddress:t,tokenAddress:r,tokenName:a,type:o,version:s,minGasLimit:i=$}=e,c=await(o==="eip-2612"?Pe:ge)(e);return c?await b.estimateGas(n,{to:r,account:t,data:c})>i?{type:o,version:s,name:a}:{...T,name:a}:{...T,name:a}}async function ge(e){const{eip2612PermitUtils:n,tokenAddress:t,walletAddress:r,spender:a,nonce:o,chainId:s,tokenName:i,version:u}=e;return await n.getPermitTypeHash(t)===D?O({eip2612Utils:n,callDataParams:[{...pe,holder:r,spender:a,nonce:o},s,i,t,u]}):!1}async function Pe(e){const{eip2612PermitUtils:n,walletAddress:t,spender:r,nonce:a,chainId:o,tokenName:s,tokenAddress:i,version:u,amount:c}=e;return H({eip2612Utils:n,callDataParams:[{...me,owner:t,spender:r,nonce:a,value:c?c.toString():x.toString()},+o,s,i,u]})}async function he(e,n){return await n.getPermitTypeHash(e)===D}const be=[{constant:!1,inputs:[{internalType:"address",name:"holder",type:"address"},{internalType:"address",name:"spender",type:"address"},{internalType:"uint256",name:"nonce",type:"uint256"},{internalType:"uint256",name:"expiry",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"}],name:"permit",outputs:[],payable:!1,stateMutability:"nonpayable",type:"function"}],Ee=[{type:"function",name:"permit",stateMutability:"nonpayable",inputs:[{name:"owner",type:"address"},{name:"spender",type:"address"},{name:"value",type:"uint256"},{name:"deadline",type:"uint256"},{name:"v",type:"uint8"},{name:"r",type:"bytes32"},{name:"s",type:"bytes32"}],outputs:[]}],I="0xd505accf",w="0x8fcbaf0c",D="0xea2aa0a1be11a07ed86d755c93467f4f82362b452371d1ba94d1715123511acb",Te=Object.freeze(Object.defineProperty({__proto__:null,DAI_EIP_2612_PERMIT_ABI:be,DAI_LIKE_PERMIT_TYPEHASH:D,DAI_PERMIT_SELECTOR:w,EIP_2612_PERMIT_ABI:Ee,EIP_2612_PERMIT_SELECTOR:I},Symbol.toStringTag,{value:"Module"}));exports.DEFAULT_MIN_GAS_LIMIT=$;exports.PERMIT_ACCOUNT=v;exports.checkIsCallDataAValidPermit=J;exports.generatePermitHook=X;exports.getPermitUtilsInstance=j;exports.getTokenPermitInfo=fe;exports.isSupportedPermitInfo=F;exports.oneInchPermitUtilsConsts=Te;
|
package/index.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { maxUint256 as
|
|
2
|
-
import { privateKeyToAccount as
|
|
3
|
-
import { estimateGas as
|
|
4
|
-
import { PERMIT_HOOK_DAPP_ID as
|
|
5
|
-
import { getTokenId as
|
|
6
|
-
const
|
|
7
|
-
async function
|
|
1
|
+
import { maxUint256 as B, ExecutionRevertedError as v, BaseError as K, bytesToHex as V, encodeFunctionData as q, decodeAbiParameters as k, toHex as z, createWalletClient as W, http as J, erc20Abi as Q } from "viem";
|
|
2
|
+
import { privateKeyToAccount as Y } from "viem/accounts";
|
|
3
|
+
import { estimateGas as x, readContract as C } from "wagmi/actions";
|
|
4
|
+
import { PERMIT_HOOK_DAPP_ID as X } from "@cowprotocol/hook-dapp-lib";
|
|
5
|
+
import { getTokenId as Z } from "@cowprotocol/cow-sdk";
|
|
6
|
+
const ee = "0xa50dc0f7fc051309434deb3b1c71e927dbb711759231d8ecbf630c85d94a42fe", _ = Y(ee), te = 55000n, I = 80000n, O = B, ne = 157788e6;
|
|
7
|
+
async function Ne(e, n, t, r, a, o, {
|
|
8
8
|
version: s,
|
|
9
|
-
type:
|
|
9
|
+
type: i,
|
|
10
10
|
name: u
|
|
11
11
|
}) {
|
|
12
|
-
const c = u ||
|
|
13
|
-
if (
|
|
12
|
+
const c = u || a;
|
|
13
|
+
if (i === "unsupported")
|
|
14
14
|
return !1;
|
|
15
15
|
if (!c)
|
|
16
16
|
throw new Error(`No token name for ${r}`);
|
|
@@ -18,216 +18,228 @@ async function Ae(a, n, e, r, t, i, {
|
|
|
18
18
|
chainId: n,
|
|
19
19
|
tokenName: c,
|
|
20
20
|
tokenAddress: r,
|
|
21
|
-
callData:
|
|
21
|
+
callData: o,
|
|
22
22
|
version: s
|
|
23
23
|
};
|
|
24
24
|
let m;
|
|
25
|
-
if (
|
|
25
|
+
if (o.startsWith(E) ? m = t.recoverPermitOwnerFromCallData({
|
|
26
26
|
...l,
|
|
27
27
|
// I don't know why this was removed, ok?
|
|
28
28
|
// We added it back on buildPermitCallData.ts
|
|
29
29
|
// But it looks like this is needed 🤷
|
|
30
30
|
// Check the test for this method https://github.com/1inch/permit-signed-approvals-utils/blob/master/src/eip-2612-permit.test.ts#L85-L106
|
|
31
|
-
callData:
|
|
32
|
-
}) :
|
|
31
|
+
callData: o.replace(E, "0x")
|
|
32
|
+
}) : o.startsWith(T) && (m = t.recoverDaiLikePermitOwnerFromCallData({
|
|
33
33
|
...l,
|
|
34
|
-
callData:
|
|
34
|
+
callData: o.replace(T, "0x")
|
|
35
35
|
})), !!m)
|
|
36
36
|
try {
|
|
37
|
-
return (await m).toLowerCase() ===
|
|
37
|
+
return (await m).toLowerCase() === e.toLowerCase();
|
|
38
38
|
} catch (f) {
|
|
39
|
-
console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${
|
|
39
|
+
console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${e} with callData ${o}`, f);
|
|
40
40
|
return;
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
-
async function
|
|
44
|
-
eip2612Utils:
|
|
43
|
+
async function H({
|
|
44
|
+
eip2612Utils: e,
|
|
45
45
|
callDataParams: n
|
|
46
46
|
}) {
|
|
47
|
-
return (await
|
|
47
|
+
return (await e.buildDaiLikePermitCallData(...n)).replace("0x", T);
|
|
48
48
|
}
|
|
49
|
-
async function
|
|
50
|
-
eip2612Utils:
|
|
49
|
+
async function F({
|
|
50
|
+
eip2612Utils: e,
|
|
51
51
|
callDataParams: n
|
|
52
52
|
}) {
|
|
53
|
-
const [
|
|
54
|
-
return (await
|
|
53
|
+
const [t, r, a, ...o] = n;
|
|
54
|
+
return (await e.buildPermitCallData(t, r, a, ...o)).replace("0x", E);
|
|
55
55
|
}
|
|
56
|
-
function
|
|
57
|
-
return Math.ceil((Date.now() +
|
|
56
|
+
function D() {
|
|
57
|
+
return Math.ceil((Date.now() + ne) / 1e3);
|
|
58
58
|
}
|
|
59
|
-
function
|
|
60
|
-
return !!
|
|
59
|
+
function ae(e) {
|
|
60
|
+
return !!e && e.type !== "unsupported";
|
|
61
61
|
}
|
|
62
|
-
const
|
|
63
|
-
async function
|
|
64
|
-
const n =
|
|
65
|
-
if (
|
|
66
|
-
return await
|
|
67
|
-
const r =
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
62
|
+
const w = {}, re = [4001, -32e3], oe = ["user denied", "user rejected", "rejected transaction", "transaction was rejected"];
|
|
63
|
+
async function $e(e) {
|
|
64
|
+
const n = ce(e), t = w[n];
|
|
65
|
+
if (t)
|
|
66
|
+
return await t;
|
|
67
|
+
const r = se(e).catch((a) => {
|
|
68
|
+
const o = G(a);
|
|
69
|
+
if (ue(o) || o instanceof v)
|
|
70
|
+
throw o;
|
|
71
|
+
console.debug("[generatePermitHook] cached request failed", o);
|
|
71
72
|
}).finally(() => {
|
|
72
|
-
delete
|
|
73
|
+
delete w[n];
|
|
73
74
|
});
|
|
74
|
-
return
|
|
75
|
+
return w[n] = r, r;
|
|
75
76
|
}
|
|
76
|
-
async function
|
|
77
|
-
data:
|
|
77
|
+
async function ie({
|
|
78
|
+
data: e,
|
|
78
79
|
from: n,
|
|
79
|
-
to:
|
|
80
|
+
to: t,
|
|
80
81
|
config: r,
|
|
81
|
-
isUserAccount:
|
|
82
|
+
isUserAccount: a
|
|
82
83
|
}) {
|
|
83
84
|
try {
|
|
84
|
-
const
|
|
85
|
+
const o = await x(r, {
|
|
85
86
|
account: n,
|
|
86
|
-
to:
|
|
87
|
-
data:
|
|
88
|
-
}), s =
|
|
89
|
-
return s >
|
|
90
|
-
} catch (
|
|
91
|
-
|
|
87
|
+
to: t,
|
|
88
|
+
data: e
|
|
89
|
+
}), s = a ? o : o + o / 10n;
|
|
90
|
+
return s > I ? s : I;
|
|
91
|
+
} catch (o) {
|
|
92
|
+
const s = G(o), i = a ? le(s) : void 0;
|
|
93
|
+
if (i) throw i;
|
|
94
|
+
return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default", s), I;
|
|
92
95
|
}
|
|
93
96
|
}
|
|
94
|
-
async function
|
|
97
|
+
async function se(e) {
|
|
95
98
|
const {
|
|
96
99
|
inputToken: n,
|
|
97
|
-
spender:
|
|
100
|
+
spender: t,
|
|
98
101
|
chainId: r,
|
|
99
|
-
permitInfo:
|
|
100
|
-
config:
|
|
102
|
+
permitInfo: a,
|
|
103
|
+
config: o,
|
|
101
104
|
account: s,
|
|
102
|
-
eip2612Utils:
|
|
105
|
+
eip2612Utils: i,
|
|
103
106
|
nonce: u
|
|
104
|
-
} =
|
|
105
|
-
if (!
|
|
107
|
+
} = e, c = n.address, l = a.name || n.name;
|
|
108
|
+
if (!ae(a))
|
|
106
109
|
throw new Error(`Trying to generate permit hook for unsupported token: ${c}`);
|
|
107
110
|
if (!l)
|
|
108
111
|
throw new Error(`No token name for token: ${c}`);
|
|
109
|
-
const m = s ||
|
|
110
|
-
eip2612Utils:
|
|
112
|
+
const m = s || _.address, f = u === void 0 ? await i.getTokenNonce(c, m) : u, y = D(), g = e.amount || O, h = a.type === "eip-2612" ? await F({
|
|
113
|
+
eip2612Utils: i,
|
|
111
114
|
callDataParams: [{
|
|
112
115
|
owner: m,
|
|
113
|
-
spender:
|
|
116
|
+
spender: t,
|
|
114
117
|
value: g.toString(),
|
|
115
118
|
nonce: f,
|
|
116
119
|
deadline: y
|
|
117
|
-
}, r, l, c,
|
|
118
|
-
}) : await
|
|
119
|
-
eip2612Utils:
|
|
120
|
+
}, r, l, c, a.version]
|
|
121
|
+
}) : await H({
|
|
122
|
+
eip2612Utils: i,
|
|
120
123
|
callDataParams: [{
|
|
121
124
|
holder: m,
|
|
122
|
-
spender:
|
|
125
|
+
spender: t,
|
|
123
126
|
allowed: !0,
|
|
124
127
|
value: g.toString(),
|
|
125
128
|
nonce: f,
|
|
126
129
|
expiry: y
|
|
127
|
-
}, r, l, c,
|
|
128
|
-
}), p = await
|
|
130
|
+
}, r, l, c, a.version]
|
|
131
|
+
}), p = await ie({
|
|
129
132
|
data: h,
|
|
130
133
|
from: m,
|
|
131
134
|
to: c,
|
|
132
|
-
config:
|
|
135
|
+
config: o,
|
|
133
136
|
isUserAccount: !!s
|
|
134
137
|
});
|
|
135
138
|
return {
|
|
136
139
|
target: c,
|
|
137
140
|
callData: h,
|
|
138
141
|
gasLimit: p.toString(),
|
|
139
|
-
dappId:
|
|
142
|
+
dappId: X
|
|
140
143
|
};
|
|
141
144
|
}
|
|
142
|
-
function
|
|
145
|
+
function ce(e) {
|
|
143
146
|
const {
|
|
144
147
|
inputToken: n,
|
|
145
|
-
chainId:
|
|
148
|
+
chainId: t,
|
|
146
149
|
account: r,
|
|
147
|
-
amount:
|
|
148
|
-
} =
|
|
149
|
-
return `${n.address.toLowerCase()}-${
|
|
150
|
+
amount: a
|
|
151
|
+
} = e;
|
|
152
|
+
return `${n.address.toLowerCase()}-${t}${r ? `-${r.toLowerCase()}` : ""}${a ? `-${a.toString()}` : ""}`;
|
|
153
|
+
}
|
|
154
|
+
function le(e) {
|
|
155
|
+
if (!(e instanceof K)) return;
|
|
156
|
+
const n = e.walk((t) => t instanceof v);
|
|
157
|
+
return n instanceof v ? n : void 0;
|
|
150
158
|
}
|
|
151
|
-
function
|
|
152
|
-
if (
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
return ne.some((e) => n.includes(e));
|
|
159
|
+
function ue(e) {
|
|
160
|
+
if (e.code !== void 0 && re.includes(e.code)) return !0;
|
|
161
|
+
const n = e.message.toLowerCase();
|
|
162
|
+
return oe.some((t) => n.includes(t));
|
|
156
163
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
164
|
+
function G(e) {
|
|
165
|
+
if (e instanceof Error) return e;
|
|
166
|
+
const n = typeof e == "object" && e !== null && "message" in e && typeof e.message == "string" ? e.message : String(e), t = new Error(n);
|
|
167
|
+
return typeof e == "object" && e !== null && "code" in e && typeof e.code == "number" && (t.code = e.code), t;
|
|
168
|
+
}
|
|
169
|
+
class de {
|
|
170
|
+
constructor(n, t) {
|
|
171
|
+
this.publicClient = n, this.walletClient = t;
|
|
160
172
|
}
|
|
161
|
-
contractEncodeABI(n,
|
|
162
|
-
const
|
|
163
|
-
return
|
|
173
|
+
contractEncodeABI(n, t, r, a) {
|
|
174
|
+
const o = a.map((s) => s instanceof Uint8Array ? V(s) : s);
|
|
175
|
+
return q({
|
|
164
176
|
abi: n,
|
|
165
177
|
functionName: r,
|
|
166
|
-
args:
|
|
178
|
+
args: o
|
|
167
179
|
});
|
|
168
180
|
}
|
|
169
|
-
signTypedData(n,
|
|
170
|
-
const
|
|
181
|
+
signTypedData(n, t, r) {
|
|
182
|
+
const a = Object.fromEntries(Object.entries(t.types).filter(([s]) => s !== "EIP712Domain")), o = this.walletClient.account ?? n;
|
|
171
183
|
return this.walletClient.signTypedData({
|
|
172
|
-
account:
|
|
173
|
-
domain:
|
|
174
|
-
types:
|
|
175
|
-
primaryType:
|
|
176
|
-
message:
|
|
184
|
+
account: o,
|
|
185
|
+
domain: t.domain,
|
|
186
|
+
types: a,
|
|
187
|
+
primaryType: t.primaryType,
|
|
188
|
+
message: t.message
|
|
177
189
|
});
|
|
178
190
|
}
|
|
179
|
-
ethCall(n,
|
|
191
|
+
ethCall(n, t) {
|
|
180
192
|
return this.publicClient.call({
|
|
181
193
|
to: n,
|
|
182
|
-
data:
|
|
194
|
+
data: t
|
|
183
195
|
}).then(({
|
|
184
196
|
data: r
|
|
185
197
|
}) => r || "0x");
|
|
186
198
|
}
|
|
187
|
-
decodeABIParameter(n,
|
|
199
|
+
decodeABIParameter(n, t) {
|
|
188
200
|
const [r] = k([{
|
|
189
201
|
type: n
|
|
190
|
-
}],
|
|
202
|
+
}], t);
|
|
191
203
|
return r;
|
|
192
204
|
}
|
|
193
|
-
decodeABIParameters(n,
|
|
194
|
-
const r = k(n,
|
|
195
|
-
return n.forEach((
|
|
196
|
-
const
|
|
197
|
-
|
|
198
|
-
}),
|
|
205
|
+
decodeABIParameters(n, t) {
|
|
206
|
+
const r = k(n, t), a = {};
|
|
207
|
+
return n.forEach((o, s) => {
|
|
208
|
+
const i = r[s], u = typeof i == "bigint" ? z(i) : i;
|
|
209
|
+
a[s] = u, o.name && (a[o.name] = u);
|
|
210
|
+
}), a;
|
|
199
211
|
}
|
|
200
212
|
}
|
|
201
|
-
const
|
|
202
|
-
async function
|
|
203
|
-
chainId:
|
|
213
|
+
const N = /* @__PURE__ */ new Map(), $ = /* @__PURE__ */ new Map();
|
|
214
|
+
async function me({
|
|
215
|
+
chainId: e,
|
|
204
216
|
publicClient: n,
|
|
205
|
-
account:
|
|
217
|
+
account: t,
|
|
206
218
|
walletClient: r
|
|
207
219
|
}) {
|
|
208
|
-
const
|
|
209
|
-
if (!
|
|
210
|
-
return
|
|
211
|
-
const
|
|
220
|
+
const a = N.get(e);
|
|
221
|
+
if (!t && a)
|
|
222
|
+
return a;
|
|
223
|
+
const o = `${e}-${t}`, s = $.get(o);
|
|
212
224
|
if (s)
|
|
213
225
|
return s;
|
|
214
|
-
let
|
|
215
|
-
if (!
|
|
216
|
-
|
|
217
|
-
account:
|
|
226
|
+
let i;
|
|
227
|
+
if (!t)
|
|
228
|
+
i = W({
|
|
229
|
+
account: _,
|
|
218
230
|
chain: n.chain,
|
|
219
|
-
transport:
|
|
231
|
+
transport: J()
|
|
220
232
|
});
|
|
221
233
|
else if (r)
|
|
222
|
-
|
|
234
|
+
i = r;
|
|
223
235
|
else
|
|
224
236
|
throw new Error("Permit signing needs an active wallet client. Reconnect your wallet or use an injected extension.");
|
|
225
|
-
const u = new
|
|
237
|
+
const u = new de(n, i), c = await import("./1inchPermitUtils-CXu6m1-9.js").then((m) => m.Eip2612PermitUtils), l = new c(u, {
|
|
226
238
|
enabledCheckSalt: !0
|
|
227
239
|
});
|
|
228
|
-
return
|
|
240
|
+
return t ? $.set(o, l) : N.set(e, l), l;
|
|
229
241
|
}
|
|
230
|
-
const
|
|
242
|
+
const pe = [{
|
|
231
243
|
constant: !0,
|
|
232
244
|
inputs: [],
|
|
233
245
|
name: "name",
|
|
@@ -273,48 +285,48 @@ const le = [{
|
|
|
273
285
|
stateMutability: "view",
|
|
274
286
|
type: "function"
|
|
275
287
|
}];
|
|
276
|
-
async function
|
|
277
|
-
const [r,
|
|
278
|
-
abi:
|
|
279
|
-
address:
|
|
288
|
+
async function fe(e, n, t) {
|
|
289
|
+
const [r, a, o, s, i, u, c] = await C(t, {
|
|
290
|
+
abi: pe,
|
|
291
|
+
address: e,
|
|
280
292
|
functionName: "eip712Domain"
|
|
281
293
|
}), l = {
|
|
282
294
|
fields: r,
|
|
283
|
-
name:
|
|
284
|
-
version:
|
|
295
|
+
name: a,
|
|
296
|
+
version: o,
|
|
285
297
|
chainId: s.toString(),
|
|
286
|
-
verifyingContract:
|
|
298
|
+
verifyingContract: i,
|
|
287
299
|
salt: u,
|
|
288
300
|
extensions: c.map((m) => String(m))
|
|
289
301
|
};
|
|
290
|
-
return
|
|
302
|
+
return ye(l, n, e);
|
|
291
303
|
}
|
|
292
|
-
async function
|
|
304
|
+
async function ye(e, n, t) {
|
|
293
305
|
const {
|
|
294
306
|
extensions: r,
|
|
295
|
-
...
|
|
296
|
-
} =
|
|
307
|
+
...a
|
|
308
|
+
} = e;
|
|
297
309
|
if (r.length > 0)
|
|
298
310
|
throw Error("Extensions not implemented");
|
|
299
|
-
if (
|
|
300
|
-
throw Error(`ChainId mismatch. Received: '${
|
|
301
|
-
if (
|
|
302
|
-
throw Error(`Address mismatch. Received: '${
|
|
303
|
-
return
|
|
311
|
+
if (a.chainId && n !== parseInt(a.chainId, 16))
|
|
312
|
+
throw Error(`ChainId mismatch. Received: '${a.chainId}', expected: '${n}'`);
|
|
313
|
+
if (a.verifyingContract && t.toLowerCase() !== a.verifyingContract.toLowerCase())
|
|
314
|
+
throw Error(`Address mismatch. Received: '${a.verifyingContract}', expected: '${t}'`);
|
|
315
|
+
return he(a);
|
|
304
316
|
}
|
|
305
|
-
const
|
|
306
|
-
function
|
|
307
|
-
const n = parseInt(
|
|
308
|
-
return
|
|
317
|
+
const ge = ["name", "version", "chainId", "verifyingContract", "salt"];
|
|
318
|
+
function he(e) {
|
|
319
|
+
const n = parseInt(e.fields, 16);
|
|
320
|
+
return ge.reduce((t, r, a) => (n & 1 << a && (t[r] = e[r]), t), {});
|
|
309
321
|
}
|
|
310
|
-
async function
|
|
311
|
-
return C(
|
|
312
|
-
abi:
|
|
313
|
-
address:
|
|
322
|
+
async function be(e, n, t) {
|
|
323
|
+
return C(t, {
|
|
324
|
+
abi: Q,
|
|
325
|
+
address: e,
|
|
314
326
|
functionName: "name"
|
|
315
327
|
});
|
|
316
328
|
}
|
|
317
|
-
const
|
|
329
|
+
const Pe = [[{
|
|
318
330
|
constant: !0,
|
|
319
331
|
inputs: [],
|
|
320
332
|
name: "version",
|
|
@@ -337,71 +349,71 @@ const ye = [[{
|
|
|
337
349
|
stateMutability: "view",
|
|
338
350
|
type: "function"
|
|
339
351
|
}]];
|
|
340
|
-
async function
|
|
341
|
-
const r =
|
|
352
|
+
async function j(e, n, t = 0) {
|
|
353
|
+
const r = Pe[t];
|
|
342
354
|
if (!r)
|
|
343
355
|
return "1";
|
|
344
356
|
try {
|
|
345
|
-
const
|
|
357
|
+
const a = await C(n, {
|
|
346
358
|
abi: r,
|
|
347
|
-
address:
|
|
359
|
+
address: e,
|
|
348
360
|
functionName: r[0].name
|
|
349
361
|
});
|
|
350
|
-
if (
|
|
362
|
+
if (a === "0x" || Number.isNaN(Number(a)))
|
|
351
363
|
return "1";
|
|
352
|
-
if (!
|
|
353
|
-
return
|
|
354
|
-
const [
|
|
364
|
+
if (!a.startsWith("0x"))
|
|
365
|
+
return a;
|
|
366
|
+
const [o] = k([{
|
|
355
367
|
type: "string"
|
|
356
|
-
}],
|
|
357
|
-
return
|
|
368
|
+
}], a);
|
|
369
|
+
return o;
|
|
358
370
|
} catch {
|
|
359
|
-
return
|
|
371
|
+
return j(e, n, t + 1);
|
|
360
372
|
}
|
|
361
373
|
}
|
|
362
|
-
const
|
|
374
|
+
const Ee = {
|
|
363
375
|
nonce: 0,
|
|
364
|
-
deadline:
|
|
365
|
-
},
|
|
376
|
+
deadline: D()
|
|
377
|
+
}, Te = {
|
|
366
378
|
allowed: !0,
|
|
367
379
|
nonce: 0,
|
|
368
|
-
expiry:
|
|
369
|
-
},
|
|
380
|
+
expiry: D()
|
|
381
|
+
}, M = {}, P = {
|
|
370
382
|
type: "unsupported"
|
|
371
383
|
};
|
|
372
|
-
async function
|
|
384
|
+
async function Me(e) {
|
|
373
385
|
const {
|
|
374
386
|
tokenAddress: n,
|
|
375
|
-
chainId:
|
|
376
|
-
} =
|
|
387
|
+
chainId: t
|
|
388
|
+
} = e, r = Z({
|
|
377
389
|
address: n,
|
|
378
|
-
chainId:
|
|
379
|
-
}),
|
|
380
|
-
if (
|
|
381
|
-
return
|
|
382
|
-
const
|
|
383
|
-
return
|
|
390
|
+
chainId: t
|
|
391
|
+
}), a = M[r];
|
|
392
|
+
if (a)
|
|
393
|
+
return a;
|
|
394
|
+
const o = Ie(e);
|
|
395
|
+
return M[r] = o, o;
|
|
384
396
|
}
|
|
385
|
-
async function
|
|
397
|
+
async function Ie(e) {
|
|
386
398
|
const {
|
|
387
399
|
amount: n,
|
|
388
|
-
chainId:
|
|
400
|
+
chainId: t,
|
|
389
401
|
config: r,
|
|
390
|
-
minGasLimit:
|
|
391
|
-
publicClient:
|
|
402
|
+
minGasLimit: a,
|
|
403
|
+
publicClient: o,
|
|
392
404
|
spender: s,
|
|
393
|
-
tokenAddress:
|
|
394
|
-
} =
|
|
395
|
-
chainId:
|
|
396
|
-
publicClient:
|
|
397
|
-
}), c =
|
|
405
|
+
tokenAddress: i
|
|
406
|
+
} = e, u = await me({
|
|
407
|
+
chainId: t,
|
|
408
|
+
publicClient: o
|
|
409
|
+
}), c = _.address;
|
|
398
410
|
let l;
|
|
399
411
|
try {
|
|
400
|
-
l = await
|
|
412
|
+
l = await fe(i, t, r);
|
|
401
413
|
} catch {
|
|
402
|
-
console.debug(`[checkTokenIsPermittable] Couldn't fetch eip712domain for token ${
|
|
414
|
+
console.debug(`[checkTokenIsPermittable] Couldn't fetch eip712domain for token ${i}`);
|
|
403
415
|
}
|
|
404
|
-
const m = !l?.name, f = l?.version === void 0, [y, g, h] = await Promise.allSettled([m ?
|
|
416
|
+
const m = !l?.name, f = l?.version === void 0, [y, g, h] = await Promise.allSettled([m ? be(i, t, r) : Promise.resolve(l?.name), u.getTokenNonce(i, c), f ? j(i, r) : Promise.resolve(l?.version)]);
|
|
405
417
|
let p = l?.name;
|
|
406
418
|
if (y.status === "fulfilled")
|
|
407
419
|
p = y.value;
|
|
@@ -411,137 +423,137 @@ async function Pe(a) {
|
|
|
411
423
|
return {
|
|
412
424
|
error: "Failed to fetch token name from contract. RPC connection error"
|
|
413
425
|
};
|
|
414
|
-
console.debug(`[checkTokenIsPermittable] Couldn't fetch token name from the contract for token ${
|
|
426
|
+
console.debug(`[checkTokenIsPermittable] Couldn't fetch token name from the contract for token ${i}, using provided '${p}'`, d);
|
|
415
427
|
}
|
|
416
428
|
if (!p)
|
|
417
429
|
return {
|
|
418
|
-
error: `Token name could not be determined for ${
|
|
430
|
+
error: `Token name could not be determined for ${i}`
|
|
419
431
|
};
|
|
420
|
-
let
|
|
432
|
+
let R;
|
|
421
433
|
if (g.status === "fulfilled")
|
|
422
|
-
|
|
434
|
+
R = g.value;
|
|
423
435
|
else {
|
|
424
436
|
const d = g.reason;
|
|
425
|
-
return d === "nonce not supported" || d.message === "nonce is NaN" ? (console.debug(`[checkTokenIsPermittable] Not a permittable token ${
|
|
426
|
-
...
|
|
437
|
+
return d === "nonce not supported" || d.message === "nonce is NaN" ? (console.debug(`[checkTokenIsPermittable] Not a permittable token ${i} - ${p}`, d?.message || d), {
|
|
438
|
+
...P,
|
|
427
439
|
name: p
|
|
428
|
-
}) : (console.debug(`[checkTokenIsPermittable] Failed to get nonce for ${
|
|
440
|
+
}) : (console.debug(`[checkTokenIsPermittable] Failed to get nonce for ${i} - ${p}`, d), {
|
|
429
441
|
error: d.message || d.toString()
|
|
430
442
|
});
|
|
431
443
|
}
|
|
432
|
-
let
|
|
433
|
-
h.status === "fulfilled" ?
|
|
434
|
-
const
|
|
435
|
-
chainId:
|
|
444
|
+
let S = l?.version;
|
|
445
|
+
h.status === "fulfilled" ? S = h.value : f && console.debug(`[checkTokenIsPermittable] Failed to get version for ${i} - ${p}`, h.reason);
|
|
446
|
+
const L = {
|
|
447
|
+
chainId: t,
|
|
436
448
|
eip2612PermitUtils: u,
|
|
437
|
-
nonce:
|
|
449
|
+
nonce: R,
|
|
438
450
|
spender: s,
|
|
439
|
-
tokenAddress:
|
|
451
|
+
tokenAddress: i,
|
|
440
452
|
tokenName: p,
|
|
441
453
|
walletAddress: c,
|
|
442
|
-
version:
|
|
443
|
-
minGasLimit:
|
|
454
|
+
version: S,
|
|
455
|
+
minGasLimit: a,
|
|
444
456
|
amount: n
|
|
445
457
|
};
|
|
446
458
|
try {
|
|
447
|
-
return await
|
|
448
|
-
...
|
|
459
|
+
return await U({
|
|
460
|
+
...L,
|
|
449
461
|
type: "eip-2612",
|
|
450
462
|
config: r
|
|
451
463
|
});
|
|
452
464
|
} catch (d) {
|
|
453
465
|
try {
|
|
454
|
-
return await
|
|
455
|
-
...
|
|
466
|
+
return await ke(i, u) ? await U({
|
|
467
|
+
...L,
|
|
456
468
|
type: "dai-like",
|
|
457
469
|
config: r
|
|
458
|
-
}) : /invalid signature/.test(d) || d?.code === "UNPREDICTABLE_GAS_LIMIT" ? (console.debug(`[checkTokenIsPermittable] Token ${
|
|
459
|
-
...
|
|
470
|
+
}) : /invalid signature/.test(d) || d?.code === "UNPREDICTABLE_GAS_LIMIT" ? (console.debug(`[checkTokenIsPermittable] Token ${i} - ${p} might be permittable, but it's not supported for now. Reason:`, d?.reason), {
|
|
471
|
+
...P,
|
|
460
472
|
name: p
|
|
461
|
-
}) : (console.debug(`[checkTokenIsPermittable] Failed to estimate eip-2612 permit for ${
|
|
473
|
+
}) : (console.debug(`[checkTokenIsPermittable] Failed to estimate eip-2612 permit for ${i} - ${p}`, d), {
|
|
462
474
|
error: d.message || d.toString()
|
|
463
475
|
});
|
|
464
|
-
} catch (
|
|
465
|
-
return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${
|
|
466
|
-
error:
|
|
476
|
+
} catch (b) {
|
|
477
|
+
return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${i} - ${p}`, b), {
|
|
478
|
+
error: b.message || b.toString()
|
|
467
479
|
};
|
|
468
480
|
}
|
|
469
481
|
}
|
|
470
482
|
}
|
|
471
|
-
async function
|
|
483
|
+
async function U(e) {
|
|
472
484
|
const {
|
|
473
485
|
config: n,
|
|
474
|
-
walletAddress:
|
|
486
|
+
walletAddress: t,
|
|
475
487
|
tokenAddress: r,
|
|
476
|
-
tokenName:
|
|
477
|
-
type:
|
|
488
|
+
tokenName: a,
|
|
489
|
+
type: o,
|
|
478
490
|
version: s,
|
|
479
|
-
minGasLimit:
|
|
480
|
-
} =
|
|
481
|
-
return c ? await
|
|
491
|
+
minGasLimit: i = te
|
|
492
|
+
} = e, c = await (o === "eip-2612" ? ve : we)(e);
|
|
493
|
+
return c ? await x(n, {
|
|
482
494
|
to: r,
|
|
483
|
-
account:
|
|
495
|
+
account: t,
|
|
484
496
|
data: c
|
|
485
|
-
}) >
|
|
486
|
-
type:
|
|
497
|
+
}) > i ? {
|
|
498
|
+
type: o,
|
|
487
499
|
version: s,
|
|
488
|
-
name:
|
|
500
|
+
name: a
|
|
489
501
|
} : {
|
|
490
|
-
...
|
|
491
|
-
name:
|
|
502
|
+
...P,
|
|
503
|
+
name: a
|
|
492
504
|
} : {
|
|
493
|
-
...
|
|
494
|
-
name:
|
|
505
|
+
...P,
|
|
506
|
+
name: a
|
|
495
507
|
};
|
|
496
508
|
}
|
|
497
|
-
async function
|
|
509
|
+
async function we(e) {
|
|
498
510
|
const {
|
|
499
511
|
eip2612PermitUtils: n,
|
|
500
|
-
tokenAddress:
|
|
512
|
+
tokenAddress: t,
|
|
501
513
|
walletAddress: r,
|
|
502
|
-
spender:
|
|
503
|
-
nonce:
|
|
514
|
+
spender: a,
|
|
515
|
+
nonce: o,
|
|
504
516
|
chainId: s,
|
|
505
|
-
tokenName:
|
|
517
|
+
tokenName: i,
|
|
506
518
|
version: u
|
|
507
|
-
} =
|
|
508
|
-
return await n.getPermitTypeHash(
|
|
519
|
+
} = e;
|
|
520
|
+
return await n.getPermitTypeHash(t) === A ? H({
|
|
509
521
|
eip2612Utils: n,
|
|
510
522
|
callDataParams: [{
|
|
511
|
-
...
|
|
523
|
+
...Te,
|
|
512
524
|
holder: r,
|
|
513
|
-
spender:
|
|
514
|
-
nonce:
|
|
515
|
-
}, s,
|
|
525
|
+
spender: a,
|
|
526
|
+
nonce: o
|
|
527
|
+
}, s, i, t, u]
|
|
516
528
|
}) : !1;
|
|
517
529
|
}
|
|
518
|
-
async function
|
|
530
|
+
async function ve(e) {
|
|
519
531
|
const {
|
|
520
532
|
eip2612PermitUtils: n,
|
|
521
|
-
walletAddress:
|
|
533
|
+
walletAddress: t,
|
|
522
534
|
spender: r,
|
|
523
|
-
nonce:
|
|
524
|
-
chainId:
|
|
535
|
+
nonce: a,
|
|
536
|
+
chainId: o,
|
|
525
537
|
tokenName: s,
|
|
526
|
-
tokenAddress:
|
|
538
|
+
tokenAddress: i,
|
|
527
539
|
version: u,
|
|
528
540
|
amount: c
|
|
529
|
-
} =
|
|
530
|
-
return
|
|
541
|
+
} = e;
|
|
542
|
+
return F({
|
|
531
543
|
eip2612Utils: n,
|
|
532
544
|
callDataParams: [{
|
|
533
|
-
...
|
|
534
|
-
owner:
|
|
545
|
+
...Ee,
|
|
546
|
+
owner: t,
|
|
535
547
|
spender: r,
|
|
536
|
-
nonce:
|
|
537
|
-
value: c ? c.toString() :
|
|
538
|
-
}, +
|
|
548
|
+
nonce: a,
|
|
549
|
+
value: c ? c.toString() : O.toString()
|
|
550
|
+
}, +o, s, i, u]
|
|
539
551
|
});
|
|
540
552
|
}
|
|
541
|
-
async function
|
|
542
|
-
return await n.getPermitTypeHash(
|
|
553
|
+
async function ke(e, n) {
|
|
554
|
+
return await n.getPermitTypeHash(e) === A;
|
|
543
555
|
}
|
|
544
|
-
const
|
|
556
|
+
const Ce = [{
|
|
545
557
|
constant: !1,
|
|
546
558
|
inputs: [{
|
|
547
559
|
internalType: "address",
|
|
@@ -581,7 +593,7 @@ const we = [{
|
|
|
581
593
|
payable: !1,
|
|
582
594
|
stateMutability: "nonpayable",
|
|
583
595
|
type: "function"
|
|
584
|
-
}],
|
|
596
|
+
}], _e = [{
|
|
585
597
|
type: "function",
|
|
586
598
|
name: "permit",
|
|
587
599
|
stateMutability: "nonpayable",
|
|
@@ -608,21 +620,21 @@ const we = [{
|
|
|
608
620
|
type: "bytes32"
|
|
609
621
|
}],
|
|
610
622
|
outputs: []
|
|
611
|
-
}],
|
|
623
|
+
}], E = "0xd505accf", T = "0x8fcbaf0c", A = "0xea2aa0a1be11a07ed86d755c93467f4f82362b452371d1ba94d1715123511acb", Ue = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
612
624
|
__proto__: null,
|
|
613
|
-
DAI_EIP_2612_PERMIT_ABI:
|
|
614
|
-
DAI_LIKE_PERMIT_TYPEHASH:
|
|
615
|
-
DAI_PERMIT_SELECTOR:
|
|
616
|
-
EIP_2612_PERMIT_ABI:
|
|
617
|
-
EIP_2612_PERMIT_SELECTOR:
|
|
625
|
+
DAI_EIP_2612_PERMIT_ABI: Ce,
|
|
626
|
+
DAI_LIKE_PERMIT_TYPEHASH: A,
|
|
627
|
+
DAI_PERMIT_SELECTOR: T,
|
|
628
|
+
EIP_2612_PERMIT_ABI: _e,
|
|
629
|
+
EIP_2612_PERMIT_SELECTOR: E
|
|
618
630
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
619
631
|
export {
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
632
|
+
te as DEFAULT_MIN_GAS_LIMIT,
|
|
633
|
+
_ as PERMIT_ACCOUNT,
|
|
634
|
+
Ne as checkIsCallDataAValidPermit,
|
|
635
|
+
$e as generatePermitHook,
|
|
636
|
+
me as getPermitUtilsInstance,
|
|
637
|
+
Me as getTokenPermitInfo,
|
|
638
|
+
ae as isSupportedPermitInfo,
|
|
639
|
+
Ue as oneInchPermitUtilsConsts
|
|
628
640
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/permit-utils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.8.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/cowprotocol/cowswap.git",
|
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
"access": "public"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@cowprotocol/cow-sdk": "9.2.
|
|
17
|
+
"@cowprotocol/cow-sdk": "9.2.11",
|
|
18
18
|
"@1inch/permit-signed-approvals-utils": "1.5.2",
|
|
19
|
-
"@cowprotocol/hook-dapp-lib": "^2.
|
|
19
|
+
"@cowprotocol/hook-dapp-lib": "^2.3.0",
|
|
20
20
|
"ms.macro": "2.0.0",
|
|
21
21
|
"viem": "2.48.8",
|
|
22
22
|
"wagmi": "3.6.9"
|