forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello-stellar.ts
More file actions
56 lines (47 loc) · 1.63 KB
/
Copy pathhello-stellar.ts
File metadata and controls
56 lines (47 loc) · 1.63 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
52
53
54
55
56
/**
* hello-stellar.ts
* Quick-Start: fetch a Stellar testnet account balance.
*
* Usage:
* npx ts-node scripts/hello-stellar.ts
* # or add to package.json scripts: "hello-stellar": "ts-node scripts/hello-stellar.ts"
*
* Set STELLAR_PUBLIC_KEY in your environment or .env file.
* If unset, a random keypair is generated and funded via Friendbot.
*/
import { Horizon, Keypair } from "@stellar/stellar-sdk";
import https from "https";
const HORIZON_URL = "https://horizon-testnet.stellar.org";
const FRIENDBOT_URL = "https://friendbot.stellar.org";
async function fundWithFriendbot(publicKey: string): Promise<void> {
return new Promise((resolve, reject) => {
https
.get(`${FRIENDBOT_URL}?addr=${publicKey}`, (res) => {
res.resume(); // drain response
res.on("end", resolve);
})
.on("error", reject);
});
}
async function main() {
const server = new Horizon.Server(HORIZON_URL);
let publicKey = process.env.STELLAR_PUBLIC_KEY;
if (!publicKey) {
const keypair = Keypair.random();
publicKey = keypair.publicKey();
console.log(`No STELLAR_PUBLIC_KEY set — generated: ${publicKey}`);
console.log("Funding via Friendbot (testnet only)…");
await fundWithFriendbot(publicKey);
}
const account = await server.loadAccount(publicKey);
console.log(`\nAccount: ${publicKey}`);
console.log("Balances:");
for (const { asset_type, asset_code, balance } of account.balances) {
const label = asset_type === "native" ? "XLM" : asset_code;
console.log(` ${label}: ${balance}`);
}
}
main().catch((err) => {
console.error("Error:", err.message ?? err);
process.exit(1);
});