@cowprotocol/permit-utils 0.1.1-RC.0 → 0.1.1-RC.1

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.
Files changed (4) hide show
  1. package/const.d.ts +1 -2
  2. package/index.cjs +1 -1
  3. package/index.js +140 -141
  4. package/package.json +1 -1
package/const.d.ts CHANGED
@@ -1,7 +1,6 @@
1
- import { SupportedChainId } from '@cowprotocol/cow-sdk';
2
1
  import { Wallet } from '@ethersproject/wallet';
3
2
  export declare const PERMIT_SIGNER: Wallet;
4
- export declare const PERMIT_GAS_LIMIT_MIN: Record<SupportedChainId, number>;
3
+ export declare const PERMIT_GAS_LIMIT_MIN: Record<number, number>;
5
4
  export declare const DEFAULT_PERMIT_GAS_LIMIT = "80000";
6
5
  export declare const DEFAULT_PERMIT_VALUE: string;
7
6
  export declare const DEFAULT_PERMIT_DURATION: number;
package/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const I=require("@cowprotocol/cow-sdk"),O=require("@ethersproject/constants"),F=require("@ethersproject/wallet"),p=require("@1inch/permit-signed-approvals-utils"),b=require("@ethersproject/abi"),x=require("@ethersproject/bignumber"),G=require("@ethersproject/contracts"),v=require("@ethersproject/address"),q="0x4dae303b820e9878cafeb0f84edcc015e8a81b1bff510e824e4fc27544e458dd",P=new F.Wallet(q),T=55e3,K={[I.SupportedChainId.MAINNET]:T,[I.SupportedChainId.GNOSIS_CHAIN]:T,[I.SupportedChainId.SEPOLIA]:T,[I.SupportedChainId.GOERLI]:36e3},E="80000",L=O.MaxUint256.toString(),V=157788e6,B=new Set(["0x6b175474e89094c44da98b954eedeac495271d0f"]);async function j(r,t,e,a,n,o,{version:c,type:s,name:d}){const l=d||n;if(s==="unsupported")return!1;if(!l)throw new Error(`No token name for ${a}`);const m={chainId:t,tokenName:l,tokenAddress:a,callData:o,version:c};let u;if(o.startsWith(p.EIP_2612_PERMIT_SELECTOR)?u=e.recoverPermitOwnerFromCallData({...m,callData:o.replace(p.EIP_2612_PERMIT_SELECTOR,"0x")}):o.startsWith(p.DAI_PERMIT_SELECTOR)&&(u=e.recoverDaiLikePermitOwnerFromCallData({...m,callData:o.replace(p.DAI_PERMIT_SELECTOR,"0x")})),!!u)try{return(await u).toLowerCase()===r.toLowerCase()}catch(i){return console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${r} with callData ${o}`,i),!1}}async function R({eip2162Utils:r,callDataParams:t}){const[e,a,n,...o]=t;return(await r.buildPermitCallData(e,a,n,...o)).replace("0x",p.EIP_2612_PERMIT_SELECTOR)}async function $({eip2162Utils:r,callDataParams:t}){return(await r.buildDaiLikePermitCallData(...t)).replace("0x",p.DAI_PERMIT_SELECTOR)}function y(){return Math.ceil((Date.now()+V)/1e3)}function M(r){return!!r&&r.type!=="unsupported"}const g={};async function W(r){const t=z(r),e=g[t];if(e)try{return await e}catch(n){console.debug("[generatePermitHookWith] cached request failed",n),delete g[t]}const a=Q(r).then(n=>(delete g[t],n));return g[t]=a,a}async function Q(r){const{inputToken:t,spender:e,chainId:a,permitInfo:n,provider:o,account:c,eip2162Utils:s,nonce:d}=r,l=t.address,m=n.name||t.name;if(!M(n))throw new Error(`Trying to generate permit hook for unsupported token: ${l}`);if(!m)throw new Error(`No token name for token: ${l}`);const u=c||P.address,i=d===void 0?await s.getTokenNonce(l,u):d,f=y(),k=L,C=n.type==="eip-2612"?await R({eip2162Utils:s,callDataParams:[{owner:u,spender:e,value:k,nonce:i,deadline:f},a,m,l,n.version]}):await $({eip2162Utils:s,callDataParams:[{holder:u,spender:e,allowed:!0,value:k,nonce:i,expiry:f},a,m,l,n.version]}),H=await Y(C,u,l,o,!!c);return{target:l,callData:C,gasLimit:H}}async function Y(r,t,e,a,n){try{const o=await a.estimateGas({data:r,from:t,to:e}),c=n?o:o.add(o.div(10));return c.gt(E)?c.toString():E}catch(o){return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default",o),E}}function z(r){const{inputToken:t,chainId:e,account:a}=r;return`${t.address.toLowerCase()}-${e}${a?`-${a.toLowerCase()}`:""}`}function w(r,t,e){return new G.Contract(r,t,e)}class J{constructor(t,e){this.provider=t,this.walletSigner=e}contractEncodeABI(t,e,a,n){return w(e||"",t,this.provider).interface.encodeFunctionData(a,n)}signTypedData(t,e,a){const n=Object.keys(e.types).reduce((c,s)=>(s!=="EIP712Domain"&&(c[s]=e.types[s]),c),{});return(this.walletSigner||this.provider.getSigner())._signTypedData(e.domain,n,e.message)}ethCall(t,e){return this.provider.call({to:t,data:e})}decodeABIParameter(t,e){return b.defaultAbiCoder.decode([t],e)[0]}decodeABIParameters(t,e){const a=b.defaultAbiCoder.decode(t,e);if(a&&typeof a=="object"){const n={};return Object.keys(a).forEach(o=>{const c=a[o];x.BigNumber.isBigNumber(c)?n[o]=c.toHexString():n[o]=c}),n}return a}}const _=new Map,A=new Map;function N(r,t,e){const a=_.get(r);if(!e&&a)return a;const n=`${r}-${e}`,o=A.get(n);if(o)return o;const c=new J(t,e?void 0:P),s=new p.Eip2612PermitUtils(c);return e?(console.log(`[getPermitUtilsInstance] Set cached provider utils for chain ${r}-${e}`,s),A.set(n,s)):(console.log(`[getPermitUtilsInstance] Set cached chain utils for chain ${r}`,s),_.set(r,s)),s}const U=[{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 X(r,t,e){const a=v.getAddress(r),o=await w(a,U,e).callStatic.eip712Domain();return Z(o,t,r)}async function Z(r,t,e){const{extensions:a,...n}=r;if(a.length>0)throw Error("Extensions not implemented");if(n.chainId&&t!==parseInt(n.chainId,16))throw Error(`ChainId mismatch. Received: '${n.chainId}', expected: '${t}'`);if(n.verifyingContract&&e.toLowerCase()!==n.verifyingContract.toLowerCase())throw Error(`Address mismatch. Received: '${n.verifyingContract}', expected: '${e}'`);return te(n)}const ee=["name","version","chainId","verifyingContract","salt"];function te(r){const t=parseInt(r.fields,16);return ee.reduce((e,a,n)=>(t&1<<n&&(e[a]=r[a]),e),{})}async function ne(r,t,e){const a=v.getAddress(r);return w(a,U,e).callStatic.name()}const re={value:L,nonce:0,deadline:y()},ae={allowed:!0,nonce:0,expiry:y()},S={},h={type:"unsupported"};async function oe(r){const{tokenAddress:t,chainId:e}=r,a=`${e}-${t.toLowerCase()}`,n=S[a];if(n)return n;const o=ie(r);return S[a]=o,o}async function ie(r){const{spender:t,tokenAddress:e,chainId:a,provider:n}=r,o=N(a,n),c=P.address;let s;try{s=await X(e,a,n)}catch(i){console.debug(`[checkTokenIsPermittable] Couldn't fetch eip712domain for token ${e}`,i)}let d=s==null?void 0:s.name;try{d||(d=await ne(e,a,n))}catch(i){if(/ETIMEDOUT/.test(i)&&!d)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 ${e}, using provided '${d}'`,i)}if(!d)return{error:`Token name could not be determined for ${e}`};let l;try{l=await o.getTokenNonce(e,c)}catch(i){return i==="nonce not supported"||i.message==="nonce is NaN"?(console.debug(`[checkTokenIsPermittable] Not a permittable token ${e} - ${d}`,(i==null?void 0:i.message)||i),{...h,name:d}):(console.debug(`[checkTokenIsPermittable] Failed to get nonce for ${e} - ${d}`,i),{error:i.message||i.toString()})}let m=s==null?void 0:s.version;if(!B.has(e)&&m===void 0)try{m=await o.getTokenVersion(e)}catch(i){console.debug(`[checkTokenIsPermittable] Failed to get version for ${e} - ${d}`,i)}const u={chainId:a,eip2612PermitUtils:o,nonce:l,spender:t,tokenAddress:e,tokenName:d,walletAddress:c,version:m};try{return await D({...u,type:"eip-2612",provider:n})}catch(i){try{return await ce(e,o)?await D({...u,type:"dai-like",provider:n}):/invalid signature/.test(i)||(i==null?void 0:i.code)==="UNPREDICTABLE_GAS_LIMIT"?(console.debug(`[checkTokenIsPermittable] Token ${e} - ${d} might be permittable, but it's not supported for now. Reason:`,i==null?void 0:i.reason),{...h,name:d}):(console.debug(`[checkTokenIsPermittable] Failed to estimate eip-2612 permit for ${e} - ${d}`,i),{error:i.message||i.toString()})}catch(f){return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${e} - ${d}`,f),{error:f.message||f.toString()}}}}async function D(r){const{provider:t,chainId:e,walletAddress:a,tokenAddress:n,tokenName:o,type:c,version:s}=r,l=await(c==="eip-2612"?se:de)(r);return l?(await t.estimateGas({data:l,from:a,to:n})).toNumber()>K[e]?{type:c,version:s,name:o}:{...h,name:o}:{...h,name:o}}async function se(r){const{eip2612PermitUtils:t,walletAddress:e,spender:a,nonce:n,chainId:o,tokenName:c,tokenAddress:s,version:d}=r;return R({eip2162Utils:t,callDataParams:[{...re,owner:e,spender:a,nonce:n},+o,c,s,d]})}async function ce(r,t){return await t.getPermitTypeHash(r)===p.DAI_LIKE_PERMIT_TYPEHASH}async function de(r){const{eip2612PermitUtils:t,tokenAddress:e,walletAddress:a,spender:n,nonce:o,chainId:c,tokenName:s,version:d}=r;return await t.getPermitTypeHash(e)===p.DAI_LIKE_PERMIT_TYPEHASH?$({eip2162Utils:t,callDataParams:[{...ae,holder:a,spender:n,nonce:o},c,s,e,d]}):!1}exports.PERMIT_SIGNER=P;exports.checkIsCallDataAValidPermit=j;exports.generatePermitHook=W;exports.getPermitUtilsInstance=N;exports.getTokenPermitInfo=oe;exports.isSupportedPermitInfo=M;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const H=require("@ethersproject/constants"),O=require("@ethersproject/wallet"),p=require("@1inch/permit-signed-approvals-utils"),C=require("@ethersproject/abi"),F=require("@ethersproject/bignumber"),x=require("@ethersproject/contracts"),v=require("@ethersproject/address"),G="0x4dae303b820e9878cafeb0f84edcc015e8a81b1bff510e824e4fc27544e458dd",I=new O.Wallet(G),h=55e3,q={1:h,100:h,11155111:h,5:36e3},T="80000",S=H.MaxUint256.toString(),K=157788e6,V=new Set(["0x6b175474e89094c44da98b954eedeac495271d0f"]);async function B(r,t,e,a,n,o,{version:c,type:s,name:l}){const d=l||n;if(s==="unsupported")return!1;if(!d)throw new Error(`No token name for ${a}`);const m={chainId:t,tokenName:d,tokenAddress:a,callData:o,version:c};let u;if(o.startsWith(p.EIP_2612_PERMIT_SELECTOR)?u=e.recoverPermitOwnerFromCallData({...m,callData:o.replace(p.EIP_2612_PERMIT_SELECTOR,"0x")}):o.startsWith(p.DAI_PERMIT_SELECTOR)&&(u=e.recoverDaiLikePermitOwnerFromCallData({...m,callData:o.replace(p.DAI_PERMIT_SELECTOR,"0x")})),!!u)try{return(await u).toLowerCase()===r.toLowerCase()}catch(i){return console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${r} with callData ${o}`,i),!1}}async function L({eip2162Utils:r,callDataParams:t}){const[e,a,n,...o]=t;return(await r.buildPermitCallData(e,a,n,...o)).replace("0x",p.EIP_2612_PERMIT_SELECTOR)}async function R({eip2162Utils:r,callDataParams:t}){return(await r.buildDaiLikePermitCallData(...t)).replace("0x",p.DAI_PERMIT_SELECTOR)}function y(){return Math.ceil((Date.now()+K)/1e3)}function $(r){return!!r&&r.type!=="unsupported"}const g={};async function j(r){const t=Y(r),e=g[t];if(e)try{return await e}catch(n){console.debug("[generatePermitHookWith] cached request failed",n),delete g[t]}const a=W(r).then(n=>(delete g[t],n));return g[t]=a,a}async function W(r){const{inputToken:t,spender:e,chainId:a,permitInfo:n,provider:o,account:c,eip2162Utils:s,nonce:l}=r,d=t.address,m=n.name||t.name;if(!$(n))throw new Error(`Trying to generate permit hook for unsupported token: ${d}`);if(!m)throw new Error(`No token name for token: ${d}`);const u=c||I.address,i=l===void 0?await s.getTokenNonce(d,u):l,f=y(),w=S,k=n.type==="eip-2612"?await L({eip2162Utils:s,callDataParams:[{owner:u,spender:e,value:w,nonce:i,deadline:f},a,m,d,n.version]}):await R({eip2162Utils:s,callDataParams:[{holder:u,spender:e,allowed:!0,value:w,nonce:i,expiry:f},a,m,d,n.version]}),U=await Q(k,u,d,o,!!c);return{target:d,callData:k,gasLimit:U}}async function Q(r,t,e,a,n){try{const o=await a.estimateGas({data:r,from:t,to:e}),c=n?o:o.add(o.div(10));return c.gt(T)?c.toString():T}catch(o){return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default",o),T}}function Y(r){const{inputToken:t,chainId:e,account:a}=r;return`${t.address.toLowerCase()}-${e}${a?`-${a.toLowerCase()}`:""}`}function E(r,t,e){return new x.Contract(r,t,e)}class z{constructor(t,e){this.provider=t,this.walletSigner=e}contractEncodeABI(t,e,a,n){return E(e||"",t,this.provider).interface.encodeFunctionData(a,n)}signTypedData(t,e,a){const n=Object.keys(e.types).reduce((c,s)=>(s!=="EIP712Domain"&&(c[s]=e.types[s]),c),{});return(this.walletSigner||this.provider.getSigner())._signTypedData(e.domain,n,e.message)}ethCall(t,e){return this.provider.call({to:t,data:e})}decodeABIParameter(t,e){return C.defaultAbiCoder.decode([t],e)[0]}decodeABIParameters(t,e){const a=C.defaultAbiCoder.decode(t,e);if(a&&typeof a=="object"){const n={};return Object.keys(a).forEach(o=>{const c=a[o];F.BigNumber.isBigNumber(c)?n[o]=c.toHexString():n[o]=c}),n}return a}}const b=new Map,_=new Map;function M(r,t,e){const a=b.get(r);if(!e&&a)return a;const n=`${r}-${e}`,o=_.get(n);if(o)return o;const c=new z(t,e?void 0:I),s=new p.Eip2612PermitUtils(c);return e?(console.log(`[getPermitUtilsInstance] Set cached provider utils for chain ${r}-${e}`,s),_.set(n,s)):(console.log(`[getPermitUtilsInstance] Set cached chain utils for chain ${r}`,s),b.set(r,s)),s}const N=[{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 J(r,t,e){const a=v.getAddress(r),o=await E(a,N,e).callStatic.eip712Domain();return X(o,t,r)}async function X(r,t,e){const{extensions:a,...n}=r;if(a.length>0)throw Error("Extensions not implemented");if(n.chainId&&t!==parseInt(n.chainId,16))throw Error(`ChainId mismatch. Received: '${n.chainId}', expected: '${t}'`);if(n.verifyingContract&&e.toLowerCase()!==n.verifyingContract.toLowerCase())throw Error(`Address mismatch. Received: '${n.verifyingContract}', expected: '${e}'`);return ee(n)}const Z=["name","version","chainId","verifyingContract","salt"];function ee(r){const t=parseInt(r.fields,16);return Z.reduce((e,a,n)=>(t&1<<n&&(e[a]=r[a]),e),{})}async function te(r,t,e){const a=v.getAddress(r);return E(a,N,e).callStatic.name()}const ne={value:S,nonce:0,deadline:y()},re={allowed:!0,nonce:0,expiry:y()},A={},P={type:"unsupported"};async function ae(r){const{tokenAddress:t,chainId:e}=r,a=`${e}-${t.toLowerCase()}`,n=A[a];if(n)return n;const o=oe(r);return A[a]=o,o}async function oe(r){const{spender:t,tokenAddress:e,chainId:a,provider:n}=r,o=M(a,n),c=I.address;let s;try{s=await J(e,a,n)}catch(i){console.debug(`[checkTokenIsPermittable] Couldn't fetch eip712domain for token ${e}`,i)}let l=s==null?void 0:s.name;try{l||(l=await te(e,a,n))}catch(i){if(/ETIMEDOUT/.test(i)&&!l)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 ${e}, using provided '${l}'`,i)}if(!l)return{error:`Token name could not be determined for ${e}`};let d;try{d=await o.getTokenNonce(e,c)}catch(i){return i==="nonce not supported"||i.message==="nonce is NaN"?(console.debug(`[checkTokenIsPermittable] Not a permittable token ${e} - ${l}`,(i==null?void 0:i.message)||i),{...P,name:l}):(console.debug(`[checkTokenIsPermittable] Failed to get nonce for ${e} - ${l}`,i),{error:i.message||i.toString()})}let m=s==null?void 0:s.version;if(!V.has(e)&&m===void 0)try{m=await o.getTokenVersion(e)}catch(i){console.debug(`[checkTokenIsPermittable] Failed to get version for ${e} - ${l}`,i)}const u={chainId:a,eip2612PermitUtils:o,nonce:d,spender:t,tokenAddress:e,tokenName:l,walletAddress:c,version:m};try{return await D({...u,type:"eip-2612",provider:n})}catch(i){try{return await se(e,o)?await D({...u,type:"dai-like",provider:n}):/invalid signature/.test(i)||(i==null?void 0:i.code)==="UNPREDICTABLE_GAS_LIMIT"?(console.debug(`[checkTokenIsPermittable] Token ${e} - ${l} might be permittable, but it's not supported for now. Reason:`,i==null?void 0:i.reason),{...P,name:l}):(console.debug(`[checkTokenIsPermittable] Failed to estimate eip-2612 permit for ${e} - ${l}`,i),{error:i.message||i.toString()})}catch(f){return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${e} - ${l}`,f),{error:f.message||f.toString()}}}}async function D(r){const{provider:t,chainId:e,walletAddress:a,tokenAddress:n,tokenName:o,type:c,version:s}=r,d=await(c==="eip-2612"?ie:ce)(r);return d?(await t.estimateGas({data:d,from:a,to:n})).toNumber()>q[e]?{type:c,version:s,name:o}:{...P,name:o}:{...P,name:o}}async function ie(r){const{eip2612PermitUtils:t,walletAddress:e,spender:a,nonce:n,chainId:o,tokenName:c,tokenAddress:s,version:l}=r;return L({eip2162Utils:t,callDataParams:[{...ne,owner:e,spender:a,nonce:n},+o,c,s,l]})}async function se(r,t){return await t.getPermitTypeHash(r)===p.DAI_LIKE_PERMIT_TYPEHASH}async function ce(r){const{eip2612PermitUtils:t,tokenAddress:e,walletAddress:a,spender:n,nonce:o,chainId:c,tokenName:s,version:l}=r;return await t.getPermitTypeHash(e)===p.DAI_LIKE_PERMIT_TYPEHASH?R({eip2162Utils:t,callDataParams:[{...re,holder:a,spender:n,nonce:o},c,s,e,l]}):!1}exports.PERMIT_SIGNER=I;exports.checkIsCallDataAValidPermit=B;exports.generatePermitHook=j;exports.getPermitUtilsInstance=M;exports.getTokenPermitInfo=ae;exports.isSupportedPermitInfo=$;
package/index.js CHANGED
@@ -1,18 +1,17 @@
1
- import { SupportedChainId as f } from "@cowprotocol/cow-sdk";
2
- import { MaxUint256 as O } from "@ethersproject/constants";
1
+ import { MaxUint256 as H } from "@ethersproject/constants";
3
2
  import { Wallet as F } from "@ethersproject/wallet";
4
- import { EIP_2612_PERMIT_SELECTOR as P, DAI_PERMIT_SELECTOR as T, Eip2612PermitUtils as x, DAI_LIKE_PERMIT_TYPEHASH as L } from "@1inch/permit-signed-approvals-utils";
5
- import { defaultAbiCoder as v } from "@ethersproject/abi";
6
- import { BigNumber as G } from "@ethersproject/bignumber";
7
- import { Contract as K } from "@ethersproject/contracts";
3
+ import { EIP_2612_PERMIT_SELECTOR as P, DAI_PERMIT_SELECTOR as I, Eip2612PermitUtils as x, DAI_LIKE_PERMIT_TYPEHASH as L } from "@1inch/permit-signed-approvals-utils";
4
+ import { defaultAbiCoder as b } from "@ethersproject/abi";
5
+ import { BigNumber as O } from "@ethersproject/bignumber";
6
+ import { Contract as G } from "@ethersproject/contracts";
8
7
  import { getAddress as $ } from "@ethersproject/address";
9
- const B = "0x4dae303b820e9878cafeb0f84edcc015e8a81b1bff510e824e4fc27544e458dd", w = new F(B), y = 55e3, V = {
10
- [f.MAINNET]: y,
11
- [f.GNOSIS_CHAIN]: y,
12
- [f.SEPOLIA]: y,
13
- [f.GOERLI]: 36e3
14
- }, I = "80000", N = O.toString(), q = 157788e6, W = /* @__PURE__ */ new Set(["0x6b175474e89094c44da98b954eedeac495271d0f"]);
15
- async function Ie(r, t, e, o, n, a, {
8
+ const K = "0x4dae303b820e9878cafeb0f84edcc015e8a81b1bff510e824e4fc27544e458dd", T = new F(K), h = 55e3, B = {
9
+ 1: h,
10
+ 100: h,
11
+ 11155111: h,
12
+ 5: 36e3
13
+ }, y = "80000", S = H.toString(), V = 157788e6, q = /* @__PURE__ */ new Set(["0x6b175474e89094c44da98b954eedeac495271d0f"]);
14
+ async function he(r, t, e, a, n, o, {
16
15
  version: c,
17
16
  type: s,
18
17
  name: l
@@ -21,135 +20,135 @@ async function Ie(r, t, e, o, n, a, {
21
20
  if (s === "unsupported")
22
21
  return !1;
23
22
  if (!d)
24
- throw new Error(`No token name for ${o}`);
23
+ throw new Error(`No token name for ${a}`);
25
24
  const u = {
26
25
  chainId: t,
27
26
  tokenName: d,
28
- tokenAddress: o,
29
- callData: a,
27
+ tokenAddress: a,
28
+ callData: o,
30
29
  version: c
31
30
  };
32
31
  let m;
33
- if (a.startsWith(P) ? m = e.recoverPermitOwnerFromCallData({
32
+ if (o.startsWith(P) ? m = e.recoverPermitOwnerFromCallData({
34
33
  ...u,
35
34
  // I don't know why this was removed, ok?
36
35
  // We added it back on buildPermitCallData.ts
37
36
  // But it looks like this is needed 🤷
38
37
  // Check the test for this method https://github.com/1inch/permit-signed-approvals-utils/blob/master/src/eip-2612-permit.test.ts#L85-L106
39
- callData: a.replace(P, "0x")
40
- }) : a.startsWith(T) && (m = e.recoverDaiLikePermitOwnerFromCallData({
38
+ callData: o.replace(P, "0x")
39
+ }) : o.startsWith(I) && (m = e.recoverDaiLikePermitOwnerFromCallData({
41
40
  ...u,
42
- callData: a.replace(T, "0x")
41
+ callData: o.replace(I, "0x")
43
42
  })), !!m)
44
43
  try {
45
44
  return (await m).toLowerCase() === r.toLowerCase();
46
45
  } catch (i) {
47
- return console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${r} with callData ${a}`, i), !1;
46
+ return console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${r} with callData ${o}`, i), !1;
48
47
  }
49
48
  }
50
49
  async function R({
51
50
  eip2162Utils: r,
52
51
  callDataParams: t
53
52
  }) {
54
- const [e, o, n, ...a] = t;
55
- return (await r.buildPermitCallData(e, o, n, ...a)).replace("0x", P);
53
+ const [e, a, n, ...o] = t;
54
+ return (await r.buildPermitCallData(e, a, n, ...o)).replace("0x", P);
56
55
  }
57
- async function M({
56
+ async function N({
58
57
  eip2162Utils: r,
59
58
  callDataParams: t
60
59
  }) {
61
- return (await r.buildDaiLikePermitCallData(...t)).replace("0x", T);
60
+ return (await r.buildDaiLikePermitCallData(...t)).replace("0x", I);
62
61
  }
63
- function k() {
64
- return Math.ceil((Date.now() + q) / 1e3);
62
+ function w() {
63
+ return Math.ceil((Date.now() + V) / 1e3);
65
64
  }
66
- function j(r) {
65
+ function W(r) {
67
66
  return !!r && r.type !== "unsupported";
68
67
  }
69
- const h = {};
70
- async function Pe(r) {
71
- const t = z(r), e = h[t];
68
+ const f = {};
69
+ async function ye(r) {
70
+ const t = Y(r), e = f[t];
72
71
  if (e)
73
72
  try {
74
73
  return await e;
75
74
  } catch (n) {
76
- console.debug("[generatePermitHookWith] cached request failed", n), delete h[t];
75
+ console.debug("[generatePermitHookWith] cached request failed", n), delete f[t];
77
76
  }
78
- const o = Q(r).then((n) => (delete h[t], n));
79
- return h[t] = o, o;
77
+ const a = j(r).then((n) => (delete f[t], n));
78
+ return f[t] = a, a;
80
79
  }
81
- async function Q(r) {
80
+ async function j(r) {
82
81
  const {
83
82
  inputToken: t,
84
83
  spender: e,
85
- chainId: o,
84
+ chainId: a,
86
85
  permitInfo: n,
87
- provider: a,
86
+ provider: o,
88
87
  account: c,
89
88
  eip2162Utils: s,
90
89
  nonce: l
91
90
  } = r, d = t.address, u = n.name || t.name;
92
- if (!j(n))
91
+ if (!W(n))
93
92
  throw new Error(`Trying to generate permit hook for unsupported token: ${d}`);
94
93
  if (!u)
95
94
  throw new Error(`No token name for token: ${d}`);
96
- const m = c || w.address, i = l === void 0 ? await s.getTokenNonce(d, m) : l, p = k(), C = N, b = n.type === "eip-2612" ? await R({
95
+ const m = c || T.address, i = l === void 0 ? await s.getTokenNonce(d, m) : l, p = w(), E = S, C = n.type === "eip-2612" ? await R({
97
96
  eip2162Utils: s,
98
97
  callDataParams: [{
99
98
  owner: m,
100
99
  spender: e,
101
- value: C,
100
+ value: E,
102
101
  nonce: i,
103
102
  deadline: p
104
- }, o, u, d, n.version]
105
- }) : await M({
103
+ }, a, u, d, n.version]
104
+ }) : await N({
106
105
  eip2162Utils: s,
107
106
  callDataParams: [{
108
107
  holder: m,
109
108
  spender: e,
110
109
  allowed: !0,
111
- value: C,
110
+ value: E,
112
111
  nonce: i,
113
112
  expiry: p
114
- }, o, u, d, n.version]
115
- }), H = await Y(b, m, d, a, !!c);
113
+ }, a, u, d, n.version]
114
+ }), U = await Q(C, m, d, o, !!c);
116
115
  return {
117
116
  target: d,
118
- callData: b,
119
- gasLimit: H
117
+ callData: C,
118
+ gasLimit: U
120
119
  };
121
120
  }
122
- async function Y(r, t, e, o, n) {
121
+ async function Q(r, t, e, a, n) {
123
122
  try {
124
- const a = await o.estimateGas({
123
+ const o = await a.estimateGas({
125
124
  data: r,
126
125
  from: t,
127
126
  to: e
128
- }), c = n ? a : a.add(a.div(10));
129
- return c.gt(I) ? c.toString() : I;
130
- } catch (a) {
131
- return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default", a), I;
127
+ }), c = n ? o : o.add(o.div(10));
128
+ return c.gt(y) ? c.toString() : y;
129
+ } catch (o) {
130
+ return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default", o), y;
132
131
  }
133
132
  }
134
- function z(r) {
133
+ function Y(r) {
135
134
  const {
136
135
  inputToken: t,
137
136
  chainId: e,
138
- account: o
137
+ account: a
139
138
  } = r;
140
- return `${t.address.toLowerCase()}-${e}${o ? `-${o.toLowerCase()}` : ""}`;
139
+ return `${t.address.toLowerCase()}-${e}${a ? `-${a.toLowerCase()}` : ""}`;
141
140
  }
142
- function E(r, t, e) {
143
- return new K(r, t, e);
141
+ function k(r, t, e) {
142
+ return new G(r, t, e);
144
143
  }
145
- class J {
144
+ class z {
146
145
  constructor(t, e) {
147
146
  this.provider = t, this.walletSigner = e;
148
147
  }
149
- contractEncodeABI(t, e, o, n) {
150
- return E(e || "", t, this.provider).interface.encodeFunctionData(o, n);
148
+ contractEncodeABI(t, e, a, n) {
149
+ return k(e || "", t, this.provider).interface.encodeFunctionData(a, n);
151
150
  }
152
- signTypedData(t, e, o) {
151
+ signTypedData(t, e, a) {
153
152
  const n = Object.keys(e.types).reduce((c, s) => (s !== "EIP712Domain" && (c[s] = e.types[s]), c), {});
154
153
  return (this.walletSigner || this.provider.getSigner())._signTypedData(e.domain, n, e.message);
155
154
  }
@@ -160,32 +159,32 @@ class J {
160
159
  });
161
160
  }
162
161
  decodeABIParameter(t, e) {
163
- return v.decode([t], e)[0];
162
+ return b.decode([t], e)[0];
164
163
  }
165
164
  decodeABIParameters(t, e) {
166
- const o = v.decode(t, e);
167
- if (o && typeof o == "object") {
165
+ const a = b.decode(t, e);
166
+ if (a && typeof a == "object") {
168
167
  const n = {};
169
- return Object.keys(o).forEach((a) => {
170
- const c = o[a];
171
- G.isBigNumber(c) ? n[a] = c.toHexString() : n[a] = c;
168
+ return Object.keys(a).forEach((o) => {
169
+ const c = a[o];
170
+ O.isBigNumber(c) ? n[o] = c.toHexString() : n[o] = c;
172
171
  }), n;
173
172
  }
174
- return o;
173
+ return a;
175
174
  }
176
175
  }
177
- const D = /* @__PURE__ */ new Map(), A = /* @__PURE__ */ new Map();
178
- function X(r, t, e) {
179
- const o = D.get(r);
180
- if (!e && o)
181
- return o;
182
- const n = `${r}-${e}`, a = A.get(n);
183
- if (a)
176
+ const v = /* @__PURE__ */ new Map(), D = /* @__PURE__ */ new Map();
177
+ function J(r, t, e) {
178
+ const a = v.get(r);
179
+ if (!e && a)
184
180
  return a;
185
- const c = new J(t, e ? void 0 : w), s = new x(c);
186
- return e ? (console.log(`[getPermitUtilsInstance] Set cached provider utils for chain ${r}-${e}`, s), A.set(n, s)) : (console.log(`[getPermitUtilsInstance] Set cached chain utils for chain ${r}`, s), D.set(r, s)), s;
181
+ const n = `${r}-${e}`, o = D.get(n);
182
+ if (o)
183
+ return o;
184
+ const c = new z(t, e ? void 0 : T), s = new x(c);
185
+ return e ? (console.log(`[getPermitUtilsInstance] Set cached provider utils for chain ${r}-${e}`, s), D.set(n, s)) : (console.log(`[getPermitUtilsInstance] Set cached chain utils for chain ${r}`, s), v.set(r, s)), s;
187
186
  }
188
- const U = [
187
+ const M = [
189
188
  {
190
189
  constant: !0,
191
190
  inputs: [],
@@ -244,69 +243,69 @@ const U = [
244
243
  type: "function"
245
244
  }
246
245
  ];
247
- async function Z(r, t, e) {
248
- const o = $(r), a = await E(o, U, e).callStatic.eip712Domain();
249
- return ee(a, t, r);
246
+ async function X(r, t, e) {
247
+ const a = $(r), o = await k(a, M, e).callStatic.eip712Domain();
248
+ return Z(o, t, r);
250
249
  }
251
- async function ee(r, t, e) {
250
+ async function Z(r, t, e) {
252
251
  const {
253
- extensions: o,
252
+ extensions: a,
254
253
  ...n
255
254
  } = r;
256
- if (o.length > 0)
255
+ if (a.length > 0)
257
256
  throw Error("Extensions not implemented");
258
257
  if (n.chainId && t !== parseInt(n.chainId, 16))
259
258
  throw Error(`ChainId mismatch. Received: '${n.chainId}', expected: '${t}'`);
260
259
  if (n.verifyingContract && e.toLowerCase() !== n.verifyingContract.toLowerCase())
261
260
  throw Error(`Address mismatch. Received: '${n.verifyingContract}', expected: '${e}'`);
262
- return ne(n);
261
+ return te(n);
263
262
  }
264
- const te = ["name", "version", "chainId", "verifyingContract", "salt"];
265
- function ne(r) {
263
+ const ee = ["name", "version", "chainId", "verifyingContract", "salt"];
264
+ function te(r) {
266
265
  const t = parseInt(r.fields, 16);
267
- return te.reduce((e, o, n) => (t & 1 << n && (e[o] = r[o]), e), {});
266
+ return ee.reduce((e, a, n) => (t & 1 << n && (e[a] = r[a]), e), {});
268
267
  }
269
- async function re(r, t, e) {
270
- const o = $(r);
271
- return E(o, U, e).callStatic.name();
268
+ async function ne(r, t, e) {
269
+ const a = $(r);
270
+ return k(a, M, e).callStatic.name();
272
271
  }
273
- const oe = {
274
- value: N,
272
+ const re = {
273
+ value: S,
275
274
  nonce: 0,
276
- deadline: k()
275
+ deadline: w()
277
276
  }, ae = {
278
277
  allowed: !0,
279
278
  nonce: 0,
280
- expiry: k()
281
- }, _ = {}, g = {
279
+ expiry: w()
280
+ }, A = {}, g = {
282
281
  type: "unsupported"
283
282
  };
284
- async function Te(r) {
283
+ async function Pe(r) {
285
284
  const {
286
285
  tokenAddress: t,
287
286
  chainId: e
288
- } = r, o = `${e}-${t.toLowerCase()}`, n = _[o];
287
+ } = r, a = `${e}-${t.toLowerCase()}`, n = A[a];
289
288
  if (n)
290
289
  return n;
291
- const a = ie(r);
292
- return _[o] = a, a;
290
+ const o = oe(r);
291
+ return A[a] = o, o;
293
292
  }
294
- async function ie(r) {
293
+ async function oe(r) {
295
294
  const {
296
295
  spender: t,
297
296
  tokenAddress: e,
298
- chainId: o,
297
+ chainId: a,
299
298
  provider: n
300
- } = r, a = X(o, n), c = w.address;
299
+ } = r, o = J(a, n), c = T.address;
301
300
  let s;
302
301
  try {
303
- s = await Z(e, o, n);
302
+ s = await X(e, a, n);
304
303
  } catch (i) {
305
304
  console.debug(`[checkTokenIsPermittable] Couldn't fetch eip712domain for token ${e}`, i);
306
305
  }
307
306
  let l = s == null ? void 0 : s.name;
308
307
  try {
309
- l || (l = await re(e, o, n));
308
+ l || (l = await ne(e, a, n));
310
309
  } catch (i) {
311
310
  if (/ETIMEDOUT/.test(i) && !l)
312
311
  return {
@@ -320,7 +319,7 @@ async function ie(r) {
320
319
  };
321
320
  let d;
322
321
  try {
323
- d = await a.getTokenNonce(e, c);
322
+ d = await o.getTokenNonce(e, c);
324
323
  } catch (i) {
325
324
  return i === "nonce not supported" || i.message === "nonce is NaN" ? (console.debug(`[checkTokenIsPermittable] Not a permittable token ${e} - ${l}`, (i == null ? void 0 : i.message) || i), {
326
325
  ...g,
@@ -330,15 +329,15 @@ async function ie(r) {
330
329
  });
331
330
  }
332
331
  let u = s == null ? void 0 : s.version;
333
- if (!W.has(e) && u === void 0)
332
+ if (!q.has(e) && u === void 0)
334
333
  try {
335
- u = await a.getTokenVersion(e);
334
+ u = await o.getTokenVersion(e);
336
335
  } catch (i) {
337
336
  console.debug(`[checkTokenIsPermittable] Failed to get version for ${e} - ${l}`, i);
338
337
  }
339
338
  const m = {
340
- chainId: o,
341
- eip2612PermitUtils: a,
339
+ chainId: a,
340
+ eip2612PermitUtils: o,
342
341
  nonce: d,
343
342
  spender: t,
344
343
  tokenAddress: e,
@@ -347,14 +346,14 @@ async function ie(r) {
347
346
  version: u
348
347
  };
349
348
  try {
350
- return await S({
349
+ return await _({
351
350
  ...m,
352
351
  type: "eip-2612",
353
352
  provider: n
354
353
  });
355
354
  } catch (i) {
356
355
  try {
357
- return await ce(e, a) ? await S({
356
+ return await se(e, o) ? await _({
358
357
  ...m,
359
358
  type: "dai-like",
360
359
  provider: n
@@ -371,39 +370,39 @@ async function ie(r) {
371
370
  }
372
371
  }
373
372
  }
374
- async function S(r) {
373
+ async function _(r) {
375
374
  const {
376
375
  provider: t,
377
376
  chainId: e,
378
- walletAddress: o,
377
+ walletAddress: a,
379
378
  tokenAddress: n,
380
- tokenName: a,
379
+ tokenName: o,
381
380
  type: c,
382
381
  version: s
383
- } = r, d = await (c === "eip-2612" ? se : le)(r);
382
+ } = r, d = await (c === "eip-2612" ? ie : ce)(r);
384
383
  return d ? (await t.estimateGas({
385
384
  data: d,
386
- from: o,
385
+ from: a,
387
386
  to: n
388
- })).toNumber() > V[e] ? {
387
+ })).toNumber() > B[e] ? {
389
388
  type: c,
390
389
  version: s,
391
- name: a
390
+ name: o
392
391
  } : {
393
392
  ...g,
394
- name: a
393
+ name: o
395
394
  } : {
396
395
  ...g,
397
- name: a
396
+ name: o
398
397
  };
399
398
  }
400
- async function se(r) {
399
+ async function ie(r) {
401
400
  const {
402
401
  eip2612PermitUtils: t,
403
402
  walletAddress: e,
404
- spender: o,
403
+ spender: a,
405
404
  nonce: n,
406
- chainId: a,
405
+ chainId: o,
407
406
  tokenName: c,
408
407
  tokenAddress: s,
409
408
  version: l
@@ -411,42 +410,42 @@ async function se(r) {
411
410
  return R({
412
411
  eip2162Utils: t,
413
412
  callDataParams: [{
414
- ...oe,
413
+ ...re,
415
414
  owner: e,
416
- spender: o,
415
+ spender: a,
417
416
  nonce: n
418
- }, +a, c, s, l]
417
+ }, +o, c, s, l]
419
418
  });
420
419
  }
421
- async function ce(r, t) {
420
+ async function se(r, t) {
422
421
  return await t.getPermitTypeHash(r) === L;
423
422
  }
424
- async function le(r) {
423
+ async function ce(r) {
425
424
  const {
426
425
  eip2612PermitUtils: t,
427
426
  tokenAddress: e,
428
- walletAddress: o,
427
+ walletAddress: a,
429
428
  spender: n,
430
- nonce: a,
429
+ nonce: o,
431
430
  chainId: c,
432
431
  tokenName: s,
433
432
  version: l
434
433
  } = r;
435
- return await t.getPermitTypeHash(e) === L ? M({
434
+ return await t.getPermitTypeHash(e) === L ? N({
436
435
  eip2162Utils: t,
437
436
  callDataParams: [{
438
437
  ...ae,
439
- holder: o,
438
+ holder: a,
440
439
  spender: n,
441
- nonce: a
440
+ nonce: o
442
441
  }, c, s, e, l]
443
442
  }) : !1;
444
443
  }
445
444
  export {
446
- w as PERMIT_SIGNER,
447
- Ie as checkIsCallDataAValidPermit,
448
- Pe as generatePermitHook,
449
- X as getPermitUtilsInstance,
450
- Te as getTokenPermitInfo,
451
- j as isSupportedPermitInfo
445
+ T as PERMIT_SIGNER,
446
+ he as checkIsCallDataAValidPermit,
447
+ ye as generatePermitHook,
448
+ J as getPermitUtilsInstance,
449
+ Pe as getTokenPermitInfo,
450
+ W as isSupportedPermitInfo
452
451
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cowprotocol/permit-utils",
3
- "version": "0.1.1-RC.0",
3
+ "version": "0.1.1-RC.1",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "ethers": "^5.7.2",