-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtransfer-ae.js
executable file
·67 lines (58 loc) · 2.96 KB
/
transfer-ae.js
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
#!/usr/bin/env node
// # Transfer AE
//
// ## Introduction
// The whole script is [located in the repository](https://github.com/aeternity/aepp-sdk-js/blob/568c291b92c030011ca9e68169f328be6ff79488/examples/node/transfer-ae.js)
// and this page explains in detail how to:
//
// - initialize an instance of the SDK with a pre-funded account
// - transfer AE to another account
// ## 1. Specify imports
// You need to import `AeSdk`, `Node` and `MemoryAccount` classes from the SDK.
import { AeSdk, Node, MemoryAccount } from '@aeternity/aepp-sdk';
// **Note**:
//
// - You need to have the SDK installed via `npm i @aetenity/aepp-sdk -g` to run that example code.
// ## 2. Define constants
// The following constants are used in the subsequent code snippets.
const ACCOUNT_KEYPAIR = {
publicKey: 'ak_21A27UVVt3hDkBE5J7rhhqnH5YNb4Y1dqo4PnSybrH85pnWo7E',
secretKey: 'sk_2CuofqWZHrABCrM7GY95YSQn8PyFvKQadnvFnpwhjUnDCFAWmf',
};
const NODE_URL = 'https://testnet.aeternity.io';
const [amount = 1, recipient = ACCOUNT_KEYPAIR.publicKey] = process.argv.slice(2);
// Note:
//
// - The secret key of the account is pre-funded and only used for demonstration purpose
// - You can replace it with your own
// (see [Create an Account](../../quick-start.md#2-create-a-sender-account))
// - In case the account runs out of funds you can always request AE using the [Faucet](https://faucet.aepps.com/)
// - By default the script will transfer `1 aetto` and use the demo account itself as recipient
// - Optionally you can provide the amount and a different recipient by providing the
// arguments when executing the script,
// e.g. `node transfer-ae.js 3 ak_6D2uyunJaERXfgbsc94G8vrp79nZrbtorL7VCRXk3sWiFK5jb`
// ## 3. Create object instances
const account = new MemoryAccount(ACCOUNT_KEYPAIR.secretKey);
const node = new Node(NODE_URL);
const aeSdk = new AeSdk({
nodes: [{ name: 'testnet', instance: node }],
accounts: [account],
});
// ## 4. Get AE balance of recipient (before transfer)
// Before the transfer of AE you can check the AE balance of the recipient.
const balanceBefore = await aeSdk.getBalance(recipient);
console.log(`Balance of ${recipient} (before): ${balanceBefore} aettos`);
// ## 5. Transfer AE
// Calling the `spend` function will create, sign and broadcast a `SpendTx` to the network.
const tx = await aeSdk.spend(amount, recipient);
console.log('Transaction mined', tx);
// Alternatively, you can use [transferFunds](https://docs.aeternity.com/aepp-sdk-js/v13.2.2/api/functions/transferFunds.html)
// method to transfer a fraction of your AE to another account.
// ## 6. Get AE balance of recipient (after transfer)
const balanceAfter = await aeSdk.getBalance(recipient);
console.log(`Balance of ${recipient} (after): ${balanceAfter} aettos`);
// Note:
//
// - If the recipient is the same account as the sender (default of the script if no arguments
// provided) the balance will be lower after transfer because a transaction `fee` has been
// paid to the miners.