-
Notifications
You must be signed in to change notification settings - Fork 10
Basic Contract
Ethan Wilding edited this page Mar 18, 2016
·
1 revision
On March 4th we demonstrated how to build a website that made some queries to your local Geth/Ethereum accounts. This involved running a local instance in Geth and then using the Web3.js javascript API to check balances and send ether. A glossary / key concepts section is at the bottom.
<script src="web3.min.js"></script><body>
<h3>Account</h3>
<p id="account"></p>
<h3>Account Balance</h3>
<p id="balance"></p> ether
<button id="sendEther" onclick="sendSomeEther()">Send 1 Ether</button>
<p id="response"></p>
<button id="getR" onclick="getR()">What Happened!</button>
<p id="response2"></p>
<script>
window.web3 = new Web3();
window.transactionHash = '';
var getR = function() {
console.log(window.transactionHash);
web3.eth.getTransactionReceipt(window.transactionHash, function(err, receipt){
document.getElementById("response2").innerHTML = receipt.transactionHash;
});
};
function sendSomeEther(){
var txObject = {
from: web3.eth.defaultAccount,
gas: 30000000,
value: web3.toWei(12, 'ether'),
to: '0xcc6ead2599da2d6ef899ff65a36fd899b9dbc4d0'
};
web3.eth.sendTransaction(txObject, function(err, result){
if(err)
return document.getElementById("response").innerHTML = 'Error: ' + err;
document.getElementById("response").innerHTML = 'Yay, first one! transaction hash:' + result;
window.transactionHash = result;
});
};
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
web3.eth.getAccounts(function(err, accounts){
web3.eth.defaultAccount = accounts[0];met
document.getElementById("account").innerHTML = accounts[0];
web3.eth.getBalance(accounts[0], function(err, balance){
document.getElementById("balance").innerHTML = web3.fromWei(balance, 'ether');
});
});
</script>
</body>
- import web3.js script
- web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
- web3.eth.getTransactionReceipt
- web3.eth.getAccounts
- web3.eth.getAccounts
- web3.toWei(amount, 'ether'),
- Function:
function sendSomeEther(){..txObject}
- Objects:
var txObject = { from: web3.eth.defaultAccount, gas: 30000000, value: web3.toWei(12, 'ether'), to: '0x0ed.....' };
- document.getElementById
- callbacks: function(err, receipt){..}