-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathpaying-for-spend-tx.js
executable file
·93 lines (79 loc) · 3.27 KB
/
paying-for-spend-tx.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
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
#!/usr/bin/env node
// # InnerTx: SpendTx
//
// ## Introduction
// The whole script is [located in the repository](https://github.com/aeternity/aepp-sdk-js/blob/568c291b92c030011ca9e68169f328be6ff79488/examples/node/paying-for-spend-tx.js)
// and this page explains in detail how to:
//
// - Create and sign a `SpendTx` for an account with the `innerTx` option.
// - Wrap the signed `SpendTx` in a `PayingForTx`, signing it using an account that pays the fees
// of the inner `SpendTx` and broadcasts it to the network.
//
// Note:
//
// - This can be done for ***any*** transaction type!
// ## 1. Specify imports
// You need to import `AeSdk`, `Node` and `MemoryAccount` classes from the SDK.
import { AeSdk, Node, MemoryAccount, Tag } 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 PAYER_ACCOUNT_SECRET_KEY = 'sk_2CuofqWZHrABCrM7GY95YSQn8PyFvKQadnvFnpwhjUnDCFAWmf';
const NODE_URL = 'https://testnet.aeternity.io';
const AMOUNT = 1;
// 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/)
// - The `AMOUNT` (in `aettos`) will be send to the new user and returned to the payer.
// ## 3. Create object instances
const payerAccount = new MemoryAccount(PAYER_ACCOUNT_SECRET_KEY);
const newUserAccount = MemoryAccount.generate();
const node = new Node(NODE_URL);
const aeSdk = new AeSdk({
nodes: [{ name: 'testnet', instance: node }],
accounts: [payerAccount, newUserAccount],
});
// ## 4. Send 1 `aetto` from payer to new user
const spendTxResult = await aeSdk.spend(AMOUNT, newUserAccount.address, {
onAccount: payerAccount,
});
console.log(spendTxResult);
// ## 5. Check balance of new user (before)
const newUserBalanceBefore = await aeSdk.getBalance(newUserAccount.address);
console.log(`new user balance (before): ${newUserBalanceBefore}`);
// Note:
//
// - The balance should now be 1
// ## 6. Create and sign `SpendTx` on behalf of new user
const spendTx = await aeSdk.buildTx({
tag: Tag.SpendTx,
senderId: newUserAccount.address,
recipientId: payerAccount.address,
amount: AMOUNT,
});
const signedSpendTx = await aeSdk.signTransaction(spendTx, {
onAccount: newUserAccount,
innerTx: true,
});
// Note:
//
// - The provided [transaction option](../../transaction-options.md) `innerTx` indicates that
// the transaction needs to be signed in a special way
// ## 7. Create, sign & broadcast the `PayingForTx` as payer
const payForTx = await aeSdk.payForTransaction(signedSpendTx, { onAccount: payerAccount });
console.log(payForTx);
// Note:
//
// - Normally sending the whole balance (1 `aetto`) would not be possible as the new user would
// have to cover the transaction fee.
// ## 8. Check balance of new user (after)
const newUserBalanceAfter = await aeSdk.getBalance(newUserAccount.address);
console.log(`new user balance (after): ${newUserBalanceAfter}`);
// Note:
//
// - The balance should now be 0