Skip to content

Commit 27b1fe5

Browse files
committed
add internal _decreaseAllowance tests
1 parent c2c7ad0 commit 27b1fe5

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

contracts/token/ERC20/base/ERC20BaseMock.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,12 @@ contract ERC20BaseMock is ERC20Base {
2828
) external {
2929
_approve(holder, spender, amount);
3030
}
31+
32+
function __decreaseAllowance(
33+
address holder,
34+
address spender,
35+
uint256 amount
36+
) external {
37+
_decreaseAllowance(holder, spender, amount);
38+
}
3139
}

test/token/ERC20/ERC20Base.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,87 @@ describe('ERC20Base', function () {
249249
});
250250
});
251251

252+
describe('#_decreaseAllowance(address,address,uint256)', function () {
253+
it('reduces approval of spender with respect to holder by given amount', async function () {
254+
await instance.__approve(
255+
holder.address,
256+
spender.address,
257+
ethers.constants.Two,
258+
);
259+
260+
await instance
261+
.connect(holder)
262+
.__decreaseAllowance(
263+
holder.address,
264+
spender.address,
265+
ethers.constants.One,
266+
);
267+
268+
await expect(
269+
await instance.callStatic['allowance(address,address)'](
270+
holder.address,
271+
spender.address,
272+
),
273+
).to.equal(ethers.constants.One);
274+
275+
// decreases are cumulative
276+
await instance
277+
.connect(holder)
278+
.__decreaseAllowance(
279+
holder.address,
280+
spender.address,
281+
ethers.constants.One,
282+
);
283+
284+
await expect(
285+
await instance.callStatic['allowance(address,address)'](
286+
holder.address,
287+
spender.address,
288+
),
289+
).to.equal(ethers.constants.Zero);
290+
});
291+
292+
it('emits Approval event', async function () {
293+
await instance.__approve(
294+
holder.address,
295+
spender.address,
296+
ethers.constants.Two,
297+
);
298+
299+
await expect(
300+
instance.__decreaseAllowance(
301+
holder.address,
302+
spender.address,
303+
ethers.constants.One,
304+
),
305+
)
306+
.to.emit(instance, 'Approval')
307+
.withArgs(holder.address, spender.address, ethers.constants.One);
308+
});
309+
310+
describe('reverts if', function () {
311+
it('holder is the zero address', async function () {
312+
await expect(
313+
instance.__decreaseAllowance(
314+
ethers.constants.AddressZero,
315+
spender.address,
316+
ethers.constants.Zero,
317+
),
318+
).to.be.revertedWith('ERC20: approve from the zero address');
319+
});
320+
321+
it('spender is the zero address', async function () {
322+
await expect(
323+
instance.__decreaseAllowance(
324+
holder.address,
325+
ethers.constants.AddressZero,
326+
ethers.constants.Zero,
327+
),
328+
).to.be.revertedWith('ERC20: approve to the zero address');
329+
});
330+
});
331+
});
332+
252333
describe('#_beforeTokenTransfer(address,address,uint256)', function () {
253334
it('todo');
254335
});

0 commit comments

Comments
 (0)