forked from sushi-labs/sushiswap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.js
45 lines (41 loc) · 1.3 KB
/
utilities.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const {
ethers: { utils: { keccak256, defaultAbiCoder, toUtf8Bytes, solidityPack } },
} = require("ethers")
const PERMIT_TYPEHASH = keccak256(toUtf8Bytes("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"))
function getDomainSeparator(name, chainId, tokenAddress) {
return keccak256(
defaultAbiCoder.encode(
["bytes32", "bytes32", "bytes32", "uint256", "address"],
[
keccak256(toUtf8Bytes("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")),
keccak256(toUtf8Bytes(name)),
keccak256(toUtf8Bytes("1")),
chainId,
tokenAddress,
]
)
)
}
async function getApprovalDigest(token, chainId, approve, nonce, deadline) {
const name = await token.name()
const DOMAIN_SEPARATOR = getDomainSeparator(name, chainId, token.address)
return keccak256(
solidityPack(
["bytes1", "bytes1", "bytes32", "bytes32"],
[
"0x19",
"0x01",
DOMAIN_SEPARATOR,
keccak256(
defaultAbiCoder.encode(
["bytes32", "address", "address", "uint256", "uint256", "uint256"],
[PERMIT_TYPEHASH, approve.owner, approve.spender, approve.value, nonce, deadline]
)
),
]
)
)
}
module.exports = {
getApprovalDigest
}