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 0f1d83d
Show file tree
Hide file tree
Showing 11 changed files with 150 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
85 changes: 85 additions & 0 deletions apps/namadillo/src/registry/namada-local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { AssetList, Chain } from "@chain-registry/types";

export const namadaLocal = (chainId: string): Chain => ({
$schema: "../chain.schema.json",
chain_name: "localnet",
status: "upcoming",
network_type: "testnet",
website: "https://namada.net",
pretty_name: "Namada",
chain_id: chainId,
slip44: 877,
bech32_prefix: "tnam",
daemon_name: "unam",
key_algos: ["ed25519"],
fees: {
fee_tokens: [
{
denom: "unam",
fixed_min_gas_price: 0.0025,
low_gas_price: 0.0025,
average_gas_price: 0.025,
high_gas_price: 0.04,
},
],
},
staking: {
staking_tokens: [
{
denom: "unam",
},
],
lock_duration: {
time: "1209600s",
},
},
codebase: {
git_repo: "https://github.com/anoma/namada",
},
apis: {
rpc: [
{
address: "127.0.0.1:27657",
provider: "Localnet",
},
],
rest: [
{
address: "127.0.0.1:27657",
provider: "Localnet",
},
],
},
logo_URIs: {
svg: "https://raw.githubusercontent.com/anoma/namada-chain-registry/main/namada/images/namada.svg",
},
});

export const namadaLocalAsset = (tokenAddress: string): AssetList => ({
$schema: "../assetlist.schema.json",
chain_name: "localnet",
assets: [
{
description: "The native token of Namada.",
denom_units: [
{
denom: "unam",
exponent: 0,
},
{
denom: "nam",
exponent: 6,
},
],
type_asset: "sdk.coin",
base: "unam",
name: "NAM",
display: "nam",
symbol: "NAM",
address: tokenAddress,
logo_URIs: {
svg: "https://raw.githubusercontent.com/anoma/namada-chain-registry/main/namada/images/namada.svg",
},
},
],
});
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 0f1d83d

Please sign in to comment.