Use hardhat-exposed to reduce the need for mocks (#3666)
Co-authored-by: Francisco <fg@frang.io>
This commit is contained in:
@ -1,12 +1,13 @@
|
||||
const { constants, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
|
||||
const SafeERC20 = artifacts.require('$SafeERC20');
|
||||
const ERC20ReturnFalseMock = artifacts.require('ERC20ReturnFalseMock');
|
||||
const ERC20ReturnTrueMock = artifacts.require('ERC20ReturnTrueMock');
|
||||
const ERC20NoReturnMock = artifacts.require('ERC20NoReturnMock');
|
||||
const ERC20PermitNoRevertMock = artifacts.require('ERC20PermitNoRevertMock');
|
||||
const SafeERC20Wrapper = artifacts.require('SafeERC20Wrapper');
|
||||
|
||||
const { EIP712Domain, Permit } = require('../../../helpers/eip712');
|
||||
const { getChainId } = require('../../../helpers/chainid');
|
||||
|
||||
const { fromRpcSig } = require('ethereumjs-util');
|
||||
const ethSigUtil = require('eth-sig-util');
|
||||
@ -15,9 +16,13 @@ const Wallet = require('ethereumjs-wallet').default;
|
||||
contract('SafeERC20', function (accounts) {
|
||||
const [ hasNoCode ] = accounts;
|
||||
|
||||
before(async function () {
|
||||
this.mock = await SafeERC20.new();
|
||||
});
|
||||
|
||||
describe('with address that has no contract code', function () {
|
||||
beforeEach(async function () {
|
||||
this.wrapper = await SafeERC20Wrapper.new(hasNoCode);
|
||||
this.token = { address: hasNoCode };
|
||||
});
|
||||
|
||||
shouldRevertOnAllCalls('Address: call to non-contract');
|
||||
@ -25,7 +30,7 @@ contract('SafeERC20', function (accounts) {
|
||||
|
||||
describe('with token that returns false on all calls', function () {
|
||||
beforeEach(async function () {
|
||||
this.wrapper = await SafeERC20Wrapper.new((await ERC20ReturnFalseMock.new()).address);
|
||||
this.token = await ERC20ReturnFalseMock.new();
|
||||
});
|
||||
|
||||
shouldRevertOnAllCalls('SafeERC20: ERC20 operation did not succeed');
|
||||
@ -33,7 +38,7 @@ contract('SafeERC20', function (accounts) {
|
||||
|
||||
describe('with token that returns true on all calls', function () {
|
||||
beforeEach(async function () {
|
||||
this.wrapper = await SafeERC20Wrapper.new((await ERC20ReturnTrueMock.new()).address);
|
||||
this.token = await ERC20ReturnTrueMock.new();
|
||||
});
|
||||
|
||||
shouldOnlyRevertOnErrors();
|
||||
@ -41,7 +46,7 @@ contract('SafeERC20', function (accounts) {
|
||||
|
||||
describe('with token that returns no boolean values', function () {
|
||||
beforeEach(async function () {
|
||||
this.wrapper = await SafeERC20Wrapper.new((await ERC20NoReturnMock.new()).address);
|
||||
this.token = await ERC20NoReturnMock.new();
|
||||
});
|
||||
|
||||
shouldOnlyRevertOnErrors();
|
||||
@ -53,10 +58,9 @@ contract('SafeERC20', function (accounts) {
|
||||
const spender = hasNoCode;
|
||||
|
||||
beforeEach(async function () {
|
||||
this.token = await ERC20PermitNoRevertMock.new();
|
||||
this.wrapper = await SafeERC20Wrapper.new(this.token.address);
|
||||
const chainId = await getChainId();
|
||||
|
||||
const chainId = await this.token.getChainId();
|
||||
this.token = await ERC20PermitNoRevertMock.new();
|
||||
|
||||
this.data = {
|
||||
primaryType: 'Permit',
|
||||
@ -71,7 +75,8 @@ contract('SafeERC20', function (accounts) {
|
||||
expect(await this.token.nonces(owner)).to.be.bignumber.equal('0');
|
||||
expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal('0');
|
||||
|
||||
await this.wrapper.permit(
|
||||
await this.mock.$safePermit(
|
||||
this.token.address,
|
||||
this.data.message.owner,
|
||||
this.data.message.spender,
|
||||
this.data.message.value,
|
||||
@ -88,7 +93,8 @@ contract('SafeERC20', function (accounts) {
|
||||
it('revert on reused signature', async function () {
|
||||
expect(await this.token.nonces(owner)).to.be.bignumber.equal('0');
|
||||
// use valid signature and consume nounce
|
||||
await this.wrapper.permit(
|
||||
await this.mock.$safePermit(
|
||||
this.token.address,
|
||||
this.data.message.owner,
|
||||
this.data.message.spender,
|
||||
this.data.message.value,
|
||||
@ -111,7 +117,8 @@ contract('SafeERC20', function (accounts) {
|
||||
expect(await this.token.nonces(owner)).to.be.bignumber.equal('1');
|
||||
// invalid call revert when called through the SafeERC20 library
|
||||
await expectRevert(
|
||||
this.wrapper.permit(
|
||||
this.mock.$safePermit(
|
||||
this.token.address,
|
||||
this.data.message.owner,
|
||||
this.data.message.spender,
|
||||
this.data.message.value,
|
||||
@ -146,7 +153,8 @@ contract('SafeERC20', function (accounts) {
|
||||
|
||||
// invalid call revert when called through the SafeERC20 library
|
||||
await expectRevert(
|
||||
this.wrapper.permit(
|
||||
this.mock.$safePermit(
|
||||
this.token.address,
|
||||
this.data.message.owner,
|
||||
this.data.message.spender,
|
||||
this.data.message.value,
|
||||
@ -163,58 +171,67 @@ contract('SafeERC20', function (accounts) {
|
||||
|
||||
function shouldRevertOnAllCalls (reason) {
|
||||
it('reverts on transfer', async function () {
|
||||
await expectRevert(this.wrapper.transfer(), reason);
|
||||
await expectRevert(
|
||||
this.mock.$safeTransfer(this.token.address, constants.ZERO_ADDRESS, 0),
|
||||
reason,
|
||||
);
|
||||
});
|
||||
|
||||
it('reverts on transferFrom', async function () {
|
||||
await expectRevert(this.wrapper.transferFrom(), reason);
|
||||
await expectRevert(
|
||||
this.mock.$safeTransferFrom(this.token.address, this.mock.address, constants.ZERO_ADDRESS, 0),
|
||||
reason,
|
||||
);
|
||||
});
|
||||
|
||||
it('reverts on approve', async function () {
|
||||
await expectRevert(this.wrapper.approve(0), reason);
|
||||
await expectRevert(
|
||||
this.mock.$safeApprove(this.token.address, constants.ZERO_ADDRESS, 0),
|
||||
reason,
|
||||
);
|
||||
});
|
||||
|
||||
it('reverts on increaseAllowance', async function () {
|
||||
// [TODO] make sure it's reverting for the right reason
|
||||
await expectRevert.unspecified(this.wrapper.increaseAllowance(0));
|
||||
await expectRevert.unspecified(this.mock.$safeIncreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 0));
|
||||
});
|
||||
|
||||
it('reverts on decreaseAllowance', async function () {
|
||||
// [TODO] make sure it's reverting for the right reason
|
||||
await expectRevert.unspecified(this.wrapper.decreaseAllowance(0));
|
||||
await expectRevert.unspecified(this.mock.$safeDecreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 0));
|
||||
});
|
||||
}
|
||||
|
||||
function shouldOnlyRevertOnErrors () {
|
||||
it('doesn\'t revert on transfer', async function () {
|
||||
await this.wrapper.transfer();
|
||||
await this.mock.$safeTransfer(this.token.address, constants.ZERO_ADDRESS, 0);
|
||||
});
|
||||
|
||||
it('doesn\'t revert on transferFrom', async function () {
|
||||
await this.wrapper.transferFrom();
|
||||
await this.mock.$safeTransferFrom(this.token.address, this.mock.address, constants.ZERO_ADDRESS, 0);
|
||||
});
|
||||
|
||||
describe('approvals', function () {
|
||||
context('with zero allowance', function () {
|
||||
beforeEach(async function () {
|
||||
await this.wrapper.setAllowance(0);
|
||||
await this.token.setAllowance(this.mock.address, 0);
|
||||
});
|
||||
|
||||
it('doesn\'t revert when approving a non-zero allowance', async function () {
|
||||
await this.wrapper.approve(100);
|
||||
await this.mock.$safeApprove(this.token.address, constants.ZERO_ADDRESS, 100);
|
||||
});
|
||||
|
||||
it('doesn\'t revert when approving a zero allowance', async function () {
|
||||
await this.wrapper.approve(0);
|
||||
await this.mock.$safeApprove(this.token.address, constants.ZERO_ADDRESS, 0);
|
||||
});
|
||||
|
||||
it('doesn\'t revert when increasing the allowance', async function () {
|
||||
await this.wrapper.increaseAllowance(10);
|
||||
await this.mock.$safeIncreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 10);
|
||||
});
|
||||
|
||||
it('reverts when decreasing the allowance', async function () {
|
||||
await expectRevert(
|
||||
this.wrapper.decreaseAllowance(10),
|
||||
this.mock.$safeDecreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 10),
|
||||
'SafeERC20: decreased allowance below zero',
|
||||
);
|
||||
});
|
||||
@ -222,31 +239,31 @@ function shouldOnlyRevertOnErrors () {
|
||||
|
||||
context('with non-zero allowance', function () {
|
||||
beforeEach(async function () {
|
||||
await this.wrapper.setAllowance(100);
|
||||
await this.token.setAllowance(this.mock.address, 100);
|
||||
});
|
||||
|
||||
it('reverts when approving a non-zero allowance', async function () {
|
||||
await expectRevert(
|
||||
this.wrapper.approve(20),
|
||||
this.mock.$safeApprove(this.token.address, constants.ZERO_ADDRESS, 20),
|
||||
'SafeERC20: approve from non-zero to non-zero allowance',
|
||||
);
|
||||
});
|
||||
|
||||
it('doesn\'t revert when approving a zero allowance', async function () {
|
||||
await this.wrapper.approve(0);
|
||||
await this.mock.$safeApprove(this.token.address, constants.ZERO_ADDRESS, 0);
|
||||
});
|
||||
|
||||
it('doesn\'t revert when increasing the allowance', async function () {
|
||||
await this.wrapper.increaseAllowance(10);
|
||||
await this.mock.$safeIncreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 10);
|
||||
});
|
||||
|
||||
it('doesn\'t revert when decreasing the allowance to a positive value', async function () {
|
||||
await this.wrapper.decreaseAllowance(50);
|
||||
await this.mock.$safeDecreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 50);
|
||||
});
|
||||
|
||||
it('reverts when decreasing the allowance to a negative value', async function () {
|
||||
await expectRevert(
|
||||
this.wrapper.decreaseAllowance(200),
|
||||
this.mock.$safeDecreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 200),
|
||||
'SafeERC20: decreased allowance below zero',
|
||||
);
|
||||
});
|
||||
|
||||
@ -2,7 +2,7 @@ const { BN, expectRevert, time } = require('@openzeppelin/test-helpers');
|
||||
|
||||
const { expect } = require('chai');
|
||||
|
||||
const ERC20Mock = artifacts.require('ERC20Mock');
|
||||
const ERC20 = artifacts.require('$ERC20');
|
||||
const TokenTimelock = artifacts.require('TokenTimelock');
|
||||
|
||||
contract('TokenTimelock', function (accounts) {
|
||||
@ -15,7 +15,7 @@ contract('TokenTimelock', function (accounts) {
|
||||
|
||||
context('with token', function () {
|
||||
beforeEach(async function () {
|
||||
this.token = await ERC20Mock.new(name, symbol, beneficiary, 0); // We're not using the preminted tokens
|
||||
this.token = await ERC20.new(name, symbol);
|
||||
});
|
||||
|
||||
it('rejects a release time in the past', async function () {
|
||||
@ -30,7 +30,7 @@ contract('TokenTimelock', function (accounts) {
|
||||
beforeEach(async function () {
|
||||
this.releaseTime = (await time.latest()).add(time.duration.years(1));
|
||||
this.timelock = await TokenTimelock.new(this.token.address, beneficiary, this.releaseTime);
|
||||
await this.token.mint(this.timelock.address, amount);
|
||||
await this.token.$_mint(this.timelock.address, amount);
|
||||
});
|
||||
|
||||
it('can get state', async function () {
|
||||
|
||||
Reference in New Issue
Block a user