forked from Pulsefy/QiuckEx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_test_payment.js
More file actions
51 lines (46 loc) · 1.58 KB
/
Copy pathsend_test_payment.js
File metadata and controls
51 lines (46 loc) · 1.58 KB
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
const Stellar = require("stellar-sdk");
const fetch = require("node-fetch");
(async function () {
try {
const server = new Stellar.Horizon.Server(
"https://horizon-testnet.stellar.org",
);
const source = Stellar.Keypair.random();
console.log("Source public:", source.publicKey());
// Fund via friendbot
console.log("Funding via friendbot...");
const fbResp = await fetch(
`https://friendbot.stellar.org?addr=${source.publicKey()}`,
);
if (!fbResp.ok) {
throw new Error("Friendbot failed: " + fbResp.statusText);
}
console.log("Friendbot funded");
// Ensure destination exists by funding it via friendbot (so payment op will succeed)
const destination =
"GAMOSFOKEYHFDGMXIEFEYBUYK3ZMFYN3PFLOTBRXFGBFGRKBKLQSLGLP";
console.log("Funding destination via friendbot (if needed)...");
await fetch(`https://friendbot.stellar.org?addr=${destination}`);
// Build & send 2 XLM
const account = await server.loadAccount(source.publicKey());
const tx = new Stellar.TransactionBuilder(account, {
fee: Stellar.BASE_FEE,
networkPassphrase: Stellar.Networks.TESTNET,
})
.addOperation(
Stellar.Operation.payment({
destination,
asset: Stellar.Asset.native(),
amount: "2",
}),
)
.setTimeout(30)
.build();
tx.sign(source);
const resp = await server.submitTransaction(tx);
console.log("Submitted tx:", resp.hash);
} catch (err) {
console.error("Error:", err && err.toString ? err.toString() : err);
process.exitCode = 1;
}
})();