Merge branch 'master' into audit/wip/2a-2b

This commit is contained in:
Hadrien Croubois
2023-09-13 11:14:09 +02:00
72 changed files with 1159 additions and 1430 deletions

View File

@ -14,6 +14,10 @@ contract('Ownable', function (accounts) {
this.ownable = await Ownable.new(owner);
});
it('rejects zero address for initialOwner', async function () {
await expectRevertCustomError(Ownable.new(constants.ZERO_ADDRESS), 'OwnableInvalidOwner', [constants.ZERO_ADDRESS]);
});
it('has an owner', async function () {
expect(await this.ownable.owner()).to.equal(owner);
});

View File

@ -23,7 +23,7 @@ contract('VestingWallet', function (accounts) {
it('rejects zero address for beneficiary', async function () {
await expectRevertCustomError(
VestingWallet.new(constants.ZERO_ADDRESS, this.start, duration),
'VestingWalletInvalidBeneficiary',
'OwnableInvalidOwner',
[constants.ZERO_ADDRESS],
);
});

View File

@ -42,167 +42,6 @@ contract('ERC20', function (accounts) {
expect(await this.token.decimals()).to.be.bignumber.equal('18');
});
describe('decrease allowance', function () {
describe('when the spender is not the zero address', function () {
const spender = recipient;
function shouldDecreaseApproval(value) {
describe('when there was no approved value before', function () {
it('reverts', async function () {
const allowance = await this.token.allowance(initialHolder, spender);
await expectRevertCustomError(
this.token.decreaseAllowance(spender, value, { from: initialHolder }),
'ERC20FailedDecreaseAllowance',
[spender, allowance, value],
);
});
});
describe('when the spender had an approved value', function () {
const approvedValue = value;
beforeEach(async function () {
await this.token.approve(spender, approvedValue, { from: initialHolder });
});
it('emits an approval event', async function () {
expectEvent(
await this.token.decreaseAllowance(spender, approvedValue, { from: initialHolder }),
'Approval',
{ owner: initialHolder, spender: spender, value: new BN(0) },
);
});
it('decreases the spender allowance subtracting the requested value', async function () {
await this.token.decreaseAllowance(spender, approvedValue.subn(1), { from: initialHolder });
expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal('1');
});
it('sets the allowance to zero when all allowance is removed', async function () {
await this.token.decreaseAllowance(spender, approvedValue, { from: initialHolder });
expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal('0');
});
it('reverts when more than the full allowance is removed', async function () {
await expectRevertCustomError(
this.token.decreaseAllowance(spender, approvedValue.addn(1), { from: initialHolder }),
'ERC20FailedDecreaseAllowance',
[spender, approvedValue, approvedValue.addn(1)],
);
});
});
}
describe('when the sender has enough balance', function () {
const value = initialSupply;
shouldDecreaseApproval(value);
});
describe('when the sender does not have enough balance', function () {
const value = initialSupply.addn(1);
shouldDecreaseApproval(value);
});
});
describe('when the spender is the zero address', function () {
const value = initialSupply;
const spender = ZERO_ADDRESS;
it('reverts', async function () {
await expectRevertCustomError(
this.token.decreaseAllowance(spender, value, { from: initialHolder }),
'ERC20FailedDecreaseAllowance',
[spender, 0, value],
);
});
});
});
describe('increase allowance', function () {
const value = initialSupply;
describe('when the spender is not the zero address', function () {
const spender = recipient;
describe('when the sender has enough balance', function () {
it('emits an approval event', async function () {
expectEvent(await this.token.increaseAllowance(spender, value, { from: initialHolder }), 'Approval', {
owner: initialHolder,
spender: spender,
value: value,
});
});
describe('when there was no approved value before', function () {
it('approves the requested value', async function () {
await this.token.increaseAllowance(spender, value, { from: initialHolder });
expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(value);
});
});
describe('when the spender had an approved value', function () {
beforeEach(async function () {
await this.token.approve(spender, new BN(1), { from: initialHolder });
});
it('increases the spender allowance adding the requested value', async function () {
await this.token.increaseAllowance(spender, value, { from: initialHolder });
expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(value.addn(1));
});
});
});
describe('when the sender does not have enough balance', function () {
const value = initialSupply.addn(1);
it('emits an approval event', async function () {
expectEvent(await this.token.increaseAllowance(spender, value, { from: initialHolder }), 'Approval', {
owner: initialHolder,
spender: spender,
value: value,
});
});
describe('when there was no approved value before', function () {
it('approves the requested value', async function () {
await this.token.increaseAllowance(spender, value, { from: initialHolder });
expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(value);
});
});
describe('when the spender had an approved value', function () {
beforeEach(async function () {
await this.token.approve(spender, new BN(1), { from: initialHolder });
});
it('increases the spender allowance adding the requested value', async function () {
await this.token.increaseAllowance(spender, value, { from: initialHolder });
expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(value.addn(1));
});
});
});
});
describe('when the spender is the zero address', function () {
const spender = ZERO_ADDRESS;
it('reverts', async function () {
await expectRevertCustomError(
this.token.increaseAllowance(spender, value, { from: initialHolder }),
'ERC20InvalidSpender',
[ZERO_ADDRESS],
);
});
});
});
describe('_mint', function () {
const value = new BN(50);
it('rejects a null account', async function () {

View File

@ -4,16 +4,10 @@ const SafeERC20 = artifacts.require('$SafeERC20');
const ERC20ReturnFalseMock = artifacts.require('$ERC20ReturnFalseMock');
const ERC20ReturnTrueMock = artifacts.require('$ERC20'); // default implementation returns true
const ERC20NoReturnMock = artifacts.require('$ERC20NoReturnMock');
const ERC20PermitNoRevertMock = artifacts.require('$ERC20PermitNoRevertMock');
const ERC20ForceApproveMock = artifacts.require('$ERC20ForceApproveMock');
const { getDomain, domainType, Permit } = require('../../../helpers/eip712');
const { expectRevertCustomError } = require('../../../helpers/customError');
const { fromRpcSig } = require('ethereumjs-util');
const ethSigUtil = require('eth-sig-util');
const Wallet = require('ethereumjs-wallet').default;
const name = 'ERC20Mock';
const symbol = 'ERC20Mock';
@ -122,123 +116,6 @@ contract('SafeERC20', function (accounts) {
shouldOnlyRevertOnErrors(accounts);
});
describe("with token that doesn't revert on invalid permit", function () {
const wallet = Wallet.generate();
const owner = wallet.getAddressString();
const spender = hasNoCode;
beforeEach(async function () {
this.token = await ERC20PermitNoRevertMock.new(name, symbol, name);
this.data = await getDomain(this.token).then(domain => ({
primaryType: 'Permit',
types: { EIP712Domain: domainType(domain), Permit },
domain,
message: { owner, spender, value: '42', nonce: '0', deadline: constants.MAX_UINT256 },
}));
this.signature = fromRpcSig(ethSigUtil.signTypedMessage(wallet.getPrivateKey(), { data: this.data }));
});
it('accepts owner signature', async function () {
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.mock.$safePermit(
this.token.address,
this.data.message.owner,
this.data.message.spender,
this.data.message.value,
this.data.message.deadline,
this.signature.v,
this.signature.r,
this.signature.s,
);
expect(await this.token.nonces(owner)).to.be.bignumber.equal('1');
expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(this.data.message.value);
});
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.mock.$safePermit(
this.token.address,
this.data.message.owner,
this.data.message.spender,
this.data.message.value,
this.data.message.deadline,
this.signature.v,
this.signature.r,
this.signature.s,
);
expect(await this.token.nonces(owner)).to.be.bignumber.equal('1');
// invalid call does not revert for this token implementation
await this.token.permit(
this.data.message.owner,
this.data.message.spender,
this.data.message.value,
this.data.message.deadline,
this.signature.v,
this.signature.r,
this.signature.s,
);
expect(await this.token.nonces(owner)).to.be.bignumber.equal('1');
// invalid call revert when called through the SafeERC20 library
await expectRevertCustomError(
this.mock.$safePermit(
this.token.address,
this.data.message.owner,
this.data.message.spender,
this.data.message.value,
this.data.message.deadline,
this.signature.v,
this.signature.r,
this.signature.s,
),
'SafeERC20FailedOperation',
[this.token.address],
);
expect(await this.token.nonces(owner)).to.be.bignumber.equal('1');
});
it('revert on invalid signature', async function () {
// signature that is not valid for owner
const invalidSignature = {
v: 27,
r: '0x71753dc5ecb5b4bfc0e3bc530d79ce5988760ed3f3a234c86a5546491f540775',
s: '0x0049cedee5aed990aabed5ad6a9f6e3c565b63379894b5fa8b512eb2b79e485d',
};
// invalid call does not revert for this token implementation
await this.token.permit(
this.data.message.owner,
this.data.message.spender,
this.data.message.value,
this.data.message.deadline,
invalidSignature.v,
invalidSignature.r,
invalidSignature.s,
);
// invalid call revert when called through the SafeERC20 library
await expectRevertCustomError(
this.mock.$safePermit(
this.token.address,
this.data.message.owner,
this.data.message.spender,
this.data.message.value,
this.data.message.deadline,
invalidSignature.v,
invalidSignature.r,
invalidSignature.s,
),
'SafeERC20FailedOperation',
[this.token.address],
);
});
});
describe('with usdt approval beaviour', function () {
const spender = hasNoCode;