-
Notifications
You must be signed in to change notification settings - Fork 39
/
melt.ts
83 lines (68 loc) · 2.59 KB
/
melt.ts
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
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import { getUnlockedWallet } from '../../wallet/common';
// The amount of native tokens to melt.
const MELT_AMOUNT = BigInt(10);
// In this example we will melt an existing native token with its foundry.
//
// Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by
// running the `how_tos/accounts_and_addresses/create-account` example!
//
// Rename `.env.example` to `.env` first, then run
// yarn run-example ./how_tos/native_tokens/melt.ts
async function run() {
for (const envVar of ['EXPLORER_URL']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}
try {
// Create the wallet
const wallet = await getUnlockedWallet();
// Get the account we generated with `01-create-wallet`
const account = await wallet.getAccount('Alice');
// May want to ensure the account is synced before sending a transaction.
let balance = await account.sync();
if (balance.foundries.length == 0) {
throw new Error(`No Foundry available in account 'Alice'`);
}
// Find first foundry and corresponding token id
const tokenId = balance.foundries[0];
let token = balance.nativeTokens.find(
(nativeToken) => nativeToken.tokenId == tokenId,
);
if (token == null) {
throw new Error(
`Couldn't find native token '${tokenId}' in the account`,
);
}
console.log(`Balance before melting: ${token.available}`);
// Melt some of the circulating supply
const transaction = await account.meltNativeToken(
token.tokenId,
MELT_AMOUNT,
);
console.log(`Transaction sent: ${transaction.transactionId}`);
// Wait for transaction to get included
const blockId = await account.retryTransactionUntilIncluded(
transaction.transactionId,
);
console.log(
`Block included: ${process.env.EXPLORER_URL}/block/${blockId}`,
);
balance = await account.sync();
token = balance.nativeTokens.find(
(nativeToken) => nativeToken.tokenId == tokenId,
);
if (token == null) {
throw new Error(
`Couldn't find native token '${tokenId}' in the account`,
);
}
console.log(`Balance after melting: ${token.available}`);
} catch (error) {
console.log('Error: ', error);
}
process.exit(0);
}
run();