-
Notifications
You must be signed in to change notification settings - Fork 39
/
request-funds.ts
72 lines (59 loc) · 2.27 KB
/
request-funds.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
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import { Utils, Wallet, initLogger } from '@iota/sdk';
// This example uses secrets in environment variables for simplicity which should not be done in production.
//
// Make sure that `example.stronghold` and `example.walletdb` already exist by
// running the `how_tos/accounts_and_addresses/create-account` example!
//
require('dotenv').config({ path: '.env' });
// Run with command:
// yarn run-example ./how_tos/alias-wallet/request-funds.ts
// In this example we request funds to an alias wallet.
async function run() {
initLogger();
for (const envVar of ['WALLET_DB_PATH', 'FAUCET_URL']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}
try {
const faucetUrl = process.env.FAUCET_URL as string;
// Create the wallet
const wallet = new Wallet({
storagePath: process.env.WALLET_DB_PATH,
});
// Get the account we generated with `create_wallet`
const account = await wallet.getAccount('Alice');
const balance = await account.sync();
const totalBaseTokenBalance = balance.baseCoin.total;
console.log(
`Balance before requesting funds on alias address: ${totalBaseTokenBalance}`,
);
const aliasId = balance.aliases[0];
console.log(`Alias Id: ${aliasId}`);
// Get Alias address
const aliasAddress = Utils.aliasIdToBech32(
aliasId,
await (await wallet.getClient()).getBech32Hrp(),
);
const faucetResponse = await (
await wallet.getClient()
).requestFundsFromFaucet(faucetUrl, aliasAddress);
console.log(faucetResponse);
await new Promise((resolve) => setTimeout(resolve, 10000));
const syncOptions = {
alias: {
basicOutputs: true,
},
};
const totalBaseTokenBalanceAfter = (await account.sync(syncOptions))
.baseCoin.total;
console.log(
`Balance after requesting funds on alias address: ${totalBaseTokenBalanceAfter}`,
);
} catch (error) {
console.error('Error: ', error);
}
}
run().then(() => process.exit());