forked from iotaledger/iota-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsolidate-outputs.ts
97 lines (81 loc) · 3.5 KB
/
consolidate-outputs.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import { CommonOutput, Wallet, initLogger } from '@iota/sdk';
// This example uses secrets in environment variables for simplicity which should not be done in production.
require('dotenv').config({ path: '.env' });
// Run with command:
// yarn run-example ./how_tos/wallet/consolidate-outputs.ts
// In this example we will consolidate basic outputs from an wallet with only an AddressUnlockCondition by sending
// them to the same address again.
async function run() {
initLogger();
try {
for (const envVar of [
'WALLET_DB_PATH',
'STRONGHOLD_PASSWORD',
'EXPLORER_URL',
])
if (!(envVar in process.env)) {
throw new Error(
`.env ${envVar} is undefined, see .env.example`,
);
}
const wallet = await Wallet.create({
storagePath: process.env.WALLET_DB_PATH,
});
// To create an address we need to unlock stronghold.
await wallet.setStrongholdPassword(
process.env.STRONGHOLD_PASSWORD as string,
);
// Sync wallet to make sure wallet is updated with outputs from previous examples
await wallet.sync();
console.log('Wallet synced');
// List unspent outputs before consolidation.
// The output we created with example `request_funds` and the basic output from `mint` have only one
// unlock condition and it is an `AddressUnlockCondition`, and so they are valid for consolidation. They have the
// same `AddressUnlockCondition`(the address of the wallet), so they will be consolidated into one
// output.
const outputs = await wallet.unspentOutputs();
console.log('Outputs BEFORE consolidation:');
outputs.forEach(({ output }, i) => {
console.log(`OUTPUT #${i}`);
console.log(
'- amount: %d\n- native token: %s',
output.getAmount(),
output instanceof CommonOutput
? (output as CommonOutput).getNativeToken() ?? []
: [],
);
});
console.log('Sending consolidation transaction...');
// Consolidate unspent outputs and print the consolidation transaction ID
// Set `force` to true to force the consolidation even though the `output_threshold` isn't reached
const transaction = await wallet.consolidateOutputs({
force: true,
});
console.log('Transaction sent: %s', transaction.transactionId);
// Wait for the consolidation transaction to get accepted
await wallet.waitForTransactionAcceptance(transaction.transactionId);
console.log(
`Tx accepted: ${process.env.EXPLORER_URL}/transactions/${transaction.transactionId}`,
);
// Sync wallet
await wallet.sync();
console.log('Wallet synced');
// Outputs after consolidation
console.log('Outputs AFTER consolidation:');
outputs.forEach(({ output }, i) => {
console.log(`OUTPUT #${i}`);
console.log(
'- amount: %d\n- native tokens: %s',
output.getAmount(),
output instanceof CommonOutput
? (output as CommonOutput).getNativeToken()
: undefined,
);
});
} catch (error) {
console.error('Error: ', error);
}
}
void run().then(() => process.exit());