From c40220575b6c2a9833c40042b311bc52a059e816 Mon Sep 17 00:00:00 2001 From: Owen Voke Date: Thu, 30 May 2024 09:37:15 +0100 Subject: [PATCH] feat: add asset functions --- src/api/getAddress.test.ts | 51 +++++++++++++++++++++++++ src/api/getAlias.ts | 8 ++-- src/api/getAssetBalance.ts | 33 ++++++++++++++++ src/api/getAssetOrders.ts | 29 ++++++++++++++ src/api/getAssets.ts | 25 ++++++++++++ src/api/getBalance.ts | 12 +++--- src/api/getBlockTransactions.ts | 12 +++--- src/api/getPendingBalance.ts | 8 ++-- src/api/getTransactions.ts | 12 +++--- src/api/models/account-keypair.model.ts | 8 ++++ src/api/models/asset-balance.model.ts | 15 ++++++++ src/api/models/asset-order.model.ts | 29 ++++++++++++++ src/api/models/asset.model.ts | 27 +++++++++++++ src/api/models/index.ts | 3 ++ 14 files changed, 242 insertions(+), 30 deletions(-) create mode 100644 src/api/getAddress.test.ts create mode 100644 src/api/getAssetBalance.ts create mode 100644 src/api/getAssetOrders.ts create mode 100644 src/api/getAssets.ts create mode 100644 src/api/models/asset-balance.model.ts create mode 100644 src/api/models/asset-order.model.ts create mode 100644 src/api/models/asset.model.ts diff --git a/src/api/getAddress.test.ts b/src/api/getAddress.test.ts new file mode 100644 index 0000000..3f2f9e4 --- /dev/null +++ b/src/api/getAddress.test.ts @@ -0,0 +1,51 @@ +import { http, HttpResponse } from "msw"; +import { setupServer } from "msw/node"; + +import { buildNodeConfiguration } from "../utils/public"; +import { getAddress } from "./getAddress"; + +const server = setupServer(); + +describe("Function: getAddress", () => { + // MSW Setup + beforeAll(() => server.listen()); + afterEach(() => server.resetHandlers()); + afterAll(() => server.close()); + + it("returns address for valid ", async () => { + // ARRANGE + const nodeConfiguration = buildNodeConfiguration({ + url: "http://localhost", + }); + + const mockResponse = { + status: "ok", + data: "2z2jCebUcMDWxsvVXxSjGCEspg3sobCoQQQJRZY3g6985uhLJKEAJ3ofcUeHU9rpN9dWYirNVGmneiaiYwb7Jxh", + coin: "arionum", + }; + + let requestUrl = ""; + + server.use( + http.get(`http://localhost/api.php`, (info) => { + requestUrl = info.request.url; + return HttpResponse.json(mockResponse); + }), + ); + + // ACT + const response = await getAddress(nodeConfiguration, { + publicKey: + "PZ8Tyr4Nx8MHsRAGMpZmZ6TWY63dXWSCy82Bs7eNrGVcNW1p9cgZMe954tCXagAhmKqd1VtjRzwBYCFdh8KuwXqDiX58nRhU4CGGXcRDzFEf9ttUMQjGFjzv", + }); + + // ASSERT + expect(requestUrl).toContain( + "public_key=PZ8Tyr4Nx8MHsRAGMpZmZ6TWY63dXWSCy82Bs7eNrGVcNW1p9cgZMe954tCXagAhmKqd1VtjRzwBYCFdh8KuwXqDiX58nRhU4CGGXcRDzFEf9ttUMQjGFjzv", + ); + + expect(response).toEqual( + "2z2jCebUcMDWxsvVXxSjGCEspg3sobCoQQQJRZY3g6985uhLJKEAJ3ofcUeHU9rpN9dWYirNVGmneiaiYwb7Jxh", + ); + }); +}); diff --git a/src/api/getAlias.ts b/src/api/getAlias.ts index 25622f4..bb1b080 100644 --- a/src/api/getAlias.ts +++ b/src/api/getAlias.ts @@ -12,13 +12,11 @@ export const getAlias = async ( q: "getAlias", }; - if (!publicKey && !address) { - throw new Error("At least one account identifier must be provided."); - } - if (publicKey) { queryParams.public_key = publicKey; - } else if (address) { + } + + if (address) { queryParams.account = address; } diff --git a/src/api/getAssetBalance.ts b/src/api/getAssetBalance.ts new file mode 100644 index 0000000..e3fedc0 --- /dev/null +++ b/src/api/getAssetBalance.ts @@ -0,0 +1,33 @@ +import { NodeConfiguration } from "../utils/public"; +import { buildRequestUrl } from "../utils/internal"; +import { call } from "../utils/internal"; +import { AssetBalance, assetBalanceFromApi } from "./models"; + +export const getAssetBalance = async ( + nodeConfiguration: NodeConfiguration, + payload: { address?: string; asset?: string; publicKey?: string }, +): Promise> => { + const { address, asset, publicKey } = payload; + + const queryParams: Record = { + q: "asset-orders", + }; + + if (publicKey) { + queryParams.public_key = publicKey; + } + + if (address) { + queryParams.account = address; + } + + if (asset) { + queryParams.asset = asset; + } + + const url = buildRequestUrl(nodeConfiguration.url, "/api.php", queryParams); + + return (await call>({ url })).map((data: any) => + assetBalanceFromApi(data), + ); +}; diff --git a/src/api/getAssetOrders.ts b/src/api/getAssetOrders.ts new file mode 100644 index 0000000..0e3e078 --- /dev/null +++ b/src/api/getAssetOrders.ts @@ -0,0 +1,29 @@ +import { NodeConfiguration } from "../utils/public"; +import { buildRequestUrl } from "../utils/internal"; +import { call } from "../utils/internal"; +import { AssetOrder, assetOrderFromApi } from "./models"; + +export const getAssetOrders = async ( + nodeConfiguration: NodeConfiguration, + payload: { address?: string; asset?: string }, +): Promise> => { + const { address, asset } = payload; + + const queryParams: Record = { + q: "asset-orders", + }; + + if (address) { + queryParams.account = address; + } + + if (asset) { + queryParams.asset = asset; + } + + const url = buildRequestUrl(nodeConfiguration.url, "/api.php", queryParams); + + return (await call>({ url })).map((data: any) => + assetOrderFromApi(data), + ); +}; diff --git a/src/api/getAssets.ts b/src/api/getAssets.ts new file mode 100644 index 0000000..eb61400 --- /dev/null +++ b/src/api/getAssets.ts @@ -0,0 +1,25 @@ +import { NodeConfiguration } from "../utils/public"; +import { buildRequestUrl } from "../utils/internal"; +import { call } from "../utils/internal"; +import { Asset, assetFromApi } from "./models"; + +export const getAssets = async ( + nodeConfiguration: NodeConfiguration, + payload: { asset?: string }, +): Promise> => { + const { asset } = payload; + + const queryParams: Record = { + q: "assets", + }; + + if (asset) { + queryParams.asset = asset; + } + + const url = buildRequestUrl(nodeConfiguration.url, "/api.php", queryParams); + + return (await call>({ url })).map((data: any) => + assetFromApi(data), + ); +}; diff --git a/src/api/getBalance.ts b/src/api/getBalance.ts index 80a4e55..adb42dc 100644 --- a/src/api/getBalance.ts +++ b/src/api/getBalance.ts @@ -12,15 +12,15 @@ export const getBalance = async ( q: "getBalance", }; - if (!publicKey && !address && !alias) { - throw new Error("At least one account identifier must be provided."); - } - if (publicKey) { queryParams.public_key = publicKey; - } else if (address) { + } + + if (address) { queryParams.account = address; - } else if (alias) { + } + + if (alias) { queryParams.alias = alias; } diff --git a/src/api/getBlockTransactions.ts b/src/api/getBlockTransactions.ts index 8519cc4..1f075af 100644 --- a/src/api/getBlockTransactions.ts +++ b/src/api/getBlockTransactions.ts @@ -17,13 +17,11 @@ export const getBlockTransactions = async ( q: "getBlockTransactions", }; - if (!height && !blockId) { - throw new Error("At least one block identifier must be provided."); - } - if (height) { queryParams.height = height; - } else if (blockId) { + } + + if (blockId) { queryParams.block = blockId; } @@ -33,7 +31,7 @@ export const getBlockTransactions = async ( const url = buildRequestUrl(nodeConfiguration.url, "/api.php", queryParams); - return (await call>({ url })).map((transaction: any) => - transactionFromApi(transaction), + return (await call>({ url })).map((data: any) => + transactionFromApi(data), ); }; diff --git a/src/api/getPendingBalance.ts b/src/api/getPendingBalance.ts index d5af47e..69db0f9 100644 --- a/src/api/getPendingBalance.ts +++ b/src/api/getPendingBalance.ts @@ -12,13 +12,11 @@ export const getPendingBalance = async ( q: "getPendingBalance", }; - if (!publicKey && !address) { - throw new Error("At least one account identifier must be provided."); - } - if (publicKey) { queryParams.public_key = publicKey; - } else if (address) { + } + + if (address) { queryParams.account = address; } diff --git a/src/api/getTransactions.ts b/src/api/getTransactions.ts index cf858e3..8a2c1bf 100644 --- a/src/api/getTransactions.ts +++ b/src/api/getTransactions.ts @@ -13,13 +13,11 @@ export const getTransactions = async ( q: "getTransactions", }; - if (!publicKey && !account) { - throw new Error("At least one account identifier must be provided."); - } - if (publicKey) { queryParams.public_key = publicKey; - } else if (account) { + } + + if (account) { queryParams.account = account; } @@ -29,7 +27,7 @@ export const getTransactions = async ( const url = buildRequestUrl(nodeConfiguration.url, "/api.php", queryParams); - return (await call>({ url })).map((transaction: any) => - transactionFromApi(transaction), + return (await call>({ url })).map((data: any) => + transactionFromApi(data), ); }; diff --git a/src/api/models/account-keypair.model.ts b/src/api/models/account-keypair.model.ts index 391dda6..6aa2ffe 100644 --- a/src/api/models/account-keypair.model.ts +++ b/src/api/models/account-keypair.model.ts @@ -3,3 +3,11 @@ export interface AccountKeypair { publicKey: string; privateKey: string; } + +export const accountKeypairFromApi = (data: any) => { + return { + address: data.address, + publicKey: data.public_key, + privateKey: data.private_key, + }; +}; diff --git a/src/api/models/asset-balance.model.ts b/src/api/models/asset-balance.model.ts new file mode 100644 index 0000000..4b8e13d --- /dev/null +++ b/src/api/models/asset-balance.model.ts @@ -0,0 +1,15 @@ +export interface AssetBalance { + asset: string; + alias: string; + address: string; + balance: number; +} + +export const assetBalanceFromApi = (data: any): AssetBalance => { + return { + asset: data.asset, + alias: data.alias, + address: data.account, + balance: data.balance, + }; +}; diff --git a/src/api/models/asset-order.model.ts b/src/api/models/asset-order.model.ts new file mode 100644 index 0000000..6ac47e9 --- /dev/null +++ b/src/api/models/asset-order.model.ts @@ -0,0 +1,29 @@ +export type AssetOrderType = "bid"; + +export interface AssetOrder { + id: string; + address: string; + asset: string; + price: number; + date: number; + status: boolean; + type: AssetOrderType; + value: number; + valueDone: number; + cancelable: boolean; +} + +export const assetOrderFromApi = (data: any): AssetOrder => { + return { + id: data.id, + address: data.account, + asset: data.asset, + price: data.price, + date: data.date, + status: data.status, + type: data.type, + value: data.val, + valueDone: data.val_done, + cancelable: data.cancelable, + }; +}; diff --git a/src/api/models/asset.model.ts b/src/api/models/asset.model.ts new file mode 100644 index 0000000..d66d116 --- /dev/null +++ b/src/api/models/asset.model.ts @@ -0,0 +1,27 @@ +export interface Asset { + id: string; + maxSupply: number; + tradeable: boolean; + price: number; + dividendOnly: boolean; + autoDividend: boolean; + allowBids: boolean; + blockHeight: number; + alias: string | null; + balance: number; +} + +export const assetFromApi = (data: any): Asset => { + return { + id: data.id, + maxSupply: data.max_supply, + tradeable: data.tradable, + price: data.price, + dividendOnly: data.dividend_only, + autoDividend: data.auto_dividend, + allowBids: data.allow_bid, + blockHeight: data.height, + alias: data.alias, + balance: data.balance, + }; +}; diff --git a/src/api/models/index.ts b/src/api/models/index.ts index 1c682f2..e002e1f 100644 --- a/src/api/models/index.ts +++ b/src/api/models/index.ts @@ -1,4 +1,7 @@ export * from "./account-keypair.model"; +export * from "./asset.model"; +export * from "./asset-balance.model"; +export * from "./asset-order.model"; export * from "./block.model"; export * from "./masternode.model"; export * from "./node-info.model";