Skip to content

Commit

Permalink
Make SafeMath a library
Browse files Browse the repository at this point in the history
  • Loading branch information
maraoz committed Apr 19, 2017
1 parent 5881429 commit 4fad150
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion contracts/SafeMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.4.8;
/**
* Math operations with safety checks
*/
contract SafeMath {
library SafeMath {
function safeMul(uint a, uint b) internal returns (uint) {
uint c = a * b;
assert(a == 0 || c / a == b);
Expand Down
10 changes: 6 additions & 4 deletions contracts/payment/PullPayment.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import '../SafeMath.sol';
* Base contract supporting async send for pull payments.
* Inherit from this contract and use asyncSend instead of send.
*/
contract PullPayment is SafeMath {
contract PullPayment {
using SafeMath for uint;

mapping(address => uint) public payments;
uint public totalPayments;

// store sent amount as credit to be pulled, called by payer
function asyncSend(address dest, uint amount) internal {
payments[dest] = safeAdd(payments[dest], amount);
totalPayments = safeAdd(totalPayments, amount);
payments[dest] = payments[dest].safeAdd(amount);
totalPayments = totalPayments.safeAdd(amount);
}

// withdraw accumulated balance, called by payee
Expand All @@ -32,7 +34,7 @@ contract PullPayment is SafeMath {
throw;
}

totalPayments = safeSub(totalPayments, payment);
totalPayments = totalPayments.safeSub(payment);
payments[payee] = 0;

if (!payee.send(payment)) {
Expand Down
7 changes: 4 additions & 3 deletions contracts/token/BasicToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import '../SafeMath.sol';
* Basic token
* Basic version of StandardToken, with no allowances
*/
contract BasicToken is ERC20Basic, SafeMath {
contract BasicToken is ERC20Basic {
using SafeMath for uint;

mapping(address => uint) balances;

Expand All @@ -22,8 +23,8 @@ contract BasicToken is ERC20Basic, SafeMath {
}

function transfer(address _to, uint _value) onlyPayloadSize(2 * 32) {
balances[msg.sender] = safeSub(balances[msg.sender], _value);
balances[_to] = safeAdd(balances[_to], _value);
balances[msg.sender] = balances[msg.sender].safeSub(_value);
balances[_to] = balances[_to].safeAdd(_value);
Transfer(msg.sender, _to, _value);
}

Expand Down
6 changes: 3 additions & 3 deletions contracts/token/CrowdsaleToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ contract CrowdsaleToken is StandardToken {
throw;
}

uint tokens = safeMul(msg.value, getPrice());
totalSupply = safeAdd(totalSupply, tokens);
uint tokens = msg.value.safeMul(getPrice());
totalSupply = totalSupply.safeAdd(tokens);

balances[recipient] = safeAdd(balances[recipient], tokens);
balances[recipient] = balances[recipient].safeAdd(tokens);

if (!multisig.send(msg.value)) {
throw;
Expand Down
6 changes: 3 additions & 3 deletions contracts/token/StandardToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ contract StandardToken is BasicToken, ERC20 {
// Check is not needed because safeSub(_allowance, _value) will already throw if this condition is not met
// if (_value > _allowance) throw;

balances[_to] = safeAdd(balances[_to], _value);
balances[_from] = safeSub(balances[_from], _value);
allowed[_from][msg.sender] = safeSub(_allowance, _value);
balances[_to] = balances[_to].safeAdd(_value);
balances[_from] = balances[_from].safeSub(_value);
allowed[_from][msg.sender] = _allowance.safeSub(_value);
Transfer(_from, _to, _value);
}

Expand Down
19 changes: 10 additions & 9 deletions contracts/token/VestedToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ contract VestedToken is StandardToken, LimitedTransferToken {
grants[_holder][_grantId] = grants[_holder][grants[_holder].length - 1];
grants[_holder].length -= 1;

balances[msg.sender] = safeAdd(balances[msg.sender], nonVested);
balances[_holder] = safeSub(balances[_holder], nonVested);
balances[msg.sender] = balances[msg.sender].safeAdd(nonVested);
balances[_holder] = balances[_holder].safeSub(nonVested);
Transfer(_holder, msg.sender, nonVested);
}

Expand Down Expand Up @@ -98,32 +98,33 @@ contract VestedToken is StandardToken, LimitedTransferToken {
return tokens;
}

uint256 cliffTokens = safeDiv(safeMul(tokens, safeSub(cliff, start)), safeSub(vesting, start));
uint256 cliffTokens = tokens.safeMul(cliff.safeSub(start)).safeDiv(vesting.safeSub(start));
vestedTokens = cliffTokens;

uint256 vestingTokens = safeSub(tokens, cliffTokens);
uint256 vestingTokens = tokens.safeSub(cliffTokens);

vestedTokens = safeAdd(vestedTokens, safeDiv(safeMul(vestingTokens, safeSub(time, cliff)), safeSub(vesting, cliff)));
vestedTokens = vestedTokens.safeAdd(vestingTokens.safeMul(time.safeSub(cliff)).safeDiv(vesting.safeSub(cliff)));
}

function nonVestedTokens(TokenGrant grant, uint64 time) private constant returns (uint256) {
return safeSub(grant.value, vestedTokens(grant, time));
return grant.value.safeSub(vestedTokens(grant, time));
}

function lastTokenIsTransferableDate(address holder) constant public returns (uint64 date) {
date = uint64(now);
uint256 grantIndex = grants[holder].length;
for (uint256 i = 0; i < grantIndex; i++) {
date = max64(grants[holder][i].vesting, date);
date = SafeMath.max64(grants[holder][i].vesting, date);
}
}

function transferableTokens(address holder, uint64 time) constant public returns (uint256 nonVested) {
uint256 grantIndex = grants[holder].length;
for (uint256 i = 0; i < grantIndex; i++) {
nonVested = safeAdd(nonVested, nonVestedTokens(grants[holder][i], time));
uint256 current = nonVestedTokens(grants[holder][i], time);
nonVested = nonVested.safeAdd(current);
}

return min256(safeSub(balances[holder], nonVested), super.transferableTokens(holder, time));
return SafeMath.min256(balances[holder].safeSub(nonVested), super.transferableTokens(holder, time));
}
}
8 changes: 4 additions & 4 deletions test/helpers/SafeMathMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ pragma solidity ^0.4.8;
import '../../contracts/SafeMath.sol';


contract SafeMathMock is SafeMath {
contract SafeMathMock {
uint public result;

function multiply(uint a, uint b) {
result = safeMul(a, b);
result = SafeMath.safeMul(a, b);
}

function subtract(uint a, uint b) {
result = safeSub(a, b);
result = SafeMath.safeSub(a, b);
}

function add(uint a, uint b) {
result = safeAdd(a, b);
result = SafeMath.safeAdd(a, b);
}
}

0 comments on commit 4fad150

Please sign in to comment.