2025-06-22 00:21:09 +03:00

66 lines
2.0 KiB
JavaScript

import { e as error } from "./index.js";
import { Horizon, StrKey } from "@stellar/stellar-sdk";
const horizonUrl = "https://horizon-testnet.stellar.org";
const server = new Horizon.Server(horizonUrl);
async function fetchAccount(publicKey) {
if (StrKey.isValidEd25519PublicKey(publicKey)) {
try {
let account = await server.accounts().accountId(publicKey).call();
return account;
} catch (err) {
if (err.response?.status === 404) {
try {
await fundWithFriendbot(publicKey);
let account = await server.accounts().accountId(publicKey).call();
return account;
} catch (err2) {
throw error(500, {
message: `Unable to fund account ${publicKey}: ${err2.message}`
});
}
} else {
throw error(err.response?.status ?? 400, {
// @ts-ignore
message: `${err.response?.title} - ${err.response?.detail}`
});
}
}
} else {
throw error(400, { message: "invalid public key" });
}
}
async function fetchAccountBalances(publicKey) {
const { balances } = await fetchAccount(publicKey);
return balances;
}
async function fetchRecentPayments(publicKey, limit = 10) {
const { records } = await server.payments().forAccount(publicKey).limit(limit).order("desc").call();
return records;
}
async function fundWithFriendbot(publicKey) {
console.log(`i am requesting a friendbot funding for ${publicKey}`);
await server.friendbot(publicKey).call();
}
async function fetchAssetsWithHomeDomains(balances) {
let homeDomains = await Promise.all(
balances.map(async (asset) => {
if ("asset_issuer" in asset) {
let account = await fetchAccount(asset.asset_issuer);
if ("home_domain" in account) {
return {
...asset,
home_domain: account.home_domain
};
}
}
})
);
return homeDomains.filter((balance) => balance);
}
export {
fetchRecentPayments as a,
fetchAssetsWithHomeDomains as b,
fetchAccountBalances as f,
server as s
};