Skip to content

Commit

Permalink
Merge pull request Consensys#79 from ConsenSys/staging
Browse files Browse the repository at this point in the history
This merge bring @Janaka-Steph, and @abandeali1's contributions into the master branch. 
Thank you both.
  • Loading branch information
maurelian authored Oct 2, 2017
2 parents 75817ca + d30e289 commit bb15a1f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 7 additions & 2 deletions contracts/StandardToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import "./Token.sol";

contract StandardToken is Token {

uint256 constant MAX_UINT256 = 2**256 - 1;

function transfer(address _to, uint256 _value) returns (bool success) {
//Default assumes totalSupply can't be over max (2^256 - 1).
//If your token leaves out totalSupply and can issue more tokens as time goes on, you need to check if it doesn't wrap.
Expand All @@ -27,10 +29,13 @@ contract StandardToken is Token {
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
//same as above. Replace this line with the following if you want to protect against wrapping uints.
//require(balances[_from] >= _value && allowed[_from][msg.sender] >= _value && balances[_to] + _value > balances[_to]);
require(balances[_from] >= _value && allowed[_from][msg.sender] >= _value);
uint256 allowance = allowed[_from][msg.sender];
require(balances[_from] >= _value && allowance >= _value);
balances[_to] += _value;
balances[_from] -= _value;
allowed[_from][msg.sender] -= _value;
if (allowance < MAX_UINT256) {
allowed[_from][msg.sender] -= _value;
}
Transfer(_from, _to, _value);
return true;
}
Expand Down
21 changes: 21 additions & 0 deletions test/humanStandardToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,27 @@ contract('HumanStandardToken', function (accounts) {
assert(allowance.equals('1.15792089237316195423570985008687907853269984665640564039457584007913129639935e+77'))
})

// should approve max of msg.sender & withdraw 20 without changing allowance (should succeed).
it('approvals: msg.sender approves accounts[1] of max (2^256 - 1) & withdraws 20', async () => {
const balance0 = await HST.balanceOf.call(accounts[0])
assert.strictEqual(balance0.toNumber(), 10000)

const max = '1.15792089237316195423570985008687907853269984665640564039457584007913129639935e+77'
await HST.approve(accounts[1], max, {from: accounts[0]})
const balance2 = await HST.balanceOf.call(accounts[2])
assert.strictEqual(balance2.toNumber(), 0, 'balance2 not correct')

await HST.transferFrom(accounts[0], accounts[2], 20, {from: accounts[1]})
const allowance01 = await HST.allowance.call(accounts[0], accounts[1])
assert(allowance01.equals(max))

const balance22 = await HST.balanceOf.call(accounts[2])
assert.strictEqual(balance22.toNumber(), 20)

const balance02 = await HST.balanceOf.call(accounts[0])
assert.strictEqual(balance02.toNumber(), 9980)
})

it('events: should fire Transfer event properly', async () => {
const res = await HST.transfer(accounts[1], '2666', {from: accounts[0]})
const transferLog = res.logs.find(element => element.event.match('Transfer'))
Expand Down

0 comments on commit bb15a1f

Please sign in to comment.