Migrate utils to ethersjs v6 (#4736)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
Renan Souza
2023-11-24 01:32:30 +00:00
committed by GitHub
parent 330c39b662
commit 78d5708340
13 changed files with 497 additions and 602 deletions

View File

@ -1,44 +1,47 @@
const { expectRevert } = require('@openzeppelin/test-helpers');
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { expectRevertCustomError } = require('../helpers/customError');
async function fixture() {
const mock = await ethers.deployContract('ReentrancyMock');
return { mock };
}
const ReentrancyMock = artifacts.require('ReentrancyMock');
const ReentrancyAttack = artifacts.require('ReentrancyAttack');
contract('ReentrancyGuard', function () {
describe('ReentrancyGuard', function () {
beforeEach(async function () {
this.reentrancyMock = await ReentrancyMock.new();
expect(await this.reentrancyMock.counter()).to.be.bignumber.equal('0');
Object.assign(this, await loadFixture(fixture));
});
it('nonReentrant function can be called', async function () {
expect(await this.reentrancyMock.counter()).to.be.bignumber.equal('0');
await this.reentrancyMock.callback();
expect(await this.reentrancyMock.counter()).to.be.bignumber.equal('1');
expect(await this.mock.counter()).to.equal(0n);
await this.mock.callback();
expect(await this.mock.counter()).to.equal(1n);
});
it('does not allow remote callback', async function () {
const attacker = await ReentrancyAttack.new();
await expectRevert(this.reentrancyMock.countAndCall(attacker.address), 'ReentrancyAttack: failed call', []);
const attacker = await ethers.deployContract('ReentrancyAttack');
await expect(this.mock.countAndCall(attacker)).to.be.revertedWith('ReentrancyAttack: failed call');
});
it('_reentrancyGuardEntered should be true when guarded', async function () {
await this.reentrancyMock.guardedCheckEntered();
await this.mock.guardedCheckEntered();
});
it('_reentrancyGuardEntered should be false when unguarded', async function () {
await this.reentrancyMock.unguardedCheckNotEntered();
await this.mock.unguardedCheckNotEntered();
});
// The following are more side-effects than intended behavior:
// I put them here as documentation, and to monitor any changes
// in the side-effects.
it('does not allow local recursion', async function () {
await expectRevertCustomError(this.reentrancyMock.countLocalRecursive(10), 'ReentrancyGuardReentrantCall', []);
await expect(this.mock.countLocalRecursive(10n)).to.be.revertedWithCustomError(
this.mock,
'ReentrancyGuardReentrantCall',
);
});
it('does not allow indirect local recursion', async function () {
await expectRevert(this.reentrancyMock.countThisRecursive(10), 'ReentrancyMock: failed call', []);
await expect(this.mock.countThisRecursive(10n)).to.be.revertedWith('ReentrancyMock: failed call');
});
});