Skip to content

Commit

Permalink
feat: add static chains config (#1103)
Browse files Browse the repository at this point in the history
* feat: add  static chains config

* fix: code review fixes
  • Loading branch information
mateuszjasiuk authored Sep 19, 2024
1 parent 2c1b5d5 commit dcb53aa
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 6 deletions.
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"
}
}

1 comment on commit dcb53aa

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.