-
Notifications
You must be signed in to change notification settings - Fork 1k
/
permit.ts
61 lines (59 loc) 路 1.47 KB
/
permit.ts
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { BigNumberish, constants, Signature, Wallet } from 'ethers'
import { splitSignature } from 'ethers/lib/utils'
import { TestERC20, TestERC20PermitAllowed } from '../../typechain'
export async function getPermitSignature(
wallet: Wallet,
token: TestERC20 | TestERC20PermitAllowed,
spender: string,
value: BigNumberish = constants.MaxUint256,
deadline = constants.MaxUint256,
permitConfig?: { nonce?: BigNumberish; name?: string; chainId?: number; version?: string }
): Promise<Signature> {
const [nonce, name, version, chainId] = await Promise.all([
permitConfig?.nonce ?? token.nonces(wallet.address),
permitConfig?.name ?? token.name(),
permitConfig?.version ?? '1',
permitConfig?.chainId ?? wallet.getChainId(),
])
return splitSignature(
await wallet._signTypedData(
{
name,
version,
chainId,
verifyingContract: token.address,
},
{
Permit: [
{
name: 'owner',
type: 'address',
},
{
name: 'spender',
type: 'address',
},
{
name: 'value',
type: 'uint256',
},
{
name: 'nonce',
type: 'uint256',
},
{
name: 'deadline',
type: 'uint256',
},
],
},
{
owner: wallet.address,
spender,
value,
nonce,
deadline,
}
)
)
}