基于Solc和Web3.js实现Solidity0.5.0智能合约的编译和部署

对于刚入门以太坊的朋友来说,最简单的智能合约编译部署就是使用Remix部署了。如果有想尝试自己写代码体会编译部署过程的朋友,请看以下内容。希望对你有所帮助。

而且由于solidity 0.4 和 0.5 版本之间的存在较大的变化,编译方式产生了较大的差异,网上许多教程都是基于0.4的,在0.5版本上几乎无法使用。本人踩了不少坑,最终找到了正确的方法。

本文代码文件的结构如下:

按上述文件结构建立好代码文件。

 

Solidity代码

测试合约代码文件 Team.sol。文件名最好和合约名字保持一致。首行指明solidity版本。

pragma solidity ^0.5.0;

contract Team{
    string public teamName

    constructor(string memory teamName) public{
        TeamName = teamName;
    }

    function getTeamName()public view returns(string memory){
        return TeamName;
    }

    function setTeamName(string memory _teamName)public{
        TeamName = _teamName;
    }
}

编译

编译之前需要确定好自己是否安装好nodejs以及npm包管理器。我的环境如下:

我所使用的编译工具是solc,可以通过npm下载。

npm install solc@0.5.0

在 compile_team.js 输入以下代码,每一步有详细的解释。

编译前需要构建一个json编译对象,详细见solc.js - github的README.md。 

var path =  require('path');
var fs = require('fs');
var solc =  require('solc');

// 获取智能合约的绝对路径
let contractPath = path.resolve('../', 'contracts', 'Team.sol');
//console.log("contracts absolute path: " + contractPath);

// 读取合约内容
let contractSource = fs.readFileSync(contractPath, 'utf-8');

//预先定义好编译源json对象
let jsonContractSource = JSON.stringify({
    language: 'Solidity',
    sources: {
      'Team.sol': {  // 指明编译的文件名
        content: contractSource, // solidity 源代码
      },
    },
    settings: { // 自定义编译输出的格式。以下选择输出全部结果。
        outputSelection: {
			'*': {
				'*': [ '*' ]
			}
		}
    },
  });

// 编译得到结果
let output = JSON.parse(solc.compile(jsonContractSource));  

teamJson = {
  'abi': {},
  'bytecode': ''
};

// output 为json对象,根据json结构保存对应的abi和bytecode
for (var contractName in output.contracts['Team.sol']) {
    teamJson.abi = output.contracts['Team.sol'][contractName].abi;
    teamJson.bytecode = output.contracts['Team.sol'][contractName].evm.bytecode.object;
}

console.log(teamJson);  

// 将teamJson数据输出到team.json文件
fs.writeFile('team.json', JSON.stringify(teamJson), function(err){
    if(err)
      console.error(err);
    console.log("team contract compiled sucessfully.")
})

我们需要保存编译输出对象中的 abi 和 bytecode字段。 abi 是应用程序字段,保存了web3.js访问合约方法。而bytecode则是在部署合约时需要引用(见下文)。

然后使用在文件当前路径下使用命令执行文件:

node compile_team.js


部署

在这里我选择 ganache-cli 作为我们的本地测试环境,将合约部署到本地。安装命令为:

npm install -g ganache-cli@6.2.3

此处是全局安装。当要进行部署的时候,可以直接新开一个终端,输入命令 ganache-cli ,即开启本地测试环境。

部署过程是先读取json文件的bytecode 然后传入参数进行部署。

var ganache = require('ganache-cli');
var Web3 = require('web3');
var web3 = new Web3(ganache.provider()); // 得到接入ganache测试环境的web3对象
var fs = require('fs');


// 获取json文件中的 abi  bytecode
var teamjson;
let abi;
let bytecode;
fs.readFile('../compile/team.json', 'utf-8', (err, data)=>{
    if(err) throw err;
    teamjson = JSON.parse(data);
    abi = teamjson.abi;
    bytecode = teamjson.bytecode;
});


var deployTeam = async(teamName)=>{
    try{
        console.log("to get the accounts");
        var accounts = await web3.eth.getAccounts(); // 获取账户
        console.log(accounts[0]);
        console.log("To deploy team contract.");
        var result = await new web3.eth.Contract(abi).deploy(
            {
                data: '0x' + bytecode, // 需要注意 字节码需要添加 '0x' 不然会有各种错误   
                arguments: [teamName]  // 此处是参数列表
            })
            .send({
                from: accounts[0],
                gas: '5000000' 
            });
        console.log("successfully! Team address: " + result.options.address);
        return result;
    }
    catch(error){
        console.log("team 合约部署失败");
        console.error(error);
    }
};
deployTeam('teamName'); // 测试
module.exports = deployTeam;

然后使用以下命令进行部署。在ganache-cli终端可以观察到部署的过程。  

node deploy_team.js


更多精彩内容