This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
forked from iotaledger/iota.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09_transaction.ts
60 lines (50 loc) · 1.84 KB
/
09_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
// Copyright 2021-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import { Client, initLogger } from '@iota/client';
require('dotenv').config({ path: '../.env' });
// Run with command:
// node ./dist/09_transaction.js
// In this example we will send a transaction
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
}
const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
localPow: true,
});
try {
if (!process.env.NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1) {
throw new Error('.env mnemonic is undefined, see .env.example');
}
// Configure your own mnemonic in ".env". Since the output amount cannot be zero, the mnemonic must contain non-zero
// balance
const secretManager = {
mnemonic: process.env.NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1,
};
// We generate an address from our own mnemonic so that we send the funds to ourselves
const addresses = await client.generateAddresses(secretManager, {
range: {
start: 1,
end: 2,
},
});
// We prepare the transaction
// Insert the output address and amount to spend. The amount cannot be zero.
const blockIdAndBlock = await client.buildAndPostBlock(secretManager, {
output: {
address: addresses[0],
amount: '1000000',
},
});
console.log('Block: ', blockIdAndBlock, '\n');
console.log(
`Transaction sent: ${process.env.EXPLORER_URL}/block/${blockIdAndBlock[0]}`,
);
} catch (error) {
console.error('Error: ', error);
}
}
run().then(() => process.exit());