Skip to content

Commit

Permalink
chore: remove generics as it will forbid the use of generic Api creation
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 df617b8 commit f9b64f6
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 12 deletions.
12 changes: 4 additions & 8 deletions libs/coin-framework/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,18 @@ export type Operation = {
transactionSequenceNumber: number;
};

export type Transaction<M, S> = {
mode: M;
export type Transaction = {
mode: string;
recipient: string;
amount: bigint;
fee: bigint;
supplement: S;
supplement?: unknown;
};

export type Api = {
broadcast: (tx: string) => Promise<string>;
combine: (tx: string, signature: string, pubkey?: string) => string;
craftTransaction: <M, S>(
address: string,
transaction: Transaction<M, S>,
pubkey?: string,
) => Promise<string>;
craftTransaction: (address: string, transaction: Transaction, pubkey?: string) => Promise<string>;
estimateFees: (addr: string, amount: bigint) => Promise<bigint>;
getBalance: (address: string) => Promise<bigint>;
lastBlock: () => Promise<BlockInfo>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions libs/coin-modules/coin-polkadot/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
const extrinsicArg = defaultExtrinsicArg(transaction.amount, transaction.recipient);
Expand Down
46 changes: 44 additions & 2 deletions libs/coin-modules/coin-stellar/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import coinConfig, { type StellarConfig } from "../config";
import {
// broadcast,
// combine,
// craftTransaction,
craftTransaction,
// estimateFees,
getBalance,
listOperations,
// lastBlock,
// rawEncode,
} from "../logic";

Expand All @@ -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<string> {
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;
}
3 changes: 1 addition & 2 deletions libs/coin-modules/coin-stellar/src/logic/craftTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function craftTransaction(
address: string;
},
transaction: {
mode: "send" | "changeTrust";
mode: string;
recipient: string;
amount: bigint;
fee: bigint;
Expand All @@ -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);

Expand Down
1 change: 1 addition & 0 deletions libs/coin-modules/coin-tezos/src/api/index.integ.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions libs/coin-modules/coin-xrp/src/api/index.integ.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions libs/coin-modules/coin-xrp/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function createApi(config: XrpConfig): Api {
async function craft(
address: string,
transaction: {
mode: string;
recipient: string;
amount: bigint;
fee: bigint;
Expand Down

0 comments on commit f9b64f6

Please sign in to comment.