forked from hedera-dev/hedera-code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-and-mint.js
61 lines (51 loc) · 2.08 KB
/
create-and-mint.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
import {
Client,
AccountId,
PrivateKey,
TokenCreateTransaction,
AccountBalanceQuery,
} from '@hashgraph/sdk';
import dotenv from 'dotenv';
dotenv.config();
// ensure required environment variables are available
if (!process.env.OPERATOR_ID || !process.env.OPERATOR_KEY) {
throw new Error('Must set OPERATOR_ID and OPERATOR_KEY in .env');
}
// configure client using environment variables
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromString(process.env.OPERATOR_KEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
// entry point for execution of this example (called at the bottom of the file)
async function main() {
console.log('Operator Account ID', operatorId.toString());
// create fungible token
const ftCreateTx = await new TokenCreateTransaction()
.setTokenName("snippetft")
.setTokenSymbol("SNIPPET")
.setDecimals(2)
.setInitialSupply(1_000_000)
.setTreasuryAccountId(operatorId)
.setAdminKey(operatorKey)
.setFreezeDefault(false)
.freezeWith(client);
const ftCreateTxSigned = await ftCreateTx.sign(operatorKey);
const ftCreateTxSubmitted = await ftCreateTxSigned.execute(client);
const ftCreateTxRecord = await ftCreateTxSubmitted.getRecord(client);
console.log('ftCreateTxRecord', transactionHashscanUrl(ftCreateTxRecord));
// identify assigned token id from transaction receipt
const ftId = ftCreateTxRecord.receipt.tokenId;
console.log('ftId', ftId.toString());
// query the operators token balance
const balanceQuery = new AccountBalanceQuery()
.setAccountId(operatorId);
const balanceQueryResponse = await balanceQuery.execute(client);
const operatorFtBalance = balanceQueryResponse.tokens.get(ftId);
console.log('operatorFtBalance', operatorFtBalance);
// force exit
process.exit(0);
}
function transactionHashscanUrl(txRecord) {
const txId = txRecord.transactionId.toString();
return `https://hashscan.io/testnet/transaction/${txId}`;
}
main();