Skip to content

Commit

Permalink
refactor fetching balances
Browse files Browse the repository at this point in the history
  • Loading branch information
boscohyun committed May 31, 2024
1 parent 59d7d9a commit 9c0ace1
Showing 1 changed file with 44 additions and 21 deletions.
65 changes: 44 additions & 21 deletions pages/[network]/avatar/[address].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import type { NextPage, GetServerSideProps } from "next";
import { getBalance } from "../../../apiClient";
import { getNetworkType, getNodeType } from "../../../utils/network";
import { getSdk } from "../../../utils/mimirGraphQLClient";
import { NetworkType, NodeType } from "../../../constants/network";

const CURRENCY_TICKERS = [
const AGENT_CURRENCY_TICKERS = [
"CRYSTAL",
];
const AVATAR_CURRENCY_TICKERS = [
"RUNESTONE_FENRIR1",
"RUNESTONE_FENRIR2",
"RUNESTONE_FENRIR3",
Expand All @@ -19,7 +22,7 @@ const CURRENCY_TICKERS = [
"SOULSTONE_1002",
"SOULSTONE_1003",
"SOULSTONE_1004",
] as const;
];

interface FAV {
ticker: string;
Expand Down Expand Up @@ -128,24 +131,16 @@ export const getServerSideProps: GetServerSideProps<AvatarPageProps> = async (
const avatarJsonObj = await sdk.avatar(address);
const inventoryJsonObj = avatarJsonObj.inventory;
const inventoryObj = parseToInventory(inventoryJsonObj);
const balanceJsonObjs = avatarJsonObj.agentAddress === null
? []
: await Promise.all(
CURRENCY_TICKERS.map(
(currencyTicker, index) => getBalance(
nodeType,
networkType,
index <= 0 ? avatarJsonObj.agentAddress : address,
currencyTicker
)
)
);
const balanceObjs = balanceJsonObjs.map((resp, index) => {
return {
ticker: CURRENCY_TICKERS[index],
amount: resp === null ? 0 : parseFloat(resp.quantity),
};
});
const agentBalanceJsonObjs = await getBalances(
nodeType,
networkType,
AGENT_CURRENCY_TICKERS,
avatarJsonObj.agentAddress);
const avatarBalanceJsonObjs = await getBalances(
nodeType,
networkType,
AVATAR_CURRENCY_TICKERS,
address);

function parseToInventory(inventoryJsonObj: any | null): Inventory {
if (inventoryJsonObj === null) {
Expand Down Expand Up @@ -176,14 +171,42 @@ export const getServerSideProps: GetServerSideProps<AvatarPageProps> = async (
};
}

async function getBalances(
nodeType: NodeType,
networkType: NetworkType,
tickers: string[],
address: any | null
): Promise<FAV[]> {
if (address === null) {
return [];
}

const balanceJsonObjects = await Promise.all(
tickers.map(
ticker => getBalance(
nodeType,
networkType,
address,
ticker
)
)
);
return balanceJsonObjects.map((resp, index) => {
return {
ticker: tickers[index],
amount: resp === null ? 0 : parseFloat(resp.quantity),
};
});
}

return {
props: {
avatar: {
address: avatarJsonObj.address,
name: avatarJsonObj.name,
actionPoint: avatarJsonObj.actionPoint,
level: avatarJsonObj.level,
favs: balanceObjs,
favs: agentBalanceJsonObjs.concat(avatarBalanceJsonObjs),
inventory: inventoryObj,
},
},
Expand Down

0 comments on commit 9c0ace1

Please sign in to comment.