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 static chains config #1103

Merged
merged 2 commits into from
Sep 19, 2024
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
7 changes: 6 additions & 1 deletion apps/namadillo/src/atoms/accounts/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { namadaExtensionConnectedAtom } from "atoms/settings";
import { queryDependentFn } from "atoms/utils";
import BigNumber from "bignumber.js";
import { atomWithMutation, atomWithQuery } from "jotai-tanstack-query";
import { chainConfigByName } from "registry";
import {
fetchAccountBalance,
fetchAccounts,
Expand Down Expand Up @@ -50,6 +51,7 @@ export const accountBalanceAtom = atomWithQuery<BigNumber>((get) => {
const tokenAddress = get(nativeTokenAddressAtom);
const enablePolling = get(shouldUpdateBalanceAtom);
const api = get(indexerApiAtom);
const chainConfig = chainConfigByName("namada");

return {
// TODO: subscribe to indexer events when it's done
Expand All @@ -59,7 +61,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);
};
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"
}
}
54 changes: 54 additions & 0 deletions apps/namadillo/src/registry/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import cosmoshub from "./cosmoshub.json";
import namada from "./namada.json";

type MinimalDenom = string;
type ConfigName = string;
type ChainName = "cosmoshub" | "namada";
type ChainMinDenom = "uatom" | "namnam";

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: ChainMinDenom): ChainConfig {
const index = minimalDenomMap.get(minDenom);
if (!index) {
throw new Error("Chain config not found");
}

return loadedConfigs[index];
}

export function chainConfigByName(name: ChainName): ChainConfig {
const index = nameMap.get(name);
if (!index) {
throw new Error("Chain config not found");
}
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"
}
}
Loading