Update docs

This commit is contained in:
github-actions
2023-05-09 19:56:30 +00:00
parent 47e38c7bda
commit 6ae39c4dc1
579 changed files with 30453 additions and 21485 deletions

View File

@ -1,69 +1,75 @@
const { constants, expectRevert } = require('@openzeppelin/test-helpers');
const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
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 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 { EIP712Domain, Permit } = require('../../../helpers/eip712');
const { getDomain, domainType, Permit } = require('../../../helpers/eip712');
const { fromRpcSig } = require('ethereumjs-util');
const ethSigUtil = require('eth-sig-util');
const Wallet = require('ethereumjs-wallet').default;
const name = 'ERC20Mock';
const symbol = 'ERC20Mock';
contract('SafeERC20', function (accounts) {
const [ hasNoCode ] = 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');
shouldRevertOnAllCalls(accounts, 'Address: call to non-contract');
});
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(name, symbol);
});
shouldRevertOnAllCalls('SafeERC20: ERC20 operation did not succeed');
shouldRevertOnAllCalls(accounts, 'SafeERC20: ERC20 operation did not succeed');
});
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(name, symbol);
});
shouldOnlyRevertOnErrors();
shouldOnlyRevertOnErrors(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(name, symbol);
});
shouldOnlyRevertOnErrors();
shouldOnlyRevertOnErrors(accounts);
});
describe('with token that doesn\'t revert on invalid permit', function () {
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();
this.wrapper = await SafeERC20Wrapper.new(this.token.address);
this.token = await ERC20PermitNoRevertMock.new(name, symbol, name);
const chainId = await this.token.getChainId();
this.data = {
this.data = await getDomain(this.token).then(domain => ({
primaryType: 'Permit',
types: { EIP712Domain, Permit },
domain: { name: 'ERC20PermitNoRevertMock', version: '1', chainId, verifyingContract: this.token.address },
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 }));
});
@ -71,7 +77,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 +95,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 +119,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 +155,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,
@ -159,62 +169,134 @@ contract('SafeERC20', function (accounts) {
);
});
});
describe('with usdt approval beaviour', function () {
const spender = hasNoCode;
beforeEach(async function () {
this.token = await ERC20ForceApproveMock.new(name, symbol);
});
describe('with initial approval', function () {
beforeEach(async function () {
await this.token.$_approve(this.mock.address, spender, 100);
});
it('safeApprove fails to update approval to non-zero', async function () {
await expectRevert(
this.mock.$safeApprove(this.token.address, spender, 200),
'SafeERC20: approve from non-zero to non-zero allowance',
);
});
it('safeApprove can update approval to zero', async function () {
await this.mock.$safeApprove(this.token.address, spender, 0);
});
it('safeApprove can increase approval', async function () {
await expectRevert(this.mock.$safeIncreaseAllowance(this.token.address, spender, 10), 'USDT approval failure');
});
it('safeApprove can decrease approval', async function () {
await expectRevert(this.mock.$safeDecreaseAllowance(this.token.address, spender, 10), 'USDT approval failure');
});
it('forceApprove works', async function () {
await this.mock.$forceApprove(this.token.address, spender, 200);
});
});
});
});
function shouldRevertOnAllCalls (reason) {
function shouldRevertOnAllCalls([receiver, spender], reason) {
it('reverts on transfer', async function () {
await expectRevert(this.wrapper.transfer(), reason);
await expectRevert(this.mock.$safeTransfer(this.token.address, receiver, 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, receiver, 0), reason);
});
it('reverts on approve', async function () {
await expectRevert(this.wrapper.approve(0), reason);
await expectRevert(this.mock.$safeApprove(this.token.address, spender, 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, spender, 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, spender, 0));
});
it('reverts on forceApprove', async function () {
await expectRevert(this.mock.$forceApprove(this.token.address, spender, 0), reason);
});
}
function shouldOnlyRevertOnErrors () {
it('doesn\'t revert on transfer', async function () {
await this.wrapper.transfer();
});
function shouldOnlyRevertOnErrors([owner, receiver, spender]) {
describe('transfers', function () {
beforeEach(async function () {
await this.token.$_mint(owner, 100);
await this.token.$_mint(this.mock.address, 100);
await this.token.approve(this.mock.address, constants.MAX_UINT256, { from: owner });
});
it('doesn\'t revert on transferFrom', async function () {
await this.wrapper.transferFrom();
it("doesn't revert on transfer", async function () {
const { tx } = await this.mock.$safeTransfer(this.token.address, receiver, 10);
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: this.mock.address,
to: receiver,
value: '10',
});
});
it("doesn't revert on transferFrom", async function () {
const { tx } = await this.mock.$safeTransferFrom(this.token.address, owner, receiver, 10);
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: owner,
to: receiver,
value: '10',
});
});
});
describe('approvals', function () {
context('with zero allowance', function () {
beforeEach(async function () {
await this.wrapper.setAllowance(0);
await this.token.$_approve(this.mock.address, spender, 0);
});
it('doesn\'t revert when approving a non-zero allowance', async function () {
await this.wrapper.approve(100);
it("doesn't revert when approving a non-zero allowance", async function () {
await this.mock.$safeApprove(this.token.address, spender, 100);
expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('100');
});
it('doesn\'t revert when approving a zero allowance', async function () {
await this.wrapper.approve(0);
it("doesn't revert when approving a zero allowance", async function () {
await this.mock.$safeApprove(this.token.address, spender, 0);
expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('0');
});
it('doesn\'t revert when increasing the allowance', async function () {
await this.wrapper.increaseAllowance(10);
it("doesn't revert when force approving a non-zero allowance", async function () {
await this.mock.$forceApprove(this.token.address, spender, 100);
expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('100');
});
it("doesn't revert when force approving a zero allowance", async function () {
await this.mock.$forceApprove(this.token.address, spender, 0);
expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('0');
});
it("doesn't revert when increasing the allowance", async function () {
await this.mock.$safeIncreaseAllowance(this.token.address, spender, 10);
expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('10');
});
it('reverts when decreasing the allowance', async function () {
await expectRevert(
this.wrapper.decreaseAllowance(10),
this.mock.$safeDecreaseAllowance(this.token.address, spender, 10),
'SafeERC20: decreased allowance below zero',
);
});
@ -222,31 +304,44 @@ function shouldOnlyRevertOnErrors () {
context('with non-zero allowance', function () {
beforeEach(async function () {
await this.wrapper.setAllowance(100);
await this.token.$_approve(this.mock.address, spender, 100);
});
it('reverts when approving a non-zero allowance', async function () {
await expectRevert(
this.wrapper.approve(20),
this.mock.$safeApprove(this.token.address, spender, 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);
it("doesn't revert when approving a zero allowance", async function () {
await this.mock.$safeApprove(this.token.address, spender, 0);
expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('0');
});
it('doesn\'t revert when increasing the allowance', async function () {
await this.wrapper.increaseAllowance(10);
it("doesn't revert when force approving a non-zero allowance", async function () {
await this.mock.$forceApprove(this.token.address, spender, 20);
expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('20');
});
it('doesn\'t revert when decreasing the allowance to a positive value', async function () {
await this.wrapper.decreaseAllowance(50);
it("doesn't revert when force approving a zero allowance", async function () {
await this.mock.$forceApprove(this.token.address, spender, 0);
expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('0');
});
it("doesn't revert when increasing the allowance", async function () {
await this.mock.$safeIncreaseAllowance(this.token.address, spender, 10);
expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('110');
});
it("doesn't revert when decreasing the allowance to a positive value", async function () {
await this.mock.$safeDecreaseAllowance(this.token.address, spender, 50);
expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('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, spender, 200),
'SafeERC20: decreased allowance below zero',
);
});

View File

@ -2,11 +2,11 @@ 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) {
const [ beneficiary ] = accounts;
const [beneficiary] = accounts;
const name = 'My Token';
const symbol = 'MTKN';
@ -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 () {