-
Notifications
You must be signed in to change notification settings - Fork 39
/
advanced_transaction.ts
75 lines (64 loc) · 2.27 KB
/
advanced_transaction.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
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import {
AddressUnlockCondition,
Client,
Ed25519Address,
TimelockUnlockCondition,
Utils,
Wallet,
initLogger,
} from '@iota/sdk';
require('dotenv').config({ path: '.env' });
// Run with command:
// yarn run-example ./how_tos/advanced_transactions/advanced_transaction.ts
// This example syncs the account and sends an output with a timelock unlock condition
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 = new Wallet({
storagePath: process.env.WALLET_DB_PATH,
});
const account = await wallet.getAccount('Alice');
await account.sync();
// To sign a transaction we need to unlock stronghold.
await wallet.setStrongholdPassword(
process.env.STRONGHOLD_PASSWORD as string,
);
// Create an output with amount 1_000_000 and a timelock of 1 hour
const in_an_hour = Math.floor(Date.now() / 1000) + 3600;
const basicOutput = await new Client({}).buildBasicOutput({
unlockConditions: [
new AddressUnlockCondition(
new Ed25519Address(
Utils.bech32ToHex(
'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy',
),
),
),
new TimelockUnlockCondition(in_an_hour),
],
});
const transaction = await account.sendOutputs([basicOutput]);
console.log(`Transaction sent: ${transaction.transactionId}`);
console.log('Waiting until included in block...');
const blockId = await account.retryTransactionUntilIncluded(
transaction.transactionId,
);
console.log(`Block sent: ${process.env.EXPLORER_URL}/block/${blockId}`);
} catch (error) {
console.error('Error: ', error);
}
}
run().then(() => process.exit());