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

Remove evm network type from local storage & fix network checks #854

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/components/mixins/NetworkFormatterMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { state, getter } from '@/store/decorators';

@Component
export default class NetworkFormatterMixin extends Vue {
@state.web3.networkType networkType!: EvmNetworkType;
@state.web3.networkType networkType!: Nullable<EvmNetworkType>;
@state.wallet.settings.soraNetwork soraNetwork!: Nullable<WALLET_CONSTS.SoraNetwork>;

@getter.web3.defaultNetworkType defaultNetworkType!: Nullable<EvmNetworkType>;
Expand Down
2 changes: 1 addition & 1 deletion src/store/web3/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const actions = defineActions({
const { commit } = web3ActionContext(context);
let networkType = '';
if (!network) {
networkType = await ethersUtil.getEvmNetworkType();
networkType = await ethersUtil.fetchEvmNetworkType();
} else {
networkType = EvmNetworkTypeName[network];
}
Expand Down
3 changes: 3 additions & 0 deletions src/store/web3/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const getters = defineGetters<Web3State>()({
},
isValidNetworkType(...args): boolean {
const { state, getters } = web3GetterContext(args);
if (!state.networkType) {
return false;
}
return state.networkType === getters.defaultNetworkType;
},
});
Expand Down
1 change: 0 additions & 1 deletion src/store/web3/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const mutations = defineMutations<Web3State>()({
},
setNetworkType(state, networkType: string): void {
state.networkType = networkType;
ethersUtil.storeEvmNetworkType(networkType);
},
setSubNetworks(state, subNetworks: Array<SubNetwork>): void {
state.subNetworks = subNetworks;
Expand Down
4 changes: 2 additions & 2 deletions src/store/web3/state.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BridgeNetworks } from '@sora-substrate/util';

import { ZeroStringValue } from '@/consts';
import ethersUtil from '@/utils/ethers-util';
import ethersUtil, { EvmNetworkType } from '@/utils/ethers-util';
import type { Web3State } from './types';

export function initialState(): Web3State {
return {
evmAddress: ethersUtil.getEvmUserAddress(),
evmBalance: ZeroStringValue,
networkType: ethersUtil.getEvmNetworkTypeFromStorage(),
networkType: null,
subNetworks: [],
evmNetwork: BridgeNetworks.ETH_NETWORK_ID,
contractAddress: {
Expand Down
2 changes: 1 addition & 1 deletion src/store/web3/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Contracts = Partial<{
export type Web3State = {
evmAddress: string;
evmBalance: CodecString;
networkType: string;
networkType: Nullable<string>;
subNetworks: Array<SubNetwork>;
evmNetwork: BridgeNetworks;
contractAddress: { [key in BridgeNetworks]: Contracts };
Expand Down
36 changes: 6 additions & 30 deletions src/utils/ethers-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,32 +303,11 @@ function removeEvmUserAddress(): void {
storage.remove('evmAddress');
}

function storeEvmNetworkType(network: string): void {
storage.set('evmNetworkType', EvmNetworkTypeName[network] || network);
}

function getEvmNetworkTypeFromStorage(): string {
// return storage.get('evmNetworkType') || '' TODO: [1.5] return it back after 1.4 release to mainnet
let evmNetworkType = storage.get('evmNetworkType') || '';
if (evmNetworkType === 'homestead') {
evmNetworkType = 'main';
}
return evmNetworkType;
}

function removeEvmNetworkType(): void {
storage.remove('evmNetworkType');
}

async function getEvmNetworkType(): Promise<string> {
const networkType = getEvmNetworkTypeFromStorage();
if (!networkType || networkType === 'undefined') {
const ethersInstance = await getEthersInstance();
const network = await ethersInstance.getNetwork();
const networkType = ethers.utils.hexValue(network.chainId);
return EvmNetworkTypeName[networkType];
}
return networkType;
async function fetchEvmNetworkType(): Promise<string> {
const ethersInstance = await getEthersInstance();
const network = await ethersInstance.getNetwork();
const networkType = ethers.utils.hexValue(network.chainId);
return EvmNetworkTypeName[networkType];
}

/**
Expand Down Expand Up @@ -394,10 +373,7 @@ export default {
checkAccountIsConnected,
storeEvmUserAddress,
getEvmUserAddress,
storeEvmNetworkType,
getEvmNetworkType,
getEvmNetworkTypeFromStorage,
removeEvmNetworkType,
fetchEvmNetworkType,
getEthersInstance,
removeEvmUserAddress,
watchEthereum,
Expand Down