diff --git a/BlindAuction.sol b/contracts/BlindAuction.sol similarity index 100% rename from BlindAuction.sol rename to contracts/BlindAuction.sol diff --git a/contracts/ConvertLib.sol b/contracts/ConvertLib.sol new file mode 100644 index 000000000..b5d30aa01 --- /dev/null +++ b/contracts/ConvertLib.sol @@ -0,0 +1,6 @@ +library ConvertLib{ + function convert(uint amount,uint conversionRate) returns (uint convertedAmount) + { + return amount * conversionRate; + } +} \ No newline at end of file diff --git a/contracts/MetaCoin.sol b/contracts/MetaCoin.sol new file mode 100644 index 000000000..128f746c1 --- /dev/null +++ b/contracts/MetaCoin.sol @@ -0,0 +1,29 @@ +import "ConvertLib.sol"; + +// This is just a simple example of a coin-like contract. +// It is not standards compatible and cannot be expected to talk to other +// coin/token contracts. If you want to create a standards-compliant +// token, see: https://github.com/ConsenSys/Tokens. Cheers! + +contract MetaCoin { + mapping (address => uint) balances; + + function MetaCoin() { + balances[tx.origin] = 10000; + } + + function sendCoin(address receiver, uint amount) returns(bool sufficient) { + if (balances[msg.sender] < amount) return false; + balances[msg.sender] -= amount; + balances[receiver] += amount; + return true; + } + + function getBalanceInEth(address addr) returns(uint){ + return ConvertLib.convert(getBalance(addr),2); + } + + function getBalance(address addr) returns(uint) { + return balances[addr]; + } +} diff --git a/contracts/Migrations.sol b/contracts/Migrations.sol new file mode 100644 index 000000000..132f325c1 --- /dev/null +++ b/contracts/Migrations.sol @@ -0,0 +1,21 @@ +contract Migrations { + address public owner; + uint public last_completed_migration; + + modifier restricted() { + if (msg.sender == owner) _ + } + + function Migrations() { + owner = msg.sender; + } + + function setCompleted(uint completed) restricted { + last_completed_migration = completed; + } + + function upgrade(address new_address) restricted { + Migrations upgraded = Migrations(new_address); + upgraded.setCompleted(last_completed_migration); + } +} diff --git a/SimpleAuction.sol b/contracts/SimpleAuction.sol similarity index 100% rename from SimpleAuction.sol rename to contracts/SimpleAuction.sol diff --git a/migrations/1_initial_migration.js b/migrations/1_initial_migration.js new file mode 100644 index 000000000..5b309e062 --- /dev/null +++ b/migrations/1_initial_migration.js @@ -0,0 +1,3 @@ +module.exports = function(deployer) { + deployer.deploy(Migrations); +}; diff --git a/migrations/2_deploy_contracts.js b/migrations/2_deploy_contracts.js new file mode 100644 index 000000000..ebb3a480d --- /dev/null +++ b/migrations/2_deploy_contracts.js @@ -0,0 +1,5 @@ +module.exports = function(deployer) { + deployer.deploy(ConvertLib); + deployer.autolink(); + deployer.deploy(MetaCoin); +}; diff --git a/test/metacoin.js b/test/metacoin.js new file mode 100644 index 000000000..1f611eea5 --- /dev/null +++ b/test/metacoin.js @@ -0,0 +1,57 @@ +contract('MetaCoin', function(accounts) { + it("should put 10000 MetaCoin in the first account", function() { + var meta = MetaCoin.deployed(); + + return meta.getBalance.call(accounts[0]).then(function(balance) { + assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account"); + }); + }); + it("should call a function that depends on a linked library ", function(){ + var meta = MetaCoin.deployed(); + var metaCoinBalance; + var metaCoinEthBalance; + + return meta.getBalance.call(accounts[0]).then(function(outCoinBalance){ + metaCoinBalance = outCoinBalance.toNumber(); + return meta.getBalanceInEth.call(accounts[0]); + }).then(function(outCoinBalanceEth){ + metaCoinEthBalance = outCoinBalanceEth.toNumber(); + + }).then(function(){ + assert.equal(metaCoinEthBalance,2*metaCoinBalance,"Library function returned unexpeced function, linkage may be broken"); + + }); + }); + it("should send coin correctly", function() { + var meta = MetaCoin.deployed(); + + // Get initial balances of first and second account. + var account_one = accounts[0]; + var account_two = accounts[1]; + + var account_one_starting_balance; + var account_two_starting_balance; + var account_one_ending_balance; + var account_two_ending_balance; + + var amount = 10; + + return meta.getBalance.call(account_one).then(function(balance) { + account_one_starting_balance = balance.toNumber(); + return meta.getBalance.call(account_two); + }).then(function(balance) { + account_two_starting_balance = balance.toNumber(); + return meta.sendCoin(account_two, amount, {from: account_one}); + }).then(function() { + return meta.getBalance.call(account_one); + }).then(function(balance) { + account_one_ending_balance = balance.toNumber(); + return meta.getBalance.call(account_two); + }).then(function(balance) { + account_two_ending_balance = balance.toNumber(); + + assert.equal(account_one_ending_balance, account_one_starting_balance - amount, "Amount wasn't correctly taken from the sender"); + assert.equal(account_two_ending_balance, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver"); + }); + }); +}); diff --git a/truffle.js b/truffle.js new file mode 100644 index 000000000..6e3970801 --- /dev/null +++ b/truffle.js @@ -0,0 +1,16 @@ +module.exports = { + build: { + "index.html": "index.html", + "app.js": [ + "javascripts/app.js" + ], + "app.css": [ + "stylesheets/app.css" + ], + "images/": "images/" + }, + rpc: { + host: "localhost", + port: 8545 + } +};