Skip to content

Commit

Permalink
feat: update chain lookup, add shielded rewards and failsafe config
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Nov 14, 2024
1 parent 6e78d1e commit 342c9a3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
2 changes: 1 addition & 1 deletion apps/namadillo/src/App/AppRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const MainRoutes = (): JSX.Element => {
const settingsAnimationKey =
location.pathname.indexOf(routes.settings) > -1 ?
"settings-modal"
: location.pathname;
: location.pathname;

return (
<>
Expand Down
1 change: 0 additions & 1 deletion apps/namadillo/src/atoms/chain/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export const nativeTokenAddressAtom = atomWithQuery<string>((get) => {
queryKey: ["native-token-address"],
enabled: chain.isSuccess,
queryFn: async () => {
console.log("native token", chain.data!.nativeTokenAddress);
return chain.data!.nativeTokenAddress;
},
};
Expand Down
45 changes: 22 additions & 23 deletions apps/namadillo/src/atoms/settings/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export const applicationFeaturesAtom = atomWithQuery((get) => {
const chainId = chainParameters.data?.chainId;
if (chainId) {
const features = await fetchEnabledFeatures(chainId);
console.log("FEATURES", features);
return features;
}
return defaultApplicationFeatures;
Expand Down Expand Up @@ -77,35 +76,35 @@ export const settingsAtom = atomWithStorage<SettingsStorage>(

const changeSettings =
<T>(key: keyof SettingsStorage) =>
(get: Getter, set: Setter, value: T) => {
const settings = get(settingsAtom);
set(settingsAtom, { ...settings, [key]: value });
};
(get: Getter, set: Setter, value: T) => {
const settings = get(settingsAtom);
set(settingsAtom, { ...settings, [key]: value });
};

const changeSettingsUrl =
(
key: keyof SettingsStorage,
healthCheck: (url: string) => Promise<boolean>,
allowEmpty = false
) =>
async (inputUrl: string) => {
const allowedEmpty = allowEmpty && inputUrl.length === 0;
const url = allowedEmpty ? "" : sanitizeUrl(inputUrl);

if (!allowedEmpty && !isUrlValid(url)) {
throw new Error(
"Invalid URL. The URL should be valid starting with 'http', 'https', 'ws', or 'wss'."
);
}
if (allowedEmpty || (await healthCheck(url))) {
const { get, set } = getDefaultStore();
changeSettings(key)(get, set, url);
} else {
throw new Error(
"Couldn't reach the URL. Please provide a valid Namada URL service."
);
}
};
async (inputUrl: string) => {
const allowedEmpty = allowEmpty && inputUrl.length === 0;
const url = allowedEmpty ? "" : sanitizeUrl(inputUrl);

if (!allowedEmpty && !isUrlValid(url)) {
throw new Error(
"Invalid URL. The URL should be valid starting with 'http', 'https', 'ws', or 'wss'."
);
}
if (allowedEmpty || (await healthCheck(url))) {
const { get, set } = getDefaultStore();
changeSettings(key)(get, set, url);
} else {
throw new Error(
"Couldn't reach the URL. Please provide a valid Namada URL service."
);
}
};

export const updateSettingsProps = atomWithMutation(() => {
return {
Expand Down
27 changes: 25 additions & 2 deletions apps/namadillo/src/atoms/settings/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const namadaChainRegistryUrl =

const namadaChainRegistryMap = new Map<string, string>([
["namada-dryrun.abaaeaf7b78cb3ac", "namadadryrun"],
["housefire-cotton.d3c912fee7462", "namadahousefire"],
["housefire-equal.130b1076e3250f", "namadahousefire"],
["internal-devnet-44a.1bd3e6ca62", "namadainternaldevnet"],
]);

Expand All @@ -24,6 +24,15 @@ type Feature =
| "shieldingRewards"
| "namTransfers";

const allFeaturesEnabled = {
claimRewardsEnabled: true,
shieldingRewardsEnabled: true,
maspEnabled: true,
ibcTransfersEnabled: true,
ibcShieldingEnabled: true,
namTransfersEnabled: true,
};

export const isIndexerAlive = async (url: string): Promise<boolean> => {
if (!isUrlValid(url)) {
return false;
Expand Down Expand Up @@ -68,19 +77,30 @@ export const fetchDefaultTomlConfig =
return toml.parse(await response.text()) as SettingsTomlOptions;
};

// TODO: Clean this whole thing up!
export const fetchEnabledFeatures = async (
chainId: string
): Promise<ApplicationFeatures> => {
const chainName = namadaChainRegistryMap.get(chainId);

if (!chainName) {
// Enable every feature for non-mapped chains
return allFeaturesEnabled;
}
const chainConfigFile = "chain.json";

const options = defaultApplicationFeatures;

const response = await fetch(
`${namadaChainRegistryUrl}/${chainName}/${chainConfigFile}`
);

const { features } = (await response.json()) as { features: Feature[] };
console.log("features?", features);

if (!features || features.length === 0) {
// Enable every feature for non-registry chains
return allFeaturesEnabled;
}

features.forEach((feature: Feature) => {
switch (feature) {
Expand All @@ -99,6 +119,9 @@ export const fetchEnabledFeatures = async (
case "namTransfers":
options.namTransfersEnabled = true;
break;
case "shieldingRewards":
options.shieldingRewardsEnabled = true;
break;
}
});

Expand Down

0 comments on commit 342c9a3

Please sign in to comment.