forked from Dapp-Learning-DAO/Dapp-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (69 loc) · 2.13 KB
/
index.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
let Web3 = require('web3');
let solc = require('solc');
let fs = require('fs');
// Get privatekey from environment
require('dotenv').config();
const privatekey = process.env.PRIVATE_KEY;
// Load contract
const source = fs.readFileSync('Incrementer.sol', 'utf8');
// compile solidity
const input = {
language: 'Solidity',
sources: {
'Incrementer.sol': {
content: source,
},
},
settings: {
outputSelection: {
'*': {
'*': ['*'],
},
},
},
};
const tempFile = JSON.parse(solc.compile(JSON.stringify(input)));
const contractFile = tempFile.contracts['Incrementer.sol']['Incrementer'];
// Get bin & abi
const bytecode = contractFile.evm.bytecode.object;
const abi = contractFile.abi;
// Create web3 with kovan provider,you can change kovan to other testnet
const web3 = new Web3('https://kovan.infura.io/v3/' + process.env.INFURA_ID);
// Create account from privatekey
const account = web3.eth.accounts.privateKeyToAccount(privatekey);
const account_from = {
privateKey: privatekey,
accountAddress: account.address,
};
/*
-- Deploy Contract --
*/
const Deploy = async () => {
// Create contract instance
const deployContract = new web3.eth.Contract(abi);
// Create Tx
const deployTx = deployContract.deploy({
data: bytecode,
arguments: [0], // Pass arguments to the contract constructor on deployment(_initialNumber in Incremental.sol)
});
// Sign Tx
const deployTransaction = await web3.eth.accounts.signTransaction(
{
data: deployTx.encodeABI(),
gas: 8000000,
},
account_from.privateKey
);
const deployReceipt = await web3.eth.sendSignedTransaction(deployTransaction.rawTransaction);
// Your deployed contrac can be viewed at: https://kovan.etherscan.io/address/${deployReceipt.contractAddress}
// You can change kovan in above url to your selected testnet.
console.log(`Contract deployed at address: ${deployReceipt.contractAddress}`);
};
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
Deploy()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});