Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tests contract deployment via web3 using manually signed transactions
const Tx = require('ethereumjs-tx');
const Web3 = require('web3')
const web3 = new Web3(new Web3.providers.HttpProvider(url));
function getCodeFromTxHash(hash, source) {
web3.eth.getTransactionReceipt(hash, function (err, receipt) {
if (err) {
console.error(source, "ERROR on getting tx receipt from hash", err);
}
if (receipt && receipt.blockHash) {
if (!receipt.contractAddress) {
console.error(source, 'ContractAddress is null (returning)')
return
}
web3.eth.getCode(receipt.contractAddress, function (err, code) {
if (err) {
console.error(source, "ERROR getting code", err)
}
if (!code) {
console.log(source, "Got no code in", receipt.contractAddress)
return;
}
if (code.length > 3) {
console.log(source, "Got code: ", code.substr(0, 10))
} else {
console.log(source, "Got empty code: ", code)
}
})
} else {
console.log(source, "Got no receipt (returning)")
return
}
});
}
function getNonce() {
return new Promise((resolve, reject) => {
web3.eth.getTransactionCount(from, 'pending', function(err, data) {
if (err) reject(err)
else resolve(data)
})
})
}
async function main() {
const nonce = await getNonce();
const rawTx = {
nonce: '0x' + nonce.toString(16),
gasPrice: '0x' + (6e9).toString(16),
gasLimit: '0x' + (3000000).toString(16),
to: null, //'0x0000000000000000000000000000000000000000',
value: '0x00',
data
}
const tx = new Tx(rawTx)
tx.sign(privateKey)
const serializedTx = '0x' + tx.serialize().toString('hex');
console.log('Sending deployment tx', serializedTx.substr(0, 20))
web3.eth.sendRawTransaction(serializedTx, function(err, txHash) {
if (err) {
console.error("ERROR sending tx", err)
return
}
console.log('Deployment tx sent')
// Install filter and check contract code
web3.eth.filter('latest', function(err) {
if (err) {
console.error("ERROR on installing filter: ", err);
return;
}
getCodeFromTxHash(txHash, '[filter]')
});
// Try to get code on a loop as well
setInterval(function() {
getCodeFromTxHash(txHash, '[loop]')
}, 1000)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.