How to deploy smart contract with web3

Examples of contract

pragma solidity ^0.4.18;

contract CallMeChallenge {
    bool public isComplete = false;

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

Solc can be used to compile a contract can be compiled with Remix.

If solc compile it, you need to download the same version of the same solidity contracts solc version.

npm install [email protected]

Remix compiled bytecode directly copied into the code can be.

web3.js deployment contract

Environment: 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;
    }
}

Experience in running the same script when the metaphysical problem, Error: Invalid JSON RPC response: undefinedreported this error, looking for a long time only to find that a network problem, load infura sometimes need to hang a proxy to access, if you have encountered this problem, hang a proxy to run run.

web3.py deployment contract

Environment: 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'])

After words

Here with JavaScript and Python web3 two versions of the contract to automate the deployment, eliminating the need for manually deployed Remix, in addition to the Java version of web3 and so on.

Guess you like

Origin www.cnblogs.com/KRDecad3/p/11914985.html