Skip to content

Commit

Permalink
feat: handle nam as denominated amount
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Nov 22, 2024
1 parent db40ff0 commit afbcacc
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 22 deletions.
23 changes: 21 additions & 2 deletions apps/namadillo/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
import { createDefaultPreset, pathsToModuleNameMapper } from "ts-jest";
import { pathsToModuleNameMapper } from "ts-jest";
import { compilerOptions } from "../../tsconfig.base.json";

/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
...createDefaultPreset(),
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
diagnostics: {
ignoreCodes: [1343],
},
astTransformers: {
before: [
{
path: "../../node_modules/ts-jest-mock-import-meta",
options: {
metaObjectReplacement: { env: {} },
},
},
],
},
},
],
},
roots: ["src"],
displayName: "Namadillo",
testEnvironment: "jsdom",
Expand Down
1 change: 1 addition & 0 deletions apps/namadillo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"release-it": "^17.0.1",
"tailwindcss": "^3.4.0",
"ts-jest": "^29.2.5",
"ts-jest-mock-import-meta": "^1.2.1",
"ts-node": "^10.9.1",
"tsconfig-paths-webpack-plugin": "^4.1.0",
"typescript": "^5.5.4",
Expand Down
7 changes: 1 addition & 6 deletions apps/namadillo/src/atoms/accounts/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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 @@ -85,7 +84,6 @@ 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 @@ -95,10 +93,7 @@ export const accountBalanceAtom = atomWithQuery<BigNumber>((get) => {
return await fetchNamAccountBalance(
api,
defaultAccount.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.data!
);
}, [tokenAddress, defaultAccount]),
};
Expand Down
8 changes: 2 additions & 6 deletions apps/namadillo/src/atoms/accounts/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ export const fetchDefaultAccount = async (): Promise<Account | undefined> => {
export const fetchNamAccountBalance = async (
api: DefaultApi,
account: Account | undefined,
tokenAddress: string,
decimals: number
tokenAddress: string
): Promise<BigNumber> => {
if (!account) return BigNumber(0);
const balancesResponse = await api.apiV1AccountAddressGet(account.address);

const balance = balancesResponse.data
// TODO: add filter to the api call
.filter(({ tokenAddress: ta }) => ta === tokenAddress)
.map(({ tokenAddress, balance }) => {
return {
Expand All @@ -34,9 +32,7 @@ export const fetchNamAccountBalance = async (
})
.at(0);

return balance ?
BigNumber(balance.amount).shiftedBy(-decimals)
: BigNumber(0);
return balance ? BigNumber(balance.amount) : BigNumber(0);
};

export const fetchAccountBalance = async (
Expand Down
6 changes: 1 addition & 5 deletions apps/namadillo/src/atoms/balance/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ export const shieldedBalanceAtom = atomWithQuery<
);
const shieldedBalance = response.map(([address, amount]) => ({
address,
amount:
// Sdk returns the nam amount as `nam` instead of `namnam`
namTokenAddressQuery.data === address ?
new BigNumber(amount).shiftedBy(6)
: new BigNumber(amount),
amount: new BigNumber(amount),
}));
return shieldedBalance;
}, [viewingKeyQuery, tokenAddressesQuery, namTokenAddressQuery]),
Expand Down
19 changes: 19 additions & 0 deletions apps/namadillo/src/atoms/integrations/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ import internalDevnetCosmosTestnetIbc from "namada-chain-registry/_IBC/namadaint

// TODO: this causes a big increase on bundle size. See #1224.
import cosmosRegistry from "chain-registry";
import { namadaLocal, namadaLocalAsset } from "registry/namada-local";

const {
VITE_LOCALNET: localnet = false,
VITE_LOCALNET_CHAIN_ID: localChainId,
VITE_LOCALNET_NAM_TOKEN: localToken,
} = import.meta.env;

cosmosRegistry.chains.push(internalDevnetChain, housefireChain, dryrunChain);

Expand Down Expand Up @@ -75,6 +82,18 @@ const testnetChains: ChainRegistryEntry[] = [

const mainnetAndTestnetChains = [...mainnetChains, ...testnetChains];

if (localnet && localChainId && localToken) {
const localChain: ChainRegistryEntry = {
chain: namadaLocal(localChainId),
assets: namadaLocalAsset(localToken),
};

cosmosRegistry.chains.push(localChain.chain);
cosmosRegistry.assets.push(localChain.assets);

mainnetChains.push(localChain);
}

export const getKnownChains = (
includeTestnets?: boolean
): ChainRegistryEntry[] => {
Expand Down
2 changes: 1 addition & 1 deletion apps/namadillo/src/hooks/useRegistryFeatures.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { useAtom, useAtomValue } from "jotai";
import { useEffect } from "react";

const { VITE_PROXY } = process.env;
const { VITE_PROXY } = import.meta.env;

const namadaChainRegistryUrl =
VITE_PROXY ?
Expand Down
22 changes: 22 additions & 0 deletions apps/namadillo/src/registry/namada-local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AssetList, Chain } from "@chain-registry/types";
import internalDevnetAssets from "namada-chain-registry/namadainternaldevnet/assetlist.json";
import internalDevnetChain from "namada-chain-registry/namadainternaldevnet/chain.json";

export const namadaLocal = (chainId: string): Chain => ({
...internalDevnetChain,
chain_name: "localnet",
chain_id: chainId,
});

export const namadaLocalAsset = (tokenAddress: string): AssetList => ({
...internalDevnetAssets,
chain_name: "localnet",
assets: internalDevnetAssets.assets.map((asset) =>
asset.name === "NAM" ?
{
...asset,
address: tokenAddress,
}
: asset
),
});
8 changes: 7 additions & 1 deletion apps/namadillo/src/registry/namadaAsset.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Asset } from "@chain-registry/types";
import namadaShieldedSvg from "./assets/namada-shielded.svg";

const { VITE_LOCALNET: localnet = false, VITE_LOCALNET_NAM_TOKEN: localToken } =
import.meta.env;

export const namadaAsset = {
name: "Namada",
base: "unam",
address: "tnam1qy440ynh9fwrx8aewjvvmu38zxqgukgc259fzp6h",
address:
localnet && localToken ? localToken : (
"tnam1qxp7u2vmlgcrpn9j0ml7hrtr79g2w2fdesteh7tu"
),
display: "nam",
symbol: "NAM",
denom_units: [
Expand Down
3 changes: 2 additions & 1 deletion apps/namadillo/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export const toDisplayAmount = (
asset: Asset,
baseAmount: BigNumber
): BigNumber => {
if (isNamAsset(asset)) return baseAmount;

const displayUnit = findDisplayUnit(asset);
if (!displayUnit) {
return baseAmount;
Expand All @@ -139,7 +141,6 @@ export const toBaseAmount = (
asset: Asset,
displayAmount: BigNumber
): BigNumber => {
if (isNamAsset(asset)) return displayAmount;
const displayUnit = findDisplayUnit(asset);
if (!displayUnit) {
return displayAmount;
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3691,6 +3691,7 @@ __metadata:
toml: "npm:^3.0.0"
traverse: "npm:^0.6.9"
ts-jest: "npm:^29.2.5"
ts-jest-mock-import-meta: "npm:^1.2.1"
ts-node: "npm:^10.9.1"
tsconfig-paths-webpack-plugin: "npm:^4.1.0"
typescript: "npm:^5.5.4"
Expand Down Expand Up @@ -20865,6 +20866,15 @@ __metadata:
languageName: node
linkType: hard

"ts-jest-mock-import-meta@npm:^1.2.1":
version: 1.2.1
resolution: "ts-jest-mock-import-meta@npm:1.2.1"
peerDependencies:
ts-jest: ">=20.0.0"
checksum: 86eb214916bfbf0934eadde56c8c4562f98b0bd5d03d24baa2b636746f248b4d96d5b489a4e0b25b5aec33f9d7d93ef289c9def47d848b46f6a456e9a8f9e2cd
languageName: node
linkType: hard

"ts-jest@npm:^29.2.5":
version: 29.2.5
resolution: "ts-jest@npm:29.2.5"
Expand Down

0 comments on commit afbcacc

Please sign in to comment.