|
|
@ -1,10 +1,11 @@ |
|
|
|
const { constants, expectRevert } = require('@openzeppelin/test-helpers'); |
|
|
|
const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers'); |
|
|
|
|
|
|
|
|
|
|
|
const SafeERC20 = artifacts.require('$SafeERC20'); |
|
|
|
const SafeERC20 = artifacts.require('$SafeERC20'); |
|
|
|
const ERC20ReturnFalseMock = artifacts.require('ERC20ReturnFalseMock'); |
|
|
|
const ERC20ReturnFalseMock = artifacts.require('$ERC20ReturnFalseMock'); |
|
|
|
const ERC20ReturnTrueMock = artifacts.require('ERC20ReturnTrueMock'); |
|
|
|
const ERC20ReturnTrueMock = artifacts.require('$ERC20'); // default implementation returns true
|
|
|
|
const ERC20NoReturnMock = artifacts.require('ERC20NoReturnMock'); |
|
|
|
const ERC20NoReturnMock = artifacts.require('$ERC20NoReturnMock'); |
|
|
|
const ERC20PermitNoRevertMock = artifacts.require('ERC20PermitNoRevertMock'); |
|
|
|
const ERC20PermitNoRevertMock = artifacts.require('$ERC20PermitNoRevertMock'); |
|
|
|
|
|
|
|
const ERC20ForceApproveMock = artifacts.require('$ERC20ForceApproveMock'); |
|
|
|
|
|
|
|
|
|
|
|
const { getDomain, domainType, Permit } = require('../../../helpers/eip712'); |
|
|
|
const { getDomain, domainType, Permit } = require('../../../helpers/eip712'); |
|
|
|
|
|
|
|
|
|
|
@ -12,6 +13,9 @@ const { fromRpcSig } = require('ethereumjs-util'); |
|
|
|
const ethSigUtil = require('eth-sig-util'); |
|
|
|
const ethSigUtil = require('eth-sig-util'); |
|
|
|
const Wallet = require('ethereumjs-wallet').default; |
|
|
|
const Wallet = require('ethereumjs-wallet').default; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const name = 'ERC20Mock'; |
|
|
|
|
|
|
|
const symbol = 'ERC20Mock'; |
|
|
|
|
|
|
|
|
|
|
|
contract('SafeERC20', function (accounts) { |
|
|
|
contract('SafeERC20', function (accounts) { |
|
|
|
const [hasNoCode] = accounts; |
|
|
|
const [hasNoCode] = accounts; |
|
|
|
|
|
|
|
|
|
|
@ -24,31 +28,31 @@ contract('SafeERC20', function (accounts) { |
|
|
|
this.token = { address: 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 () { |
|
|
|
describe('with token that returns false on all calls', function () { |
|
|
|
beforeEach(async function () { |
|
|
|
beforeEach(async function () { |
|
|
|
this.token = await ERC20ReturnFalseMock.new(); |
|
|
|
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 () { |
|
|
|
describe('with token that returns true on all calls', function () { |
|
|
|
beforeEach(async function () { |
|
|
|
beforeEach(async function () { |
|
|
|
this.token = await ERC20ReturnTrueMock.new(); |
|
|
|
this.token = await ERC20ReturnTrueMock.new(name, symbol); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
shouldOnlyRevertOnErrors(); |
|
|
|
shouldOnlyRevertOnErrors(accounts); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('with token that returns no boolean values', function () { |
|
|
|
describe('with token that returns no boolean values', function () { |
|
|
|
beforeEach(async function () { |
|
|
|
beforeEach(async function () { |
|
|
|
this.token = await ERC20NoReturnMock.new(); |
|
|
|
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 () { |
|
|
@ -57,7 +61,7 @@ contract('SafeERC20', function (accounts) { |
|
|
|
const spender = hasNoCode; |
|
|
|
const spender = hasNoCode; |
|
|
|
|
|
|
|
|
|
|
|
beforeEach(async function () { |
|
|
|
beforeEach(async function () { |
|
|
|
this.token = await ERC20PermitNoRevertMock.new(); |
|
|
|
this.token = await ERC20PermitNoRevertMock.new(name, symbol, name); |
|
|
|
|
|
|
|
|
|
|
|
this.data = await getDomain(this.token).then(domain => ({ |
|
|
|
this.data = await getDomain(this.token).then(domain => ({ |
|
|
|
primaryType: 'Permit', |
|
|
|
primaryType: 'Permit', |
|
|
@ -165,65 +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 () { |
|
|
|
it('reverts on transfer', async function () { |
|
|
|
await expectRevert(this.mock.$safeTransfer(this.token.address, constants.ZERO_ADDRESS, 0), reason); |
|
|
|
await expectRevert(this.mock.$safeTransfer(this.token.address, receiver, 0), reason); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('reverts on transferFrom', async function () { |
|
|
|
it('reverts on transferFrom', async function () { |
|
|
|
await expectRevert( |
|
|
|
await expectRevert(this.mock.$safeTransferFrom(this.token.address, this.mock.address, receiver, 0), reason); |
|
|
|
this.mock.$safeTransferFrom(this.token.address, this.mock.address, constants.ZERO_ADDRESS, 0), |
|
|
|
|
|
|
|
reason, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('reverts on approve', async function () { |
|
|
|
it('reverts on approve', async function () { |
|
|
|
await expectRevert(this.mock.$safeApprove(this.token.address, constants.ZERO_ADDRESS, 0), reason); |
|
|
|
await expectRevert(this.mock.$safeApprove(this.token.address, spender, 0), reason); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('reverts on increaseAllowance', async function () { |
|
|
|
it('reverts on increaseAllowance', async function () { |
|
|
|
// [TODO] make sure it's reverting for the right reason
|
|
|
|
// [TODO] make sure it's reverting for the right reason
|
|
|
|
await expectRevert.unspecified(this.mock.$safeIncreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 0)); |
|
|
|
await expectRevert.unspecified(this.mock.$safeIncreaseAllowance(this.token.address, spender, 0)); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('reverts on decreaseAllowance', async function () { |
|
|
|
it('reverts on decreaseAllowance', async function () { |
|
|
|
// [TODO] make sure it's reverting for the right reason
|
|
|
|
// [TODO] make sure it's reverting for the right reason
|
|
|
|
await expectRevert.unspecified(this.mock.$safeDecreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 0)); |
|
|
|
await expectRevert.unspecified(this.mock.$safeDecreaseAllowance(this.token.address, spender, 0)); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function shouldOnlyRevertOnErrors() { |
|
|
|
it('reverts on forceApprove', async function () { |
|
|
|
it("doesn't revert on transfer", async function () { |
|
|
|
await expectRevert(this.mock.$forceApprove(this.token.address, spender, 0), reason); |
|
|
|
await this.mock.$safeTransfer(this.token.address, constants.ZERO_ADDRESS, 0); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 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 () { |
|
|
|
it("doesn't revert on transferFrom", async function () { |
|
|
|
await this.mock.$safeTransferFrom(this.token.address, this.mock.address, constants.ZERO_ADDRESS, 0); |
|
|
|
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 () { |
|
|
|
describe('approvals', function () { |
|
|
|
context('with zero allowance', function () { |
|
|
|
context('with zero allowance', function () { |
|
|
|
beforeEach(async function () { |
|
|
|
beforeEach(async function () { |
|
|
|
await this.token.setAllowance(this.mock.address, 0); |
|
|
|
await this.token.$_approve(this.mock.address, spender, 0); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it("doesn't revert when approving a non-zero allowance", async function () { |
|
|
|
it("doesn't revert when approving a non-zero allowance", async function () { |
|
|
|
await this.mock.$safeApprove(this.token.address, constants.ZERO_ADDRESS, 100); |
|
|
|
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 () { |
|
|
|
it("doesn't revert when approving a zero allowance", async function () { |
|
|
|
await this.mock.$safeApprove(this.token.address, constants.ZERO_ADDRESS, 0); |
|
|
|
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 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 () { |
|
|
|
it("doesn't revert when increasing the allowance", async function () { |
|
|
|
await this.mock.$safeIncreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 10); |
|
|
|
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 () { |
|
|
|
it('reverts when decreasing the allowance', async function () { |
|
|
|
await expectRevert( |
|
|
|
await expectRevert( |
|
|
|
this.mock.$safeDecreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 10), |
|
|
|
this.mock.$safeDecreaseAllowance(this.token.address, spender, 10), |
|
|
|
'SafeERC20: decreased allowance below zero', |
|
|
|
'SafeERC20: decreased allowance below zero', |
|
|
|
); |
|
|
|
); |
|
|
|
}); |
|
|
|
}); |
|
|
@ -231,31 +304,44 @@ function shouldOnlyRevertOnErrors() { |
|
|
|
|
|
|
|
|
|
|
|
context('with non-zero allowance', function () { |
|
|
|
context('with non-zero allowance', function () { |
|
|
|
beforeEach(async function () { |
|
|
|
beforeEach(async function () { |
|
|
|
await this.token.setAllowance(this.mock.address, 100); |
|
|
|
await this.token.$_approve(this.mock.address, spender, 100); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('reverts when approving a non-zero allowance', async function () { |
|
|
|
it('reverts when approving a non-zero allowance', async function () { |
|
|
|
await expectRevert( |
|
|
|
await expectRevert( |
|
|
|
this.mock.$safeApprove(this.token.address, constants.ZERO_ADDRESS, 20), |
|
|
|
this.mock.$safeApprove(this.token.address, spender, 20), |
|
|
|
'SafeERC20: approve from non-zero to non-zero allowance', |
|
|
|
'SafeERC20: approve from non-zero to non-zero allowance', |
|
|
|
); |
|
|
|
); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it("doesn't revert when approving a zero allowance", async function () { |
|
|
|
it("doesn't revert when approving a zero allowance", async function () { |
|
|
|
await this.mock.$safeApprove(this.token.address, constants.ZERO_ADDRESS, 0); |
|
|
|
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 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 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 () { |
|
|
|
it("doesn't revert when increasing the allowance", async function () { |
|
|
|
await this.mock.$safeIncreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 10); |
|
|
|
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 () { |
|
|
|
it("doesn't revert when decreasing the allowance to a positive value", async function () { |
|
|
|
await this.mock.$safeDecreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 50); |
|
|
|
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 () { |
|
|
|
it('reverts when decreasing the allowance to a negative value', async function () { |
|
|
|
await expectRevert( |
|
|
|
await expectRevert( |
|
|
|
this.mock.$safeDecreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 200), |
|
|
|
this.mock.$safeDecreaseAllowance(this.token.address, spender, 200), |
|
|
|
'SafeERC20: decreased allowance below zero', |
|
|
|
'SafeERC20: decreased allowance below zero', |
|
|
|
); |
|
|
|
); |
|
|
|
}); |
|
|
|
}); |
|
|
|