-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_creation.js
106 lines (95 loc) · 3.4 KB
/
token_creation.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
console.clear();
const {
Client,
PrivateKey,
TokenCreateTransaction,
TokenType,
TokenSupplyType,
TokenInfoQuery,
AccountBalanceQuery,
TokenMintTransaction,
TokenBurnTransaction,
} = require("@hashgraph/sdk");
require("dotenv").config();
//Grab your Hedera testnet account ID and private key from your .env file
const myAccountId = process.env.MY_ACCOUNT_ID;
const myPrivateKey = PrivateKey.fromStringDer(process.env.MY_PRIVATE_KEY);
// If we weren't able to grab it, we should throw a new error
if (!myAccountId || !myPrivateKey) {
throw new Error(
"Environment variables MY_ACCOUNT_ID and MY_PRIVATE_KEY must be present"
);
}
//Create your Hedera Testnet client
const client = Client.forTestnet();
//Set your account as the client's operator
client.setOperator(myAccountId, myPrivateKey);
async function createToken() {
console.log("CreateToken---------------------");
let tokenCreateTx = await new TokenCreateTransaction()
.setTokenName("MyToken")
.setTokenSymbol("MYT")
.setTokenType(TokenType.FungibleCommon)
.setDecimals(2)
.setInitialSupply(10000)
.setTreasuryAccountId(myAccountId)
.setSupplyType(TokenSupplyType.Infinite)
.setSupplyKey(myPrivateKey)
.setFreezeKey(myPrivateKey)
.setPauseKey(myPrivateKey)
.setAdminKey(myPrivateKey)
.setWipeKey(myPrivateKey)
//.setKycKey(myPrivateKey)
.freezeWith(client);
let tokenCreateSign = await tokenCreateTx.sign(myPrivateKey);
let tokenCreateSubmit = await tokenCreateSign.execute(client);
let tokenCreateRx = await tokenCreateSubmit.getReceipt(client);
let tokenId = tokenCreateRx.tokenId;
console.log(`- Created token with ID: ${tokenId}`);
console.log("-----------------------------------");
return tokenId;
}
async function queryTokenInfo(tokenId) {
console.log("QueryTokenInfo---------------------");
const query = new TokenInfoQuery().setTokenId(tokenId);
const tokenInfo = await query.execute(client);
console.log(JSON.stringify(tokenInfo, null, 4));
console.log("-----------------------------------");
}
async function queryAccountBalance(accountId) {
console.log("QueryAccountBalance----------------");
const balanceQuery = new AccountBalanceQuery().setAccountId(accountId);
const accountBalance = await balanceQuery.execute(client);
console.log(JSON.stringify(accountBalance, null, 4));
console.log("-----------------------------------");
}
async function mintToken(tokenId, amount) {
console.log("MintToken--------------------------");
const txResponse = await new TokenMintTransaction()
.setTokenId(tokenId)
.setAmount(amount)
.execute(client);
const receipt = await txResponse.getReceipt(client);
console.log("Minted token: ", receipt);
console.log("-----------------------------------");
}
async function burnToken(tokenId, amount) {
console.log("BurnToken--------------------------");
const txResponse = await new TokenBurnTransaction()
.setTokenId(tokenId)
.setAmount(amount)
.execute(client);
const receipt = await txResponse.getReceipt(client);
console.log("Burned token: ", receipt);
console.log("-----------------------------------");
}
async function main() {
const tokenId = await createToken();
// await queryTokenInfo(tokenId);
await queryAccountBalance(myAccountId);
await mintToken(tokenId, 1000);
await queryAccountBalance(myAccountId);
await burnToken(tokenId, 500);
await queryAccountBalance(myAccountId);
}
main();