forked from hedera-dev/hedera-code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-fungible-token-metadata.js
49 lines (41 loc) · 1.7 KB
/
update-fungible-token-metadata.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
const dotenv = require('dotenv');
dotenv.config();
const {
AccountId,
PrivateKey,
Client,
TokenInfoQuery,
TokenUpdateTransaction,
} = require('@hashgraph/sdk');
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromString(process.env.OPERATOR_KEY);
const tokenId = process.env.TOKEN_ID;
const newMetadata = process.env.IPFS_CID;
const metadataKey = PrivateKey.fromString(process.env.METADATA_KEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
async function updateTokenMetadata(tokenId, newMetadata, metadataKey) {
try {
let tokenInfo = await new TokenInfoQuery()
.setTokenId(tokenId)
.execute(client);
console.log(`Token metadata before update:`, tokenInfo.metadata);
const tokenUpdateTx = new TokenUpdateTransaction()
.setTokenId(tokenId)
.setMetadata(Buffer.from(newMetadata))
.freezeWith(client);
const signedTokenUpdateTx = await tokenUpdateTx.sign(metadataKey);
const tokenUpdateTxResponse = await signedTokenUpdateTx.execute(client);
const tokenUpdateTxReceipt = await tokenUpdateTxResponse.getReceipt(client);
console.log(`Status of token update transaction: ${tokenUpdateTxReceipt.status.toString()}`);
// Get updated token info
tokenInfo = await new TokenInfoQuery()
.setTokenId(tokenId)
.execute(client);
console.log(`Token updated metadata:`, tokenInfo.metadata);
} catch (error) {
console.error("Error during token metadata update:", error);
}
}
updateTokenMetadata(tokenId, newMetadata, metadataKey).finally(() => {
client.close();
});