forked from hiero-ledger/hiero-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-fungible-token-metadata-with-admin-key.js
104 lines (88 loc) · 3.2 KB
/
update-fungible-token-metadata-with-admin-key.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
import {
TokenCreateTransaction,
TokenInfoQuery,
TokenType,
PrivateKey,
Client,
AccountId,
TokenUpdateTransaction,
} from "@hashgraph/sdk";
import dotenv from "dotenv";
dotenv.config();
/**
* @summary E2E-HIP-646 https://hips.hedera.com/hip/hip-646
* @description Update fungible token metadata with admin key
*/
async function main() {
if (
!process.env.OPERATOR_KEY ||
!process.env.OPERATOR_ID ||
!process.env.HEDERA_NETWORK
) {
throw new Error("Please set required keys in .env file.");
}
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromStringDer(process.env.OPERATOR_KEY);
const network = process.env.HEDERA_NETWORK;
const client = Client.forName(network).setOperator(operatorId, operatorKey);
// Generate a admin key
const adminKey = PrivateKey.generateED25519();
// Initial metadata
const metadata = new Uint8Array([1]);
// New metadata
const newMetadata = new Uint8Array([1, 2]);
let tokenInfo;
try {
// Create a non fungible token
let createTokenTx = new TokenCreateTransaction()
.setTokenName("Test")
.setTokenSymbol("T")
.setMetadata(metadata)
.setTokenType(TokenType.FungibleCommon) // The same flow can be executed with a TokenType.NON_FUNGIBLE_UNIQUE (i.e. HIP-765)
.setDecimals(3)
.setInitialSupply(10000)
.setTreasuryAccountId(operatorId)
.setAdminKey(adminKey)
.freezeWith(client);
// Sign and execute create token transaction
const tokenCreateTxResponse = await (
await createTokenTx.sign(adminKey)
).execute(client);
// Get receipt for create token transaction
const tokenCreateTxReceipt =
await tokenCreateTxResponse.getReceipt(client);
console.log(
`Status of token create transction: ${tokenCreateTxReceipt.status.toString()}`,
);
// Get token id
const tokenId = tokenCreateTxReceipt.tokenId;
console.log(`Token id: ${tokenId.toString()}`);
// Get token info
tokenInfo = await new TokenInfoQuery()
.setTokenId(tokenId)
.execute(client);
console.log(`Token metadata:`, tokenInfo.metadata);
const tokenUpdateTx = new TokenUpdateTransaction()
.setTokenId(tokenId)
.setMetadata(newMetadata)
.freezeWith(client);
// Sign transactions with admin key and execute it
const tokenUpdateTxResponse = await (
await tokenUpdateTx.sign(adminKey)
).execute(client);
// Get receipt for token update transaction
const tokenUpdateTxReceipt =
await tokenUpdateTxResponse.getReceipt(client);
console.log(
`Status of token update transction: ${tokenUpdateTxReceipt.status.toString()}`,
);
tokenInfo = await new TokenInfoQuery()
.setTokenId(tokenId)
.execute(client);
console.log(`Token updated metadata:`, tokenInfo.metadata);
} catch (error) {
console.log(error);
}
client.close();
}
void main();