/
send-payment.js
54 lines (44 loc) · 1.61 KB
/
send-payment.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
const StellarSdk = require("stellar-sdk");
const { Keypair, TransactionBuilder } = require('../');
const { createTestnetAccount } = require('./utils');
const server = new StellarSdk.Server("https://horizon-testnet.stellar.org");
async function sendPayment() {
const senderKeypair = await Keypair.randomLocalPartyThreshSig();
const destination = "GAIH3ULLFQ4DGSECF2AR555KZ4KNDGEKN4AFI4SU2M7B43MGK3QJZNSR";
const amount = "9998";
console.log(
`Making a new test account and sending ${amount} lumens to ${destination}`,
);
await createTestnetAccount(senderKeypair.publicKey());
const [ { max_fee: { mode: fee } }, sender,] = await Promise.all([
server.feeStats(),
server.loadAccount(senderKeypair.publicKey()),
]);
const transaction = new TransactionBuilder(sender, {
fee,
networkPassphrase: StellarSdk.Networks.TESTNET,
})
.addOperation(
// This operation sends the destination account XLM
StellarSdk.Operation.payment({
destination,
asset: StellarSdk.Asset.native(),
amount,
}),
)
.setTimeout(30)
.build();
await transaction.sign(senderKeypair);
try {
// Submit the transaction to the Stellar network.
const transactionResult = await server.submitTransaction(transaction);
console.log(transactionResult);
console.error(`Success! ${senderKeypair.publicKey()} paid ${destination} ${amount} XLM`);
} catch (e) {
console.error("Oh no! Something went wrong.");
console.error(e.response.data.detail);
console.error(e.response.data.extras.result_codes);
console.error(e.response.data.type);
}
}
sendPayment();