forked from Dapp-Learning-DAO/Dapp-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (89 loc) · 2.38 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const Web3 = require('web3');
const fs = require('fs');
const contractFile = require('./compile');
require('dotenv').config();
const privatekey = process.env.PRIVATE_KEY;
/*
-- Define Provider & Variables --
*/
const receiver = '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266';
// Provider
const web3 = new Web3(
new Web3.providers.HttpProvider(
'https://goerli.infura.io/v3/' + process.env.INFURA_ID
)
);
//account
const account = web3.eth.accounts.privateKeyToAccount(privatekey);
const account_from = {
privateKey: account.privateKey,
accountaddress: account.address,
};
// sol ---> abi + bin
const bytecode = contractFile.evm.bytecode.object;
const abi = contractFile.abi;
/*
-- Deploy Contract --
*/
const Trans = async () => {
console.log(
`Attempting to deploy from account ${account_from.accountaddress}`
);
web3.eth.getBlockNumber(function (error, result) {
console.log(result);
});
// Create deploy Contract Instance
const deployContract = new web3.eth.Contract(abi);
// method 1
// Create Constructor Tx
const deployTx = deployContract.deploy({
data: bytecode,
arguments: ['DAPPLEARNING', 'DAPP', 0, 10000000],
});
// Sign Transacation and Send
const deployTransaction = await web3.eth.accounts.signTransaction(
{
data: deployTx.encodeABI(),
gas: '8000000',
},
account_from.privateKey
);
// Send Tx and Wait for Receipt
const deployReceipt = await web3.eth.sendSignedTransaction(
deployTransaction.rawTransaction
);
console.log(`Contract deployed at address: ${deployReceipt.contractAddress}`);
const erc20Contract = new web3.eth.Contract(
abi,
deployReceipt.contractAddress
);
//build the Tx
const transferTx = erc20Contract.methods
.transfer(receiver, 100000)
.encodeABI();
// Sign Tx with PK
const transferTransaction = await web3.eth.accounts.signTransaction(
{
to: deployReceipt.contractAddress,
data: transferTx,
gas: 8000000,
},
account_from.privateKey
);
// Send Tx and Wait for Receipt
await web3.eth.sendSignedTransaction(
transferTransaction.rawTransaction
);
await erc20Contract.methods
.balanceOf(receiver)
.call()
.then((result) => {
console.log(`The balance of receiver is ${result}`);
});
};
Trans()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});