-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.js
52 lines (41 loc) · 1.43 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
const fs = require('fs');
const solc = require('solc');
const { Web3 } = require('web3');
const HDWalletProvider = require('@truffle/hdwallet-provider');
const privateKey = '<CHANGE IT WITH YOUR PRIVATE KEY>'; // Ganti dengan kunci privat Anda
const providerOrUrl = 'https://goerli.infura.io/v3/d135d94519b342329bff2a4e334306bf';
const provider = new HDWalletProvider(privateKey, providerOrUrl);
const web3 = new Web3(provider);
const content = fs.readFileSync('./MyContract.sol', 'utf8');
const input = {
language: 'Solidity',
sources: {
'MyContract.sol': { content }
},
settings: {
outputSelection: { '*': { '*': ['*'] } }
}
};
async function deploy() {
try {
/* 1. Dapatkan Akun Ethereum */
const [account] = await web3.eth.getAccounts();
/* 2. Compile Smart Contract */
const { contracts } = JSON.parse(
solc.compile(JSON.stringify(input))
);
const contract = contracts['MyContract.sol'].MyContract;
/* 3. Ekstrak ABI dan Bytecode dari Kontrak */
const abi = contract.abi;
const bytecode = contract.evm.bytecode.object;
/* 4. Kirim Smart Contract ke Blockchain */
const { _address } = await new web3.eth.Contract(abi)
.deploy({ data: bytecode })
.send({ from: account, gas: 1000000 });
console.log("Alamat Kontrak =>", _address);
console.log("This code created by Ufik");
} catch (error) {
console.error("Error during deployment:", error);
}
}
deploy();