Skip to content

Commit

Permalink
Merge pull request #36 from planetarium/use-mimir-for-avatar
Browse files Browse the repository at this point in the history
feat: use mimir APIs to get balances
  • Loading branch information
boscohyun authored May 22, 2024
2 parents 85df429 + 813c7e0 commit 4025835
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 51 deletions.
16 changes: 16 additions & 0 deletions apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ export async function getSheet(
});
}

export async function getBalance(
nodeType: NodeType,
networkType: NetworkType,
address: string,
currencyTicker: string,
): Promise<any | null> {
try {
return await fetchAPI<any>(
nodeType,
`${networkType}/balances/${address}/${currencyTicker}`
);
} catch (error) {
return null;
}
}

export async function getAvatar(
nodeType: NodeType,
networkType: NetworkType,
Expand Down
71 changes: 20 additions & 51 deletions pages/[network]/avatar/[address].tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import type { NextPage, GetServerSideProps } from "next";
import { getAvatar, getAvatarInventory } from "../../../apiClient";
import { networkToSDK } from "../../../sdk";
import { CurrencyInput } from "../../../generated/graphql-request";
import { getBalance, getAvatar, getAvatarInventory } from "../../../apiClient";
import { getNetworkType, getNodeType } from "../../../utils/network";

const CURRENCIES: CurrencyInput[] = [
{
ticker: "CRYSTAL",
decimalPlaces: 18,
minters: [],
},
];

const TICKERS = [
const CURRENCY_TICKERS = [
"CRYSTAL",
"RUNESTONE_FENRIR1",
"RUNESTONE_FENRIR2",
"RUNESTONE_FENRIR3",
Expand All @@ -28,13 +19,6 @@ const TICKERS = [
"SOULSTONE_1003",
"SOULSTONE_1004",
] as const;
for (const ticker of TICKERS) {
CURRENCIES.push({
ticker: ticker,
decimalPlaces: 0,
minters: [],
});
}

interface FAV {
ticker: string;
Expand Down Expand Up @@ -127,7 +111,7 @@ export const getServerSideProps: GetServerSideProps<AvatarPageProps> = async (
context
) => {
const network = context.query.network;
if (typeof(network) !== "string") {
if (typeof (network) !== "string") {
throw new Error("Network parameter is not a string.");
}

Expand All @@ -136,40 +120,25 @@ export const getServerSideProps: GetServerSideProps<AvatarPageProps> = async (
throw new Error("Address parameter is not a string.");
}

const sdk = networkToSDK(network);

const blockIndexString = context.query.blockIndex;
const blockIndex =
blockIndexString === undefined ? -1 : Number(blockIndexString);
const hash = (
await sdk.GetBlockHashByBlockIndex({
index: blockIndex as unknown as string, // Break assumption ID must be string.
})
).chainQuery.blockQuery?.block?.hash;

const avatarJsonObj = await getAvatar(
getNodeType(network),
getNetworkType(network),
address);
const inventoryJsonObj = await getAvatarInventory(
getNodeType(network),
getNetworkType(network),
address);
const nodeType = getNodeType(network);
const networkType = getNetworkType(network);
const avatarJsonObj = await getAvatar(nodeType, networkType, address);
const inventoryJsonObj = await getAvatarInventory(nodeType, networkType, address);
const inventoryObj = parseToInventory(inventoryJsonObj);

const fetchedFavs = await Promise.all(
CURRENCIES.map(
(currency) => sdk.GetBalance({
currency: currency,
address: currency.ticker === "CRYSTAL" ? avatarJsonObj.agentAddress : address,
hash: hash,
})
const balanceJsonObjs = await Promise.all(
CURRENCY_TICKERS.map(
(currencyTicker, index) => getBalance(
nodeType,
networkType,
index <= 0 ? avatarJsonObj.agentAddress : address,
currencyTicker
)
)
);
const favs = fetchedFavs.map((resp, index) => {
const balanceObjs = balanceJsonObjs.map((resp, index) => {
return {
ticker: CURRENCIES[index].ticker,
amount: parseFloat(resp.stateQuery.balance.quantity),
ticker: CURRENCY_TICKERS[index],
amount: resp === null ? 0 : parseFloat(resp.quantity),
};
});

Expand Down Expand Up @@ -210,7 +179,7 @@ export const getServerSideProps: GetServerSideProps<AvatarPageProps> = async (
name: avatarJsonObj.avatarName,
actionPoint: avatarJsonObj.actionPoint,
level: avatarJsonObj.level,
favs,
favs: balanceObjs,
inventory: inventoryObj,
},
},
Expand Down

0 comments on commit 4025835

Please sign in to comment.