使用web3.js进行编译发布及调用智能合约

简要:

web3.js 是一个库的集合,允许您使用HTTP或IPC连接与本地或远程以太节点进行交互。

solc.js 是solidity的编译器。官方推荐编译方式。

Ganache CLI是以太坊开发工具Truffle套件的一部分,是Ganache的命令行版本

Ganache CLI使用ethereumjs来模拟完整的客户端行为,并使开发以太坊应用程序更快,更轻松,更安全。它还包括所有流行的RPC功能和特性(如事件),并可以确定性地运行以使开发变得简单。

注:testrpc已被废弃,现已更名为Ganache CLI

本文环境:

node

macOs 10.13.4

web3.js 1.0.0-beta.34

solc 0.4.22

环境安装:

web3.js

npm install web3

ganache-cli

npm i ganache-cli

solc

npm install solc

solidity代码:

demo.sol

pragma solidity ^0.4.0;

contract Calc{
  uint count;
  function add(uint a, uint b) returns(uint){
    count++;
    return a + b;
  }
  function getCount() returns (uint){
    return count;
  }
}

接下来通过web3.js进行编译、发布:

 (一)启动Ganache

ganache-cli

这时会列出预先创建的10个账户和10个私钥

创建本地私有链地址:localhost:8545

(二)引入所需的模块:

web3.js

let Web3 = require('web3');
let solc = require("solc");
let fs   = require('fs');

(三)实例web3对象,并使用solc编译solidity,获取发布所需对象

if(typeof web3 != 'undefined'){
	web3=new Web3(web3.currentProvider);
}else{
	web3 = new Web3('http://localhost:8545');
}
let source=fs.readFileSync("./demo.sol","utf8");
let cacl=solc.compile(source,1);
let abi= JSON.parse(cacl.contracts[':Calc'].interface);
let bytecode=cacl.contracts[':Calc'].bytecode;
注意:solc在编译完时,会在solidity的类方法前加一个冒号。

(三)发布时,需要一个账户。如果账户未激活,需unlockAccount()进行激活。

web3.eth.getAccounts().then(data=>{
	web3.eth.personal.unlockAccount(data[0])
})
注意:web3.eth.getAccounts()为异步方法,直接读取会读取不到数据。

(四)进行部署,部署成功将返回合约地址。

var rsContract=new web3.eth.Contract(abi).deploy({
	data:'0x'+bytecode,	//已0x开头
	arguments:[],	//传递构造函数的参数
}).send({
	from:data[0],
	gas:1500000,
	gasPrice:'30000000000000'
},function(error,transactionHash){
	console.log("send回调");
	console.log("error:"+error);
	console.log("send transactionHash:"+transactionHash);
})
.on('error', function(error){ console.error(error) })
.then(function(newContractInstance){
	var newContractAddress=newContractInstance.options.address
	console.log("新合约地址:"+newContractAddress);
});

注意:

    1、deploy中的data需以0x开头。

    2、from为账户地址,非合约地址

    3、发布时需传入gas和gasPrice,gas过小或过大,都会报错

    gas默认值为90000

    gasPrice默认值为20000000000 

(五)调用测试

var MyContract = new web3.eth.Contract(abi,newContractAddress);
    MyContract.methods.add(1,3).call().then(console.log);

传入abi及合约地址,便可通过web3.js的call方法进行调用合约方法。

总结:

文本注意之处,都是踩坑之处,希望可以帮助到大家。以下为完整实例代码:

let Web3 = require('web3');
let solc = require("solc");
let fs   = require('fs');
if(typeof web3 != 'undefined'){
	web3=new Web3(web3.currentProvider);
}else{
	web3 = new Web3('http://localhost:8545');
}
let source=fs.readFileSync("./demo.sol","utf8");
let cacl=solc.compile(source,1);
let abi= JSON.parse(cacl.contracts[':Calc'].interface);
let bytecode=cacl.contracts[':Calc'].bytecode;		//合约二进制码
web3.eth.getAccounts().then(data=>{
	web3.eth.personal.unlockAccount(data[0]).then(openAccountState=>{
		if(openAccountState){
			console.log("开户状态:"+openAccountState);
			var rsContract=new web3.eth.Contract(abi).deploy({
				data:'0x'+bytecode,
				arguments:[],	//传递构造函数的参数
			}).send({
				from:data[0],
				gas:1500000,
				gasPrice:'30000000000000'
			},function(error,transactionHash){
				console.log("send回调");
				console.log("error:"+error);
				console.log("send transactionHash:"+transactionHash);
			})
			.on('error', function(error){ console.error(error) })
			// .on('transactionHash', function(transactionHash){ console.log("hash:",transactionHash)})
			// .on('receipt', function(receipt){
			//    console.log(receipt.contractAddress) // contains the new contract address
			// })
			//.on('confirmation', function(confirmationNumber, receipt){console.log("receipt,",receipt)})
			.then(function(newContractInstance){
				var newContractAddress=newContractInstance.options.address
				console.log("新合约地址:"+newContractAddress);

				web3.eth.getBlockNumber().then(blockNum=>{
					console.log("当前块号:"+blockNum);
					web3.eth.getBlock(blockNum).then(data=>{
						console.log("当前块信息:");
						console.log(data);
					})
				});
				var MyContract = new web3.eth.Contract(abi,newContractAddress);
					MyContract.methods.add(1,3).call().then(console.log);




			});
			
		}
	});
});

引用:

http://web3js.readthedocs.io/en/1.0/web3.html#version

https://www.npmjs.com/package/ganache-cli

https://www.npmjs.com/package/solc


版权声明:本文为cs380637384原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。