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