From 3e41a2189a7ddf6d3135d8def7bf81434c34f936 Mon Sep 17 00:00:00 2001 From: Jordi Baylina Date: Wed, 17 May 2017 09:33:56 +0200 Subject: [PATCH] Truffle preparation --- .gitignore | 2 +- contracts/ContributionWallet.sol | 2 +- contracts/Migrations.sol | 23 +++++++++++ contracts/MultisigWallet.sol | 2 +- contracts/SGT.sol | 2 +- contracts/SNT.sol | 2 +- contracts/SNTPlaceHolder.sol | 5 +-- migrations/1_initial_migration.js | 5 +++ migrations/2_deploy_contracts.js | 8 ++++ package.json | 2 - test/TestMetacoin.sol | 25 ++++++++++++ test/metacoin.js | 63 +++++++++++++++++++++++++++++++ truffle.js | 9 +++++ 13 files changed, 140 insertions(+), 10 deletions(-) create mode 100644 contracts/Migrations.sol create mode 100644 migrations/1_initial_migration.js create mode 100644 migrations/2_deploy_contracts.js create mode 100644 test/TestMetacoin.sol create mode 100644 test/metacoin.js create mode 100644 truffle.js diff --git a/.gitignore b/.gitignore index 552f221..b38db2f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ node_modules/ -*.log +build/ diff --git a/contracts/ContributionWallet.sol b/contracts/ContributionWallet.sol index 8407d7d..a803ff7 100644 --- a/contracts/ContributionWallet.sol +++ b/contracts/ContributionWallet.sol @@ -1,6 +1,6 @@ pragma solidity ^0.4.8; -import "./StatusContribution.sol" +import "./StatusContribution.sol"; // @dev Contract to hold sale raised funds during the sale period. // Prevents attack in which the Aragon Multisig sends raised ether diff --git a/contracts/Migrations.sol b/contracts/Migrations.sol new file mode 100644 index 0000000..7e7fe8d --- /dev/null +++ b/contracts/Migrations.sol @@ -0,0 +1,23 @@ +pragma solidity ^0.4.4; + +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/contracts/MultisigWallet.sol b/contracts/MultisigWallet.sol index 5f5df3c..5018042 100644 --- a/contracts/MultisigWallet.sol +++ b/contracts/MultisigWallet.sol @@ -1,4 +1,4 @@ -pragma solidity 0.4.8; +pragma solidity ^0.4.8; /// @title Multisignature wallet - Allows multiple parties to agree on transactions before execution. diff --git a/contracts/SGT.sol b/contracts/SGT.sol index 9918f4e..9d44dca 100644 --- a/contracts/SGT.sol +++ b/contracts/SGT.sol @@ -9,7 +9,7 @@ import "./MiniMeToken.sol"; */ contract SGT is MiniMeToken { - uint constant D160 = 0x10000000000000000000000000000000000000000; + uint constant D160 = 0x0010000000000000000000000000000000000000000; function SGT( address _tokenFactory diff --git a/contracts/SNT.sol b/contracts/SNT.sol index fef7ebf..e3ccf86 100644 --- a/contracts/SNT.sol +++ b/contracts/SNT.sol @@ -12,7 +12,7 @@ contract SNT is MiniMeToken { // @dev SNT constructor just parametrizes the MiniMeIrrevocableVestedToken constructor function SNT( address _tokenFactory - ) MiniMeIrrevocableVestedToken( + ) MiniMeToken( _tokenFactory, 0x0, // no parent token 0, // no snapshot block number from parent diff --git a/contracts/SNTPlaceHolder.sol b/contracts/SNTPlaceHolder.sol index 81a4c3d..d219a22 100644 --- a/contracts/SNTPlaceHolder.sol +++ b/contracts/SNTPlaceHolder.sol @@ -1,8 +1,7 @@ pragma solidity ^0.4.8; import "./MiniMeToken.sol"; -import "./Controller.sol"; -import "./StatusContribution"; +import "./StatusContribution.sol"; /* @@ -18,7 +17,7 @@ import "./StatusContribution"; asks it to do so. */ -contract SNTPlaceHolder is Controller { +contract SNTPlaceHolder is TokenController { address public owner; MiniMeToken public snt; StatusContribution public contribution; diff --git a/migrations/1_initial_migration.js b/migrations/1_initial_migration.js new file mode 100644 index 0000000..4d5f3f9 --- /dev/null +++ b/migrations/1_initial_migration.js @@ -0,0 +1,5 @@ +var Migrations = artifacts.require("./Migrations.sol"); + +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 0000000..b3dc3e9 --- /dev/null +++ b/migrations/2_deploy_contracts.js @@ -0,0 +1,8 @@ +var ConvertLib = artifacts.require("./ConvertLib.sol"); +var MetaCoin = artifacts.require("./MetaCoin.sol"); + +module.exports = function(deployer) { + deployer.deploy(ConvertLib); + deployer.link(ConvertLib, MetaCoin); + deployer.deploy(MetaCoin); +}; diff --git a/package.json b/package.json index ced916f..5dc75a8 100644 --- a/package.json +++ b/package.json @@ -16,8 +16,6 @@ "babel-preset-stage-2": "^6.18.0", "babel-preset-stage-3": "^6.17.0", "babel-register": "^6.23.0", - "ethereumjs-testrpc": "^3.0.2", - "truffle": "git+https://github.com/ConsenSys/truffle.git#3.1.9" }, "dependencies": { "async": "^2.4.0", diff --git a/test/TestMetacoin.sol b/test/TestMetacoin.sol new file mode 100644 index 0000000..96ccc1e --- /dev/null +++ b/test/TestMetacoin.sol @@ -0,0 +1,25 @@ +pragma solidity ^0.4.2; + +import "truffle/Assert.sol"; +import "truffle/DeployedAddresses.sol"; +import "../contracts/MetaCoin.sol"; + +contract TestMetacoin { + + function testInitialBalanceUsingDeployedContract() { + MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin()); + + uint expected = 10000; + + Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially"); + } + + function testInitialBalanceWithNewMetaCoin() { + MetaCoin meta = new MetaCoin(); + + uint expected = 10000; + + Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially"); + } + +} diff --git a/test/metacoin.js b/test/metacoin.js new file mode 100644 index 0000000..c61c093 --- /dev/null +++ b/test/metacoin.js @@ -0,0 +1,63 @@ +var MetaCoin = artifacts.require("./MetaCoin.sol"); + +contract('MetaCoin', function(accounts) { + it("should put 10000 MetaCoin in the first account", function() { + return MetaCoin.deployed().then(function(instance) { + return instance.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; + var metaCoinBalance; + var metaCoinEthBalance; + + return MetaCoin.deployed().then(function(instance) { + meta = instance; + 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 unexpected function, linkage may be broken"); + }); + }); + it("should send coin correctly", function() { + var meta; + + // 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 MetaCoin.deployed().then(function(instance) { + meta = instance; + 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 0000000..2491c7c --- /dev/null +++ b/truffle.js @@ -0,0 +1,9 @@ +module.exports = { + networks: { + development: { + host: "localhost", + port: 8545, + network_id: "*" // Match any network id + } + } +};