-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path8_MintTo.js
188 lines (151 loc) · 5.7 KB
/
8_MintTo.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const fs = require("fs");
const {
TokenCreateTransaction,
FileCreateTransaction,
FileAppendTransaction,
AccountId,
PrivateKey,
ContractCreateTransaction,
TokenType,
TokenSupplyType,
Hbar,
Client,
ContractId,
AccountCreateTransaction,
KeyList,
ContractUpdateTransaction,
ContractInfoQuery,
ContractExecuteTransaction,
ContractFunctionParameters,
AccountBalanceQuery,
TokenUpdateTransaction,
TokenInfoQuery,
TokenAssociateTransaction,
} = require("@hashgraph/sdk");
const dotenv = require("dotenv");
dotenv.config();
async function main() {
const operatorKey = PrivateKey.fromString(process.env.OPERATOR_PVKEY);
let client = Client.forTestnet().setOperator(
AccountId.fromString(process.env.OPERATOR_ID),
operatorKey
);
console.log(`\nSTEP 0 - Create accounts`);
const adminKey = PrivateKey.generateED25519();
const aliceKey = PrivateKey.generateED25519();
let createAccountTx = await new AccountCreateTransaction()
.setKey(adminKey.publicKey)
.setInitialBalance(10)
.execute(client);
let createAccountRx = await createAccountTx.getReceipt(client);
const adminAccount = createAccountRx.accountId;
console.log(`- Admin account is ${adminAccount.toString()}`);
createAccountTx = await new AccountCreateTransaction()
.setKey(aliceKey.publicKey)
.setMaxAutomaticTokenAssociations(1)
.setInitialBalance(10)
.execute(client);
createAccountRx = await createAccountTx.getReceipt(client);
const aliceAccount = createAccountRx.accountId;
console.log(`- Alice account is ${aliceAccount.toString()}`);
// switch client to admin
client.setOperator(adminAccount, adminKey);
console.log(`\nSTEP 1 - Create file`);
const bytecode = fs.readFileSync("./MintTo_sol_MintTo.bin");
//Create a file on Hedera and store the hex-encoded bytecode
const fileCreateTx = await new FileCreateTransaction().setKeys([adminKey]).execute(client);
const fileCreateRx = await fileCreateTx.getReceipt(client);
const bytecodeFileId = fileCreateRx.fileId;
console.log(`- The smart contract bytecode file ID is: ${bytecodeFileId}`);
// Append contents to the file
const fileAppendTx = await new FileAppendTransaction()
.setFileId(bytecodeFileId)
.setContents(bytecode)
.setMaxChunks(10)
.setMaxTransactionFee(new Hbar(2))
.execute(client);
await fileAppendTx.getReceipt(client);
console.log(`- Content added`);
console.log(`\nSTEP 2 - Create contract`);
const contractCreateTx = await new ContractCreateTransaction()
.setAdminKey(adminKey)
.setBytecodeFileId(bytecodeFileId)
.setGas(100000)
.execute(client);
const contractCreateRx = await contractCreateTx.getReceipt(client);
const contractId = contractCreateRx.contractId.toString();
console.log(`- Contract created ${contractId}`);
console.log(`\nSTEP 3 - Create token`);
const tokenCreateTx = await new TokenCreateTransaction()
.setTokenName("test")
.setTokenSymbol("tst")
.setDecimals(0)
.setInitialSupply(0)
.setTokenType(TokenType.FungibleCommon)
.setSupplyType(TokenSupplyType.Infinite)
// create the token with the contract as supply and treasury
// .setSupplyKey(ContractId.fromString(contractId))
// .setTreasuryAccountId(contractId)
// create the token with a key and account for treasury
.setSupplyKey(adminKey)
.setTreasuryAccountId(adminAccount)
.setAdminKey(adminKey)
.execute(client);
const tokenCreateRx = await tokenCreateTx.getReceipt(client);
const tokenId = tokenCreateRx.tokenId;
console.log(`- Token created ${tokenId}`);
// associate contract to token
const associateTx = await new TokenAssociateTransaction()
.setTokenIds([tokenId])
.setAccountId(AccountId.fromString(contractId))
.execute(client);
await associateTx.getReceipt(client);
// only update the token if it was setup with a key and account
console.log(`\nSTEP 3.5 - Update the token`);
const tokenUpdateTx = await new TokenUpdateTransaction()
.setTokenId(tokenId)
.setSupplyKey(ContractId.fromString(contractId))
.setTreasuryAccountId(contractId)
.execute(client);
const tokenUpdateRx = await tokenUpdateTx.getReceipt(client);
console.log(`- Token updated`);
console.log(`\nSTEP 3.6 - Token Query`);
const tokenInfo = await new TokenInfoQuery().setTokenId(tokenId).execute(client);
console.log(`${tokenInfo.adminKey}`);
console.log(`${tokenInfo.treasuryAccountId}`);
console.log(`\nSTEP 4 - Update Contract`);
const contractUpdateTx = await new ContractUpdateTransaction()
.setContractId(contractId)
.setAdminKey(new KeyList())
.execute(client);
await contractUpdateTx.getReceipt(client);
console.log(`\nSTEP 4 - Query Contract`);
const contractInfo = await new ContractInfoQuery().setContractId(contractId).execute(client);
console.log(`- contract admin key is now ${contractInfo.adminKey}`);
console.log(`\n STEP 4 - call the contract to set the token id`);
let contractFunctionParameters = new ContractFunctionParameters().addAddress(
tokenId.toSolidityAddress()
);
const contractTokenTx = await new ContractExecuteTransaction()
.setContractId(contractId)
.setFunction("setToken", contractFunctionParameters)
.setGas(500000)
.execute(client);
await contractTokenTx.getReceipt(client);
console.log(`\n STEP 5 - minting 1 to Alice`);
contractFunctionParameters = new ContractFunctionParameters().addAddress(
aliceAccount.toSolidityAddress()
);
// switch client to alice
client.setOperator(aliceAccount, aliceKey);
const contractMintTx = await new ContractExecuteTransaction()
.setContractId(contractId)
.setFunction("mintTo", contractFunctionParameters)
.setGas(500000)
.execute(client);
await contractMintTx.getReceipt(client);
console.log(`\n STEP 6 - Alice Balance`);
const aliceBalance = await new AccountBalanceQuery().setAccountId(aliceAccount).execute(client);
console.log(aliceBalance);
}
main();