commit
4bab15a4e6
@ -1,12 +1,16 @@ |
||||
contract ERC20 { |
||||
function totalSupply() constant returns (uint); |
||||
function balanceOf(address who) constant returns (uint); |
||||
function allowance(address owner, address spender) constant returns (uint); |
||||
pragma solidity ^0.4.0; |
||||
|
||||
|
||||
function transfer(address to, uint value) returns (bool ok); |
||||
function transferFrom(address from, address to, uint value) returns (bool ok); |
||||
function approve(address spender, uint value) returns (bool ok); |
||||
// see https://github.com/ethereum/EIPs/issues/20 |
||||
|
||||
contract ERC20 { |
||||
uint public totalSupply; |
||||
function balanceOf(address who) constant returns (uint); |
||||
function allowance(address owner, address spender) constant returns (uint); |
||||
|
||||
event Transfer(address indexed from, address indexed to, uint value); |
||||
event Approval(address indexed owner, address indexed spender, uint value); |
||||
function transfer(address to, uint value) returns (bool ok); |
||||
function transferFrom(address from, address to, uint value) returns (bool ok); |
||||
function approve(address spender, uint value) returns (bool ok); |
||||
event Transfer(address indexed from, address indexed to, uint value); |
||||
event Approval(address indexed owner, address indexed spender, uint value); |
||||
} |
||||
|
@ -0,0 +1,28 @@ |
||||
pragma solidity ^0.4.0; |
||||
|
||||
/** |
||||
* Math operations with safety checks |
||||
*/ |
||||
contract SafeMath { |
||||
function safeMul(uint a, uint b) internal returns (uint) { |
||||
uint c = a * b; |
||||
assert(a == 0 || c / a == b); |
||||
return c; |
||||
} |
||||
|
||||
function safeSub(uint a, uint b) internal returns (uint) { |
||||
assert(b <= a); |
||||
return a - b; |
||||
} |
||||
|
||||
function safeAdd(uint a, uint b) internal returns (uint) { |
||||
uint c = a + b; |
||||
assert(c>=a && c>=b); |
||||
return c; |
||||
} |
||||
|
||||
function assert(bool assertion) internal { |
||||
if (!assertion) throw; |
||||
} |
||||
} |
||||
|
@ -0,0 +1,56 @@ |
||||
pragma solidity ^0.4.0; |
||||
|
||||
import './ERC20.sol'; |
||||
import './SafeMath.sol'; |
||||
|
||||
/** |
||||
* ERC20 token |
||||
* |
||||
* https://github.com/ethereum/EIPs/issues/20 |
||||
* Based on code by FirstBlood: |
||||
* https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol |
||||
*/ |
||||
contract StandardToken is ERC20, SafeMath { |
||||
|
||||
mapping(address => uint) balances; |
||||
mapping (address => mapping (address => uint)) allowed; |
||||
|
||||
function transfer(address _to, uint _value) returns (bool success) { |
||||
if (balances[msg.sender] < _value) { |
||||
throw; |
||||
} |
||||
balances[msg.sender] = safeSub(balances[msg.sender], _value); |
||||
balances[_to] = safeAdd(balances[_to], _value); |
||||
Transfer(msg.sender, _to, _value); |
||||
return true; |
||||
} |
||||
|
||||
function transferFrom(address _from, address _to, uint _value) returns (bool success) { |
||||
var _allowance = allowed[_from][msg.sender]; |
||||
if (balances[_from] < _value || |
||||
_allowance < _value) { |
||||
throw; |
||||
} |
||||
|
||||
balances[_to] = safeAdd(balances[_to], _value); |
||||
balances[_from] = safeSub(balances[_from], _value); |
||||
allowed[_from][msg.sender] = safeSub(_allowance, _value); |
||||
Transfer(_from, _to, _value); |
||||
return true; |
||||
} |
||||
|
||||
function balanceOf(address _owner) constant returns (uint balance) { |
||||
return balances[_owner]; |
||||
} |
||||
|
||||
function approve(address _spender, uint _value) returns (bool success) { |
||||
allowed[msg.sender][_spender] = _value; |
||||
Approval(msg.sender, _spender, _value); |
||||
return true; |
||||
} |
||||
|
||||
function allowance(address _owner, address _spender) constant returns (uint remaining) { |
||||
return allowed[_owner][_spender]; |
||||
} |
||||
|
||||
} |
@ -1,72 +0,0 @@ |
||||
// Source: https://github.com/nexusdev/erc20 |
||||
// Flat file implementation of `dappsys/token/base.sol::DSTokenBase` |
||||
|
||||
// Everything throws instead of returning false on failure. |
||||
import './ERC20.sol'; |
||||
|
||||
contract Token is ERC20 { |
||||
|
||||
mapping( address => uint ) _balances; |
||||
mapping( address => mapping( address => uint ) ) _approvals; |
||||
uint _supply; |
||||
|
||||
function Token( uint initial_balance ) { |
||||
_balances[msg.sender] = initial_balance; |
||||
_supply = initial_balance; |
||||
} |
||||
|
||||
function totalSupply() constant returns (uint supply) { |
||||
return _supply; |
||||
} |
||||
|
||||
function balanceOf( address who ) constant returns (uint value) { |
||||
return _balances[who]; |
||||
} |
||||
|
||||
function transfer( address to, uint value) returns (bool ok) { |
||||
if( _balances[msg.sender] < value ) { |
||||
throw; |
||||
} |
||||
if( !safeToAdd(_balances[to], value) ) { |
||||
throw; |
||||
} |
||||
_balances[msg.sender] -= value; |
||||
_balances[to] += value; |
||||
Transfer( msg.sender, to, value ); |
||||
return true; |
||||
} |
||||
|
||||
function transferFrom( address from, address to, uint value) returns (bool ok) { |
||||
// if you don't have enough balance, throw |
||||
if( _balances[from] < value ) { |
||||
throw; |
||||
} |
||||
// if you don't have approval, throw |
||||
if( _approvals[from][msg.sender] < value ) { |
||||
throw; |
||||
} |
||||
if( !safeToAdd(_balances[to], value) ) { |
||||
throw; |
||||
} |
||||
// transfer and return true |
||||
_approvals[from][msg.sender] -= value; |
||||
_balances[from] -= value; |
||||
_balances[to] += value; |
||||
Transfer( from, to, value ); |
||||
return true; |
||||
} |
||||
|
||||
function approve(address spender, uint value) returns (bool ok) { |
||||
_approvals[msg.sender][spender] = value; |
||||
Approval( msg.sender, spender, value ); |
||||
return true; |
||||
} |
||||
|
||||
function allowance(address owner, address spender) constant returns (uint _allowance) { |
||||
return _approvals[owner][spender]; |
||||
} |
||||
|
||||
function safeToAdd(uint a, uint b) internal returns (bool) { |
||||
return (a + b >= a); |
||||
} |
||||
} |
@ -0,0 +1,39 @@ |
||||
pragma solidity ^0.4.0; |
||||
|
||||
import "../Rejector.sol"; |
||||
|
||||
/* |
||||
* Proof of Existence example contract |
||||
* see https://medium.com/zeppelin-blog/the-hitchhikers-guide-to-smart-contracts-in-ethereum-848f08001f05 |
||||
*/ |
||||
contract ProofOfExistence is Rejector { |
||||
|
||||
mapping (bytes32 => bool) public proofs; |
||||
|
||||
// store a proof of existence in the contract state |
||||
function storeProof(bytes32 proof) { |
||||
proofs[proof] = true; |
||||
} |
||||
|
||||
// calculate and store the proof for a document |
||||
function notarize(string document) { |
||||
var proof = calculateProof(document); |
||||
storeProof(proof); |
||||
} |
||||
|
||||
// helper function to get a document's sha256 |
||||
function calculateProof(string document) constant returns (bytes32) { |
||||
return sha256(document); |
||||
} |
||||
|
||||
// check if a document has been notarized |
||||
function checkDocument(string document) constant returns (bool) { |
||||
var proof = calculateProof(document); |
||||
return hasProof(proof); |
||||
} |
||||
|
||||
// returns true if proof is stored |
||||
function hasProof(bytes32 proof) constant returns (bool) { |
||||
return proofs[proof]; |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
pragma solidity ^0.4.0; |
||||
import '../PullPayment.sol'; |
||||
|
||||
// mock class using PullPayment |
||||
contract PullPaymentMock is PullPayment { |
||||
// test helper function to call asyncSend |
||||
function callSend(address dest, uint amount) { |
||||
asyncSend(dest, amount); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,34 @@ |
||||
pragma solidity ^0.4.0; |
||||
|
||||
import "../StandardToken.sol"; |
||||
|
||||
/* |
||||
* Simple ERC20 Token example, with crowdsale token creation |
||||
*/ |
||||
contract CrowdsaleToken is StandardToken { |
||||
|
||||
string public name = "CrowdsaleToken"; |
||||
string public symbol = "CRW"; |
||||
uint public decimals = 18; |
||||
|
||||
// 1 ether = 500 example tokens |
||||
uint PRICE = 500; |
||||
|
||||
function () payable { |
||||
createTokens(msg.sender); |
||||
} |
||||
|
||||
function createTokens(address recipient) payable { |
||||
if (msg.value == 0) throw; |
||||
|
||||
uint tokens = safeMul(msg.value, getPrice()); |
||||
|
||||
totalSupply = safeAdd(totalSupply, tokens); |
||||
balances[recipient] = safeAdd(balances[recipient], tokens); |
||||
} |
||||
|
||||
// replace this with any other price function |
||||
function getPrice() constant returns (uint result){ |
||||
return PRICE; |
||||
} |
||||
} |
@ -0,0 +1,22 @@ |
||||
pragma solidity ^0.4.0; |
||||
|
||||
import "../StandardToken.sol"; |
||||
|
||||
/* |
||||
* Very simple ERC20 Token example, where all tokens are pre-assigned |
||||
* to the creator. Note they can later distribute these tokens |
||||
* as they wish using `transfer` and other `StandardToken` functions. |
||||
*/ |
||||
contract SimpleToken is StandardToken { |
||||
|
||||
string public name = "SimpleToken"; |
||||
string public symbol = "SIM"; |
||||
uint public decimals = 18; |
||||
uint public INITIAL_SUPPLY = 10000; |
||||
|
||||
function SimpleToken() { |
||||
totalSupply = INITIAL_SUPPLY; |
||||
balances[msg.sender] = INITIAL_SUPPLY; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,102 @@ |
||||
contract('PullPayment', function(accounts) { |
||||
|
||||
it("can't call asyncSend externally", function(done) { |
||||
return PullPaymentMock.new() |
||||
.then(function(ppc) { |
||||
assert.isUndefined(ppc.asyncSend); |
||||
}) |
||||
.then(done); |
||||
}); |
||||
|
||||
it("can record an async payment correctly", function(done) { |
||||
var ppce; |
||||
var AMOUNT = 100; |
||||
return PullPaymentMock.new() |
||||
.then(function(_ppce) { |
||||
ppce = _ppce; |
||||
ppce.callSend(accounts[0], AMOUNT) |
||||
}) |
||||
.then(function() { |
||||
return ppce.payments(accounts[0]); |
||||
}) |
||||
.then(function(paymentsToAccount0) { |
||||
assert.equal(paymentsToAccount0, AMOUNT); |
||||
}) |
||||
.then(done); |
||||
}); |
||||
|
||||
it("can add multiple balances on one account", function(done) { |
||||
var ppce; |
||||
return PullPaymentMock.new() |
||||
.then(function(_ppce) { |
||||
ppce = _ppce; |
||||
return ppce.callSend(accounts[0], 200) |
||||
}) |
||||
.then(function() { |
||||
return ppce.callSend(accounts[0], 300) |
||||
}) |
||||
.then(function() { |
||||
return ppce.payments(accounts[0]); |
||||
}) |
||||
.then(function(paymentsToAccount0) { |
||||
assert.equal(paymentsToAccount0, 500); |
||||
}) |
||||
.then(done); |
||||
}); |
||||
|
||||
it("can add balances on multiple accounts", function(done) { |
||||
var ppce; |
||||
return PullPaymentMock.new() |
||||
.then(function(_ppce) { |
||||
ppce = _ppce; |
||||
return ppce.callSend(accounts[0], 200) |
||||
}) |
||||
.then(function() { |
||||
return ppce.callSend(accounts[1], 300) |
||||
}) |
||||
.then(function() { |
||||
return ppce.payments(accounts[0]); |
||||
}) |
||||
.then(function(paymentsToAccount0) { |
||||
assert.equal(paymentsToAccount0, 200); |
||||
}) |
||||
.then(function() { |
||||
return ppce.payments(accounts[1]); |
||||
}) |
||||
.then(function(paymentsToAccount0) { |
||||
assert.equal(paymentsToAccount0, 300); |
||||
}) |
||||
.then(done); |
||||
}); |
||||
|
||||
it("can withdraw payment", function(done) { |
||||
var ppce; |
||||
var AMOUNT = 17*1e18; |
||||
var payee = accounts[1]; |
||||
var initialBalance = web3.eth.getBalance(payee); |
||||
return PullPaymentMock.new({value: AMOUNT}) |
||||
.then(function(_ppce) { |
||||
ppce = _ppce; |
||||
return ppce.callSend(payee, AMOUNT); |
||||
}) |
||||
.then(function() { |
||||
return ppce.payments(payee); |
||||
}) |
||||
.then(function(paymentsToAccount0) { |
||||
assert.equal(paymentsToAccount0, AMOUNT); |
||||
}) |
||||
.then(function() { |
||||
return ppce.withdrawPayments({from: payee}); |
||||
}) |
||||
.then(function() { |
||||
return ppce.payments(payee); |
||||
}) |
||||
.then(function(paymentsToAccount0) { |
||||
assert.equal(paymentsToAccount0, 0); |
||||
var balance = web3.eth.getBalance(payee); |
||||
assert(Math.abs(balance-initialBalance-AMOUNT) < 1e16); |
||||
}) |
||||
.then(done); |
||||
}); |
||||
|
||||
}); |
@ -0,0 +1,25 @@ |
||||
pragma solidity ^0.4.0; |
||||
import "truffle/Assert.sol"; |
||||
import "truffle/DeployedAddresses.sol"; |
||||
import "../contracts/Ownable.sol"; |
||||
|
||||
contract TestOwnable { |
||||
Ownable ownable = new Ownable(); |
||||
|
||||
function testHasOwner() { |
||||
Assert.isNotZero(ownable.owner(), "Ownable should have an owner upon creation."); |
||||
} |
||||
|
||||
function testChangesOwner() { |
||||
address originalOwner = ownable.owner(); |
||||
ownable.transfer(0x0); |
||||
Assert.notEqual(originalOwner, ownable.owner(), "Ownable should change owners after transfer."); |
||||
} |
||||
|
||||
function testOnlyOwnerCanChangeOwner() { |
||||
Ownable deployedOwnable = Ownable(DeployedAddresses.Ownable()); |
||||
address originalOwner = deployedOwnable.owner(); |
||||
deployedOwnable.transfer(0x0); |
||||
Assert.equal(originalOwner, deployedOwnable.owner(), "Ownable should prevent non-owners from transfering"); |
||||
} |
||||
} |
Loading…
Reference in new issue