因为师门工作需要,我必须完成如题所示的相关工作。但是在网上搜了半天,确发现没有一个博文完全贴合我的需求。要么是内容不全,要么是手动编译、部署和调用智能合约。于是只能自己慢慢摸索,在遇到 n 多坑,排了 n 多雷之后,终于成功了。遂向大家分享一下经验,有需自取。欢迎打扰、讨论。(补充:如有大牛,请自行绕过……)
//编译合约源码 var output = JSON.parse(solc.compile(JSON.stringify(input)))
//获取编译后的 bytecode、abi 和 gas var abi = JSON.stringify(output.contracts['trustieTransaction.sol'].trustieTransaction.abi); // console.log("abi:", abi); var bytecode = output.contracts['trustieTransaction.sol'].trustieTransaction.evm.bytecode.object; // console.log("\n\nbytecode:", bytecode); var gas = output.contracts['trustieTransaction.sol'].trustieTransaction.evm.gasEstimates.creation.totalCost; // console.log("\n\ngas:", gas);
// 连接本地启动的 geth rpc 服务,连接成功之后进行如发起交易等操作 var Web3 = require('web3')
// 连接本地启动的 geth rpc 服务 if (typeof web3 !== 'undefined') { web3 = new Web3(web3.currentProvider); } else { web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));//默认http://localhost:8545 }