大佬教程收集整理的这篇文章主要介绍了node.js私有连智能合约开发学习1,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
geth --rpc --rpcport "8545" --rpcapi "eth,web3,personal,net" --rpccorsdomain * console 2>>test.log --dev
如下图所示:
miner.start()
const Web3 = require('web3');
const solc = require ('solc');
如果不设置的话,会报错:Error: Provider not set or invalid
let web3 = new Web3();
web3.setProvider(new Web3.providers.httpProvider("http://127.0.0.1:8545"));
类似命令行中的:
const solidity=`pragma solidity ^0.4.20; contract HelloWorldContract { function sayHi() constant returns (String){ return 'Hello World'; } }`;
const output = solc.compile(solidity.toString(),1);
const bytecode = output.contracts[':HelloWorldContract'].bytecode;
const abi = output.contracts[':HelloWorldContract'].interface;
//创建一个solidity的合约对象,用来在某个地址上初始化合约
const HelloWorldContract = web3.eth.contract(JSON.parse(abi));
const HelloWorldContracTinstance = HelloWorldContract.new({
data: '0x' + bytecode,from: web3.eth.coinbase,gas: 1000000
},(err,res) => {
if (err) {
console.log(err);
return;
}
// If we have an address property,the contract was deployed
console.log("res.address",res.address);
if (res.address) {//得有人挖矿(miner.start()),不然这个地址就是undifined
console.log('Contract address: ' + res.address);
console.log(HelloWorldContract.at(res.address).sayHi({gas: 22222}))
}
});
输出:
res@H_674_238@.address undefined
res@H_674_238@.address 0x466e386e1707f4fa9fe34deaabb6f500429521d4
Contract address: 0x466e386e1707f4fa9fe34deaabb6f500429521d4
Hello World
可以看出new的回调函数执行了2次,参考文献1
第一次回调是发送交易(sendtransaction)之后,第二次是得到交易的收据(gettransactionReceipt)之后
这个时候,挖矿了
参考文献
以上是大佬教程为你收集整理的node.js私有连智能合约开发学习1全部内容,希望文章能够帮你解决node.js私有连智能合约开发学习1所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。