-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.js
74 lines (65 loc) · 2.15 KB
/
deploy.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
/* eslint-disable import/first */
import Web3 from 'web3';
const createWeb3 = (Web3, numberOfAccounts = 10) => {
const web3 = new Web3();
const Ganache = require('ganache-core');
const ganacheOptions = {
accounts: (new Array(numberOfAccounts)).fill({balance: web3.utils.toWei('90000000')})
};
web3.setProvider(Ganache.provider(ganacheOptions));
return web3;
};
let web3;
let account;
let from;
let usingInfura = false;
const deployContract = async (json, constructorArguments, name) => {
console.log(`Deploying ${name}...`);
const deployMethod = await new web3.eth.Contract(json.abi)
.deploy({data: json.bytecode, arguments: constructorArguments});
if (!usingInfura) {
const contract = await deployMethod.send({from, gas: 6000000});
console.log(`Deployed ${name} at ${contract.options.address}`);
return contract;
}
const tx = {
from,
gas: 5000000,
gasPrice: 600000000,
data: deployMethod.encodeABI()
};
const signedTx = await account.signTransaction(tx);
const {contractAddress} = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
console.log(`Deployed ${name} at ${contractAddress}`);
return await new web3.eth.Contract(json.abi, contractAddress);
};
const sendTransaction = async (method, name, to, value = 0) => {
console.log(`Sending transaction: ${name}...`);
if (!usingInfura) {
return await method.send({from, gas: 6000000});
}
const tx = {
from,
to,
gas: 3000000,
gasPrice: 600000000,
data: method.encodeABI(),
value
};
const signedTx = await account.signTransaction(tx);
return await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
};
(async () => {
if (process.env.PRIVATE_KEY) {
usingInfura = true;
web3 = new Web3(new Web3.providers.HttpProvider('https://kovan.infura.io/YjJOPQ1J3Iw1QPeH3xRS'));
account = web3.eth.accounts.privateKeyToAccount(process.env.PRIVATE_KEY);
from = account.address;
console.log(`Using infura with account: ${from}`);
} else {
web3 = createWeb3(Web3);
[from] = await web3.eth.getAccounts();
console.log('Using ganache');
}
})().catch(console.error);
/* eslint-enable import/first */