Skip to content

Commit

Permalink
feat: change network type and add updgrade handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jtary committed Aug 31, 2023
1 parent 2b22068 commit 78c9895
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion web/src/hooks/useRpcNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const storageKey = 'rpc_settings';
export const defaultRpcSettings = {
rpcNode: 'https://rpc.akashnet.net/',
chainId: 'akashnet-2',
networkType: 'mainnet',
networkType: 'testnet',
};

export const testnetRpcSettings = {
Expand Down Expand Up @@ -36,6 +36,13 @@ function isRpcSettings(value: unknown): value is RpcSettings {
);
}

function hasNetworkUpgraded(settings: RpcSettings) {
const config = [defaultRpcSettings, testnetRpcSettings, sandboxRpcSettings]
.find((config) => config.chainId === settings.chainId);

return config ? config.networkType !== settings.networkType : false;
}

function getRpcFromStorageOrDefault(defaultValue: RpcSettings) {
const raw = localStorage.getItem(storageKey);

Expand All @@ -44,6 +51,12 @@ function getRpcFromStorageOrDefault(defaultValue: RpcSettings) {
const parsed = raw ? JSON.parse(raw) : null;

if (parsed) {
if (hasNetworkUpgraded(parsed)) {
console.warn('Network has been upgraded. Resetting RPC settings.');
deleteRpcFromStorage();
return defaultValue;
}

return parsed;
}
}
Expand Down

0 comments on commit 78c9895

Please sign in to comment.