Skip to content

Commit 72760c6

Browse files
Increase test coverage for the token contract
1 parent 849820a commit 72760c6

File tree

4 files changed

+576
-137
lines changed

4 files changed

+576
-137
lines changed

contracts/test/proxy.sol

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import '../ERC223ReceivingContract.sol';
44

55
// Contract needed for testing the token contract
66
// TODO general function call with 1 data argument in bytes
7-
contract Proxy is ERC223ReceivingContract{
8-
address public sender;
9-
uint256 public value;
10-
bytes public data;
7+
contract Proxy{
118

129
event Payable(address to, uint value, string function_string);
1310

@@ -28,6 +25,14 @@ contract Proxy is ERC223ReceivingContract{
2825
Payable(to, msg.value, function_string);
2926
return to.call.value(msg.value)(bytes4(sha3(function_string)));
3027
}
28+
}
29+
30+
contract ProxyERC223 is ERC223ReceivingContract{
31+
address public sender;
32+
uint256 public value;
33+
bytes public data;
34+
35+
function ProxyERC223() {}
3136

3237
function tokenFallback(
3338
address _from,

contracts/test/tokenTest.sol

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
pragma solidity ^0.4.11;
2+
3+
import '../ERC223ReceivingContract.sol';
4+
import '../token.sol';
5+
6+
/// @title Custom Token
7+
contract CustomToken2 is StandardToken {
8+
9+
/*
10+
* Terminology:
11+
* 1 token unit = Tei
12+
* 1 token = TKN = Tei * multiplier
13+
* multiplier set from token's number of decimals (i.e. 10**decimals)
14+
*/
15+
16+
/*
17+
* Token metadata
18+
*/
19+
string constant public name = "The Token";
20+
string constant public symbol = "TKN";
21+
uint8 public decimals;
22+
uint multiplier;
23+
24+
address public owner;
25+
address public auction_address;
26+
27+
event Deployed(
28+
address indexed _auction,
29+
uint indexed _total_supply,
30+
uint indexed _auction_supply);
31+
event Burnt(
32+
address indexed _receiver,
33+
uint indexed _num,
34+
uint indexed _total_supply);
35+
36+
/*
37+
* Public functions
38+
*/
39+
/// @dev Contract constructor function sets dutch auction contract address and assigns all tokens to dutch auction.
40+
/// @param auction Address of dutch auction contract.
41+
/// @param initial_supply Number of initially provided token units (Tei).
42+
/// @param owners Array of addresses receiving preassigned tokens.
43+
/// @param tokens Array of preassigned token units (Tei).
44+
function CustomToken2(
45+
uint8 _decimals,
46+
address auction,
47+
uint initial_supply,
48+
address[] owners,
49+
uint[] tokens)
50+
public
51+
{
52+
// Auction address should not be null.
53+
require(auction != 0x0);
54+
require(owners.length == tokens.length);
55+
// Initial supply is in Tei
56+
require(initial_supply > multiplier);
57+
58+
owner = msg.sender;
59+
auction_address = auction;
60+
decimals = _decimals;
61+
multiplier = 10**uint(decimals);
62+
63+
// Total supply of Tei at deployment
64+
totalSupply = initial_supply;
65+
66+
// Preallocate tokens to beneficiaries
67+
uint prealloc_tokens;
68+
for (uint i=0; i<owners.length; i++) {
69+
// Address should not be null.
70+
require(owners[i] != 0x0);
71+
require(tokens[i] > 0);
72+
require(balances[owners[i]] + tokens[i] > balances[owners[i]]);
73+
require(prealloc_tokens + tokens[i] > prealloc_tokens);
74+
75+
balances[owners[i]] += tokens[i];
76+
prealloc_tokens += tokens[i];
77+
Transfer(0, owners[i], tokens[i]);
78+
}
79+
80+
balances[auction_address] = totalSupply - prealloc_tokens;
81+
Transfer(0, auction_address, balances[auction]);
82+
83+
Deployed(auction_address, totalSupply, balances[auction]);
84+
85+
assert(balances[auction_address] > 0);
86+
assert(balances[auction_address] < totalSupply);
87+
assert(totalSupply == balances[auction_address] + prealloc_tokens);
88+
}
89+
90+
/// @notice Allows `msg.sender` to simply destroy `num` token units (Tei), without receiving the corresponding amount of ether. This means the total token supply will decrease.
91+
/// @dev Allows to destroy token units (Tei) without receiving the corresponding amount of ether.
92+
/// @param num Number of token units (Tei) to burn.
93+
function burn(uint num)
94+
public
95+
{
96+
require(num > 0);
97+
require(balances[msg.sender] >= num);
98+
require(totalSupply >= num);
99+
100+
uint pre_balance = balances[msg.sender];
101+
102+
balances[msg.sender] -= num;
103+
totalSupply -= num;
104+
Burnt(msg.sender, num, totalSupply);
105+
106+
assert(balances[msg.sender] == pre_balance - num);
107+
}
108+
109+
}

tests/fixtures.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from eth_utils import decode_hex, keccak
23
from functools import (
34
reduce
45
)
@@ -8,8 +9,12 @@
89
)
910

1011

12+
MAX_UINT = 2**256
13+
fake_address = 0x03432
1114
print_the_logs = False
1215
passphrase = '0'
16+
fixture_decimals = [18, 1]
17+
1318

1419
contract_args = [
1520
{
@@ -28,8 +33,15 @@
2833
}
2934
]
3035

31-
'''
36+
3237
contract_args += [
38+
{
39+
'token': 'CustomToken2',
40+
'decimals': 1,
41+
'supply': 10000000,
42+
'preallocations': [200000, 800000],
43+
'args': [5982, 59]
44+
},
3345
{
3446
'token': 'CustomToken2',
3547
'decimals': 1,
@@ -38,10 +50,14 @@
3850
'args': [10000, 7500]
3951
}
4052
]
41-
'''
53+
4254

4355
# auction_supply = initial_supply - reduce((lambda x, y: x + y), prealloc)
4456

57+
def test_bytes(value=10, size=256):
58+
hex_value = decode_hex('{:x}'.format(value).zfill(size // 4))
59+
return keccak(hex_value)
60+
4561

4662
def prepare_preallocs(multiplier, preallocs):
4763
return list(map(lambda x: x * multiplier, preallocs))
@@ -55,15 +71,17 @@ def owner(web3):
5571
@pytest.fixture()
5672
def team(web3, contract_params):
5773
index_end = len(contract_params['preallocations']) + 1
58-
return web3.eth.accounts[1:index_end]
59-
74+
return web3.eth.accounts[2:(index_end + 1)]
6075

6176
@pytest.fixture()
6277
def get_bidders(web3, contract_params, create_accounts):
6378
def get_these_bidders(number):
64-
index_start = 2+ len(contract_params['preallocations'])
65-
bidders = web3.eth.accounts[index_start:]
66-
bidders += create_accounts(number - len(bidders))
79+
index_start = 2 + len(contract_params['preallocations'])
80+
accounts_len = len(web3.eth.accounts)
81+
index_end = min(number + index_start, accounts_len)
82+
bidders = web3.eth.accounts[index_start:index_end]
83+
if number > len(bidders):
84+
bidders += create_accounts(number - len(bidders))
6785
return bidders
6886
return get_these_bidders
6987

@@ -116,11 +134,15 @@ def auction_contract(
116134
@pytest.fixture()
117135
def get_token_contract(chain, create_contract, owner):
118136
# contract can be auction contract or proxy contract
119-
def get(arguments, transaction=None, token_type='CustomToken'):
137+
def get(arguments, transaction=None, token_type='CustomToken', decimals=18):
138+
if not decimals == 18:
139+
token_type = 'CustomToken2'
140+
arguments.insert(0, decimals)
141+
120142
CustomToken = chain.provider.get_contract_factory(token_type)
121143
if not transaction:
122144
transaction = {'from': owner}
123-
145+
# print('get_token_contract token_type', token_type, decimals, arguments)
124146
token_contract = create_contract(CustomToken, arguments, transaction)
125147

126148
if print_the_logs:
@@ -138,6 +160,7 @@ def token_contract(
138160
chain,
139161
web3,
140162
owner,
163+
team,
141164
contract_params,
142165
get_token_contract):
143166
decimals = contract_params['decimals']
@@ -146,23 +169,15 @@ def token_contract(
146169
supply = contract_params['supply'] * multiplier
147170
token_type = contract_params['token']
148171

149-
owners = web3.eth.accounts[2:(len(preallocations) + 2)]
150-
151172
def get(auction_address, transaction=None):
152-
args = []
153-
154-
if token_type == 'CustomToken2':
155-
args.append(decimals)
156-
157-
args += [
173+
args = [
158174
auction_address,
159175
supply,
160-
owners,
176+
team,
161177
prepare_preallocs(multiplier, preallocations)
162178
]
163-
if not transaction:
164-
transaction = {'from': owner}
165-
token_contract = get_token_contract(args, transaction, token_type)
179+
180+
token_contract = get_token_contract(args, transaction, token_type, decimals)
166181

167182
return token_contract
168183
return get

0 commit comments

Comments
 (0)