use truffle
This commit is contained in:
6
contracts/ConvertLib.sol
Normal file
6
contracts/ConvertLib.sol
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
library ConvertLib{
|
||||||
|
function convert(uint amount,uint conversionRate) returns (uint convertedAmount)
|
||||||
|
{
|
||||||
|
return amount * conversionRate;
|
||||||
|
}
|
||||||
|
}
|
||||||
29
contracts/MetaCoin.sol
Normal file
29
contracts/MetaCoin.sol
Normal file
@ -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];
|
||||||
|
}
|
||||||
|
}
|
||||||
21
contracts/Migrations.sol
Normal file
21
contracts/Migrations.sol
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
3
migrations/1_initial_migration.js
Normal file
3
migrations/1_initial_migration.js
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module.exports = function(deployer) {
|
||||||
|
deployer.deploy(Migrations);
|
||||||
|
};
|
||||||
5
migrations/2_deploy_contracts.js
Normal file
5
migrations/2_deploy_contracts.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module.exports = function(deployer) {
|
||||||
|
deployer.deploy(ConvertLib);
|
||||||
|
deployer.autolink();
|
||||||
|
deployer.deploy(MetaCoin);
|
||||||
|
};
|
||||||
57
test/metacoin.js
Normal file
57
test/metacoin.js
Normal file
@ -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");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
16
truffle.js
Normal file
16
truffle.js
Normal file
@ -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
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user