forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellarService.js
More file actions
79 lines (74 loc) · 2.05 KB
/
Copy pathstellarService.js
File metadata and controls
79 lines (74 loc) · 2.05 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require("dotenv").config();
const { Horizon, Asset, StrKey } = require("stellar-sdk");
// Shared Horizon server instance
const server = new Horizon.Server(
process.env.HORIZON_URL || "https://horizon-testnet.stellar.org",
);
/**
* Returns a Stellar Asset object.
* Returns Asset.native() if code is 'XLM', otherwise returns a non-native Asset.
*
* @param {string} code - Asset code (e.g. 'NOVA', 'XLM')
* @param {string} [issuer] - Asset issuer public key (optional for XLM)
* @returns {Asset}
*/
function getAsset(code, issuer) {
if (code === "XLM" || code === "native") {
return Asset.native();
}
const asset = new Asset(code, issuer);
return asset;
}
// NOVA asset definition — issued by the Issuer Account
const NOVA = getAsset("NOVA", process.env.ISSUER_PUBLIC);
/**
* Validates that a string is a valid Stellar public key.
* Must be a 56-character G-prefixed Ed25519 public key.
* Requirements: 5.1
*
* @param {string} address
* @returns {boolean}
*/
function isValidStellarAddress(address) {
if (typeof address !== "string") return false;
try {
return StrKey.isValidEd25519PublicKey(address);
} catch {
return false;
}
}
/**
* Queries Horizon for the account's current NOVA token balance.
* Returns '0' if the account has no trustline for NOVA.
* Requirements: 6.1, 8.3
*
* @param {string} walletAddress - Stellar public key
* @returns {Promise<string>} NOVA balance as a string (e.g. "100.0000000")
*/
async function getNOVABalance(walletAddress) {
try {
const account = await server.loadAccount(walletAddress);
const novaBalance = account.balances.find(
(b) =>
b.asset_type !== "native" &&
b.asset_code === "NOVA" &&
b.asset_issuer === process.env.ISSUER_PUBLIC,
);
return novaBalance ? novaBalance.balance : "0";
} catch (err) {
if (
err.response?.status === 404 ||
err.message?.toLowerCase().includes("not found")
) {
return "0";
}
throw err;
}
}
module.exports = {
server,
NOVA,
isValidStellarAddress,
getNOVABalance,
getAsset,
};