如何用web3部署智能合约

合约示例

pragma solidity ^0.4.18;

contract CallMeChallenge {
    bool public isComplete = false;

    function callme() public {
        isComplete = true;
    }
}

可以用solc编译合约,也可以用Remix编译。

如果用solc编译的话,需要下载同合约使用的solidity版本相同的solc版本。

npm install [email protected]

Remix编译后直接复制bytecode到代码里即可。

web3.js部署合约

环境:nodejs

npm install web3@^0.20.1
npm install ethereumjs-tx@^1.3.7
var Web3 = require('web3');
var Tx = require('ethereumjs-tx');
var solc = require('solc');
var fs = require('fs');

//init
if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
} else {
    web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/1b8...b0"));
}

var fromAddr = '0x97...5B7';
var count = web3.eth.getTransactionCount(fromAddr);
var gasPrice = web3.eth.gasPrice;
var gasLimit = 3000000;
var privateKey = new Buffer.from('bb07...ed3c', 'hex');

//编译合约,获得bytecode
var source = fs.readFileSync("./test.sol", "utf8");
var compilied = solc.compile(source, 1);
var bytecode = compilied.contracts[':CallMeChallenge'].bytecode;

//要打包的交易信息
var rawTx = {
    'from': fromAddr,
    'nonce': web3.toHex(count),
    'gasPrice': web3.toHex(gasPrice),
    'gasLimit': web3.toHex(gasLimit),
    'value': '0x0',
    'data': '0x'+bytecode
};

var tx = new Tx(rawTx);
tx.sign(privateKey);
var serializedTx = tx.serialize();

var hashTx = web3.eth.sendRawTransaction('0x'+serializedTx.toString('hex'));
console.log('txHash: ' + hashTx);
var makeTx;
while (true) {
    makeTx = web3.eth.getTransaction(hashTx);

    if (makeTx["blockNumber"] !== null) {
        var receipt = web3.eth.getTransactionReceipt(hashTx);
        console.log("address: " + receipt["contractAddress"]);
        break;
    }
}

在运行同一个脚本的时候遇到玄学问题,Error: Invalid JSON RPC response: undefined报出这个错误,找了很久才发现是网络问题,加载infura有时候需要挂个代理来访问,如果你也遇到这个问题,就挂个代理跑跑。

web3.py部署合约

环境:python3.6

# -*- coding:utf-8 -*-

from web3 import Web3, HTTPProvider

true = True
false = False

web3 = Web3(HTTPProvider('https://ropsten.infura.io/v3/1b...b0'))

fromAddr = '0x97D7...5B7'
privateKey = '0xbb...ed3c'
nonce = web3.eth.getTransactionCount(fromAddr)
gasPrice = web3.eth.gasPrice
rawTx = {
    'from': fromAddr,
    'nonce': nonce,
    'gasPrice': gasPrice,
    'gas': 300000,
    'value': web3.toWei(0, 'ether'),
    'data': '0x606060405260008060006101000a81548160ff021916908315150217905550341561002957600080fd5b60e4806100376000396000f3006060604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063a3c8e39314604e578063b2fa1c9e146060575b600080fd5b3415605857600080fd5b605e608a565b005b3415606a57600080fd5b607060a6565b604051808215151515815260200191505060405180910390f35b60016000806101000a81548160ff021916908315150217905550565b6000809054906101000a900460ff16815600a165627a7a723058208fd18624eaaac9c24521a084590bb1b536e9a94f23086c49864b9c02300ff0c20029'
}

def deploy(rawTx):
    signedTx = web3.eth.account.signTransaction(rawTx, private_key=privateKey)
    hashTx = web3.eth.sendRawTransaction(signedTx.rawTransaction).hex()
    receipt = web3.eth.waitForTransactionReceipt(hashTx)

    return receipt

if __name__ == '__main__':
    receipt = deploy(rawTx)
    print('address: ' + receipt['contractAddress'])

后话

这里用JavaScript和Python两个web3版本来实现自动部署合约,免去了在Remix上手动部署,另外还有Java版的web3等等。

你可能感兴趣的:(如何用web3部署智能合约)