Skip to content

Commit da43a5c

Browse files
committed
first commit
0 parents  commit da43a5c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+25300
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
node_modules/

chapter1/.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["env"]
3+
}

chapter1/.eslintignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tmp/**
2+
build/**
3+
node_modules/**
4+
contracts/**
5+
migrations/1_initial_migration.js
6+
migrations/2_deploy_contracts.js
7+
test/metacoin.js

chapter1/.eslintrc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"parser": "babel-eslint",
3+
"extends": [
4+
"standard"
5+
],
6+
"plugins": [
7+
"babel"
8+
],
9+
"rules": {
10+
"key-spacing" : 0,
11+
"jsx-quotes" : [2, "prefer-single"],
12+
"max-len" : [2, 120, 2],
13+
"object-curly-spacing" : [2, "always"]
14+
}
15+
}

chapter1/app/index.html

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Truffle - Mini Dapp</title>
5+
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
6+
<link rel="stylesheet" href="https://unpkg.com/[email protected]/css/tachyons.min.css"/>
7+
<script src="./app.js"></script>
8+
</head>
9+
<body class="open-sans">
10+
<div class="absolute right-1 top-1 tc pt4 left f5 pb3 bg-black-80 w4 h4 shadow-2 br4 white">You have
11+
<div class="f3 mt3">
12+
<span id="balance" class="b"></span>
13+
ETH
14+
</div>
15+
</div>
16+
<div class="tc f2 mb5 near-black">Reward a friend for their hard work - send them Ether</div>
17+
<div class="pa4 mt5 bg-white shadow-1 br2 tc">
18+
<div class="f3 dark-green">Send Ether</div>
19+
<form class="pt3 pb4 ph5-l black-60">
20+
<fieldset class="ba b--transparent ph0 mh0">
21+
<div class="mt3 ph3 pa0-l">
22+
<input
23+
class="pa3 input-reset ba b--black-30 br2 bg-white-smoke w-100"
24+
type="number"
25+
id="amount"
26+
placeholder="Amount">
27+
</div>
28+
<div class="mt3 ph3 pa0-l">
29+
<input
30+
class="pa3 input-reset ba b--black-30 br2 bg-white-smoke w-100"
31+
type="text"
32+
id="receiver"
33+
placeholder="Address">
34+
</div>
35+
</fieldset>
36+
</form>
37+
<button
38+
class="white pa3 ph4 bg-green br2 f3 b pointer"
39+
onclick="App.reward()"
40+
id="send"
41+
>&#8594;
42+
</button>
43+
</div>
44+
<div id="status" class="pt4"></div>
45+
</body>
46+
</html>

chapter1/app/javascripts/app.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Import the page's CSS. Webpack will know what to do with it.
2+
import "../stylesheets/app.css";
3+
4+
// Import libraries we need.
5+
import { default as Web3} from 'web3';
6+
import { default as contract } from 'truffle-contract'
7+
8+
// Import our contract artifacts and turn them into usable abstractions.
9+
import organization_artifacts from '../../build/contracts/Organization.json'
10+
11+
// Organization is our usable abstraction, which we'll use through the code below.
12+
var Organization = contract(organization_artifacts);
13+
14+
// The following code is simple to show off interacting with your contracts.
15+
// As your needs grow you will likely need to change its form and structure.
16+
// For application bootstrapping, check out window.addEventListener below.
17+
var accounts;
18+
var account;
19+
20+
window.App = {
21+
start: function() {
22+
var self = this;
23+
24+
// Bootstrap the Organization abstraction for Use.
25+
Organization.setProvider(web3.currentProvider);
26+
27+
// Get the initial account balance so it can be displayed.
28+
web3.eth.getAccounts(function(err, accs) {
29+
if (err != null) {
30+
alert("There was an error fetching your accounts.");
31+
return;
32+
}
33+
34+
if (accs.length == 0) {
35+
alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
36+
return;
37+
}
38+
39+
accounts = accs;
40+
account = accounts[0];
41+
42+
self.refreshBalance();
43+
});
44+
},
45+
46+
setStatus: function(message) {
47+
var status = document.getElementById("status");
48+
status.innerHTML = message;
49+
},
50+
51+
refreshBalance: function() {
52+
var self = this;
53+
54+
var organization;
55+
Organization.deployed().then(function(instance) {
56+
organization = instance;
57+
return organization.getBalance.call(account, {from: account});
58+
}).then(function(value) {
59+
document.getElementById("balance").innerHTML = value.valueOf();
60+
document.getElementById("balance").style.color = "white";
61+
}).catch(function(e) {
62+
console.log(e);
63+
self.setStatus("Error getting balance; see log.");
64+
});
65+
},
66+
67+
reward: function() {
68+
var self = this;
69+
70+
var amount = parseInt(document.getElementById("amount").value);
71+
var receiver = document.getElementById("receiver").value;
72+
73+
this.setStatus("Initiating transaction... (please wait)");
74+
75+
var organization;
76+
Organization.deployed().then(function(instance) {
77+
organization = instance;
78+
return organization.reward(receiver, amount, {from: account});
79+
}).then(function() {
80+
self.setStatus("Transaction complete!");
81+
self.refreshBalance();
82+
}).catch(function(e) {
83+
console.log(e);
84+
self.setStatus("Error sending coin; see log.");
85+
});
86+
}
87+
};
88+
89+
window.addEventListener('load', function() {
90+
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
91+
if (typeof web3 !== 'undefined') {
92+
console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 ether, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-organizationmask")
93+
// Use Mist/MetaMask's provider
94+
window.web3 = new Web3(web3.currentProvider);
95+
} else {
96+
console.warn("No web3 detected. Falling back to http://127.0.0.1:9545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-organizationmask");
97+
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
98+
window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:9545"));
99+
}
100+
101+
App.start();
102+
});

chapter1/app/stylesheets/app.css

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
body {
2+
margin-left: 25%;
3+
margin-right: 25%;
4+
margin-top: 10%;
5+
font-family: "Open Sans", sans-serif;
6+
}
7+
8+
label {
9+
display: inline-block;
10+
width: 100px;
11+
}
12+
13+
input {
14+
width: 500px;
15+
padding: 5px;
16+
font-size: 16px;
17+
}
18+
19+
button {
20+
font-size: 16px;
21+
padding: 5px;
22+
}
23+
24+
h1, h2 {
25+
display: inline-block;
26+
vertical-align: middle;
27+
margin-top: 0px;
28+
margin-bottom: 10px;
29+
}
30+
31+
h2 {
32+
color: #AAA;
33+
font-size: 32px;
34+
}
35+
36+
h3 {
37+
font-weight: normal;
38+
color: #AAA;
39+
font-size: 24px;
40+
}
41+
42+
.black {
43+
color: black;
44+
}
45+
46+
#balance {
47+
color: black;
48+
}
49+
50+
.hint {
51+
color: #666;
52+
}

chapter1/box-img-lg.png

10 KB
Loading

chapter1/box-img-sm.png

5.26 KB
Loading

0 commit comments

Comments
 (0)