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