Skip to content

Commit

Permalink
add fee stats, freindbot, federation
Browse files Browse the repository at this point in the history
  • Loading branch information
acharb committed Dec 18, 2023
1 parent 61b6668 commit 027dc5d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/walletSdk/Horizon/Stellar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
Transaction,
TransactionBuilder as StellarTransactionBuilder,
FeeBumpTransaction,
Federation,
} from "stellar-sdk";
import axios from "axios";

import { Config } from "walletSdk";
import { AccountService } from "./AccountService";
Expand Down Expand Up @@ -224,4 +226,35 @@ export class Stellar {
decodeTransaction(xdr: string): Transaction | FeeBumpTransaction {
return StellarTransactionBuilder.fromXDR(xdr, this.cfg.stellar.network);
}

/**
* Returns the recommended fee (stroops) to use in a transaction based on the current
* stellar network fee stats.
* @returns {string} The recommended fee amount in stroops.
*/
async getRecommendedFee(): Promise<string> {
const stats = await this.server.feeStats();
return stats.max_fee.mode;
}

/**
* Resolves a federation address into a stellar address.
* @see {@link https://developers.stellar.org/docs/encyclopedia/federation}
* @param {string} fedAddress - The federation address (eg. jed*stellar.org).
* @returns {string} The stellar address associated with the federation address.
*/
async resolveFederation(fedAddress: string): Promise<string> {
const resp = await Federation.Server.resolve(fedAddress);
return resp.account_id;
}

/**
* Funds an account on the stellar test network. If it is already funded then call will error.
* Please note: only funds on the testnet network.
* @see {@link https://developers.stellar.org/docs/fundamentals-and-concepts/testnet-and-pubnet#friendbot}
* @param {string} address - The stellar address.
*/
async fundAccount(address: string) {
await axios.get(`https://friendbot.stellar.org/?addr=${address}`);
}
}
12 changes: 11 additions & 1 deletion test/stellar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("Stellar", () => {
try {
await stellar.server.loadAccount(kp.publicKey);
} catch (e) {
await axios.get("https://friendbot.stellar.org/?addr=" + kp.publicKey);
await stellar.fundAccount(kp.publicKey);
}
}, 10000);
it("should create and submit a transaction", async () => {
Expand Down Expand Up @@ -237,6 +237,16 @@ describe("Stellar", () => {
expect(txn).toBeTruthy();
}
}, 20000);

it("should return recommended fee", async () => {
const fee = await stellar.getRecommendedFee();
expect(fee).toBeTruthy();
});

it("should resolve federation address", async () => {
const resp = await stellar.resolveFederation("alec*vibrantapp.com");
expect(resp).toBeTruthy();
});
});

let txnSourceKp;
Expand Down

0 comments on commit 027dc5d

Please sign in to comment.