Skip to content

Commit

Permalink
Remove evm network type from local storage & fix network checks (#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefashkaa authored Nov 8, 2022
1 parent 5a63a4e commit e521739
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 36 deletions.
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

0 comments on commit e521739

Please sign in to comment.