Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding some helper methods #82

Merged
merged 4 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is how we're recommending fees in freighter, and I think makes sense

}

/**
* 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> {
acharb marked this conversation as resolved.
Show resolved Hide resolved
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) {
acharb marked this conversation as resolved.
Show resolved Hide resolved
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
Loading