-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
71 lines (56 loc) · 2.37 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
const {
Client,
AccountId,
PrivateKey,
ContractCreateFlow,
TokenCreateTransaction,
ContractUpdateTransaction,
Hbar,
KeyList } = require('@hashgraph/sdk');
const fs = require('fs');
// External helper function
const { accountCreator } = require("./utils");
require('dotenv').config({path: __dirname + '/../../.env'});
const operatorId = AccountId.fromString(process.env.ACCOUNT_ID);
const operatorKey = PrivateKey.fromString(process.env.PRIVATE_KEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
const main = async () => {
// Admin account creation
const adminKey = PrivateKey.generateED25519();
const adminId = await accountCreator(adminKey, 50, client);
const bytecode = fs.readFileSync("./TreasuryContract_sol_TreasuryContract.bin");
// Switch operator to admin to sign transactions (tmp)
client.setOperator(adminId, adminKey);
// Create contract with adminKey
const contractCreate = new ContractCreateFlow()
.setGas(100000)
.setBytecode(bytecode)
.setAdminKey(adminKey);
const contractCreateTx = await contractCreate.execute(client);
const contractCreateRx = await contractCreateTx.getReceipt(client);
const contractId = contractCreateRx.contractId;
console.log("Contract created with ID: " + contractId);
// Create fungible token using contract as treasury
const tokenCreate = new TokenCreateTransaction()
.setTokenName("USD Bar")
.setTokenSymbol("USDB")
.setTreasuryAccountId(AccountId.fromString(contractId))
.setInitialSupply(10000)
.setDecimals(2)
.setAutoRenewAccountId(contractId)
.setAutoRenewPeriod(7000000)
.setMaxTransactionFee(new Hbar(30))
.freezeWith(client);
const signedTokenCreate = await tokenCreate.sign(adminKey);
const tokenCreateTx = await signedTokenCreate.execute(client);
const tokenCreateRx = await tokenCreateTx.getReceipt(client);
const tokenId = tokenCreateRx.tokenId;
console.log("Token created with ID: " + tokenId);
const contractUpdate = await new ContractUpdateTransaction()
.setContractId(contractId)
.setAdminKey(new KeyList())
.execute(client);
const contractUpdateRx = await contractUpdate.getReceipt(client);
console.log("Contract update status: " + contractUpdateRx.status.toString());
}
main();