Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change network type and add upgrade handler #150

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading