This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploymentUtils.js
106 lines (94 loc) · 2.69 KB
/
deploymentUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const Web3Utils = require('web3-utils')
const Tx = require('ethereumjs-tx')
const fetch = require('node-fetch');
let {
DEPLOYMENT_ACCOUNT_ADDRESS,
DEPLOYMENT_ACCOUNT_PRIVATE_KEY,
RPC_URL,
GET_RECEIPT_INTERVAL_IN_MILLISECONDS,
GAS_PRICE
} = process.env;
GAS_PRICE = Web3Utils.toWei(GAS_PRICE, 'gwei')
const deploymentPrivateKey = Buffer.from(DEPLOYMENT_ACCOUNT_PRIVATE_KEY, 'hex')
async function deployContract({
from, nonce, web3, url, gasPrice, gasLimit, abi, bytecode, args, privateKey
}) {
let options = {
from: from || DEPLOYMENT_ACCOUNT_ADDRESS,
gasPrice: gasPrice || GAS_PRICE,
};
let instance = new web3.eth.Contract(abi, options);
const rawBytecode = await instance.deploy({
data: bytecode,
arguments: args
}).encodeABI()
console.log('estimating gas...')
const gas = await instance.deploy({
data: bytecode,
arguments: args
}).estimateGas()
console.log('gasLimit = ', gas)
console.log('sending tx...')
const tx = await sendRawTx({
data: rawBytecode,
nonce: Web3Utils.toHex(nonce),
to: null,
gas,
privateKey: privateKey || deploymentPrivateKey,
url: url || RPC_URL,
gasPrice: gasPrice || GAS_PRICE
})
if(tx.status !== '0x1'){
throw new Error('Tx failed');
}
instance.options.address = tx.contractAddress;
instance.deployedBlockNumber = tx.blockNumber
return instance;
}
async function sendRawTx({data,gas, nonce, to, privateKey, url, gasPrice}) {
var rawTx = {
nonce,
gasPrice: Web3Utils.toHex(gasPrice),
gasLimit: Web3Utils.toHex(gas),
to,
data
}
var tx = new Tx(rawTx);
tx.sign(privateKey);
var serializedTx = tx.serialize();
const txHash = await sendNodeRequest(url, "eth_sendRawTransaction", '0x' + serializedTx.toString('hex'));
console.log('txHash', txHash)
const receipt = await getReceipt(txHash, url);
return receipt
}
async function sendNodeRequest(url, method, signedData){
const request = await fetch(url, {
headers: {
'Content-type': 'application/json'
},
method: 'POST',
body: JSON.stringify({
jsonrpc: "2.0",
method,
params: [signedData],
id: 1
})
});
const json = await request.json()
return json.result;
}
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
let count = 0;
async function getReceipt(txHash, url) {
count += Number(GET_RECEIPT_INTERVAL_IN_MILLISECONDS)/1000
console.log(`pending ${count} seconds...`);
await timeout(GET_RECEIPT_INTERVAL_IN_MILLISECONDS);
let receipt = await sendNodeRequest(url, "eth_getTransactionReceipt", txHash);
if(receipt === null) {
receipt = await getReceipt(txHash, url);
}
return receipt;
}
module.exports = deployContract