Skip to content

Commit

Permalink
feat: add asset functions
Browse files Browse the repository at this point in the history
  • Loading branch information
owenvoke committed May 30, 2024
1 parent 0b7e13c commit c402205
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 30 deletions.
51 changes: 51 additions & 0 deletions src/api/getAddress.test.ts
Original file line number Diff line number Diff line change
@@ -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",
);
});
});
8 changes: 3 additions & 5 deletions src/api/getAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
33 changes: 33 additions & 0 deletions src/api/getAssetBalance.ts
Original file line number Diff line number Diff line change
@@ -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<Array<AssetBalance>> => {
const { address, asset, publicKey } = payload;

const queryParams: Record<string, number | string> = {
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<Array<any>>({ url })).map((data: any) =>
assetBalanceFromApi(data),
);
};
29 changes: 29 additions & 0 deletions src/api/getAssetOrders.ts
Original file line number Diff line number Diff line change
@@ -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<Array<AssetOrder>> => {
const { address, asset } = payload;

const queryParams: Record<string, number | string> = {
q: "asset-orders",
};

if (address) {
queryParams.account = address;
}

if (asset) {
queryParams.asset = asset;
}

const url = buildRequestUrl(nodeConfiguration.url, "/api.php", queryParams);

return (await call<Array<any>>({ url })).map((data: any) =>
assetOrderFromApi(data),
);
};
25 changes: 25 additions & 0 deletions src/api/getAssets.ts
Original file line number Diff line number Diff line change
@@ -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<Array<Asset>> => {
const { asset } = payload;

const queryParams: Record<string, number | string> = {
q: "assets",
};

if (asset) {
queryParams.asset = asset;
}

const url = buildRequestUrl(nodeConfiguration.url, "/api.php", queryParams);

return (await call<Array<any>>({ url })).map((data: any) =>
assetFromApi(data),
);
};
12 changes: 6 additions & 6 deletions src/api/getBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
12 changes: 5 additions & 7 deletions src/api/getBlockTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -33,7 +31,7 @@ export const getBlockTransactions = async (

const url = buildRequestUrl(nodeConfiguration.url, "/api.php", queryParams);

return (await call<Array<any>>({ url })).map((transaction: any) =>
transactionFromApi(transaction),
return (await call<Array<any>>({ url })).map((data: any) =>
transactionFromApi(data),
);
};
8 changes: 3 additions & 5 deletions src/api/getPendingBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
12 changes: 5 additions & 7 deletions src/api/getTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -29,7 +27,7 @@ export const getTransactions = async (

const url = buildRequestUrl(nodeConfiguration.url, "/api.php", queryParams);

return (await call<Array<any>>({ url })).map((transaction: any) =>
transactionFromApi(transaction),
return (await call<Array<any>>({ url })).map((data: any) =>
transactionFromApi(data),
);
};
8 changes: 8 additions & 0 deletions src/api/models/account-keypair.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
};
15 changes: 15 additions & 0 deletions src/api/models/asset-balance.model.ts
Original file line number Diff line number Diff line change
@@ -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,
};
};
29 changes: 29 additions & 0 deletions src/api/models/asset-order.model.ts
Original file line number Diff line number Diff line change
@@ -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,
};
};
27 changes: 27 additions & 0 deletions src/api/models/asset.model.ts
Original file line number Diff line number Diff line change
@@ -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,
};
};
3 changes: 3 additions & 0 deletions src/api/models/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down

0 comments on commit c402205

Please sign in to comment.