From f9b64f6f650b77955033c4df943745f57f18a46d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Prohaszka?= Date: Tue, 16 Jul 2024 17:01:56 +0200 Subject: [PATCH] chore: remove generics as it will forbid the use of generic Api creation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Prohaszka --- libs/coin-framework/src/api/types.ts | 12 ++--- .../coin-polkadot/src/api/index.integ.test.ts | 1 + .../coin-polkadot/src/api/index.ts | 3 ++ .../coin-stellar/src/api/index.ts | 46 ++++++++++++++++++- .../src/logic/craftTransaction.ts | 3 +- .../coin-tezos/src/api/index.integ.test.ts | 1 + .../coin-xrp/src/api/index.integ.test.ts | 1 + libs/coin-modules/coin-xrp/src/api/index.ts | 1 + 8 files changed, 56 insertions(+), 12 deletions(-) diff --git a/libs/coin-framework/src/api/types.ts b/libs/coin-framework/src/api/types.ts index 0ac7942b21ce..58be941507b6 100644 --- a/libs/coin-framework/src/api/types.ts +++ b/libs/coin-framework/src/api/types.ts @@ -17,22 +17,18 @@ export type Operation = { transactionSequenceNumber: number; }; -export type Transaction = { - mode: M; +export type Transaction = { + mode: string; recipient: string; amount: bigint; fee: bigint; - supplement: S; + supplement?: unknown; }; export type Api = { broadcast: (tx: string) => Promise; combine: (tx: string, signature: string, pubkey?: string) => string; - craftTransaction: ( - address: string, - transaction: Transaction, - pubkey?: string, - ) => Promise; + craftTransaction: (address: string, transaction: Transaction, pubkey?: string) => Promise; estimateFees: (addr: string, amount: bigint) => Promise; getBalance: (address: string) => Promise; lastBlock: () => Promise; diff --git a/libs/coin-modules/coin-polkadot/src/api/index.integ.test.ts b/libs/coin-modules/coin-polkadot/src/api/index.integ.test.ts index 0cb00ca9967e..3f138de77076 100644 --- a/libs/coin-modules/coin-polkadot/src/api/index.integ.test.ts +++ b/libs/coin-modules/coin-polkadot/src/api/index.integ.test.ts @@ -81,6 +81,7 @@ describe("Polkadot Api", () => { it("returns a raw transaction", async () => { // When const result = await module.craftTransaction(address, { + mode: "send", recipient: "16YreVmGhM8mNMqnsvK7rn7b1e4SKYsTfFUn4UfCZ65BgDjh", amount: BigInt(10), fee: BigInt(1), diff --git a/libs/coin-modules/coin-polkadot/src/api/index.ts b/libs/coin-modules/coin-polkadot/src/api/index.ts index 22fed372c431..5a9ea2a87073 100644 --- a/libs/coin-modules/coin-polkadot/src/api/index.ts +++ b/libs/coin-modules/coin-polkadot/src/api/index.ts @@ -30,8 +30,11 @@ export function createApi(config: PolkadotConfig): Api { async function craft( address: string, transaction: { + mode: string; recipient: string; amount: bigint; + fee: bigint; + supplement?: unknown; }, ): Promise { const extrinsicArg = defaultExtrinsicArg(transaction.amount, transaction.recipient); diff --git a/libs/coin-modules/coin-stellar/src/api/index.ts b/libs/coin-modules/coin-stellar/src/api/index.ts index 897ee353b983..207974688afa 100644 --- a/libs/coin-modules/coin-stellar/src/api/index.ts +++ b/libs/coin-modules/coin-stellar/src/api/index.ts @@ -3,10 +3,11 @@ import coinConfig, { type StellarConfig } from "../config"; import { // broadcast, // combine, - // craftTransaction, + craftTransaction, // estimateFees, getBalance, listOperations, + // lastBlock, // rawEncode, } from "../logic"; @@ -18,9 +19,50 @@ export function createApi(config: StellarConfig): Api { combine: () => { throw new Error("Method not supported"); }, - craftTransaction: () => Promise.reject(new Error("Method not supported")), + craftTransaction: craft, estimateFees: () => Promise.reject(new Error("Method not supported")), getBalance, + lastBlock: () => Promise.reject(new Error("Method not supported")), listOperations, }; } + +type Supplement = { + assetCode?: string | undefined; + assetIssuer?: string | undefined; + memoType?: string | null | undefined; + memoValue?: string | null | undefined; +}; +function isSupplement(supplement: unknown): supplement is Supplement { + return typeof supplement === "object"; +} +async function craft( + address: string, + transaction: { + mode: string; + recipient: string; + amount: bigint; + fee: bigint; + supplement?: unknown; + }, +): Promise { + const supplement = isSupplement(transaction.supplement) + ? { + assetCode: transaction.supplement?.assetCode, + assetIssuer: transaction.supplement?.assetIssuer, + memoType: transaction.supplement?.memoType, + memoValue: transaction.supplement?.memoValue, + } + : {}; + const tx = await craftTransaction( + { address }, + { + ...transaction, + assetCode: supplement?.assetCode, + assetIssuer: supplement?.assetIssuer, + memoType: supplement?.memoType, + memoValue: supplement?.memoValue, + }, + ); + return tx.xdr; +} diff --git a/libs/coin-modules/coin-stellar/src/logic/craftTransaction.ts b/libs/coin-modules/coin-stellar/src/logic/craftTransaction.ts index 6fdfbc0996ea..c37785967f72 100644 --- a/libs/coin-modules/coin-stellar/src/logic/craftTransaction.ts +++ b/libs/coin-modules/coin-stellar/src/logic/craftTransaction.ts @@ -19,7 +19,7 @@ export async function craftTransaction( address: string; }, transaction: { - mode: "send" | "changeTrust"; + mode: string; recipient: string; amount: bigint; fee: bigint; @@ -30,7 +30,6 @@ export async function craftTransaction( }, ): Promise<{ transaction: StellarSdkTransaction; xdr: string }> { const { amount, recipient, fee, memoType, memoValue, mode, assetCode, assetIssuer } = transaction; - console.log("amount", amount); const source = await loadAccount(account.address); diff --git a/libs/coin-modules/coin-tezos/src/api/index.integ.test.ts b/libs/coin-modules/coin-tezos/src/api/index.integ.test.ts index eba6127ef83d..679a187ef7f3 100644 --- a/libs/coin-modules/coin-tezos/src/api/index.integ.test.ts +++ b/libs/coin-modules/coin-tezos/src/api/index.integ.test.ts @@ -78,6 +78,7 @@ describe("Tezos Api", () => { it("returns a raw transaction", async () => { // When const result = await module.craftTransaction(address, { + mode: "send", recipient: "tz1aWXP237BLwNHJcCD4b3DutCevhqq2T1Z9", amount: BigInt(10), fee: BigInt(1), diff --git a/libs/coin-modules/coin-xrp/src/api/index.integ.test.ts b/libs/coin-modules/coin-xrp/src/api/index.integ.test.ts index c67ce5d4b726..b9ad99455218 100644 --- a/libs/coin-modules/coin-xrp/src/api/index.integ.test.ts +++ b/libs/coin-modules/coin-xrp/src/api/index.integ.test.ts @@ -69,6 +69,7 @@ describe("Xrp Api", () => { it("returns a raw transaction", async () => { // When const result = await module.craftTransaction(address, { + mode: "send", recipient: "rKRtUG15iBsCQRgrkeUEg5oX4Ae2zWZ89z", amount: BigInt(10), fee: BigInt(1), diff --git a/libs/coin-modules/coin-xrp/src/api/index.ts b/libs/coin-modules/coin-xrp/src/api/index.ts index 02771b24a1ea..6b70daf5e46c 100644 --- a/libs/coin-modules/coin-xrp/src/api/index.ts +++ b/libs/coin-modules/coin-xrp/src/api/index.ts @@ -28,6 +28,7 @@ export function createApi(config: XrpConfig): Api { async function craft( address: string, transaction: { + mode: string; recipient: string; amount: bigint; fee: bigint;