Skip to content

Commit

Permalink
feat: toml file instead of env
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Nov 22, 2024
1 parent afbcacc commit 5bc89d8
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 74 deletions.
1 change: 1 addition & 0 deletions apps/namadillo/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/test-results/
/playwright-report/
/playwright/.cache/
/public/localnet-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const TransparentTokensTable = ({
>
Shield
</ActionButton>
{originalAddress === namadaAsset.address && (
{originalAddress === namadaAsset().address && (
<ActionButton
size="xs"
className="w-fit mx-auto"
Expand Down Expand Up @@ -127,7 +127,7 @@ const TransparentTokensTable = ({
};

const PanelContent = ({ data }: { data: TokenBalance[] }): JSX.Element => {
const namBalance = data.find((i) => i.asset.base === namadaAsset.base);
const namBalance = data.find((i) => i.asset.base === namadaAsset().base);

return (
<div className="flex flex-col gap-2">
Expand Down
4 changes: 2 additions & 2 deletions apps/namadillo/src/App/Ibc/IbcWithdraw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export const IbcWithdraw: React.FC = () => {

const transactionFee = mapUndefined(
({ gasLimit, gasPrice }) => ({
originalAddress: namadaAsset.address,
asset: namadaAsset,
originalAddress: namadaAsset().address,
asset: namadaAsset(),
amount: gasPrice.multipliedBy(gasLimit),
}),
gasConfig
Expand Down
3 changes: 3 additions & 0 deletions apps/namadillo/src/App/Setup/ChainLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AtomErrorBoundary } from "App/Common/AtomErrorBoundary";
import { ErrorBox } from "App/Common/ErrorBox";
import { routes } from "App/routes";
import { chainAtom } from "atoms/chain";
import { localnetConfig } from "atoms/integrations";
import { useAtomValue } from "jotai";
import { ReactNode } from "react";
import { useLocation, useNavigate } from "react-router-dom";
Expand Down Expand Up @@ -31,6 +32,8 @@ export const ChainLoader = ({
children: ReactNode;
}): JSX.Element => {
const chain = useAtomValue(chainAtom);
useAtomValue(localnetConfig);

const errorContainerProps = {
className: "bg-black max-w-full rounded-sm w-full text-white min-h-full",
};
Expand Down
2 changes: 1 addition & 1 deletion apps/namadillo/src/App/Transfer/AssetImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const AssetImage = ({
<img src={image} alt={altText} className="w-full" />
{isShielded !== undefined && (
<span className="absolute -bottom-1 -right-1 w-4 aspect-square">
<AssetImage asset={namadaAsset} />
<AssetImage asset={namadaAsset()} />
</span>
)}
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ describe("Component: TransferDestination", () => {
<TransferDestination
transactionFee={{
amount: fee,
asset: namadaAsset,
originalAddress: namadaAsset.address,
asset: namadaAsset(),
originalAddress: namadaAsset().address,
}}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion apps/namadillo/src/atoms/balance/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const getTotalDollar = (list?: TokenBalance[]): BigNumber | undefined =>
sumDollars(list ?? []);

export const getTotalNam = (list?: TokenBalance[]): BigNumber =>
list?.find((i) => i.asset.base === namadaAsset.base)?.amount ??
list?.find((i) => i.asset.base === namadaAsset().base)?.amount ??
new BigNumber(0);

const tnamAddressToDenomTrace = (
Expand Down
26 changes: 26 additions & 0 deletions apps/namadillo/src/atoms/integrations/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
TransferTransactionData,
} from "types";
import {
addLocalnetToRegistry,
createIbcTx,
getIbcChannels,
getKnownChains,
Expand All @@ -28,6 +29,7 @@ import {
mapCoinsToAssets,
} from "./functions";
import {
fetchLocalnetTomlConfig,
IbcTransferParams,
queryAndStoreRpc,
queryAssetBalances,
Expand Down Expand Up @@ -198,3 +200,27 @@ export const createIbcTxAtom = atomWithMutation((get) => {
},
};
});

export const localnetConfig = atomWithQuery((_get) => {
return {
queryKey: ["localnet-config"],
staleTime: Infinity,
retry: false,

queryFn: async () => {
try {
const { enabled, chain_id, token_address } =
await fetchLocalnetTomlConfig();

if (enabled && chain_id && token_address) {
addLocalnetToRegistry(chain_id, token_address);
}

return { chainId: chain_id, tokenAddress: token_address };
} catch (_) {
// If file not found just ignore
return null;
}
},
};
});
55 changes: 35 additions & 20 deletions apps/namadillo/src/atoms/integrations/functions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Asset, Chain } from "@chain-registry/types";
import { Asset, AssetList, Chain } from "@chain-registry/types";
import { Coin } from "@cosmjs/launchpad";
import { QueryClient, setupIbcExtension } from "@cosmjs/stargate";
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
Expand Down Expand Up @@ -43,13 +43,6 @@ 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 @@ -82,18 +75,6 @@ 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 Expand Up @@ -389,3 +370,37 @@ export const createIbcTx = async (
);
return transactionPair;
};

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
),
});

export const addLocalnetToRegistry = (
chainId: string,
tokenAddress: string
): void => {
const localChain: ChainRegistryEntry = {
chain: namadaLocal(chainId),
assets: namadaLocalAsset(tokenAddress),
};

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

mainnetChains.push(localChain);
};
7 changes: 7 additions & 0 deletions apps/namadillo/src/atoms/integrations/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import { queryForAck, queryForIbcTimeout } from "atoms/transactions";
import BigNumber from "bignumber.js";
import { getDefaultStore } from "jotai";
import { createIbcTransferMessage } from "lib/transactions";
import toml from "toml";
import {
AddressWithAsset,
IbcTransferTransactionData,
LocalnetToml,
TransferStep,
} from "types";
import { toBaseAmount } from "utils";
Expand Down Expand Up @@ -189,3 +191,8 @@ export const updateIbcTransactionStatus = async (
});
}
};

export const fetchLocalnetTomlConfig = async (): Promise<LocalnetToml> => {
const response = await fetch("/localnet-config.toml");
return toml.parse(await response.text()) as LocalnetToml;
};
22 changes: 0 additions & 22 deletions apps/namadillo/src/registry/namada-local.ts

This file was deleted.

50 changes: 27 additions & 23 deletions apps/namadillo/src/registry/namadaAsset.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
import { Asset } from "@chain-registry/types";
import { localnetConfig } from "atoms/integrations";
import { getDefaultStore } from "jotai";
import namadaShieldedSvg from "./assets/namada-shielded.svg";

const { VITE_LOCALNET: localnet = false, VITE_LOCALNET_NAM_TOKEN: localToken } =
import.meta.env;
// We can't return "satisfies Asset" from fn
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const namadaAsset = () => {
const store = getDefaultStore();
const config = store.get(localnetConfig);
const localNamAddress = config.data?.tokenAddress;

export const namadaAsset = {
name: "Namada",
base: "unam",
address:
localnet && localToken ? localToken : (
"tnam1qxp7u2vmlgcrpn9j0ml7hrtr79g2w2fdesteh7tu"
),
display: "nam",
symbol: "NAM",
denom_units: [
{
denom: "unam",
exponent: 0,
},
{
denom: "nam",
exponent: 6,
},
],
logo_URIs: { svg: namadaShieldedSvg },
} satisfies Asset;
return {
name: "Namada",
base: "unam",
address: localNamAddress || "tnam1qxp7u2vmlgcrpn9j0ml7hrtr79g2w2fdesteh7tu",
display: "nam",
symbol: "NAM",
denom_units: [
{
denom: "unam",
exponent: 0,
},
{
denom: "nam",
exponent: 6,
},
],
logo_URIs: { svg: namadaShieldedSvg },
} satisfies Asset;
};
6 changes: 6 additions & 0 deletions apps/namadillo/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,9 @@ export type TransferTransactionData =

export type PartialTransferTransactionData = Partial<TransferTransactionData> &
Pick<TransferTransactionData, "type" | "chainId" | "asset">;

export type LocalnetToml = {
enabled: boolean;
chain_id: string;
token_address: string;
};
2 changes: 1 addition & 1 deletion apps/namadillo/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const findDisplayUnit = (asset: Asset): AssetDenomUnit | undefined => {
};

const isNamAsset = (asset: Asset): boolean =>
asset.symbol === namadaAsset.symbol;
asset.symbol === namadaAsset().symbol;

export const toDisplayAmount = (
asset: Asset,
Expand Down

0 comments on commit 5bc89d8

Please sign in to comment.