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