web3スマート契約を展開する方法

契約の例

pragma solidity ^0.4.18;

contract CallMeChallenge {
    bool public isComplete = false;

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

Solcがリミックスしてコンパイルすることができます契約をコンパイルするために使用することができます。

solcコンパイルそれならば、あなたは同じ堅契約solcバージョンの同じバージョンをダウンロードする必要があります。

npm install [email protected]

直接コードにコピーされたコンパイル済みのバイトコードをすることができリミックス。

展開の契約を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などのJavaバージョンに加えて、手動で展開リミックスの必要性を排除し、展開を自動化するために契約の2つのバージョンをweb3。

おすすめ

転載: www.cnblogs.com/KRDecad3/p/11914985.html