Skip to content

Commit

Permalink
feat(xlm): add estimation fees
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Prohaszka <[email protected]>
  • Loading branch information
sprohaszka-ledger committed Jul 17, 2024
1 parent 159c82e commit 2798f33
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 37 deletions.
54 changes: 28 additions & 26 deletions libs/coin-modules/coin-stellar/src/api/index.integ.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@ describe("Stellar Api", () => {
});
});

// describe("estimateFees", () => {
// it("returns a default value", async () => {
// // Given
// const address = "rDCyjRD2TcSSGUQpEcEhJGmDWfjPJpuGxu";
// const amount = BigInt(100);
describe("estimateFees", () => {
it("returns a default value", async () => {
// Given
const amount = BigInt(100_000);

// // When
// const result = await module.estimateFees(address, amount);
// When
const result = await module.estimateFees(address, amount);

// // Then
// expect(result).toEqual(BigInt(10));
// });
// });
// Then
expect(result).toEqual(BigInt(100));
});
});

describe("listOperations", () => {
it("returns a list regarding address parameter", async () => {
Expand Down Expand Up @@ -68,20 +67,23 @@ describe("Stellar Api", () => {
});
});

// describe("craftTransaction", () => {
// it("returns a raw transaction", async () => {
// // When
// const result = await module.craftTransaction(address, {
// recipient: "rKRtUG15iBsCQRgrkeUEg5oX4Ae2zWZ89z",
// amount: BigInt(10),
// fee: BigInt(1),
// });
describe("craftTransaction", () => {
it("returns a raw transaction", async () => {
// When
const result = await module.craftTransaction(address, {
mode: "send",
recipient: "GD6QELUZPSKPRWVXOQ3F6GBF4OBRMCHO5PHREXH4ZRTPJAG7V5MD7JGX",
amount: BigInt(1_000_000),
fee: BigInt(100),
});

// // Then
// expect(result.slice(0, 34)).toEqual("120000228000000024001BCDA6201B001F");
// expect(result.slice(38)).toEqual(
// "61400000000000000A6840000000000000018114CF30F590D7A9067B2604D80D46090FBF342EBE988314CA26FB6B0EF6859436C2037BA0A9913208A59B98",
// );
// });
// });
// Then
expect(result.slice(0, 68)).toEqual(
"AAAAAgAAAAD9Ai6ZfJT42rd0Nl8YJeODFgju688SXPzMZvSA369YPwAAAGQAAHloAAAI",
);
expect(result.slice(70)).toEqual(
"AAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAQAAAAD9Ai6ZfJT42rd0Nl8YJeODFgju688SXPzMZvSA369YPwAAAAAAAAAAAA9CQAAAAAAAAAAA",
);
});
});
});
4 changes: 2 additions & 2 deletions libs/coin-modules/coin-stellar/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
broadcast,
combine,
craftTransaction,
// estimateFees,
estimateFees,
getBalance,
listOperations,
lastBlock,
Expand All @@ -17,7 +17,7 @@ export function createApi(config: StellarConfig): Api {
broadcast,
combine: compose,
craftTransaction: craft,
estimateFees: () => Promise.reject(new Error("Method not supported")),
estimateFees,
getBalance,
lastBlock,
listOperations,
Expand Down
10 changes: 2 additions & 8 deletions libs/coin-modules/coin-stellar/src/logic/combine.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { Transaction as StellarSdkTransaction } from "@stellar/stellar-sdk";

// https://developers.stellar.org/docs/learn/encyclopedia/network-configuration/network-passphrases
// Mainnet
const NETWORK_PASSPHRASE = "Public Global Stellar Network ; September 2015";
// Testnet
// const NETWORK_PASSPHRASE = "Test SDF Network ; September 2015";
import { Networks, Transaction as StellarSdkTransaction } from "@stellar/stellar-sdk";

export function combine(transaction: string, signature: string, publicKey: string): string {
const unsignedTx = new StellarSdkTransaction(transaction, NETWORK_PASSPHRASE);
const unsignedTx = new StellarSdkTransaction(transaction, Networks.PUBLIC);
unsignedTx.addSignature(publicKey, signature);
return unsignedTx.toXDR();
}
6 changes: 6 additions & 0 deletions libs/coin-modules/coin-stellar/src/logic/estimateFees.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { fetchBaseFee } from "../network";

export async function estimateFees(): Promise<bigint> {
const fees = await fetchBaseFee();
return BigInt(fees.recommendedFee);
}
2 changes: 1 addition & 1 deletion libs/coin-modules/coin-stellar/src/logic/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { broadcast } from "./broadcast";
export { combine } from "./combine";
export { craftTransaction } from "./craftTransaction";
// export { estimateFees } from "./estimateFees";
export { estimateFees } from "./estimateFees";
export { getBalance } from "./getBalance";
export { lastBlock } from "./lastBlock";
export { listOperations } from "./listOperations";

0 comments on commit 2798f33

Please sign in to comment.