27 以太坊编译部署脚本

一、
编译是对合约进行部署和测试的前置步骤,编译步骤的目标是把源代码转成 ABI 和 Bytecode,并且能够处理编译时抛出的错误,确保不会在包含错误的源代码上进行编译。
1、目录结构
在这里插入图片描述
2、合约源码

pragma solidity ^0.4.22; 
contract Car { 
	string public brand; 
	constructor(string initialBrand) public { 
		brand = initialBrand; 
	} 
	function setBrand(string newBrand) public {
		brand = newBrand; 
	}
}

在这里插入图片描述
在这里插入图片描述
3、开发编译脚本

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

const contractPath = path.resolve(__dirname, '../contracts', 'Car.sol');
const contractSource = fs.readFileSync(contractPath, 'utf-8');
let compileResult = solc.compile(contractSource, 1);

console.log(compileResult);

改良后的脚本,将信息存入指定文件

const fs = require('fs-extra');  
const solc = require('solc'); 
const path = require('path');
const contractPath = path.resolve(__dirname, '../contracts', 'Car.sol'); 
const contractSource = fs.readFileSync(contractPath, 'utf8'); 
const compileResult = solc.compile(contractSource, 1); 
console.log(compileResult);
Object.keys(compileResult.contracts).forEach(name=>{
    let contractName = name.replace(/^:/,'');
    let filePath = path.resolve(__dirname, '../compiled', `${contractName}.json`);
    fs.outputJsonSync(filePath, compileResult.contracts[name]);
    console.log("Saving json file to");
});

在这里插入图片描述加入可清空以前编译过文件的功能

const fs = require('fs-extra');  
const solc = require('solc'); 
const path = require('path');

const compiledPath = path.resolve(__dirname, '../compiled');
fs.removeSync(compiledPath);
fs.ensureDirSync(compiledPath);

const contractPath = path.resolve(__dirname, '../contracts', 'Car.sol'); 
const contractSource = fs.readFileSync(contractPath, 'utf8'); 
let compileResult = solc.compile(contractSource, 1); 

Object.keys(compileResult.contracts).forEach(name=>{
    let contractName = name.replace(/^:/,'');
    let filePath = path.resolve(__dirname, '../compiled', `${contractName}.json`);
    fs.outputJsonSync(filePath, compileResult.contracts[name]);
    console.log("Saving json file to", filePath);
});

在这里插入图片描述

最终版,可编译报错

const fs = require('fs-extra'); 
const path = require('path'); 
const solc = require('solc'); 

// cleanup 
const compiledDir = path.resolve(__dirname, '../compiled');
fs.removeSync(compiledDir); 
fs.ensureDirSync(compiledDir); 

// compile 
const contractPath = path.resolve(__dirname, 
				'../contracts', 'Car.sol'); 
const contractSource = fs.readFileSync(contractPath, 'utf8'); 
const result = solc.compile(contractSource, 1); 

// check errors 
if (Array.isArray(result.errors) && result.errors.length) {
	throw new Error(result.errors[0]); 
}

// save to disk 
Object.keys(result.contracts).forEach(name => { 
const contractName = name.replace(/^:/, ''); 
const filePath = path.resolve(compiledDir, 
				`${contractName}.json`); 
fs.outputJsonSync(filePath, result.contracts[name]); 
console.log(`save compiled contract ${contractName} to 
		${filePath}`); });	

在这里插入图片描述
二、问题解决
问题:

AssertionError [ERR_ASSERTION]: Invalid callback object specified.

解决:
在这里插入图片描述
原因:
本地solc版本太高

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值