javascript 使用以太坊的网络界面探索智能合约,并为iMAL的区块链研讨会提供可靠性
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 使用以太坊的网络界面探索智能合约,并为iMAL的区块链研讨会提供可靠性相关的知识,希望对你有一定的参考价值。
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
//console.log(web3); // {eth: .., shh: ...} // it's here!
console.log("accounts:", web3.eth.accounts); // {eth: .., shh: ...} // it's here!
console.log("balance of first account:", web3.eth.getBalance(web3.eth.accounts[0]).toString(10));
console.log("balance of first account:", web3.eth.getBalance(web3.eth.accounts[1]).toString(10));
console.log("balance of first account:", web3.eth.getBalance(web3.eth.accounts[2]).toString(10));
console.log("balance of first account:", web3.eth.getBalance(web3.eth.accounts[3]).toString(10));
var firstAccount = web3.eth.accounts[0];
var secondAccount = web3.eth.accounts[1];
var thirdAccount = web3.eth.accounts[2];
console.log("number of last block:", web3.eth.blockNumber);
console.log("value of last block:", web3.eth.getBlock(web3.eth.blockNumber));
console.log("block number:", web3.eth.blockNumber);
console.log("gas price:", web3.eth.gasPrice.toString(10));
console.log("compilers:", web3.eth.getCompilers());
var ballotContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"to","type":"address"}],"name":"delegate","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"winningProposal","outputs":[{"name":"winningProposal","type":"uint8"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"voter","type":"address"}],"name":"giveRightToVote","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"proposal","type":"uint8"}],"name":"vote","outputs":[],"payable":false,"type":"function"},{"inputs":[{"name":"_numProposals","type":"uint8"}],"type":"constructor"}]);
// abi
/*
var _numProposals = 4;
var ballot = ballotContract.new(
_numProposals,
{
from: web3.eth.accounts[0],
data: '60606040526040516020806107d1833981016040528080519060200190919050505b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff02191690836c01000000000000000000000000908102040217905550600160016000506000600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600050600001600050819055508060ff1660026000508181548183558181151161010e5781836000526020600020918201910161010d91906100eb565b808211156101095760006000820160005060009055506001016100eb565b5090565b5b505050505b506106af806101226000396000f360606040526000357c0100000000000000000000000000000000000000000000000000000000900480635c19a95c1461005d578063609ff1bd1461007a5780639e7b8d61146100a5578063b3f98adc146100c257610058565b610002565b346100025761007860048080359060200190919050506100df565b005b346100025761008c6004805050610412565b604051808260ff16815260200191505060405180910390f35b34610002576100c060048080359060200190919050506104a9565b005b34610002576100dd600480803590602001909190505061058b565b005b60006000600160005060003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005091508160010160009054906101000a900460ff16156101305761040d565b5b600073ffffffffffffffffffffffffffffffffffffffff16600160005060008573ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060010160029054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161415801561023e57503373ffffffffffffffffffffffffffffffffffffffff16600160005060008573ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060010160029054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1614155b1561029f57600160005060008473ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060010160029054906101000a900473ffffffffffffffffffffffffffffffffffffffff1692508250610131565b3373ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1614156102d85761040d565b60018260010160006101000a81548160ff02191690837f0100000000000000000000000000000000000000000000000000000000000000908102040217905550828260010160026101000a81548173ffffffffffffffffffffffffffffffffffffffff02191690836c01000000000000000000000000908102040217905550600160005060008473ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005090508060010160009054906101000a900460ff16156103f057816000016000505460026000508260010160019054906101000a900460ff1660ff16815481101561000257906000526020600020900160005b5060000160008282825054019250508190555061040c565b8160000160005054816000016000828282505401925050819055505b5b505050565b60006000600060009150600090505b6002600050805490508160ff1610156104a3578160026000508260ff16815481101561000257906000526020600020900160005b506000016000505411156104955760026000508160ff16815481101561000257906000526020600020900160005b50600001600050549150815080925082505b5b8080600101915050610421565b5b505090565b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415806105425750600160005060008273ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060010160009054906101000a900460ff165b1561054c57610588565b6001600160005060008373ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600050600001600050819055505b50565b6000600160005060003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005090508060010160009054906101000a900460ff16806105e657506002600050805490508260ff1610155b156105f0576106ab565b60018160010160006101000a81548160ff02191690837f0100000000000000000000000000000000000000000000000000000000000000908102040217905550818160010160016101000a81548160ff02191690837f0100000000000000000000000000000000000000000000000000000000000000908102040217905550806000016000505460026000508360ff16815481101561000257906000526020600020900160005b506000016000828282505401925050819055505b505056',
gas: 4700000
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
});
*/
// note that it initally failed, had to unlock the account using personal.unlockAccount(accounts0)
// probably account0 became the chairperson
// commented since already created e.g. at 0xbbb8c89454da2b2f717df65efce26189a0bc4f64
var ballotContractAddress = "0xbbb8c89454da2b2f717df65efce26189a0bc4f64";
var myContractInstance = ballotContract.at(ballotContractAddress);
console.log("contract:", myContractInstance);
// set the default account
web3.eth.defaultAccount = firstAccount;
console.log("default account:", web3.eth.defaultAccount);
//console.log(myContractInstance.giveRightToVote(secondAccount));
// fails, account locked
// works (maybe, transaction went through)
// weird not have to ask for ether/gas
//var result = "0xdad4a13dba760716ec058853ddfc7a728b164debf726bdc58e36ca46fa106294";
//web3.eth.defaultAccount = secondAccount;
//var voteResult = myContractInstance.vote(0);
//console.log(voteResult);
web3.eth.defaultAccount = thirdAccount;
var voteResult = myContractInstance.vote(0);
console.log(voteResult);
//console.log(myContractInstance.Ballot(0));
以上是关于javascript 使用以太坊的网络界面探索智能合约,并为iMAL的区块链研讨会提供可靠性的主要内容,如果未能解决你的问题,请参考以下文章
基于以太坊的智能合约开发教程Solidity mapping在结构体当中的使用
头歌-信息安全技术-用Python实现自己的区块链支持以太坊的云笔记服务器端开发编写并测试用于保存云笔记的智能合约支持以太坊的云笔记小程序开发基础