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 all commits
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
21 changes: 21 additions & 0 deletions src/walletSdk/Horizon/Stellar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
TransactionBuilder as StellarTransactionBuilder,
FeeBumpTransaction,
} from "stellar-sdk";
import axios from "axios";

import { Config } from "walletSdk";
import { AccountService } from "./AccountService";
Expand Down Expand Up @@ -224,4 +225,24 @@ 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

}

/**
* 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 fundTestnetAccount(address: string) {
await axios.get(`https://friendbot.stellar.org/?addr=${address}`);
}
}
7 changes: 6 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.fundTestnetAccount(kp.publicKey);
}
}, 10000);
it("should create and submit a transaction", async () => {
Expand Down Expand Up @@ -237,6 +237,11 @@ describe("Stellar", () => {
expect(txn).toBeTruthy();
}
}, 20000);

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

let txnSourceKp;
Expand Down
4 changes: 3 additions & 1 deletion test/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ describe("Path Payment", () => {
const txBuilder = await stellar.transaction({
sourceAddress: sourceKp,
});
const txn = txBuilder.swap(new NativeAssetId(), usdcAsset, ".1").build();
const txn = txBuilder
.swap(new NativeAssetId(), new NativeAssetId(), ".1")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

unrelated. Test is failing bc of testnet reset and no offers avail yet. Just using native -> native here instead, we already have path pay tests between 2 different assets, which swap is using under the hood

.build();
sourceKp.sign(txn);
const success = await stellar.submitTransaction(txn);
expect(success).toBe(true);
Expand Down
Loading