Skip to content

Commit

Permalink
feat: add static chains config
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Sep 13, 2024
1 parent 5901f3e commit 402e4f5
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 7 deletions.
8 changes: 6 additions & 2 deletions apps/namadillo/src/atoms/accounts/atoms.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Account } from "@namada/types";
import { indexerApiAtom } from "atoms/api";
import { nativeTokenAddressAtom } from "atoms/chain";
import { chainConfigAtom, nativeTokenAddressAtom } from "atoms/chain";
import { shouldUpdateBalanceAtom } from "atoms/etc";
import { namadaExtensionConnectedAtom } from "atoms/settings";
import { queryDependentFn } from "atoms/utils";
Expand Down Expand Up @@ -35,6 +35,7 @@ export const accountBalanceAtom = atomWithQuery<BigNumber>((get) => {
const tokenAddress = get(nativeTokenAddressAtom);
const enablePolling = get(shouldUpdateBalanceAtom);
const api = get(indexerApiAtom);
const chainConfig = get(chainConfigAtom("namada"));

return {
// TODO: subscribe to indexer events when it's done
Expand All @@ -44,7 +45,10 @@ export const accountBalanceAtom = atomWithQuery<BigNumber>((get) => {
return await fetchAccountBalance(
api,
defaultAccount.data,
tokenAddress.data!
tokenAddress.data!,
// As this is a nam balance specific atom, we can safely assume that the
// first currency is the native token
chainConfig.currencies[0].coinDecimals
);
}, [tokenAddress, defaultAccount]),
};
Expand Down
13 changes: 8 additions & 5 deletions apps/namadillo/src/atoms/accounts/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@ export const fetchDefaultAccount = async (): Promise<Account | undefined> => {
export const fetchAccountBalance = async (
api: DefaultApi,
account: Account | undefined,
tokenAddress: string
tokenAddress: string,
decimals: number
): Promise<BigNumber> => {
if (!account) return BigNumber(0);
const balancesResponse = await api.apiV1AccountAddressGet(account.address);

const balances = balancesResponse.data
const balance = balancesResponse.data
// TODO: add filter to the api call
.filter(({ tokenAddress: ta }) => ta === tokenAddress)
.map(({ tokenAddress, balance }) => {
return {
token: tokenAddress,
amount: balance,
};
});
})
.at(0);

if (balances.length === 0) return BigNumber(0);
return new BigNumber(balances[0].amount || 0);
return balance ?
BigNumber(balance.amount).shiftedBy(-decimals)
: BigNumber(0);
};
7 changes: 7 additions & 0 deletions apps/namadillo/src/atoms/chain/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {
rpcUrlAtom,
} from "atoms/settings";
import { queryDependentFn } from "atoms/utils";
import { atom } from "jotai";
import { atomWithQuery } from "jotai-tanstack-query";
import { atomFamily } from "jotai/utils";
import { ChainConfig, chainConfigByName } from "registry";
import { ChainParameters, ChainSettings } from "types";
import { fetchChainParameters, fetchRpcUrlFromIndexer } from "./services";

Expand Down Expand Up @@ -73,3 +76,7 @@ export const chainParametersAtom = atomWithQuery<ChainParameters>((get) => {
queryFn: async () => fetchChainParameters(api),
};
});

export const chainConfigAtom = atomFamily((chainName: string) =>
atom<ChainConfig>(() => chainConfigByName(chainName))
);
22 changes: 22 additions & 0 deletions apps/namadillo/src/registry/cosmoshub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"chainName": "Cosmos Hub",
"currencies": [
{
"coinDecimals": 6,
"coinDenom": "ATOM",
"coinMinimalDenom": "uatom"
}
],
"feeCurrencies": [
{
"coinDecimals": 6,
"coinDenom": "ATOM",
"coinMinimalDenom": "uatom"
}
],
"stakeCurrency": {
"coinDecimals": 6,
"coinDenom": "ATOM",
"coinMinimalDenom": "uatom"
}
}
47 changes: 47 additions & 0 deletions apps/namadillo/src/registry/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import cosmoshub from "./cosmoshub.json";
import namada from "./namada.json";

type MinimalDenom = string;
type ConfigName = string;

type Currency = {
coinDecimals: number;
coinDenom: string;
coinMinimalDenom: string;
};

export type ChainConfig = {
currencies: Currency[];
feeCurrencies: Currency[];
stakeCurrency: Currency;
};

const loadedConfigs: ChainConfig[] = [];
const minimalDenomMap: Map<string, number> = new Map();
const nameMap: Map<string, number> = new Map();

export function chainConfigByMinDenom(minDenom: string): ChainConfig {
// We assume that the map always has the key
const index = minimalDenomMap.get(minDenom)!;
return loadedConfigs[index];
}

export function chainConfigByName(name: string): ChainConfig {
// We assume that the map always has the key
const index = nameMap.get(name)!;
return loadedConfigs[index];
}

function loadConfigs(configs: [MinimalDenom, ConfigName, ChainConfig][]): void {
configs.forEach(([minimalDenom, name, config], index) => {
loadedConfigs.push(config);

minimalDenomMap.set(minimalDenom, index);
nameMap.set(name, index);
});
}

loadConfigs([
["namnam", "namada", namada],
["uatom", "cosmoshub", cosmoshub],
]);
22 changes: 22 additions & 0 deletions apps/namadillo/src/registry/namada.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"chainName": "Namada",
"currencies": [
{
"coinDecimals": 6,
"coinDenom": "NAM",
"coinMinimalDenom": "namnam"
}
],
"feeCurrencies": [
{
"coinDecimals": 6,
"coinDenom": "NAM",
"coinMinimalDenom": "namnam"
}
],
"stakeCurrency": {
"coinDecimals": 6,
"coinDenom": "NAM",
"coinMinimalDenom": "namnam"
}
}

0 comments on commit 402e4f5

Please sign in to comment.