solidity0.7.1以上本地编译部署配置

由于truffle的solc list 最高只有0.7.1,笔者使用的是0.8.0,因此有了本文

一、solc

1.安装solcjs

        npm install solc

2.官方文档

        GitHub - ethereum/solc-js: Javascript bindings for the Solidity compiler

3. Compile.js 用于编译合约,生成bytecode和abi

const fs = require("fs"),
    solc = require('solc');
const contractData=fs.readFileSync('Loan.sol').toString() //读入合约文件
var input = {
    language: 'Solidity',
    sources: {
        'test.sol': {  //此处可随意更改,下面跟此处保持一致即可
            content: contractData
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': ['*']
            }
        }
    }
};

var output = JSON.parse(solc.compile(JSON.stringify(input)));
// `output` here contains the JSON output as specified in the documentation
for (var contractName in output.contracts['test.sol']) {
    fs.writeFileSync('./build/'+contractName+'_abi.js',JSON.stringify(output.contracts['test.sol'][contractName].abi))
    fs.writeFileSync('./build/'+contractName+'_bytecode.js',output.contracts['test.sol'][contractName].evm.bytecode.object)
}

4.Migration.js 用于合约部署


const fs = require("fs");
const {ethers} = require("ethers");


const LoanABI=JSON.parse(fs.readFileSync("./build/Loan_abi.js").toString())
const LoanByteCode=fs.readFileSync("./build/Loan_bytecode.js").toString()


// 合约拥有者的帐号
let url = 'http://localhost:8545'; //此处填写jrpc地址和端口
let customHttpProvider = new ethers.providers.JsonRpcProvider(url);
const privateKey = ""; //部署合约的钱包地址的私钥,metamask中选项->账户详情->导出私钥

let walletWithProvider = new ethers.Wallet(privateKey, customHttpProvider);
(async function() {

    // 常见合约工厂实例
    let factory = new ethers.ContractFactory(LoanABI, LoanByteCode, walletWithProvider);

    // 请注意,我们将 "Hello World" 作为参数传递给合约构造函数constructor
    let contract = await factory.deploy();

    // 部署交易有一旦挖出,合约地址就可用
    // 参考: https://ropsten.etherscan.io/address/0x2bd9aaa2953f988153c8629926d22a6a5f69b14e
    fs.writeFileSync('./build/contractAddress.js',contract.address)
    console.log(contract.address);
    // "0x2bD9aAa2953F988153c8629926D22A6a5F69b14E"

    // 发送到网络用来部署合约的交易
    // 查看: https://ropsten.etherscan.io/tx/0x159b76843662a15bd67e482dcfbee55e8e44efad26c5a614245e12a00d4b1a51
    console.log(contract.deployTransaction.hash);
    // "0x159b76843662a15bd67e482dcfbee55e8e44efad26c5a614245e12a00d4b1a51"

    //合约还没有部署;我们必须等到它被挖出
    await contract.deployed()

    // 好了 合约已部署。
})();

二、remix本地部署

        最近remix的网页端炸了,可以去github上下载源码在本地部署

        node版本建议使用 v16.14.0 ,之前用node14和node17下载依赖时都出错了

        GitHub - ethereum/remix-project: Remix is a browser-based compiler and IDE that enables users to build Ethereum contracts with Solidity language and to debug transactions.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值