Skip to content

Commit 3a50d24

Browse files
committed
Test reverting static methods of SafeERC20.sol
1 parent d520e20 commit 3a50d24

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Modified from https://github.com/OpenZeppelin/openzeppelin-solidity/blob/a9f910d34f0ab33a1ae5e714f69f9596a02b4d91/contracts/token/ERC20/StandardToken.sol
2+
3+
pragma solidity 0.4.24;
4+
5+
import "../../../../lib/math/SafeMath.sol";
6+
7+
8+
contract TokenRevertViewMethods {
9+
using SafeMath for uint256;
10+
mapping (address => uint256) private balances;
11+
mapping (address => mapping (address => uint256)) private allowed;
12+
uint256 private totalSupply_;
13+
bool private allowTransfer_;
14+
15+
// Allow us to set the inital balance for an account on construction
16+
constructor(address initialAccount, uint256 initialBalance) public {
17+
balances[initialAccount] = initialBalance;
18+
totalSupply_ = initialBalance;
19+
allowTransfer_ = true;
20+
}
21+
22+
function totalSupply() public view returns (uint256) {
23+
require(false, "MOCK_ERROR");
24+
return totalSupply_;
25+
}
26+
27+
/**
28+
* @dev Gets the balance of the specified address.
29+
* @param _owner The address to query the the balance of.
30+
* @return An uint256 representing the amount owned by the passed address.
31+
*/
32+
function balanceOf(address _owner) public view returns (uint256) {
33+
require(false, "MOCK_ERROR");
34+
return balances[_owner];
35+
}
36+
37+
/**
38+
* @dev Function to check the amount of tokens that an owner allowed to a spender.
39+
* @param _owner address The address which owns the funds.
40+
* @param _spender address The address which will spend the funds.
41+
* @return A uint256 specifying the amount of tokens still available for the spender.
42+
*/
43+
function allowance(address _owner, address _spender) public view returns (uint256) {
44+
require(false, "MOCK_ERROR");
45+
return allowed[_owner][_spender];
46+
}
47+
}

test/contracts/common/safe_erc20.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
const { getEventArgument } = require('../../helpers/events')
2+
const { assertRevert } = require('../../helpers/assertThrow')
3+
const reverts = require('../../helpers/revertStrings')
24

35
// Mocks
46
const SafeERC20Mock = artifacts.require('SafeERC20Mock')
57
const TokenMock = artifacts.require('TokenMock')
68
const TokenReturnFalseMock = artifacts.require('TokenReturnFalseMock')
79
const TokenReturnMissingMock = artifacts.require('TokenReturnMissingMock')
10+
const TokenRevertViewMethods = artifacts.require('TokenRevertViewMethods')
811

912
const assertMockResult = (receipt, result) => assert.equal(getEventArgument(receipt, 'Result', 'result'), result, `result does not match`)
1013

@@ -128,4 +131,33 @@ contract('SafeERC20', ([owner, receiver]) => {
128131
})
129132
})
130133
}
134+
135+
context('Reverting view methods', () => {
136+
let tokenMock
137+
138+
beforeEach(async () => {
139+
tokenMock = await TokenRevertViewMethods.new(owner, initialBalance)
140+
})
141+
142+
it('allowance', async () => {
143+
await assertRevert(
144+
safeERC20Mock.allowance(tokenMock.address, owner, owner),
145+
reverts.SAFE_ERC_20_ALLOWANCE_REVERTED
146+
)
147+
})
148+
149+
it('balanceOf', async () => {
150+
await assertRevert(
151+
safeERC20Mock.balanceOf(tokenMock.address, owner),
152+
reverts.SAFE_ERC_20_BALANCE_REVERTED
153+
)
154+
})
155+
156+
it('totalSupply', async () => {
157+
await assertRevert(
158+
safeERC20Mock.totalSupply(tokenMock.address),
159+
reverts.SAFE_ERC_20_ALLOWANCE_REVERTED
160+
)
161+
})
162+
})
131163
})

0 commit comments

Comments
 (0)