@cowprotocol/permit-utils 3.4.1 → 3.4.2
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 +59 -59
- package/package.json +3 -3
- package/types.d.ts +27 -27
- package/utils/PermitProviderConnector.d.ts +1 -1
- package/utils/buildPermitCallData.d.ts +2 -2
- package/utils/getEip712Domain.d.ts +2 -2
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("viem"),x=require("viem/accounts"),H=require("@cowprotocol/hook-dapp-lib"),P=require("wagmi/actions"),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}){const[e,r,t,...i]=n;return(await a.buildPermitCallData(e,r,t,...i)).replace("0x",I)}async function M({eip2612Utils:a,callDataParams:n}){return(await a.buildDaiLikePermitCallData(...n)).replace("0x",b)}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"];function V(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))}async function W(a){const n=Q(a),e=k[n];if(e)return await e;const r=z(a).catch(t=>{if(V(t))throw t;console.debug("[generatePermitHook] cached request failed",t)}).finally(()=>{delete k[n]});return k[n]=r,r}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 N({eip2612Utils:o,callDataParams:[{owner:c,spender:e,value:h.toString(),nonce:y,deadline:f},r,m,l,t.version]}):await M({eip2612Utils:o,callDataParams:[{holder:c,spender:e,allowed:!0,value:h.toString(),nonce:y,expiry:f},r,m,l,t.version]}),g=await J({data:u,from:c,to:l,config:i,isUserAccount:!!s});return{target:l,callData:u,gasLimit:g.toString(),dappId:H.PERMIT_HOOK_DAPP_ID}}async function J({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}}function Q(a){const{inputToken:n,chainId:e,account:r,amount:t}=a;return`${n.address.toLowerCase()}-${e}${r?`-${r.toLowerCase()}`:""}${t?`-${t.toString()}`:""}`}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 ue(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"?le:de)(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,walletAddress:e,spender:r,nonce:t,chainId:i,tokenName:s,tokenAddress:o,version:d,amount:l}=a;return N({eip2612Utils:n,callDataParams:[{...ie,owner:e,spender:r,nonce:t,value:l?l.toString():R.toString()},+i,s,o,d]})}async function ue(a,n){return await n.getPermitTypeHash(a)===_}async function de(a){const{eip2612PermitUtils:n,tokenAddress:e,walletAddress:r,spender:t,nonce:i,chainId:s,tokenName:o,version:d}=a;return await n.getPermitTypeHash(e)===_?M({eip2612Utils:n,callDataParams:[{...oe,holder:r,spender:t,nonce:i},s,o,e,d]}):!1}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=W;exports.getPermitUtilsInstance=$;exports.getTokenPermitInfo=se;exports.isSupportedPermitInfo=U;exports.oneInchPermitUtilsConsts=ye;
|
|
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;
|
package/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { maxUint256 as U, bytesToHex as x, encodeFunctionData as O, decodeAbiParameters as w, toHex as H, createWalletClient as F, http as G, erc20Abi as j } from "viem";
|
|
2
2
|
import { privateKeyToAccount as B } from "viem/accounts";
|
|
3
|
-
import { PERMIT_HOOK_DAPP_ID as K } from "@cowprotocol/hook-dapp-lib";
|
|
4
3
|
import { estimateGas as R, readContract as E } from "wagmi/actions";
|
|
4
|
+
import { PERMIT_HOOK_DAPP_ID as K } from "@cowprotocol/hook-dapp-lib";
|
|
5
5
|
import { getTokenId as V } from "@cowprotocol/cow-sdk";
|
|
6
6
|
const q = "0xa50dc0f7fc051309434deb3b1c71e927dbb711759231d8ecbf630c85d94a42fe", k = B(q), W = 55000n, P = 80000n, L = U, z = 157788e6;
|
|
7
7
|
async function ke(a, n, e, r, t, i, {
|
|
@@ -44,14 +44,14 @@ async function N({
|
|
|
44
44
|
eip2612Utils: a,
|
|
45
45
|
callDataParams: n
|
|
46
46
|
}) {
|
|
47
|
-
|
|
48
|
-
return (await a.buildPermitCallData(e, r, t, ...i)).replace("0x", T);
|
|
47
|
+
return (await a.buildDaiLikePermitCallData(...n)).replace("0x", b);
|
|
49
48
|
}
|
|
50
49
|
async function $({
|
|
51
50
|
eip2612Utils: a,
|
|
52
51
|
callDataParams: n
|
|
53
52
|
}) {
|
|
54
|
-
|
|
53
|
+
const [e, r, t, ...i] = n;
|
|
54
|
+
return (await a.buildPermitCallData(e, r, t, ...i)).replace("0x", T);
|
|
55
55
|
}
|
|
56
56
|
function C() {
|
|
57
57
|
return Math.ceil((Date.now() + z) / 1e3);
|
|
@@ -60,18 +60,12 @@ function J(a) {
|
|
|
60
60
|
return !!a && a.type !== "unsupported";
|
|
61
61
|
}
|
|
62
62
|
const I = {}, Q = [4001, -32e3], Y = ["user denied", "user rejected", "rejected transaction", "transaction was rejected"];
|
|
63
|
-
function X(a) {
|
|
64
|
-
if (!a) return !1;
|
|
65
|
-
if (Q.includes(a.code)) return !0;
|
|
66
|
-
const n = (typeof a == "string" ? a : a.message)?.toLowerCase() || "";
|
|
67
|
-
return Y.some((e) => n.includes(e));
|
|
68
|
-
}
|
|
69
63
|
async function Ce(a) {
|
|
70
|
-
const n =
|
|
64
|
+
const n = ee(a), e = I[n];
|
|
71
65
|
if (e)
|
|
72
66
|
return await e;
|
|
73
67
|
const r = Z(a).catch((t) => {
|
|
74
|
-
if (
|
|
68
|
+
if (te(t))
|
|
75
69
|
throw t;
|
|
76
70
|
console.debug("[generatePermitHook] cached request failed", t);
|
|
77
71
|
}).finally(() => {
|
|
@@ -79,6 +73,24 @@ async function Ce(a) {
|
|
|
79
73
|
});
|
|
80
74
|
return I[n] = r, r;
|
|
81
75
|
}
|
|
76
|
+
async function X({
|
|
77
|
+
data: a,
|
|
78
|
+
from: n,
|
|
79
|
+
to: e,
|
|
80
|
+
config: r,
|
|
81
|
+
isUserAccount: t
|
|
82
|
+
}) {
|
|
83
|
+
try {
|
|
84
|
+
const i = await R(r, {
|
|
85
|
+
account: n,
|
|
86
|
+
to: e,
|
|
87
|
+
data: a
|
|
88
|
+
}), s = t ? i : i + i / 10n;
|
|
89
|
+
return s > P ? s : P;
|
|
90
|
+
} catch (i) {
|
|
91
|
+
return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default", i), P;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
82
94
|
async function Z(a) {
|
|
83
95
|
const {
|
|
84
96
|
inputToken: n,
|
|
@@ -94,7 +106,7 @@ async function Z(a) {
|
|
|
94
106
|
throw new Error(`Trying to generate permit hook for unsupported token: ${l}`);
|
|
95
107
|
if (!m)
|
|
96
108
|
throw new Error(`No token name for token: ${l}`);
|
|
97
|
-
const c = s || k.address, p = d === void 0 ? await o.getTokenNonce(l, c) : d, y = C(), h = a.amount || L, u = t.type === "eip-2612" ? await
|
|
109
|
+
const c = s || k.address, p = d === void 0 ? await o.getTokenNonce(l, c) : d, y = C(), h = a.amount || L, u = t.type === "eip-2612" ? await $({
|
|
98
110
|
eip2612Utils: o,
|
|
99
111
|
callDataParams: [{
|
|
100
112
|
owner: c,
|
|
@@ -103,7 +115,7 @@ async function Z(a) {
|
|
|
103
115
|
nonce: p,
|
|
104
116
|
deadline: y
|
|
105
117
|
}, r, m, l, t.version]
|
|
106
|
-
}) : await
|
|
118
|
+
}) : await N({
|
|
107
119
|
eip2612Utils: o,
|
|
108
120
|
callDataParams: [{
|
|
109
121
|
holder: c,
|
|
@@ -113,7 +125,7 @@ async function Z(a) {
|
|
|
113
125
|
nonce: p,
|
|
114
126
|
expiry: y
|
|
115
127
|
}, r, m, l, t.version]
|
|
116
|
-
}), f = await
|
|
128
|
+
}), f = await X({
|
|
117
129
|
data: u,
|
|
118
130
|
from: c,
|
|
119
131
|
to: l,
|
|
@@ -127,25 +139,7 @@ async function Z(a) {
|
|
|
127
139
|
dappId: K
|
|
128
140
|
};
|
|
129
141
|
}
|
|
130
|
-
|
|
131
|
-
data: a,
|
|
132
|
-
from: n,
|
|
133
|
-
to: e,
|
|
134
|
-
config: r,
|
|
135
|
-
isUserAccount: t
|
|
136
|
-
}) {
|
|
137
|
-
try {
|
|
138
|
-
const i = await R(r, {
|
|
139
|
-
account: n,
|
|
140
|
-
to: e,
|
|
141
|
-
data: a
|
|
142
|
-
}), s = t ? i : i + i / 10n;
|
|
143
|
-
return s > P ? s : P;
|
|
144
|
-
} catch (i) {
|
|
145
|
-
return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default", i), P;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
function te(a) {
|
|
142
|
+
function ee(a) {
|
|
149
143
|
const {
|
|
150
144
|
inputToken: n,
|
|
151
145
|
chainId: e,
|
|
@@ -154,6 +148,12 @@ function te(a) {
|
|
|
154
148
|
} = a;
|
|
155
149
|
return `${n.address.toLowerCase()}-${e}${r ? `-${r.toLowerCase()}` : ""}${t ? `-${t.toString()}` : ""}`;
|
|
156
150
|
}
|
|
151
|
+
function te(a) {
|
|
152
|
+
if (!a) return !1;
|
|
153
|
+
if (Q.includes(a.code)) return !0;
|
|
154
|
+
const n = (typeof a == "string" ? a : a.message)?.toLowerCase() || "";
|
|
155
|
+
return Y.some((e) => n.includes(e));
|
|
156
|
+
}
|
|
157
157
|
class ne {
|
|
158
158
|
constructor(n, e) {
|
|
159
159
|
this.publicClient = n, this.walletClient = e;
|
|
@@ -453,7 +453,7 @@ async function pe(a) {
|
|
|
453
453
|
});
|
|
454
454
|
} catch (u) {
|
|
455
455
|
try {
|
|
456
|
-
return await
|
|
456
|
+
return await he(o, d) ? await S({
|
|
457
457
|
...h,
|
|
458
458
|
type: "dai-like",
|
|
459
459
|
config: r
|
|
@@ -479,7 +479,7 @@ async function S(a) {
|
|
|
479
479
|
type: i,
|
|
480
480
|
version: s,
|
|
481
481
|
minGasLimit: o = W
|
|
482
|
-
} = a, l = await (i === "eip-2612" ?
|
|
482
|
+
} = a, l = await (i === "eip-2612" ? fe : ye)(a);
|
|
483
483
|
return l ? await R(n, {
|
|
484
484
|
to: r,
|
|
485
485
|
account: e,
|
|
@@ -497,6 +497,27 @@ async function S(a) {
|
|
|
497
497
|
};
|
|
498
498
|
}
|
|
499
499
|
async function ye(a) {
|
|
500
|
+
const {
|
|
501
|
+
eip2612PermitUtils: n,
|
|
502
|
+
tokenAddress: e,
|
|
503
|
+
walletAddress: r,
|
|
504
|
+
spender: t,
|
|
505
|
+
nonce: i,
|
|
506
|
+
chainId: s,
|
|
507
|
+
tokenName: o,
|
|
508
|
+
version: d
|
|
509
|
+
} = a;
|
|
510
|
+
return await n.getPermitTypeHash(e) === _ ? N({
|
|
511
|
+
eip2612Utils: n,
|
|
512
|
+
callDataParams: [{
|
|
513
|
+
...me,
|
|
514
|
+
holder: r,
|
|
515
|
+
spender: t,
|
|
516
|
+
nonce: i
|
|
517
|
+
}, s, o, e, d]
|
|
518
|
+
}) : !1;
|
|
519
|
+
}
|
|
520
|
+
async function fe(a) {
|
|
500
521
|
const {
|
|
501
522
|
eip2612PermitUtils: n,
|
|
502
523
|
walletAddress: e,
|
|
@@ -508,7 +529,7 @@ async function ye(a) {
|
|
|
508
529
|
version: d,
|
|
509
530
|
amount: l
|
|
510
531
|
} = a;
|
|
511
|
-
return
|
|
532
|
+
return $({
|
|
512
533
|
eip2612Utils: n,
|
|
513
534
|
callDataParams: [{
|
|
514
535
|
...de,
|
|
@@ -519,30 +540,9 @@ async function ye(a) {
|
|
|
519
540
|
}, +i, s, o, d]
|
|
520
541
|
});
|
|
521
542
|
}
|
|
522
|
-
async function
|
|
543
|
+
async function he(a, n) {
|
|
523
544
|
return await n.getPermitTypeHash(a) === _;
|
|
524
545
|
}
|
|
525
|
-
async function he(a) {
|
|
526
|
-
const {
|
|
527
|
-
eip2612PermitUtils: n,
|
|
528
|
-
tokenAddress: e,
|
|
529
|
-
walletAddress: r,
|
|
530
|
-
spender: t,
|
|
531
|
-
nonce: i,
|
|
532
|
-
chainId: s,
|
|
533
|
-
tokenName: o,
|
|
534
|
-
version: d
|
|
535
|
-
} = a;
|
|
536
|
-
return await n.getPermitTypeHash(e) === _ ? $({
|
|
537
|
-
eip2612Utils: n,
|
|
538
|
-
callDataParams: [{
|
|
539
|
-
...me,
|
|
540
|
-
holder: r,
|
|
541
|
-
spender: t,
|
|
542
|
-
nonce: i
|
|
543
|
-
}, s, o, e, d]
|
|
544
|
-
}) : !1;
|
|
545
|
-
}
|
|
546
546
|
const ge = [{
|
|
547
547
|
constant: !1,
|
|
548
548
|
inputs: [{
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/permit-utils",
|
|
3
|
-
"version": "3.4.
|
|
3
|
+
"version": "3.4.2",
|
|
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.5",
|
|
18
18
|
"@1inch/permit-signed-approvals-utils": "1.5.2",
|
|
19
|
-
"@cowprotocol/hook-dapp-lib": "^2.2.
|
|
19
|
+
"@cowprotocol/hook-dapp-lib": "^2.2.10",
|
|
20
20
|
"ms.macro": "2.0.0",
|
|
21
21
|
"viem": "2.48.8",
|
|
22
22
|
"wagmi": "3.6.9"
|
package/types.d.ts
CHANGED
|
@@ -1,17 +1,24 @@
|
|
|
1
|
-
import { cowAppDataLatestScheme } from '@cowprotocol/cow-sdk';
|
|
2
|
-
import { Eip2612PermitUtils } from '@1inch/permit-signed-approvals-utils';
|
|
3
1
|
import { Address, PublicClient } from 'viem';
|
|
4
2
|
import { Config } from 'wagmi';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
version?: string | undefined;
|
|
3
|
+
import { cowAppDataLatestScheme } from '@cowprotocol/cow-sdk';
|
|
4
|
+
import { Eip2612PermitUtils } from '@1inch/permit-signed-approvals-utils';
|
|
5
|
+
export type BuildDaiLikePermitCallDataParams = BasePermitCallDataParams & {
|
|
6
|
+
callDataParams: Parameters<Eip2612PermitUtils['buildDaiLikePermitCallData']>;
|
|
10
7
|
};
|
|
11
|
-
type
|
|
12
|
-
|
|
13
|
-
name: string | undefined;
|
|
8
|
+
export type BuildEip2612PermitCallDataParams = BasePermitCallDataParams & {
|
|
9
|
+
callDataParams: Parameters<Eip2612PermitUtils['buildPermitCallData']>;
|
|
14
10
|
};
|
|
11
|
+
export type GetTokenPermitInfoParams = {
|
|
12
|
+
chainId: number;
|
|
13
|
+
config: Config;
|
|
14
|
+
publicClient: PublicClient;
|
|
15
|
+
spender: string;
|
|
16
|
+
tokenAddress: Address;
|
|
17
|
+
amount?: bigint;
|
|
18
|
+
minGasLimit?: bigint | undefined;
|
|
19
|
+
};
|
|
20
|
+
export type GetTokenPermitIntoResult = PermitInfo | FailedToIdentify;
|
|
21
|
+
export type PermitHookData = cowAppDataLatestScheme.CoWHook;
|
|
15
22
|
export type PermitHookParams = {
|
|
16
23
|
chainId: number;
|
|
17
24
|
config: Config;
|
|
@@ -23,27 +30,20 @@ export type PermitHookParams = {
|
|
|
23
30
|
amount?: bigint;
|
|
24
31
|
nonce?: number;
|
|
25
32
|
};
|
|
26
|
-
export type
|
|
27
|
-
type
|
|
28
|
-
|
|
33
|
+
export type PermitInfo = {
|
|
34
|
+
type: PermitType;
|
|
35
|
+
name?: string;
|
|
36
|
+
version?: string | undefined;
|
|
29
37
|
};
|
|
30
|
-
export type
|
|
38
|
+
export type PermitType = 'dai-like' | 'eip-2612' | 'unsupported';
|
|
31
39
|
type BasePermitCallDataParams = {
|
|
32
40
|
eip2612Utils: Eip2612PermitUtils;
|
|
33
41
|
};
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
};
|
|
37
|
-
export type BuildDaiLikePermitCallDataParams = BasePermitCallDataParams & {
|
|
38
|
-
callDataParams: Parameters<Eip2612PermitUtils['buildDaiLikePermitCallData']>;
|
|
42
|
+
type FailedToIdentify = {
|
|
43
|
+
error: string;
|
|
39
44
|
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
publicClient: PublicClient;
|
|
44
|
-
spender: string;
|
|
45
|
-
tokenAddress: Address;
|
|
46
|
-
amount?: bigint;
|
|
47
|
-
minGasLimit?: bigint | undefined;
|
|
45
|
+
type TokenInfo = {
|
|
46
|
+
address: Address;
|
|
47
|
+
name: string | undefined;
|
|
48
48
|
};
|
|
49
49
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { AbiInput, AbiItem, EIP712TypedData, ProviderConnector } from '@1inch/permit-signed-approvals-utils';
|
|
2
1
|
import { Address, WalletClient, PublicClient, Hex } from 'viem';
|
|
2
|
+
import { AbiInput, AbiItem, EIP712TypedData, ProviderConnector } from '@1inch/permit-signed-approvals-utils';
|
|
3
3
|
export declare class PermitProviderConnector implements ProviderConnector {
|
|
4
4
|
private publicClient;
|
|
5
5
|
private walletClient;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BuildDaiLikePermitCallDataParams, BuildEip2612PermitCallDataParams } from '../types';
|
|
2
1
|
import { Hex } from 'viem';
|
|
3
|
-
|
|
2
|
+
import { BuildDaiLikePermitCallDataParams, BuildEip2612PermitCallDataParams } from '../types';
|
|
4
3
|
export declare function buildDaiLikePermitCallData({ eip2612Utils, callDataParams, }: BuildDaiLikePermitCallDataParams): Promise<Hex>;
|
|
4
|
+
export declare function buildEip2612PermitCallData({ eip2612Utils, callDataParams, }: BuildEip2612PermitCallDataParams): Promise<Hex>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Address } from 'viem';
|
|
2
2
|
import { Config } from 'wagmi';
|
|
3
|
-
export
|
|
3
|
+
export type Eip712Domain = Omit<Eip5267Return, 'fields' | 'extensions'>;
|
|
4
4
|
type Eip5267Return = {
|
|
5
5
|
fields: string;
|
|
6
6
|
name?: string;
|
|
@@ -10,5 +10,5 @@ type Eip5267Return = {
|
|
|
10
10
|
salt?: string;
|
|
11
11
|
extensions?: string[];
|
|
12
12
|
};
|
|
13
|
-
export
|
|
13
|
+
export declare function getEip712Domain(tokenAddress: Address, chainId: number, config: Config): Promise<Eip712Domain>;
|
|
14
14
|
export {};
|