@cowprotocol/permit-utils
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -7,2 +7,1 @@ import { Wallet } from '@ethersproject/wallet'; | ||
export declare const DEFAULT_PERMIT_DURATION: number; | ||
export declare const TOKENS_TO_SKIP_VERSION: Set<string>; |
299
index.js
@@ -1,29 +0,29 @@ | ||
import { MaxUint256 as M } from "@ethersproject/constants"; | ||
import { Wallet as H } from "@ethersproject/wallet"; | ||
import { EIP_2612_PERMIT_SELECTOR as P, DAI_PERMIT_SELECTOR as w, Eip2612PermitUtils as F, DAI_LIKE_PERMIT_TYPEHASH as _ } from "@1inch/permit-signed-approvals-utils"; | ||
import { defaultAbiCoder as E } from "@ethersproject/abi"; | ||
import { BigNumber as x } from "@ethersproject/bignumber"; | ||
import { Contract as O } from "@ethersproject/contracts"; | ||
import { MaxUint256 as H } from "@ethersproject/constants"; | ||
import { Wallet as F } from "@ethersproject/wallet"; | ||
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"; | ||
import { defaultAbiCoder as T } from "@ethersproject/abi"; | ||
import { BigNumber as O } from "@ethersproject/bignumber"; | ||
import { Contract as G } from "@ethersproject/contracts"; | ||
import { getAddress as L } from "@ethersproject/address"; | ||
const G = "0x1b80501ea68b883241ac5b9f92e8635aa3df23c89b7bbb87e762be65b8c6eb75", T = new H(G), K = 55e3, y = "80000", $ = M.toString(), B = 157788e6, V = /* @__PURE__ */ new Set(["0x6b175474e89094c44da98b954eedeac495271d0f"]); | ||
async function ge(r, n, e, a, t, o, { | ||
version: c, | ||
const B = "0x1b80501ea68b883241ac5b9f92e8635aa3df23c89b7bbb87e762be65b8c6eb75", b = new F(B), V = 55e3, P = "80000", $ = H.toString(), K = 157788e6; | ||
async function ye(r, n, e, a, t, o, { | ||
version: i, | ||
type: l, | ||
name: d | ||
name: u | ||
}) { | ||
const s = d || t; | ||
const c = u || t; | ||
if (l === "unsupported") | ||
return !1; | ||
if (!s) | ||
if (!c) | ||
throw new Error(`No token name for ${a}`); | ||
const u = { | ||
const m = { | ||
chainId: n, | ||
tokenName: s, | ||
tokenName: c, | ||
tokenAddress: a, | ||
callData: o, | ||
version: c | ||
version: i | ||
}; | ||
let m; | ||
if (o.startsWith(P) ? m = e.recoverPermitOwnerFromCallData({ | ||
...u, | ||
let d; | ||
if (o.startsWith(w) ? d = e.recoverPermitOwnerFromCallData({ | ||
...m, | ||
// I don't know why this was removed, ok? | ||
@@ -33,9 +33,9 @@ // We added it back on buildPermitCallData.ts | ||
// Check the test for this method https://github.com/1inch/permit-signed-approvals-utils/blob/master/src/eip-2612-permit.test.ts#L85-L106 | ||
callData: o.replace(P, "0x") | ||
}) : o.startsWith(w) && (m = e.recoverDaiLikePermitOwnerFromCallData({ | ||
...u, | ||
callData: o.replace(w, "0x") | ||
})), !!m) | ||
}) : o.startsWith(I) && (d = e.recoverDaiLikePermitOwnerFromCallData({ | ||
...m, | ||
callData: o.replace(I, "0x") | ||
})), !!d) | ||
try { | ||
return (await m).toLowerCase() === r.toLowerCase(); | ||
return (await d).toLowerCase() === r.toLowerCase(); | ||
} catch (p) { | ||
@@ -50,14 +50,14 @@ return console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${r} with callData ${o}`, p), !1; | ||
const [e, a, t, ...o] = n; | ||
return (await r.buildPermitCallData(e, a, t, ...o)).replace("0x", P); | ||
return (await r.buildPermitCallData(e, a, t, ...o)).replace("0x", w); | ||
} | ||
async function R({ | ||
async function N({ | ||
eip2162Utils: r, | ||
callDataParams: n | ||
}) { | ||
return (await r.buildDaiLikePermitCallData(...n)).replace("0x", w); | ||
return (await r.buildDaiLikePermitCallData(...n)).replace("0x", I); | ||
} | ||
function I() { | ||
return Math.ceil((Date.now() + B) / 1e3); | ||
function k() { | ||
return Math.ceil((Date.now() + K) / 1e3); | ||
} | ||
function q(r) { | ||
function W(r) { | ||
return !!r && r.type !== "unsupported"; | ||
@@ -74,6 +74,6 @@ } | ||
} | ||
const a = W(r).then((t) => (delete g[n], t)); | ||
const a = q(r).then((t) => (delete g[n], t)); | ||
return g[n] = a, a; | ||
} | ||
async function W(r) { | ||
async function q(r) { | ||
const { | ||
@@ -85,23 +85,23 @@ inputToken: n, | ||
provider: o, | ||
account: c, | ||
account: i, | ||
eip2162Utils: l, | ||
nonce: d | ||
} = r, s = n.address, u = t.name || n.name; | ||
if (!q(t)) | ||
throw new Error(`Trying to generate permit hook for unsupported token: ${s}`); | ||
if (!u) | ||
throw new Error(`No token name for token: ${s}`); | ||
const m = c || T.address, p = d === void 0 ? await l.getTokenNonce(s, m) : d, i = I(), f = $, b = t.type === "eip-2612" ? await S({ | ||
nonce: u | ||
} = r, c = n.address, m = t.name || n.name; | ||
if (!W(t)) | ||
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 = i || b.address, p = u === void 0 ? await l.getTokenNonce(c, d) : u, s = k(), f = $, E = t.type === "eip-2612" ? await S({ | ||
eip2162Utils: l, | ||
callDataParams: [{ | ||
owner: m, | ||
owner: d, | ||
spender: e, | ||
value: f, | ||
nonce: p, | ||
deadline: i | ||
}, a, u, s, t.version] | ||
}) : await R({ | ||
deadline: s | ||
}, a, m, c, t.version] | ||
}) : await N({ | ||
eip2162Utils: l, | ||
callDataParams: [{ | ||
holder: m, | ||
holder: d, | ||
spender: e, | ||
@@ -111,8 +111,8 @@ allowed: !0, | ||
nonce: p, | ||
expiry: i | ||
}, a, u, s, t.version] | ||
}), U = await j(b, m, s, o, !!c); | ||
expiry: s | ||
}, a, m, c, t.version] | ||
}), U = await j(E, d, c, o, !!i); | ||
return { | ||
target: s, | ||
callData: b, | ||
target: c, | ||
callData: E, | ||
gasLimit: U | ||
@@ -127,6 +127,6 @@ }; | ||
to: e | ||
}), c = t ? o : o.add(o.div(10)); | ||
return c.gt(y) ? c.toString() : y; | ||
}), i = t ? o : o.add(o.div(10)); | ||
return i.gt(P) ? i.toString() : P; | ||
} catch (o) { | ||
return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default", o), y; | ||
return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default", o), P; | ||
} | ||
@@ -142,4 +142,4 @@ } | ||
} | ||
function k(r, n, e) { | ||
return new O(r, n, e); | ||
function h(r, n, e) { | ||
return new G(r, n, e); | ||
} | ||
@@ -151,6 +151,6 @@ class Y { | ||
contractEncodeABI(n, e, a, t) { | ||
return k(e || "", n, this.provider).interface.encodeFunctionData(a, t); | ||
return h(e || "", n, this.provider).interface.encodeFunctionData(a, t); | ||
} | ||
signTypedData(n, e, a) { | ||
const t = Object.keys(e.types).reduce((c, l) => (l !== "EIP712Domain" && (c[l] = e.types[l]), c), {}); | ||
const t = Object.keys(e.types).reduce((i, l) => (l !== "EIP712Domain" && (i[l] = e.types[l]), i), {}); | ||
return (this.walletSigner || this.provider.getSigner())._signTypedData(e.domain, t, e.message); | ||
@@ -165,11 +165,11 @@ } | ||
decodeABIParameter(n, e) { | ||
return E.decode([n], e)[0]; | ||
return T.decode([n], e)[0]; | ||
} | ||
decodeABIParameters(n, e) { | ||
const a = E.decode(n, e); | ||
const a = T.decode(n, e); | ||
if (a && typeof a == "object") { | ||
const t = {}; | ||
return Object.keys(a).forEach((o) => { | ||
const c = a[o]; | ||
x.isBigNumber(c) ? t[o] = c.toHexString() : t[o] = c; | ||
const i = a[o]; | ||
O.isBigNumber(i) ? t[o] = i.toHexString() : t[o] = i; | ||
}), t; | ||
@@ -188,6 +188,6 @@ } | ||
return o; | ||
const c = new Y(n, e ? void 0 : T), l = new F(c); | ||
const i = new Y(n, e ? void 0 : b), l = new x(i); | ||
return e ? (console.log(`[getPermitUtilsInstance] Set cached provider utils for chain ${r}-${e}`, l), v.set(t, l)) : (console.log(`[getPermitUtilsInstance] Set cached chain utils for chain ${r}`, l), C.set(r, l)), l; | ||
} | ||
const N = [ | ||
const R = [ | ||
{ | ||
@@ -252,3 +252,3 @@ constant: !0, | ||
async function J(r, n, e) { | ||
const a = L(r), o = await k(a, N, e).callStatic.eip712Domain(); | ||
const a = L(r), o = await h(a, R, e).callStatic.eip712Domain(); | ||
return X(o, n, r); | ||
@@ -276,16 +276,53 @@ } | ||
const a = L(r); | ||
return k(a, N, e).callStatic.name(); | ||
return h(a, R, e).callStatic.name(); | ||
} | ||
const ne = { | ||
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 M(r, n, e = 0) { | ||
const a = ne[e]; | ||
if (!a) | ||
return "1"; | ||
try { | ||
const o = h(r, a, n).interface.encodeFunctionData(a[0].name), i = await n.call({ | ||
to: r, | ||
data: o | ||
}); | ||
return i === "0x" || Number.isNaN(Number(i)) ? "1" : i.startsWith("0x") ? T.decode(["string"], i).toString() : i; | ||
} catch { | ||
return M(r, n, e + 1); | ||
} | ||
} | ||
const re = { | ||
value: $, | ||
nonce: 0, | ||
deadline: I() | ||
}, re = { | ||
deadline: k() | ||
}, ae = { | ||
allowed: !0, | ||
nonce: 0, | ||
expiry: I() | ||
}, D = {}, h = { | ||
expiry: k() | ||
}, D = {}, y = { | ||
type: "unsupported" | ||
}; | ||
async function ye(r) { | ||
async function Pe(r) { | ||
const { | ||
@@ -297,6 +334,6 @@ tokenAddress: n, | ||
return t; | ||
const o = ae(r); | ||
const o = oe(r); | ||
return D[a] = o, o; | ||
} | ||
async function ae(r) { | ||
async function oe(r) { | ||
const { | ||
@@ -308,50 +345,50 @@ spender: n, | ||
minGasLimit: o | ||
} = r, c = z(a, t), l = T.address; | ||
let d; | ||
} = r, i = z(a, t), l = b.address; | ||
let u; | ||
try { | ||
d = await J(e, a, t); | ||
u = await J(e, a, t); | ||
} catch { | ||
console.debug(`[checkTokenIsPermittable] Couldn't fetch eip712domain for token ${e}`); | ||
} | ||
let s = d == null ? void 0 : d.name; | ||
let c = u == null ? void 0 : u.name; | ||
try { | ||
s || (s = await te(e, a, t)); | ||
} catch (i) { | ||
if (/ETIMEDOUT/.test(i) && !s) | ||
c || (c = await te(e, a, t)); | ||
} 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 '${s}'`, i); | ||
console.debug(`[checkTokenIsPermittable] Couldn't fetch token name from the contract for token ${e}, using provided '${c}'`, s); | ||
} | ||
if (!s) | ||
if (!c) | ||
return { | ||
error: `Token name could not be determined for ${e}` | ||
}; | ||
let u; | ||
let m; | ||
try { | ||
u = await c.getTokenNonce(e, l); | ||
} catch (i) { | ||
return i === "nonce not supported" || i.message === "nonce is NaN" ? (console.debug(`[checkTokenIsPermittable] Not a permittable token ${e} - ${s}`, (i == null ? void 0 : i.message) || i), { | ||
...h, | ||
name: s | ||
}) : (console.debug(`[checkTokenIsPermittable] Failed to get nonce for ${e} - ${s}`, i), { | ||
error: i.message || i.toString() | ||
m = await i.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 m = d == null ? void 0 : d.version; | ||
if (!V.has(e) && m === void 0) | ||
let d = u == null ? void 0 : u.version; | ||
if (d === void 0) | ||
try { | ||
m = await c.getTokenVersion(e); | ||
} catch (i) { | ||
console.debug(`[checkTokenIsPermittable] Failed to get version for ${e} - ${s}`, i); | ||
d = await M(e, t); | ||
} catch (s) { | ||
console.debug(`[checkTokenIsPermittable] Failed to get version for ${e} - ${c}`, s); | ||
} | ||
const p = { | ||
chainId: a, | ||
eip2612PermitUtils: c, | ||
nonce: u, | ||
eip2612PermitUtils: i, | ||
nonce: m, | ||
spender: n, | ||
tokenAddress: e, | ||
tokenName: s, | ||
tokenName: c, | ||
walletAddress: l, | ||
version: m, | ||
version: d, | ||
minGasLimit: o | ||
@@ -365,16 +402,16 @@ }; | ||
}); | ||
} catch (i) { | ||
} catch (s) { | ||
try { | ||
return await ie(e, c) ? await A({ | ||
return await se(e, i) ? await A({ | ||
...p, | ||
type: "dai-like", | ||
provider: t | ||
}) : /invalid signature/.test(i) || (i == null ? void 0 : i.code) === "UNPREDICTABLE_GAS_LIMIT" ? (console.debug(`[checkTokenIsPermittable] Token ${e} - ${s} might be permittable, but it's not supported for now. Reason:`, i == null ? void 0 : i.reason), { | ||
...h, | ||
name: s | ||
}) : (console.debug(`[checkTokenIsPermittable] Failed to estimate eip-2612 permit for ${e} - ${s}`, i), { | ||
error: i.message || i.toString() | ||
}) : /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 (f) { | ||
return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${e} - ${s}`, f), { | ||
return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${e} - ${c}`, f), { | ||
error: f.message || f.toString() | ||
@@ -392,7 +429,7 @@ }; | ||
type: o, | ||
version: c, | ||
minGasLimit: l = K | ||
} = r, s = await (o === "eip-2612" ? oe : se)(r); | ||
return s ? (await n.estimateGas({ | ||
data: s, | ||
version: i, | ||
minGasLimit: l = V | ||
} = r, c = await (o === "eip-2612" ? ie : ce)(r); | ||
return c ? (await n.estimateGas({ | ||
data: c, | ||
from: e, | ||
@@ -402,13 +439,13 @@ to: a | ||
type: o, | ||
version: c, | ||
version: i, | ||
name: t | ||
} : { | ||
...h, | ||
...y, | ||
name: t | ||
} : { | ||
...h, | ||
...y, | ||
name: t | ||
}; | ||
} | ||
async function oe(r) { | ||
async function ie(r) { | ||
const { | ||
@@ -420,5 +457,5 @@ eip2612PermitUtils: n, | ||
chainId: o, | ||
tokenName: c, | ||
tokenName: i, | ||
tokenAddress: l, | ||
version: d | ||
version: u | ||
} = r; | ||
@@ -428,13 +465,13 @@ return S({ | ||
callDataParams: [{ | ||
...ne, | ||
...re, | ||
owner: e, | ||
spender: a, | ||
nonce: t | ||
}, +o, c, l, d] | ||
}, +o, i, l, u] | ||
}); | ||
} | ||
async function ie(r, n) { | ||
async function se(r, n) { | ||
return await n.getPermitTypeHash(r) === _; | ||
} | ||
async function se(r) { | ||
async function ce(r) { | ||
const { | ||
@@ -446,24 +483,24 @@ eip2612PermitUtils: n, | ||
nonce: o, | ||
chainId: c, | ||
chainId: i, | ||
tokenName: l, | ||
version: d | ||
version: u | ||
} = r; | ||
return await n.getPermitTypeHash(e) === _ ? R({ | ||
return await n.getPermitTypeHash(e) === _ ? N({ | ||
eip2162Utils: n, | ||
callDataParams: [{ | ||
...re, | ||
...ae, | ||
holder: a, | ||
spender: t, | ||
nonce: o | ||
}, c, l, e, d] | ||
}, i, l, e, u] | ||
}) : !1; | ||
} | ||
export { | ||
K as DEFAULT_MIN_GAS_LIMIT, | ||
T as PERMIT_SIGNER, | ||
ge as checkIsCallDataAValidPermit, | ||
V as DEFAULT_MIN_GAS_LIMIT, | ||
b as PERMIT_SIGNER, | ||
ye as checkIsCallDataAValidPermit, | ||
he as generatePermitHook, | ||
z as getPermitUtilsInstance, | ||
ye as getTokenPermitInfo, | ||
q as isSupportedPermitInfo | ||
Pe as getTokenPermitInfo, | ||
W as isSupportedPermitInfo | ||
}; |
{ | ||
"name": "@cowprotocol/permit-utils", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
Sorry, the diff of this file is not supported yet
31742
20
643