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