Last active
April 9, 2019 12:18
-
-
Save leberknecht/d914501e37ec4dac904fb3cf6bd76951 to your computer and use it in GitHub Desktop.
Sign & Deploy contract via sendRawTransaction with web3 0.20.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
pragma solidity >=0.4.25; | |
contract MyContract { | |
string someVariable; | |
constructor(string memory _someVariable) public { | |
someVariable = _someVariable; | |
} | |
function getSomeVariable() public view returns(string memory) { | |
return someVariable; | |
} | |
} | |
**/ | |
const Web3 = require('web3') | |
const Tx = require('ethereumjs-tx') | |
const Sleep = require('sleep') | |
let web3 = new Web3(new Web3.providers.HttpProvider('https://kovan.infura.io/v3/a376...')) | |
let privateKey = '49D8743ACA595EAF28963D38D42...' | |
let deployAddress = '0x668A2abb760aa8965...' | |
let contract = new web3.eth.contract([ { "constant": true, "inputs": [], "name": "getSomeVariable", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "inputs": [ { "name": "_someVariable", "type": "string" } ], "payable": false, "stateMutability": "nonpayable", "type": "constructor" } ]); | |
let byteData = contract.new.getData("whatever", {data: '0x608060405234801561001057600080fd5b506040516102f53803806102f58339810180604052602081101561003357600080fd5b81019080805164010000000081111561004b57600080fd5b8281019050602081018481111561006157600080fd5b815185600182028301116401000000008211171561007e57600080fd5b5050929190505050806000908051906020019061009c9291906100a3565b5050610148565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100e457805160ff1916838001178555610112565b82800160010185558215610112579182015b828111156101115782518255916020019190600101906100f6565b5b50905061011f9190610123565b5090565b61014591905b80821115610141576000816000905550600101610129565b5090565b90565b61019e806101576000396000f3fe60806040526004361061003b576000357c0100000000000000000000000000000000000000000000000000000000900480633d044f2e14610040575b600080fd5b34801561004c57600080fd5b506100556100d0565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561009557808201518184015260208101905061007a565b50505050905090810190601f1680156100c25780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101685780601f1061013d57610100808354040283529160200191610168565b820191906000526020600020905b81548152906001019060200180831161014b57829003601f168201915b505050505090509056fea165627a7a72305820be48bd1e365e43c8b31c16f4fb9dbfdc8e223961f1eceaa7b00cd39d56f07b4a0029'}) | |
let nonce = web3.eth.getTransactionCount(deployAddress, 'pending') | |
let balance = web3.eth.getBalance(deployAddress).toString() | |
let gasPrice = web3.eth.gasPrice.toString() | |
console.log("nonce: ", nonce) | |
console.log('balance: ', balance) | |
console.log('gasPrice: ', gasPrice) | |
let rawTx = { | |
nonce: web3.toHex(nonce), | |
to: null, | |
data: byteData, | |
gasPrice: web3.toHex(web3.toWei('2', 'gwei')), | |
gas: web3.toHex((320000)), | |
value: '0x00' | |
} | |
privateKey = new Buffer.from(privateKey, 'hex') | |
let transaction = new Tx(rawTx) | |
transaction.sign(privateKey) | |
let deployBytecode = '0x' + transaction.serialize().toString('hex') | |
let transactionHash = web3.eth.sendRawTransaction(deployBytecode) | |
console.log('transaction: ', transactionHash) | |
let receipt | |
while(null === (receipt = web3.eth.getTransactionReceipt(transactionHash))) { | |
console.log('waiting for receipt...') | |
Sleep.sleep(1) | |
} | |
console.log("contract address: ", receipt.contractAddress) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment