Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dollar support for multiple channels #1609

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 5 additions & 31 deletions apps/namadillo/src/atoms/balance/functions.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,16 @@
import { IbcToken, NativeToken } from "@namada/indexer-client";
import { nativeTokenAddressAtom } from "atoms/chain";
import { mapCoinsToAssets } from "atoms/integrations";
import BigNumber from "bignumber.js";
import { DenomTrace } from "cosmjs-types/ibc/applications/transfer/v1/transfer";
import { getDefaultStore } from "jotai";
import { AddressWithAssetAndAmountMap } from "types";
import { isNamadaAsset } from "utils";
import { TokenBalance } from "./atoms";

const getNativeTokenAddress = (): string | undefined => {
const { get } = getDefaultStore();
return get(nativeTokenAddressAtom).data;
};

/**
* Sum the dollar amount of a list of tokens
* @param tokens
* @returns The total of dollars, or `undefined` if at least one token has `dollar: undefined`
*/
export const sumDollars = (tokens: TokenBalance[]): BigNumber | undefined => {
let sum = new BigNumber(0);
for (let i = 0; i < tokens.length; i++) {
const { dollar, originalAddress } = tokens[i];
if (!dollar) {
// create an exception for native token while we don't have a market price for it
// we can safely delete this condition when osmosis api returns a native token price
if (originalAddress === getNativeTokenAddress()) {
continue;
}
return undefined;
}
sum = sum.plus(dollar);
}
return sum;
};

export const getTotalDollar = (list?: TokenBalance[]): BigNumber | undefined =>
sumDollars(list ?? []);
export const getTotalDollar = (list?: TokenBalance[]): BigNumber =>
(list ?? []).reduce(
(sum, { dollar }) => (dollar ? sum.plus(dollar) : sum),
new BigNumber(0)
);

export const getTotalNam = (list?: TokenBalance[]): BigNumber =>
list?.find((i) => isNamadaAsset(i.asset))?.amount ?? new BigNumber(0);
Expand Down
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