From 2798f33337f19d10cc4c0c6e49d0c3c94050a82f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Prohaszka?= Date: Wed, 17 Jul 2024 09:29:24 +0200 Subject: [PATCH] feat(xlm): add estimation fees MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Prohaszka --- .../coin-stellar/src/api/index.integ.test.ts | 54 ++++++++++--------- .../coin-stellar/src/api/index.ts | 4 +- .../coin-stellar/src/logic/combine.ts | 10 +--- .../coin-stellar/src/logic/estimateFees.ts | 6 +++ .../coin-stellar/src/logic/index.ts | 2 +- 5 files changed, 39 insertions(+), 37 deletions(-) create mode 100644 libs/coin-modules/coin-stellar/src/logic/estimateFees.ts diff --git a/libs/coin-modules/coin-stellar/src/api/index.integ.test.ts b/libs/coin-modules/coin-stellar/src/api/index.integ.test.ts index 214944310d97..1640376c3457 100644 --- a/libs/coin-modules/coin-stellar/src/api/index.integ.test.ts +++ b/libs/coin-modules/coin-stellar/src/api/index.integ.test.ts @@ -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 () => { @@ -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", + ); + }); + }); }); diff --git a/libs/coin-modules/coin-stellar/src/api/index.ts b/libs/coin-modules/coin-stellar/src/api/index.ts index d1b00f4aa8b7..df3d896a8458 100644 --- a/libs/coin-modules/coin-stellar/src/api/index.ts +++ b/libs/coin-modules/coin-stellar/src/api/index.ts @@ -4,7 +4,7 @@ import { broadcast, combine, craftTransaction, - // estimateFees, + estimateFees, getBalance, listOperations, lastBlock, @@ -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, diff --git a/libs/coin-modules/coin-stellar/src/logic/combine.ts b/libs/coin-modules/coin-stellar/src/logic/combine.ts index abbc9ac001bc..f909d7d16fb1 100644 --- a/libs/coin-modules/coin-stellar/src/logic/combine.ts +++ b/libs/coin-modules/coin-stellar/src/logic/combine.ts @@ -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(); } diff --git a/libs/coin-modules/coin-stellar/src/logic/estimateFees.ts b/libs/coin-modules/coin-stellar/src/logic/estimateFees.ts new file mode 100644 index 000000000000..b92d9143ea4e --- /dev/null +++ b/libs/coin-modules/coin-stellar/src/logic/estimateFees.ts @@ -0,0 +1,6 @@ +import { fetchBaseFee } from "../network"; + +export async function estimateFees(): Promise { + const fees = await fetchBaseFee(); + return BigInt(fees.recommendedFee); +} diff --git a/libs/coin-modules/coin-stellar/src/logic/index.ts b/libs/coin-modules/coin-stellar/src/logic/index.ts index 5605cbf5e923..5bbaeb2211c4 100644 --- a/libs/coin-modules/coin-stellar/src/logic/index.ts +++ b/libs/coin-modules/coin-stellar/src/logic/index.ts @@ -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";