-
Notifications
You must be signed in to change notification settings - Fork 39
/
destroy-foundry.ts
62 lines (50 loc) · 2.12 KB
/
destroy-foundry.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
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import { getUnlockedWallet } from '../../wallet/common';
// In this example we will try to destroy the first foundry there is in the account. This is only possible if its
// circulating supply is 0 and no native tokens were burned.
//
// 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/destroy-foundry.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'`);
}
// We try to destroy the first foundry in the account
const foundry = balance.foundries[0];
console.log(`Foundries before destroying: ${balance.foundries.length}`);
// Burn a foundry
const transaction = await account
.prepareDestroyFoundry(foundry)
.then((prepared) => prepared.send());
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();
console.log(`Foundries after destroying: ${balance.foundries.length}`);
} catch (error) {
console.log('Error: ', error);
}
process.exit(0);
}
run();