forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathERC20TemporaryApproval.test.js
More file actions
142 lines (128 loc) · 5.09 KB
/
ERC20TemporaryApproval.test.js
File metadata and controls
142 lines (128 loc) · 5.09 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { max, min } = require('../../../helpers/math.js');
const { shouldBehaveLikeERC20 } = require('../ERC20.behavior.js');
const name = 'My Token';
const symbol = 'MTKN';
const initialSupply = 100n;
async function fixture() {
// this.accounts is used by shouldBehaveLikeERC20
const accounts = await ethers.getSigners();
const [holder, recipient, other] = accounts;
const token = await ethers.deployContract('$ERC20TemporaryApproval', [name, symbol]);
await token.$_mint(holder, initialSupply);
const spender = await ethers.deployContract('$Address');
const batch = await ethers.deployContract('BatchCaller');
const getter = await ethers.deployContract('ERC20GetterHelper');
return { accounts, holder, recipient, other, token, spender, batch, getter };
}
describe('ERC20TemporaryApproval', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
shouldBehaveLikeERC20(initialSupply);
describe('setting and spending temporary allowance', function () {
beforeEach(async function () {
await this.token.connect(this.holder).transfer(this.batch, initialSupply);
});
for (let {
description,
persistentAllowance,
temporaryAllowance,
amount,
temporaryExpected,
persistentExpected,
} of [
{ description: 'can set temporary allowance', temporaryAllowance: 42n },
{
description: 'can set temporary allowance on top of persistent allowance',
temporaryAllowance: 42n,
persistentAllowance: 17n,
},
{ description: 'support allowance overflow', temporaryAllowance: ethers.MaxUint256, persistentAllowance: 17n },
{ description: 'consumming temporary allowance alone', temporaryAllowance: 42n, amount: 2n },
{
description: 'fallback to persistent allowance if temporary allowance is not sufficient',
temporaryAllowance: 42n,
persistentAllowance: 17n,
amount: 50n,
},
{
description: 'do not reduce infinite temporary allowance #1',
temporaryAllowance: ethers.MaxUint256,
amount: 50n,
temporaryExpected: ethers.MaxUint256,
},
{
description: 'do not reduce infinite temporary allowance #2',
temporaryAllowance: 17n,
persistentAllowance: ethers.MaxUint256,
amount: 50n,
temporaryExpected: ethers.MaxUint256,
persistentExpected: ethers.MaxUint256,
},
]) {
persistentAllowance ??= 0n;
temporaryAllowance ??= 0n;
amount ??= 0n;
temporaryExpected ??= min(persistentAllowance + temporaryAllowance - amount, ethers.MaxUint256);
persistentExpected ??= persistentAllowance - max(amount - temporaryAllowance, 0n);
it(description, async function () {
await expect(
this.batch.execute(
[
persistentAllowance && {
target: this.token,
value: 0n,
data: this.token.interface.encodeFunctionData('approve', [this.spender.target, persistentAllowance]),
},
temporaryAllowance && {
target: this.token,
value: 0n,
data: this.token.interface.encodeFunctionData('temporaryApprove', [
this.spender.target,
temporaryAllowance,
]),
},
amount && {
target: this.spender,
value: 0n,
data: this.spender.interface.encodeFunctionData('$functionCall', [
this.token.target,
this.token.interface.encodeFunctionData('transferFrom', [
this.batch.target,
this.recipient.address,
amount,
]),
]),
},
{
target: this.getter,
value: 0n,
data: this.getter.interface.encodeFunctionData('allowance', [
this.token.target,
this.batch.target,
this.spender.target,
]),
},
].filter(Boolean),
),
)
.to.emit(this.getter, 'ERC20Allowance')
.withArgs(this.token, this.batch, this.spender, temporaryExpected);
expect(await this.token.allowance(this.batch, this.spender)).to.equal(persistentExpected);
});
}
it('reverts when the recipient is the zero address', async function () {
await expect(this.token.connect(this.holder).temporaryApprove(ethers.ZeroAddress, 1n))
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidSpender')
.withArgs(ethers.ZeroAddress);
});
it('reverts when the token owner is the zero address', async function () {
await expect(this.token.$_temporaryApprove(ethers.ZeroAddress, this.recipient, 1n))
.to.be.revertedWithCustomError(this.token, 'ERC20InvalidApprover')
.withArgs(ethers.ZeroAddress);
});
});
});