truffle inits

This commit is contained in:
mbeylin 2017-07-04 15:34:20 -04:00
parent 2b964629e7
commit 6e63f4a5a2
9 changed files with 141 additions and 43 deletions

View File

@ -15,17 +15,11 @@ Ethereum smart contracts can trivially facilitate transactions of resources (or
The _StandardBounty.sol_ contract facilitates transactions on qualitative data (often representing artifacts of completion of some service), allowing bounty issuers to systematically approve the work they receive.
Bounties can be used to facilitate transactions between two parties, where a quantitative task, qualitative task or artifact is being exchanged for ETH.
Code bug bounties are included here as an implemented extension of the generalized bounty and to serve as an example of a pure quantitative bounty that can automatically be paid out.
## 2. Implementation
A single bounty contract can be used to pay amounts of ETH or a given token, based on the successful completion of specific **Milestones**. The contract aims to reduce the necessary trust in the issuer by forcing them to deposit sufficient Ether (or tokens) to at minimum pay out each milestone once.
A bounty begins in the `draft` stage, where requirements, deadlines, and reward amounts can still be altered.
A bounty begins in the `draft` stage, where requirements, deadlines, and reward amounts can still be altered.

8
contracts/ConvertLib.sol Normal file
View File

@ -0,0 +1,8 @@
pragma solidity ^0.4.4;
library ConvertLib{
function convert(uint amount,uint conversionRate) returns (uint convertedAmount)
{
return amount * conversionRate;
}
}

34
contracts/MetaCoin.sol Normal file
View File

@ -0,0 +1,34 @@
pragma solidity ^0.4.4;
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;
event Transfer(address indexed _from, address indexed _to, uint256 _value);
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;
Transfer(msg.sender, 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];
}
}

View File

@ -31,6 +31,7 @@ contract Bountied {
function checkInvariant() returns(bool){
/*
if (uint(bounty.bountyStage()) == 0){
for (uint i = 0; i < bounty.numMilestones(); i++){
@ -68,7 +69,7 @@ contract Bountied {
} else {
return false;
}
*/
return true;
}

View File

@ -1,5 +1,8 @@
let CodeBugBountyFactory = artifacts.require("./CodeBugBountyFactory.sol")
var ConvertLib = artifacts.require("./ConvertLib.sol");
var MetaCoin = artifacts.require("./MetaCoin.sol");
module.exports = function(deployer) {
deployer.deploy(CodeBugBountyFactory);
deployer.deploy(ConvertLib);
deployer.link(ConvertLib, MetaCoin);
deployer.deploy(MetaCoin);
};

Binary file not shown.

Before

Width:  |  Height:  |  Size: 429 KiB

25
test/TestMetacoin.sol Normal file
View File

@ -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");
}
}

63
test/metacoin.js Normal file
View File

@ -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");
});
});
});

View File

@ -1,39 +1,9 @@
// Allows us to use ES6 in our migrations and tests.
require('babel-register')
const HDWalletProvider = require("truffle-hdwallet-provider")
const fs = require("fs")
// First read in the secrets.json to get our mnemonic
let secrets
let mnemonic
if(fs.existsSync("secrets.json")) {
secrets = JSON.parse(fs.readFileSync("secrets.json", "utf8"))
mnemonic = secrets.mnemonic
} else {
console.log("No secrets.json found. If you are trying to publish EPM " +
"this will fail. Otherwise, you can ignore this message!")
mnemonic = ""
}
//HD Wallet params
var providerUrlRopsten = "https://ropsten.infura.io"
var providerUrlKovan = "https://kovan.infura.io"
module.exports = {
networks: {
development: {
host: 'localhost',
host: "localhost",
port: 8545,
network_id: '*'
},
ropsten: {
network_id: 3,
provider: new HDWalletProvider(mnemonic, providerUrlRopsten)
},
kovan: {
network_id: 4,
provider: new HDWalletProvider(mnemonic, providerUrlKovan)
network_id: "*" // Match any network id
}
}
}
};