diff options
Diffstat (limited to 'cmd/ethtest/example')
-rw-r--r-- | cmd/ethtest/example/contract.html | 75 | ||||
-rw-r--r-- | cmd/ethtest/example/index.html | 41 | ||||
-rw-r--r-- | cmd/ethtest/example/node-app.js | 16 |
3 files changed, 132 insertions, 0 deletions
diff --git a/cmd/ethtest/example/contract.html b/cmd/ethtest/example/contract.html new file mode 100644 index 000000000..44f0b03a1 --- /dev/null +++ b/cmd/ethtest/example/contract.html @@ -0,0 +1,75 @@ +<!doctype> +<html> + +<head> +<script type="text/javascript" src="js/es6-promise/promise.min.js"></script> +<script type="text/javascript" src="../dist/ethereum.js"></script> +<script type="text/javascript"> + + var web3 = require('web3'); + web3.setProvider(new web3.providers.AutoProvider()); + + // solidity source code + var source = "" + + "contract test {\n" + + " function multiply(uint a) returns(uint d) {\n" + + " return a * 7;\n" + + " }\n" + + "}\n"; + + // contract description, this will be autogenerated somehow + var desc = [{ + "name": "multiply", + "inputs": [ + { + "name": "a", + "type": "uint256" + } + ], + "outputs": [ + { + "name": "d", + "type": "uint256" + } + ] + }]; + + var contract; + + function createExampleContract() { + // hide create button + document.getElementById('create').style.visibility = 'hidden'; + document.getElementById('source').innerText = source; + + // create contract + web3.eth.transact({code: web3.eth.solidity(source)}).then(function (address) { + contract = web3.contract(address, desc); + document.getElementById('call').style.visibility = 'visible'; + }); + } + + function callExampleContract() { + // this should be generated by ethereum + var param = document.getElementById('value').value; + + // call the contract + contract.multiply(param).call().then(function(res) { + document.getElementById('result').innerText = res[0]; + }); + } + +</script> +</head> +<body> + <h1>contract</h1> + <div id="source"></div> + <div id='create'> + <button type="button" onClick="createExampleContract();">create example contract</button> + </div> + <div id='call' style='visibility: hidden;'> + <input type="number" id="value" onkeyup='callExampleContract()'></input> + </div> + <div id="result"></div> +</body> +</html> + diff --git a/cmd/ethtest/example/index.html b/cmd/ethtest/example/index.html new file mode 100644 index 000000000..d0bf094ef --- /dev/null +++ b/cmd/ethtest/example/index.html @@ -0,0 +1,41 @@ +<!doctype> +<html> + +<head> +<script type="text/javascript" src="js/es6-promise/promise.min.js"></script> +<script type="text/javascript" src="../dist/ethereum.js"></script> +<script type="text/javascript"> + + var web3 = require('web3'); + web3.setProvider(new web3.providers.AutoProvider()); + + function watchBalance() { + var coinbase = web3.eth.coinbase; + var originalBalance = 0; + + web3.eth.balanceAt(coinbase).then(function (balance) { + originalBalance = web3.toDecimal(balance); + document.getElementById('original').innerText = 'original balance: ' + originalBalance + ' watching...'; + }); + + web3.eth.watch({altered: coinbase}).changed(function() { + web3.eth.balanceAt(coinbase).then(function (balance) { + var currentBalance = web3.toDecimal(balance); + document.getElementById("current").innerText = 'current: ' + currentBalance; + document.getElementById("diff").innerText = 'diff: ' + (currentBalance - originalBalance); + }); + }); + } + +</script> +</head> +<body> + <h1>coinbase balance</h1> + <button type="button" onClick="watchBalance();">watch balance</button> + <div></div> + <div id="original"></div> + <div id="current"></div> + <div id="diff"></div> +</body> +</html> + diff --git a/cmd/ethtest/example/node-app.js b/cmd/ethtest/example/node-app.js new file mode 100644 index 000000000..f63fa9115 --- /dev/null +++ b/cmd/ethtest/example/node-app.js @@ -0,0 +1,16 @@ +#!/usr/bin/env node + +require('es6-promise').polyfill(); + +var web3 = require("../index.js"); + +web3.setProvider(new web3.providers.HttpRpcProvider('http://localhost:8080')); + +web3.eth.coinbase.then(function(result){ + console.log(result); + return web3.eth.balanceAt(result); +}).then(function(balance){ + console.log(web3.toDecimal(balance)); +}).catch(function(err){ + console.log(err); +});
\ No newline at end of file |