Skip to content
This repository has been archived by the owner on Sep 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #427 from TosiDrop/fix/token-info
Browse files Browse the repository at this point in the history
fix: fix destructure failure when claim
  • Loading branch information
reqlez authored Apr 26, 2023
2 parents 5889345 + 61beaac commit 54f9a45
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ node_modules
npm-debug.log*
yarn-debug.log*
yarn-error.log*
*.log
6 changes: 0 additions & 6 deletions client/src/services/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { PopUpInfo } from "src/entities/common.entities";
import { Dto, GetPoolsDto, GetQueueDto } from "src/entities/dto";
import { EpochParams, Tip } from "src/entities/koios.entities";
import { ProjectData } from "src/entities/project.entities";
import { GetTokens } from "src/entities/vm.entities";

export async function getFeatures() {
const response = await axios.get(`/features`);
Expand Down Expand Up @@ -75,11 +74,6 @@ export async function getQueue(): Promise<GetQueueDto> {
return response.data;
}

export async function getTokens(): Promise<GetTokens> {
const response = await axios.get(`/api/gettokens`);
return response.data;
}

export async function createStakeTx(
params: Dto.CreateDelegationTx["body"]
): Promise<Dto.CreateDelegationTx["response"]> {
Expand Down
14 changes: 2 additions & 12 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import TxRouter from "./routes/tx";
import UtilRouter from "./routes/util";
import {
CardanoNetwork,
ITosiFeatures,
getAccountsInfo,
getDeliveredRewards,
getEpochParams,
Expand All @@ -33,14 +34,12 @@ import {
getPoolMetadata,
getPools,
getRewards,
getTokens,
ITosiFeatures,
postFromKoios,
sanitizeString,
translateAdaHandle,
} from "./utils";
import { ICustomRewards } from "./utils/entities";
import { createErrorWithCode, HttpStatusCode } from "./utils/error";
import { HttpStatusCode, createErrorWithCode } from "./utils/error";
require("dotenv").config();
const openapi = require("@reqlez/express-openapi");
const fs = require("fs");
Expand Down Expand Up @@ -147,15 +146,6 @@ app.get(
})
);

app.get(
"/api/gettokens",
oapi.path(resp200Ok),
errorHandlerWrapper(async (_req: Request, res: Response) => {
const tokens = await getTokens();
return res.status(200).send(tokens);
})
);

app.get(
"/api/getsettings",
oapi.path(resp200Ok),
Expand Down
39 changes: 32 additions & 7 deletions server/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from "../../client/src/entities/vm.entities";
import { MinswapService } from "../service/minswap";
import { longTermCache, shortTermCache } from "./cache";
import { createErrorWithCode, HttpStatusCode } from "./error";
import { HttpStatusCode, createErrorWithCode } from "./error";

require("dotenv").config();

Expand Down Expand Up @@ -156,12 +156,24 @@ export async function getPools() {
return pools;
}

export async function getTokens(): Promise<VmTokenInfoMap> {
let tokenInfo = longTermCache.get<VmTokenInfoMap>("tokenInfo");
if (tokenInfo == null) {
export async function getTokens(options?: {
flushCache?: boolean;
}): Promise<VmTokenInfoMap> {
let tokenInfo: VmTokenInfoMap;

if (options?.flushCache) {
tokenInfo = await getFromVM<VmTokenInfoMap>("get_tokens");
longTermCache.set("tokenInfo", tokenInfo);
} else {
const tempTokenInfo = longTermCache.get<VmTokenInfoMap>("tokenInfo");
if (tempTokenInfo == null) {
tokenInfo = await getFromVM<VmTokenInfoMap>("get_tokens");
longTermCache.set("tokenInfo", tokenInfo);
} else {
tokenInfo = tempTokenInfo;
}
}

return tokenInfo;
}

Expand Down Expand Up @@ -213,7 +225,7 @@ export async function getPoolMetadata(accountInfo: any) {
}

export async function getRewards(stakeAddress: string) {
const [getRewardsResponse, tokens, prices] = await Promise.all([
let [getRewardsResponse, tokens, prices] = await Promise.all([
getFromVM<GetRewardsDto>(`get_rewards&staking_address=${stakeAddress}`),
getTokens(),
MinswapService.getPrices(),
Expand Down Expand Up @@ -254,9 +266,21 @@ export async function getRewards(stakeAddress: string) {
}
});

/** if there is no token info in the map, flush the cache and re-fetch token info */
for (const assetId of [
...Object.keys(consolidatedAvailableReward),
...Object.keys(consolidatedAvailableRewardPremium),
]) {
const token = tokens[assetId];
if (token == null) {
tokens = await getTokens({ flushCache: true });
}
}

Object.keys(consolidatedAvailableReward).forEach((assetId) => {
const token = tokens[assetId];
const { decimals: tokenDecimals, logo, ticker } = token;
/** add default values just to be safe */
const { decimals: tokenDecimals = 0, logo = "", ticker = "" } = token;
const decimals = Number(tokenDecimals);
const amount =
consolidatedAvailableReward[assetId] / Math.pow(10, decimals);
Expand All @@ -277,7 +301,8 @@ export async function getRewards(stakeAddress: string) {

Object.keys(consolidatedAvailableRewardPremium).forEach((assetId) => {
const token = tokens[assetId];
const { decimals: tokenDecimals, logo, ticker } = token;
/** add default values just to be safe */
const { decimals: tokenDecimals = 0, logo = "", ticker = "" } = token;
const decimals = Number(tokenDecimals);
const amount =
consolidatedAvailableRewardPremium[assetId] / Math.pow(10, decimals);
Expand Down

0 comments on commit 54f9a45

Please sign in to comment.