@cowprotocol/permit-utils 0.3.0-RC.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/const.d.ts +0 -1
- package/index.cjs +1 -1
- package/index.js +159 -158
- package/lib/checkIsCallDataAValidPermit.d.ts +1 -2
- package/lib/generatePermitHook.d.ts +0 -1
- package/lib/getPermitUtilsInstance.d.ts +1 -2
- package/lib/getTokenPermitInfo.d.ts +0 -1
- package/package.json +1 -1
- package/types.d.ts +2 -3
- package/utils/PermitProviderConnector.d.ts +2 -3
- package/utils/buildPermitCallData.d.ts +0 -1
- package/utils/getContract.d.ts +1 -2
- package/utils/getEip712Domain.d.ts +0 -1
- package/utils/getTokenName.d.ts +0 -1
- package/utils/getTokenPermitVersion.d.ts +0 -1
- package/utils/isSupportedPermitInfo.d.ts +0 -1
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ const permitInfo = await getTokenPermitInfo({
|
|
|
25
25
|
```typescript
|
|
26
26
|
import { getPermitUtilsInstance } from "@cowprotocol/permit-utils"
|
|
27
27
|
|
|
28
|
-
// Using
|
|
28
|
+
// Using a static account defined in the library
|
|
29
29
|
const staticEip2612PermitUtils = getPermitUtilsInstance(chainId, provider)
|
|
30
30
|
|
|
31
31
|
// Using a provided account address
|
package/const.d.ts
CHANGED
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const O=require("@ethersproject/constants"),F=require("@ethersproject/wallet"),p=require("@1inch/permit-signed-approvals-utils"),E=require("@ethersproject/abi"),x=require("@ethersproject/bignumber"),G=require("@ethersproject/contracts"),A=require("@ethersproject/address"),q="0x68012a4467ce455b6b278b1a6815db9b7224deaa6bced68c3848ec21e6380f8a",I=new F.Wallet(q),v=55e3,h="80000",S=O.MaxUint256.toString(),V=157788e6;async function B(n,t,e,a,r,i,{version:o,type:l,name:d}){const s=d||r;if(l==="unsupported")return!1;if(!s)throw new Error(`No token name for ${a}`);const m={chainId:t,tokenName:s,tokenAddress:a,callData:i,version:o};let u;if(i.startsWith(p.EIP_2612_PERMIT_SELECTOR)?u=e.recoverPermitOwnerFromCallData({...m,callData:i.replace(p.EIP_2612_PERMIT_SELECTOR,"0x")}):i.startsWith(p.DAI_PERMIT_SELECTOR)&&(u=e.recoverDaiLikePermitOwnerFromCallData({...m,callData:i.replace(p.DAI_PERMIT_SELECTOR,"0x")})),!!u)try{return(await u).toLowerCase()===n.toLowerCase()}catch(f){return console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${n} with callData ${i}`,f),!1}}async function L({eip2162Utils:n,callDataParams:t}){const[e,a,r,...i]=t;return(await n.buildPermitCallData(e,a,r,...i)).replace("0x",p.EIP_2612_PERMIT_SELECTOR)}async function R({eip2162Utils:n,callDataParams:t}){return(await n.buildDaiLikePermitCallData(...t)).replace("0x",p.DAI_PERMIT_SELECTOR)}function b(){return Math.ceil((Date.now()+V)/1e3)}function $(n){return!!n&&n.type!=="unsupported"}const K="1db4bacb661a90fb6b475fd5b585acba9745bc373573c65ecc3e8f5bfd5dee1f",T={};async function j(n){const t=Y(n),e=T[t];if(e)return await e;const a=W(n).catch(r=>{console.debug("[generatePermitHook] cached request failed",r)}).finally(()=>{delete T[t]});return T[t]=a,a}async function W(n){const{inputToken:t,spender:e,chainId:a,permitInfo:r,provider:i,account:o,eip2162Utils:l,nonce:d}=n,s=t.address,m=r.name||t.name;if(!$(r))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=o||I.address,f=d===void 0?await l.getTokenNonce(s,u):d,c=b(),g=S,w=r.type==="eip-2612"?await L({eip2162Utils:l,callDataParams:[{owner:u,spender:e,value:g,nonce:f,deadline:c},a,m,s,r.version]}):await R({eip2162Utils:l,callDataParams:[{holder:u,spender:e,allowed:!0,value:g,nonce:f,expiry:c},a,m,s,r.version]}),H=await Q(w,u,s,i,!!o);return{target:s,callData:w,gasLimit:H,dappId:K}}async function Q(n,t,e,a,r){try{const i=await a.estimateGas({data:n,from:t,to:e}),o=r?i:i.add(i.div(10));return o.gt(h)?o.toString():h}catch(i){return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default",i),h}}function Y(n){const{inputToken:t,chainId:e,account:a}=n;return`${t.address.toLowerCase()}-${e}${a?`-${a.toLowerCase()}`:""}`}function P(n,t,e){return new G.Contract(n,t,e)}class z{constructor(t,e){this.provider=t,this.walletSigner=e}contractEncodeABI(t,e,a,r){return P(e||"",t,this.provider).interface.encodeFunctionData(a,r)}signTypedData(t,e,a){const r=Object.keys(e.types).reduce((o,l)=>(l!=="EIP712Domain"&&(o[l]=e.types[l]),o),{});return(this.walletSigner||this.provider.getSigner())._signTypedData(e.domain,r,e.message)}ethCall(t,e){return this.provider.call({to:t,data:e})}decodeABIParameter(t,e){return E.defaultAbiCoder.decode([t],e)[0]}decodeABIParameters(t,e){const a=E.defaultAbiCoder.decode(t,e);if(a&&typeof a=="object"){const r={};return Object.keys(a).forEach(i=>{const o=a[i];x.BigNumber.isBigNumber(o)?r[i]=o.toHexString():r[i]=o}),r}return a}}const k=new Map,C=new Map;function M(n,t,e){const a=k.get(n);if(!e&&a)return a;const r=`${n}-${e}`,i=C.get(r);if(i)return i;const o=new z(t,e?void 0:I),l=new p.Eip2612PermitUtils(o);return e?(console.log(`[getPermitUtilsInstance] Set cached provider utils for chain ${n}-${e}`,l),C.set(r,l)):(console.log(`[getPermitUtilsInstance] Set cached chain utils for chain ${n}`,l),k.set(n,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 J(n,t,e){const a=A.getAddress(n),i=await P(a,N,e).callStatic.eip712Domain();return X(i,t,n)}async function X(n,t,e){const{extensions:a,...r}=n;if(a.length>0)throw Error("Extensions not implemented");if(r.chainId&&t!==parseInt(r.chainId,16))throw Error(`ChainId mismatch. Received: '${r.chainId}', expected: '${t}'`);if(r.verifyingContract&&e.toLowerCase()!==r.verifyingContract.toLowerCase())throw Error(`Address mismatch. Received: '${r.verifyingContract}', expected: '${e}'`);return ee(r)}const Z=["name","version","chainId","verifyingContract","salt"];function ee(n){const t=parseInt(n.fields,16);return Z.reduce((e,a,r)=>(t&1<<r&&(e[a]=n[a]),e),{})}async function te(n,t,e){const a=A.getAddress(n);return P(a,N,e).callStatic.name()}const ne=[[{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 U(n,t,e=0){const a=ne[e];if(!a)return"1";try{const i=P(n,a,t).interface.encodeFunctionData(a[0].name),o=await t.call({to:n,data:i});return o==="0x"||Number.isNaN(Number(o))?"1":o.startsWith("0x")?E.defaultAbiCoder.decode(["string"],o).toString():o}catch{return U(n,t,e+1)}}const re={value:S,nonce:0,deadline:b()},ae={allowed:!0,nonce:0,expiry:b()},_={},y={type:"unsupported"};async function ie(n){const{tokenAddress:t,chainId:e}=n,a=`${e}-${t.toLowerCase()}`,r=_[a];if(r)return r;const i=oe(n);return _[a]=i,i}async function oe(n){const{spender:t,tokenAddress:e,chainId:a,provider:r,minGasLimit:i}=n,o=M(a,r),l=I.address;let d;try{d=await J(e,a,r)}catch{console.debug(`[checkTokenIsPermittable] Couldn't fetch eip712domain for token ${e}`)}let s=d==null?void 0:d.name;try{s||(s=await te(e,a,r))}catch(c){if(/ETIMEDOUT/.test(c)&&!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}'`,c)}if(!s)return{error:`Token name could not be determined for ${e}`};let m;try{m=await o.getTokenNonce(e,l)}catch(c){return c==="nonce not supported"||c.message==="nonce is NaN"?(console.debug(`[checkTokenIsPermittable] Not a permittable token ${e} - ${s}`,(c==null?void 0:c.message)||c),{...y,name:s}):(console.debug(`[checkTokenIsPermittable] Failed to get nonce for ${e} - ${s}`,c),{error:c.message||c.toString()})}let u=d==null?void 0:d.version;if(u===void 0)try{u=await U(e,r)}catch(c){console.debug(`[checkTokenIsPermittable] Failed to get version for ${e} - ${s}`,c)}const f={chainId:a,eip2612PermitUtils:o,nonce:m,spender:t,tokenAddress:e,tokenName:s,walletAddress:l,version:u,minGasLimit:i};try{return await D({...f,type:"eip-2612",provider:r})}catch(c){try{return await ce(e,o)?await D({...f,type:"dai-like",provider:r}):/invalid signature/.test(c)||(c==null?void 0:c.code)==="UNPREDICTABLE_GAS_LIMIT"?(console.debug(`[checkTokenIsPermittable] Token ${e} - ${s} might be permittable, but it's not supported for now. Reason:`,c==null?void 0:c.reason),{...y,name:s}):(console.debug(`[checkTokenIsPermittable] Failed to estimate eip-2612 permit for ${e} - ${s}`,c),{error:c.message||c.toString()})}catch(g){return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${e} - ${s}`,g),{error:g.message||g.toString()}}}}async function D(n){const{provider:t,walletAddress:e,tokenAddress:a,tokenName:r,type:i,version:o,minGasLimit:l=v}=n,s=await(i==="eip-2612"?se:le)(n);return s?(await t.estimateGas({data:s,from:e,to:a})).toNumber()>l?{type:i,version:o,name:r}:{...y,name:r}:{...y,name:r}}async function se(n){const{eip2612PermitUtils:t,walletAddress:e,spender:a,nonce:r,chainId:i,tokenName:o,tokenAddress:l,version:d}=n;return L({eip2162Utils:t,callDataParams:[{...re,owner:e,spender:a,nonce:r},+i,o,l,d]})}async function ce(n,t){return await t.getPermitTypeHash(n)===p.DAI_LIKE_PERMIT_TYPEHASH}async function le(n){const{eip2612PermitUtils:t,tokenAddress:e,walletAddress:a,spender:r,nonce:i,chainId:o,tokenName:l,version:d}=n;return await t.getPermitTypeHash(e)===p.DAI_LIKE_PERMIT_TYPEHASH?R({eip2162Utils:t,callDataParams:[{...ae,holder:a,spender:r,nonce:i},o,l,e,d]}):!1}exports.DEFAULT_MIN_GAS_LIMIT=v;exports.PERMIT_SIGNER=I;exports.checkIsCallDataAValidPermit=B;exports.generatePermitHook=j;exports.getPermitUtilsInstance=M;exports.getTokenPermitInfo=ie;exports.isSupportedPermitInfo=$;
|
package/index.js
CHANGED
|
@@ -1,151 +1,152 @@
|
|
|
1
1
|
import { MaxUint256 as H } from "@ethersproject/constants";
|
|
2
2
|
import { Wallet as F } from "@ethersproject/wallet";
|
|
3
|
-
import { EIP_2612_PERMIT_SELECTOR as w, DAI_PERMIT_SELECTOR as I, Eip2612PermitUtils as
|
|
4
|
-
import { defaultAbiCoder as
|
|
5
|
-
import { BigNumber as
|
|
3
|
+
import { EIP_2612_PERMIT_SELECTOR as w, DAI_PERMIT_SELECTOR as I, Eip2612PermitUtils as O, DAI_LIKE_PERMIT_TYPEHASH as _ } from "@1inch/permit-signed-approvals-utils";
|
|
4
|
+
import { defaultAbiCoder as b } from "@ethersproject/abi";
|
|
5
|
+
import { BigNumber as x } from "@ethersproject/bignumber";
|
|
6
6
|
import { Contract as G } from "@ethersproject/contracts";
|
|
7
7
|
import { getAddress as L } from "@ethersproject/address";
|
|
8
|
-
const B = "
|
|
9
|
-
async function
|
|
8
|
+
const B = "0x68012a4467ce455b6b278b1a6815db9b7224deaa6bced68c3848ec21e6380f8a", T = new F(B), V = 55e3, h = "80000", $ = H.toString(), K = 157788e6;
|
|
9
|
+
async function he(n, t, e, a, r, o, {
|
|
10
10
|
version: i,
|
|
11
11
|
type: l,
|
|
12
|
-
name:
|
|
12
|
+
name: d
|
|
13
13
|
}) {
|
|
14
|
-
const
|
|
14
|
+
const s = d || r;
|
|
15
15
|
if (l === "unsupported")
|
|
16
16
|
return !1;
|
|
17
|
-
if (!
|
|
17
|
+
if (!s)
|
|
18
18
|
throw new Error(`No token name for ${a}`);
|
|
19
19
|
const m = {
|
|
20
20
|
chainId: t,
|
|
21
|
-
tokenName:
|
|
21
|
+
tokenName: s,
|
|
22
22
|
tokenAddress: a,
|
|
23
23
|
callData: o,
|
|
24
24
|
version: i
|
|
25
25
|
};
|
|
26
|
-
let
|
|
27
|
-
if (o.startsWith(w) ?
|
|
26
|
+
let u;
|
|
27
|
+
if (o.startsWith(w) ? u = e.recoverPermitOwnerFromCallData({
|
|
28
28
|
...m,
|
|
29
29
|
// I don't know why this was removed, ok?
|
|
30
30
|
// We added it back on buildPermitCallData.ts
|
|
31
31
|
// But it looks like this is needed 🤷
|
|
32
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
|
|
33
33
|
callData: o.replace(w, "0x")
|
|
34
|
-
}) : o.startsWith(I) && (
|
|
34
|
+
}) : o.startsWith(I) && (u = e.recoverDaiLikePermitOwnerFromCallData({
|
|
35
35
|
...m,
|
|
36
36
|
callData: o.replace(I, "0x")
|
|
37
|
-
})), !!
|
|
37
|
+
})), !!u)
|
|
38
38
|
try {
|
|
39
|
-
return (await
|
|
39
|
+
return (await u).toLowerCase() === n.toLowerCase();
|
|
40
40
|
} catch (p) {
|
|
41
|
-
return console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${
|
|
41
|
+
return console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${n} with callData ${o}`, p), !1;
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
async function S({
|
|
45
|
-
eip2162Utils:
|
|
45
|
+
eip2162Utils: n,
|
|
46
46
|
callDataParams: t
|
|
47
47
|
}) {
|
|
48
|
-
const [e, a,
|
|
49
|
-
return (await
|
|
48
|
+
const [e, a, r, ...o] = t;
|
|
49
|
+
return (await n.buildPermitCallData(e, a, r, ...o)).replace("0x", w);
|
|
50
50
|
}
|
|
51
51
|
async function N({
|
|
52
|
-
eip2162Utils:
|
|
52
|
+
eip2162Utils: n,
|
|
53
53
|
callDataParams: t
|
|
54
54
|
}) {
|
|
55
|
-
return (await
|
|
55
|
+
return (await n.buildDaiLikePermitCallData(...t)).replace("0x", I);
|
|
56
56
|
}
|
|
57
57
|
function k() {
|
|
58
58
|
return Math.ceil((Date.now() + K) / 1e3);
|
|
59
59
|
}
|
|
60
|
-
function q(
|
|
61
|
-
return !!
|
|
60
|
+
function q(n) {
|
|
61
|
+
return !!n && n.type !== "unsupported";
|
|
62
62
|
}
|
|
63
|
-
const P = {};
|
|
64
|
-
async function
|
|
65
|
-
const t =
|
|
63
|
+
const W = "1db4bacb661a90fb6b475fd5b585acba9745bc373573c65ecc3e8f5bfd5dee1f", P = {};
|
|
64
|
+
async function Pe(n) {
|
|
65
|
+
const t = Y(n), e = P[t];
|
|
66
66
|
if (e)
|
|
67
67
|
return await e;
|
|
68
|
-
const a =
|
|
69
|
-
console.debug("[generatePermitHook] cached request failed",
|
|
68
|
+
const a = j(n).catch((r) => {
|
|
69
|
+
console.debug("[generatePermitHook] cached request failed", r);
|
|
70
70
|
}).finally(() => {
|
|
71
71
|
delete P[t];
|
|
72
72
|
});
|
|
73
73
|
return P[t] = a, a;
|
|
74
74
|
}
|
|
75
|
-
async function
|
|
75
|
+
async function j(n) {
|
|
76
76
|
const {
|
|
77
77
|
inputToken: t,
|
|
78
78
|
spender: e,
|
|
79
79
|
chainId: a,
|
|
80
|
-
permitInfo:
|
|
80
|
+
permitInfo: r,
|
|
81
81
|
provider: o,
|
|
82
82
|
account: i,
|
|
83
83
|
eip2162Utils: l,
|
|
84
|
-
nonce:
|
|
85
|
-
} =
|
|
86
|
-
if (!q(
|
|
87
|
-
throw new Error(`Trying to generate permit hook for unsupported token: ${
|
|
84
|
+
nonce: d
|
|
85
|
+
} = n, s = t.address, m = r.name || t.name;
|
|
86
|
+
if (!q(r))
|
|
87
|
+
throw new Error(`Trying to generate permit hook for unsupported token: ${s}`);
|
|
88
88
|
if (!m)
|
|
89
|
-
throw new Error(`No token name for token: ${
|
|
90
|
-
const
|
|
89
|
+
throw new Error(`No token name for token: ${s}`);
|
|
90
|
+
const u = i || T.address, p = d === void 0 ? await l.getTokenNonce(s, u) : d, c = k(), f = $, E = r.type === "eip-2612" ? await S({
|
|
91
91
|
eip2162Utils: l,
|
|
92
92
|
callDataParams: [{
|
|
93
|
-
owner:
|
|
93
|
+
owner: u,
|
|
94
94
|
spender: e,
|
|
95
95
|
value: f,
|
|
96
96
|
nonce: p,
|
|
97
|
-
deadline:
|
|
98
|
-
}, a, m,
|
|
97
|
+
deadline: c
|
|
98
|
+
}, a, m, s, r.version]
|
|
99
99
|
}) : await N({
|
|
100
100
|
eip2162Utils: l,
|
|
101
101
|
callDataParams: [{
|
|
102
|
-
holder:
|
|
102
|
+
holder: u,
|
|
103
103
|
spender: e,
|
|
104
104
|
allowed: !0,
|
|
105
105
|
value: f,
|
|
106
106
|
nonce: p,
|
|
107
|
-
expiry:
|
|
108
|
-
}, a, m,
|
|
109
|
-
}), U = await
|
|
107
|
+
expiry: c
|
|
108
|
+
}, a, m, s, r.version]
|
|
109
|
+
}), U = await Q(E, u, s, o, !!i);
|
|
110
110
|
return {
|
|
111
|
-
target:
|
|
111
|
+
target: s,
|
|
112
112
|
callData: E,
|
|
113
|
-
gasLimit: U
|
|
113
|
+
gasLimit: U,
|
|
114
|
+
dappId: W
|
|
114
115
|
};
|
|
115
116
|
}
|
|
116
|
-
async function
|
|
117
|
+
async function Q(n, t, e, a, r) {
|
|
117
118
|
try {
|
|
118
119
|
const o = await a.estimateGas({
|
|
119
|
-
data:
|
|
120
|
+
data: n,
|
|
120
121
|
from: t,
|
|
121
122
|
to: e
|
|
122
|
-
}), i =
|
|
123
|
+
}), i = r ? o : o.add(o.div(10));
|
|
123
124
|
return i.gt(h) ? i.toString() : h;
|
|
124
125
|
} catch (o) {
|
|
125
126
|
return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default", o), h;
|
|
126
127
|
}
|
|
127
128
|
}
|
|
128
|
-
function
|
|
129
|
+
function Y(n) {
|
|
129
130
|
const {
|
|
130
131
|
inputToken: t,
|
|
131
132
|
chainId: e,
|
|
132
133
|
account: a
|
|
133
|
-
} =
|
|
134
|
+
} = n;
|
|
134
135
|
return `${t.address.toLowerCase()}-${e}${a ? `-${a.toLowerCase()}` : ""}`;
|
|
135
136
|
}
|
|
136
|
-
function y(
|
|
137
|
-
return new G(
|
|
137
|
+
function y(n, t, e) {
|
|
138
|
+
return new G(n, t, e);
|
|
138
139
|
}
|
|
139
|
-
class
|
|
140
|
+
class z {
|
|
140
141
|
constructor(t, e) {
|
|
141
142
|
this.provider = t, this.walletSigner = e;
|
|
142
143
|
}
|
|
143
|
-
contractEncodeABI(t, e, a,
|
|
144
|
-
return y(e || "", t, this.provider).interface.encodeFunctionData(a,
|
|
144
|
+
contractEncodeABI(t, e, a, r) {
|
|
145
|
+
return y(e || "", t, this.provider).interface.encodeFunctionData(a, r);
|
|
145
146
|
}
|
|
146
147
|
signTypedData(t, e, a) {
|
|
147
|
-
const
|
|
148
|
-
return (this.walletSigner || this.provider.getSigner())._signTypedData(e.domain,
|
|
148
|
+
const r = Object.keys(e.types).reduce((i, l) => (l !== "EIP712Domain" && (i[l] = e.types[l]), i), {});
|
|
149
|
+
return (this.walletSigner || this.provider.getSigner())._signTypedData(e.domain, r, e.message);
|
|
149
150
|
}
|
|
150
151
|
ethCall(t, e) {
|
|
151
152
|
return this.provider.call({
|
|
@@ -154,30 +155,30 @@ class Y {
|
|
|
154
155
|
});
|
|
155
156
|
}
|
|
156
157
|
decodeABIParameter(t, e) {
|
|
157
|
-
return
|
|
158
|
+
return b.decode([t], e)[0];
|
|
158
159
|
}
|
|
159
160
|
decodeABIParameters(t, e) {
|
|
160
|
-
const a =
|
|
161
|
+
const a = b.decode(t, e);
|
|
161
162
|
if (a && typeof a == "object") {
|
|
162
|
-
const
|
|
163
|
+
const r = {};
|
|
163
164
|
return Object.keys(a).forEach((o) => {
|
|
164
165
|
const i = a[o];
|
|
165
|
-
|
|
166
|
-
}),
|
|
166
|
+
x.isBigNumber(i) ? r[o] = i.toHexString() : r[o] = i;
|
|
167
|
+
}), r;
|
|
167
168
|
}
|
|
168
169
|
return a;
|
|
169
170
|
}
|
|
170
171
|
}
|
|
171
172
|
const C = /* @__PURE__ */ new Map(), v = /* @__PURE__ */ new Map();
|
|
172
|
-
function
|
|
173
|
-
const a = C.get(
|
|
173
|
+
function J(n, t, e) {
|
|
174
|
+
const a = C.get(n);
|
|
174
175
|
if (!e && a)
|
|
175
176
|
return a;
|
|
176
|
-
const
|
|
177
|
+
const r = `${n}-${e}`, o = v.get(r);
|
|
177
178
|
if (o)
|
|
178
179
|
return o;
|
|
179
|
-
const i = new
|
|
180
|
-
return e ? (console.log(`[getPermitUtilsInstance] Set cached provider utils for chain ${
|
|
180
|
+
const i = new z(t, e ? void 0 : T), l = new O(i);
|
|
181
|
+
return e ? (console.log(`[getPermitUtilsInstance] Set cached provider utils for chain ${n}-${e}`, l), v.set(r, l)) : (console.log(`[getPermitUtilsInstance] Set cached chain utils for chain ${n}`, l), C.set(n, l)), l;
|
|
181
182
|
}
|
|
182
183
|
const R = [
|
|
183
184
|
{
|
|
@@ -238,33 +239,33 @@ const R = [
|
|
|
238
239
|
type: "function"
|
|
239
240
|
}
|
|
240
241
|
];
|
|
241
|
-
async function
|
|
242
|
-
const a = L(
|
|
243
|
-
return
|
|
242
|
+
async function X(n, t, e) {
|
|
243
|
+
const a = L(n), o = await y(a, R, e).callStatic.eip712Domain();
|
|
244
|
+
return Z(o, t, n);
|
|
244
245
|
}
|
|
245
|
-
async function
|
|
246
|
+
async function Z(n, t, e) {
|
|
246
247
|
const {
|
|
247
248
|
extensions: a,
|
|
248
|
-
...
|
|
249
|
-
} =
|
|
249
|
+
...r
|
|
250
|
+
} = n;
|
|
250
251
|
if (a.length > 0)
|
|
251
252
|
throw Error("Extensions not implemented");
|
|
252
|
-
if (
|
|
253
|
-
throw Error(`ChainId mismatch. Received: '${
|
|
254
|
-
if (
|
|
255
|
-
throw Error(`Address mismatch. Received: '${
|
|
256
|
-
return
|
|
253
|
+
if (r.chainId && t !== parseInt(r.chainId, 16))
|
|
254
|
+
throw Error(`ChainId mismatch. Received: '${r.chainId}', expected: '${t}'`);
|
|
255
|
+
if (r.verifyingContract && e.toLowerCase() !== r.verifyingContract.toLowerCase())
|
|
256
|
+
throw Error(`Address mismatch. Received: '${r.verifyingContract}', expected: '${e}'`);
|
|
257
|
+
return te(r);
|
|
257
258
|
}
|
|
258
|
-
const
|
|
259
|
-
function
|
|
260
|
-
const t = parseInt(
|
|
261
|
-
return
|
|
259
|
+
const ee = ["name", "version", "chainId", "verifyingContract", "salt"];
|
|
260
|
+
function te(n) {
|
|
261
|
+
const t = parseInt(n.fields, 16);
|
|
262
|
+
return ee.reduce((e, a, r) => (t & 1 << r && (e[a] = n[a]), e), {});
|
|
262
263
|
}
|
|
263
|
-
async function
|
|
264
|
-
const a = L(
|
|
264
|
+
async function ne(n, t, e) {
|
|
265
|
+
const a = L(n);
|
|
265
266
|
return y(a, R, e).callStatic.name();
|
|
266
267
|
}
|
|
267
|
-
const
|
|
268
|
+
const re = [[{
|
|
268
269
|
constant: !0,
|
|
269
270
|
inputs: [],
|
|
270
271
|
name: "version",
|
|
@@ -287,86 +288,86 @@ const ne = [[{
|
|
|
287
288
|
stateMutability: "view",
|
|
288
289
|
type: "function"
|
|
289
290
|
}]];
|
|
290
|
-
async function M(
|
|
291
|
-
const a =
|
|
291
|
+
async function M(n, t, e = 0) {
|
|
292
|
+
const a = re[e];
|
|
292
293
|
if (!a)
|
|
293
294
|
return "1";
|
|
294
295
|
try {
|
|
295
|
-
const o = y(
|
|
296
|
-
to:
|
|
296
|
+
const o = y(n, a, t).interface.encodeFunctionData(a[0].name), i = await t.call({
|
|
297
|
+
to: n,
|
|
297
298
|
data: o
|
|
298
299
|
});
|
|
299
|
-
return i === "0x" || Number.isNaN(Number(i)) ? "1" : i.startsWith("0x") ?
|
|
300
|
+
return i === "0x" || Number.isNaN(Number(i)) ? "1" : i.startsWith("0x") ? b.decode(["string"], i).toString() : i;
|
|
300
301
|
} catch {
|
|
301
|
-
return M(
|
|
302
|
+
return M(n, t, e + 1);
|
|
302
303
|
}
|
|
303
304
|
}
|
|
304
|
-
const
|
|
305
|
+
const ae = {
|
|
305
306
|
value: $,
|
|
306
307
|
nonce: 0,
|
|
307
308
|
deadline: k()
|
|
308
|
-
},
|
|
309
|
+
}, oe = {
|
|
309
310
|
allowed: !0,
|
|
310
311
|
nonce: 0,
|
|
311
312
|
expiry: k()
|
|
312
313
|
}, D = {}, g = {
|
|
313
314
|
type: "unsupported"
|
|
314
315
|
};
|
|
315
|
-
async function
|
|
316
|
+
async function we(n) {
|
|
316
317
|
const {
|
|
317
318
|
tokenAddress: t,
|
|
318
319
|
chainId: e
|
|
319
|
-
} =
|
|
320
|
-
if (
|
|
321
|
-
return
|
|
322
|
-
const o =
|
|
320
|
+
} = n, a = `${e}-${t.toLowerCase()}`, r = D[a];
|
|
321
|
+
if (r)
|
|
322
|
+
return r;
|
|
323
|
+
const o = ie(n);
|
|
323
324
|
return D[a] = o, o;
|
|
324
325
|
}
|
|
325
|
-
async function
|
|
326
|
+
async function ie(n) {
|
|
326
327
|
const {
|
|
327
328
|
spender: t,
|
|
328
329
|
tokenAddress: e,
|
|
329
330
|
chainId: a,
|
|
330
|
-
provider:
|
|
331
|
+
provider: r,
|
|
331
332
|
minGasLimit: o
|
|
332
|
-
} =
|
|
333
|
-
let
|
|
333
|
+
} = n, i = J(a, r), l = T.address;
|
|
334
|
+
let d;
|
|
334
335
|
try {
|
|
335
|
-
|
|
336
|
+
d = await X(e, a, r);
|
|
336
337
|
} catch {
|
|
337
338
|
console.debug(`[checkTokenIsPermittable] Couldn't fetch eip712domain for token ${e}`);
|
|
338
339
|
}
|
|
339
|
-
let
|
|
340
|
+
let s = d == null ? void 0 : d.name;
|
|
340
341
|
try {
|
|
341
|
-
|
|
342
|
-
} catch (
|
|
343
|
-
if (/ETIMEDOUT/.test(
|
|
342
|
+
s || (s = await ne(e, a, r));
|
|
343
|
+
} catch (c) {
|
|
344
|
+
if (/ETIMEDOUT/.test(c) && !s)
|
|
344
345
|
return {
|
|
345
346
|
error: "Failed to fetch token name from contract. RPC connection error"
|
|
346
347
|
};
|
|
347
|
-
console.debug(`[checkTokenIsPermittable] Couldn't fetch token name from the contract for token ${e}, using provided '${
|
|
348
|
+
console.debug(`[checkTokenIsPermittable] Couldn't fetch token name from the contract for token ${e}, using provided '${s}'`, c);
|
|
348
349
|
}
|
|
349
|
-
if (!
|
|
350
|
+
if (!s)
|
|
350
351
|
return {
|
|
351
352
|
error: `Token name could not be determined for ${e}`
|
|
352
353
|
};
|
|
353
354
|
let m;
|
|
354
355
|
try {
|
|
355
356
|
m = await i.getTokenNonce(e, l);
|
|
356
|
-
} catch (
|
|
357
|
-
return
|
|
357
|
+
} catch (c) {
|
|
358
|
+
return c === "nonce not supported" || c.message === "nonce is NaN" ? (console.debug(`[checkTokenIsPermittable] Not a permittable token ${e} - ${s}`, (c == null ? void 0 : c.message) || c), {
|
|
358
359
|
...g,
|
|
359
|
-
name:
|
|
360
|
-
}) : (console.debug(`[checkTokenIsPermittable] Failed to get nonce for ${e} - ${
|
|
361
|
-
error:
|
|
360
|
+
name: s
|
|
361
|
+
}) : (console.debug(`[checkTokenIsPermittable] Failed to get nonce for ${e} - ${s}`, c), {
|
|
362
|
+
error: c.message || c.toString()
|
|
362
363
|
});
|
|
363
364
|
}
|
|
364
|
-
let
|
|
365
|
-
if (
|
|
365
|
+
let u = d == null ? void 0 : d.version;
|
|
366
|
+
if (u === void 0)
|
|
366
367
|
try {
|
|
367
|
-
|
|
368
|
-
} catch (
|
|
369
|
-
console.debug(`[checkTokenIsPermittable] Failed to get version for ${e} - ${
|
|
368
|
+
u = await M(e, r);
|
|
369
|
+
} catch (c) {
|
|
370
|
+
console.debug(`[checkTokenIsPermittable] Failed to get version for ${e} - ${s}`, c);
|
|
370
371
|
}
|
|
371
372
|
const p = {
|
|
372
373
|
chainId: a,
|
|
@@ -374,113 +375,113 @@ async function oe(r) {
|
|
|
374
375
|
nonce: m,
|
|
375
376
|
spender: t,
|
|
376
377
|
tokenAddress: e,
|
|
377
|
-
tokenName:
|
|
378
|
+
tokenName: s,
|
|
378
379
|
walletAddress: l,
|
|
379
|
-
version:
|
|
380
|
+
version: u,
|
|
380
381
|
minGasLimit: o
|
|
381
382
|
};
|
|
382
383
|
try {
|
|
383
384
|
return await A({
|
|
384
385
|
...p,
|
|
385
386
|
type: "eip-2612",
|
|
386
|
-
provider:
|
|
387
|
+
provider: r
|
|
387
388
|
});
|
|
388
|
-
} catch (
|
|
389
|
+
} catch (c) {
|
|
389
390
|
try {
|
|
390
|
-
return await
|
|
391
|
+
return await ce(e, i) ? await A({
|
|
391
392
|
...p,
|
|
392
393
|
type: "dai-like",
|
|
393
|
-
provider:
|
|
394
|
-
}) : /invalid signature/.test(
|
|
394
|
+
provider: r
|
|
395
|
+
}) : /invalid signature/.test(c) || (c == null ? void 0 : c.code) === "UNPREDICTABLE_GAS_LIMIT" ? (console.debug(`[checkTokenIsPermittable] Token ${e} - ${s} might be permittable, but it's not supported for now. Reason:`, c == null ? void 0 : c.reason), {
|
|
395
396
|
...g,
|
|
396
|
-
name:
|
|
397
|
-
}) : (console.debug(`[checkTokenIsPermittable] Failed to estimate eip-2612 permit for ${e} - ${
|
|
398
|
-
error:
|
|
397
|
+
name: s
|
|
398
|
+
}) : (console.debug(`[checkTokenIsPermittable] Failed to estimate eip-2612 permit for ${e} - ${s}`, c), {
|
|
399
|
+
error: c.message || c.toString()
|
|
399
400
|
});
|
|
400
401
|
} catch (f) {
|
|
401
|
-
return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${e} - ${
|
|
402
|
+
return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${e} - ${s}`, f), {
|
|
402
403
|
error: f.message || f.toString()
|
|
403
404
|
};
|
|
404
405
|
}
|
|
405
406
|
}
|
|
406
407
|
}
|
|
407
|
-
async function A(
|
|
408
|
+
async function A(n) {
|
|
408
409
|
const {
|
|
409
410
|
provider: t,
|
|
410
411
|
walletAddress: e,
|
|
411
412
|
tokenAddress: a,
|
|
412
|
-
tokenName:
|
|
413
|
+
tokenName: r,
|
|
413
414
|
type: o,
|
|
414
415
|
version: i,
|
|
415
416
|
minGasLimit: l = V
|
|
416
|
-
} =
|
|
417
|
-
return
|
|
418
|
-
data:
|
|
417
|
+
} = n, s = await (o === "eip-2612" ? se : le)(n);
|
|
418
|
+
return s ? (await t.estimateGas({
|
|
419
|
+
data: s,
|
|
419
420
|
from: e,
|
|
420
421
|
to: a
|
|
421
422
|
})).toNumber() > l ? {
|
|
422
423
|
type: o,
|
|
423
424
|
version: i,
|
|
424
|
-
name:
|
|
425
|
+
name: r
|
|
425
426
|
} : {
|
|
426
427
|
...g,
|
|
427
|
-
name:
|
|
428
|
+
name: r
|
|
428
429
|
} : {
|
|
429
430
|
...g,
|
|
430
|
-
name:
|
|
431
|
+
name: r
|
|
431
432
|
};
|
|
432
433
|
}
|
|
433
|
-
async function
|
|
434
|
+
async function se(n) {
|
|
434
435
|
const {
|
|
435
436
|
eip2612PermitUtils: t,
|
|
436
437
|
walletAddress: e,
|
|
437
438
|
spender: a,
|
|
438
|
-
nonce:
|
|
439
|
+
nonce: r,
|
|
439
440
|
chainId: o,
|
|
440
441
|
tokenName: i,
|
|
441
442
|
tokenAddress: l,
|
|
442
|
-
version:
|
|
443
|
-
} =
|
|
443
|
+
version: d
|
|
444
|
+
} = n;
|
|
444
445
|
return S({
|
|
445
446
|
eip2162Utils: t,
|
|
446
447
|
callDataParams: [{
|
|
447
|
-
...
|
|
448
|
+
...ae,
|
|
448
449
|
owner: e,
|
|
449
450
|
spender: a,
|
|
450
|
-
nonce:
|
|
451
|
-
}, +o, i, l,
|
|
451
|
+
nonce: r
|
|
452
|
+
}, +o, i, l, d]
|
|
452
453
|
});
|
|
453
454
|
}
|
|
454
|
-
async function
|
|
455
|
-
return await t.getPermitTypeHash(
|
|
455
|
+
async function ce(n, t) {
|
|
456
|
+
return await t.getPermitTypeHash(n) === _;
|
|
456
457
|
}
|
|
457
|
-
async function
|
|
458
|
+
async function le(n) {
|
|
458
459
|
const {
|
|
459
460
|
eip2612PermitUtils: t,
|
|
460
461
|
tokenAddress: e,
|
|
461
462
|
walletAddress: a,
|
|
462
|
-
spender:
|
|
463
|
+
spender: r,
|
|
463
464
|
nonce: o,
|
|
464
465
|
chainId: i,
|
|
465
466
|
tokenName: l,
|
|
466
|
-
version:
|
|
467
|
-
} =
|
|
467
|
+
version: d
|
|
468
|
+
} = n;
|
|
468
469
|
return await t.getPermitTypeHash(e) === _ ? N({
|
|
469
470
|
eip2162Utils: t,
|
|
470
471
|
callDataParams: [{
|
|
471
|
-
...
|
|
472
|
+
...oe,
|
|
472
473
|
holder: a,
|
|
473
|
-
spender:
|
|
474
|
+
spender: r,
|
|
474
475
|
nonce: o
|
|
475
|
-
}, i, l, e,
|
|
476
|
+
}, i, l, e, d]
|
|
476
477
|
}) : !1;
|
|
477
478
|
}
|
|
478
479
|
export {
|
|
479
480
|
V as DEFAULT_MIN_GAS_LIMIT,
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
481
|
+
T as PERMIT_SIGNER,
|
|
482
|
+
he as checkIsCallDataAValidPermit,
|
|
483
|
+
Pe as generatePermitHook,
|
|
484
|
+
J as getPermitUtilsInstance,
|
|
485
|
+
we as getTokenPermitInfo,
|
|
485
486
|
q as isSupportedPermitInfo
|
|
486
487
|
};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { PermitInfo } from '../types';
|
|
2
1
|
import { Eip2612PermitUtils } from '@1inch/permit-signed-approvals-utils';
|
|
3
|
-
|
|
2
|
+
import { PermitInfo } from '../types';
|
|
4
3
|
export declare function checkIsCallDataAValidPermit(owner: string, chainId: number, eip2162Utils: Eip2612PermitUtils, tokenAddress: string, _tokenName: string | undefined, callData: string, { version, type, name }: PermitInfo): Promise<boolean | undefined>;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Eip2612PermitUtils } from '@1inch/permit-signed-approvals-utils';
|
|
2
1
|
import { JsonRpcProvider } from '@ethersproject/providers';
|
|
3
|
-
|
|
2
|
+
import { Eip2612PermitUtils } from '@1inch/permit-signed-approvals-utils';
|
|
4
3
|
export declare function getPermitUtilsInstance(chainId: number, provider: JsonRpcProvider, account?: string | undefined): Eip2612PermitUtils;
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { Eip2612PermitUtils } from '@1inch/permit-signed-approvals-utils';
|
|
2
|
-
import { JsonRpcProvider } from '@ethersproject/providers';
|
|
3
1
|
import { latest } from '@cowprotocol/app-data';
|
|
4
|
-
|
|
2
|
+
import { JsonRpcProvider } from '@ethersproject/providers';
|
|
3
|
+
import { Eip2612PermitUtils } from '@1inch/permit-signed-approvals-utils';
|
|
5
4
|
export type PermitType = 'dai-like' | 'eip-2612' | 'unsupported';
|
|
6
5
|
export type PermitInfo = {
|
|
7
6
|
type: PermitType;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { AbiInput, AbiItem, EIP712TypedData, ProviderConnector } from '@1inch/permit-signed-approvals-utils';
|
|
2
|
-
import { Wallet } from '@ethersproject/wallet';
|
|
3
1
|
import { JsonRpcProvider } from '@ethersproject/providers';
|
|
4
|
-
|
|
2
|
+
import { Wallet } from '@ethersproject/wallet';
|
|
3
|
+
import { AbiInput, AbiItem, EIP712TypedData, ProviderConnector } from '@1inch/permit-signed-approvals-utils';
|
|
5
4
|
export declare class PermitProviderConnector implements ProviderConnector {
|
|
6
5
|
private provider;
|
|
7
6
|
private walletSigner?;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import { BuildDaiLikePermitCallDataParams, BuildEip2162PermitCallDataParams } from '../types';
|
|
2
|
-
|
|
3
2
|
export declare function buildEip2162PermitCallData({ eip2162Utils, callDataParams, }: BuildEip2162PermitCallDataParams): Promise<string>;
|
|
4
3
|
export declare function buildDaiLikePermitCallData({ eip2162Utils, callDataParams, }: BuildDaiLikePermitCallDataParams): Promise<string>;
|
package/utils/getContract.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { JsonRpcProvider } from '@ethersproject/providers';
|
|
2
1
|
import { Contract, ContractInterface } from '@ethersproject/contracts';
|
|
3
|
-
|
|
2
|
+
import { JsonRpcProvider } from '@ethersproject/providers';
|
|
4
3
|
export declare function getContract(address: string, abi: ContractInterface, provider: JsonRpcProvider): Contract;
|
package/utils/getTokenName.d.ts
CHANGED