Skip to content

Commit

Permalink
feat: map dollar to all tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
euharrison committed Feb 5, 2025
1 parent 8b4b923 commit d245b7b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
19 changes: 12 additions & 7 deletions apps/namadillo/src/atoms/prices/functions.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import { IbcToken, NativeToken } from "@namada/indexer-client";
import BigNumber from "bignumber.js";
import * as osmosis from "chain-registry/mainnet/osmosis";
import { Address } from "types";
import { Address, BaseDenom } from "types";
import { findAssetByToken } from "utils/assets";
import { fetchCoinPrices } from "./services";

export const fetchTokenPrices = async (
tokenAddressToFetch: Address[],
apiTokens: (NativeToken | IbcToken)[]
chainTokens: (NativeToken | IbcToken)[]
): Promise<Record<Address, BigNumber>> => {
const baseMap: Record<string, string> = {};
const baseMap: Record<BaseDenom, Address[]> = {};
tokenAddressToFetch.forEach((address) => {
const token = apiTokens?.find((t) => t.address === address);
const token = chainTokens?.find((t) => t.address === address);
if (token) {
// searching only on osmosis because these are the assets supported by fetchCoinPrices
const asset = findAssetByToken(token, osmosis.assets.assets);
if (asset) {
baseMap[asset.base] = address;
if (baseMap[asset.base]) {
baseMap[asset.base].push(address);
} else {
baseMap[asset.base] = [address];
}
}
}
});
Expand All @@ -25,10 +29,11 @@ export const fetchTokenPrices = async (

const tokenPrices: Record<string, BigNumber> = {};
Object.entries(apiResponse).forEach(([base, value]) => {
const address = baseMap[base];
const dollar = Object.values(value)[0];
if (dollar) {
tokenPrices[address] = new BigNumber(dollar);
baseMap[base].forEach((address) => {
tokenPrices[address] = new BigNumber(dollar);
});
}
});
return tokenPrices;
Expand Down
4 changes: 3 additions & 1 deletion apps/namadillo/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ export type PublicKey = string;

export type Address = string;

export type BaseDenom = string;

export type ChainId = string;

export type GasLimit = BigNumber;

export type GasPrice = BigNumber;

// For Namada chain, it should be the address. For Ibc, it should be the base denom
export type GasToken = Address | string;
export type GasToken = Address | BaseDenom;

export type AddressBalance = Record<Address, BigNumber>;

Expand Down

0 comments on commit d245b7b

Please sign in to comment.