Skip to content

Commit

Permalink
Merge pull request #1039 from tallycash/balance-disorder
Browse files Browse the repository at this point in the history
Balance Disorder: Add SerialFallbackProvider, a few multi-network refactorings
  • Loading branch information
mhluongo authored Feb 27, 2022
2 parents aa765ce + 673f656 commit 1ae6b2f
Show file tree
Hide file tree
Showing 11 changed files with 654 additions and 79 deletions.
60 changes: 56 additions & 4 deletions background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { AddressOnNetwork, NameOnNetwork } from "./accounts"
import rootReducer from "./redux-slices"
import {
loadAccount,
blockSeen,
updateAccountBalance,
updateENSName,
updateENSAvatar,
Expand All @@ -43,6 +42,7 @@ import {
updateKeyrings,
setKeyringToVerify,
} from "./redux-slices/keyrings"
import { blockSeen } from "./redux-slices/networks"
import {
initializationLoadingTimeHitLimit,
emitter as uiSliceEmitter,
Expand Down Expand Up @@ -101,7 +101,7 @@ const devToolsSanitizer = (input: unknown) => {

// The version of persisted Redux state the extension is expecting. Any previous
// state without this version, or with a lower version, ought to be migrated.
const REDUX_STATE_VERSION = 3
const REDUX_STATE_VERSION = 4

type Migration = (prevState: Record<string, unknown>) => Record<string, unknown>

Expand Down Expand Up @@ -141,6 +141,54 @@ const REDUX_MIGRATIONS: { [version: number]: Migration } = {

return newState
},
4: (prevState: Record<string, unknown>) => {
// Migrate the ETH-only block data in store.accounts.blocks[blockHeight] to
// a new networks slice. Block data is now network-specific, keyed by EVM
// chainID in store.networks.networkData[chainId].blocks
type OldState = {
account?: {
blocks?: { [blockHeight: number]: unknown }
}
}
type NetworkState = {
evm: {
[chainID: string]: {
blockHeight: number | null
blocks: {
[blockHeight: number]: unknown
}
}
}
}

const oldState = prevState as OldState

const networks: NetworkState = {
evm: {
"1": {
blocks: { ...oldState.account?.blocks },
blockHeight:
Math.max(
...Object.keys(oldState.account?.blocks ?? {}).map((s) =>
parseInt(s, 10)
)
) || null,
},
},
}

const { blocks, ...oldStateAccountWithoutBlocks } = oldState.account ?? {
blocks: undefined,
}

return {
...prevState,
// Drop blocks from account slice.
account: oldStateAccountWithoutBlocks,
// Add new networks slice data.
networks,
}
},
}

// Migrate a previous version of the Redux state to that expected by the current
Expand Down Expand Up @@ -549,7 +597,7 @@ export default class Main extends BaseService<never> {

async getAccountEthBalanceUncached(address: string): Promise<bigint> {
const amountBigNumber =
await this.chainService.pollingProviders.ethereum.getBalance(address)
await this.chainService.providers.ethereum.getBalance(address)
return amountBigNumber.toBigInt()
}

Expand Down Expand Up @@ -616,6 +664,11 @@ export default class Main extends BaseService<never> {
"requestSignature",
async ({ transaction, method }) => {
if (HIDE_IMPORT_LEDGER) {
const network = this.chainService.resolveNetwork(transaction)
if (typeof network === "undefined") {
throw new Error(`Unknown chain ID ${transaction.chainID}.`)
}

const transactionWithNonce =
await this.chainService.populateEVMTransactionNonce(transaction)

Expand All @@ -638,7 +691,6 @@ export default class Main extends BaseService<never> {
} else {
try {
const signedTx = await this.signingService.signTransaction(
this.chainService.ethereumNetwork,
transaction,
method
)
Expand Down
10 changes: 1 addition & 9 deletions background/redux-slices/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createSlice } from "@reduxjs/toolkit"
import { createBackgroundAsyncThunk } from "./utils"
import { AccountBalance, AddressOnNetwork, NameOnNetwork } from "../accounts"
import { AnyEVMBlock, Network } from "../networks"
import { Network } from "../networks"
import { AnyAsset, AnyAssetAmount, SmartContractFungibleAsset } from "../assets"
import {
AssetMainCurrencyAmount,
Expand Down Expand Up @@ -51,9 +51,6 @@ export type AccountState = {
// TODO Adapt to use AccountNetwork, probably via a Map and custom serialization/deserialization.
accountsData: { [address: string]: AccountData | "loading" }
combinedData: CombinedAccountData
// TODO the blockHeight key should be changed to something
// compatible with the idea of multiple networks.
blocks: { [blockHeight: number]: AnyEVMBlock }
}

export type CombinedAccountData = {
Expand Down Expand Up @@ -83,7 +80,6 @@ export const initialState = {
totalMainCurrencyValue: "",
assets: [],
},
blocks: {},
} as AccountState

function newAccountData(
Expand Down Expand Up @@ -140,9 +136,6 @@ const accountSlice = createSlice({
name: "account",
initialState,
reducers: {
blockSeen: (immerState, { payload: block }: { payload: AnyEVMBlock }) => {
immerState.blocks[block.blockHeight] = block
},
loadAccount: (state, { payload: accountToLoad }: { payload: string }) => {
return state.accountsData[accountToLoad]
? state // If the account data already exists, the account is already loaded.
Expand Down Expand Up @@ -256,7 +249,6 @@ export const {
updateAccountBalance,
updateENSName,
updateENSAvatar,
blockSeen,
} = accountSlice.actions

export default accountSlice.reducer
Expand Down
2 changes: 2 additions & 0 deletions background/redux-slices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import accountsReducer from "./accounts"
import assetsReducer from "./assets"
import activitiesReducer from "./activities"
import keyringsReducer from "./keyrings"
import networksReducer from "./networks"
import swapReducer from "./0x-swap"
import transactionConstructionReducer from "./transaction-construction"
import uiReducer from "./ui"
Expand All @@ -19,6 +20,7 @@ const mainReducer = combineReducers({
assets: assetsReducer,
activities: activitiesReducer,
keyrings: keyringsReducer,
networks: networksReducer,
swap: swapReducer,
transactionConstruction: transactionConstructionReducer,
ui: uiReducer,
Expand Down
48 changes: 48 additions & 0 deletions background/redux-slices/networks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { createSlice } from "@reduxjs/toolkit"

import { AnyEVMBlock } from "../networks"

type NetworkState = {
blocks: { [blockHeight: number]: AnyEVMBlock }
blockHeight: number | null
}

export type NetworksState = {
evm: {
[chainID: string]: NetworkState
}
}

export const initialState: NetworksState = {
evm: {
"1": {
blockHeight: null,
blocks: {},
},
},
}

const networksSlice = createSlice({
name: "networks",
initialState,
reducers: {
blockSeen: (immerState, { payload: block }: { payload: AnyEVMBlock }) => {
if (!(block.network.chainID in immerState.evm)) {
immerState.evm[block.network.chainID] = {
blocks: {},
blockHeight: block.blockHeight,
}
} else if (
block.blockHeight >
(immerState.evm[block.network.chainID].blockHeight || 0)
) {
immerState.evm[block.network.chainID].blockHeight = block.blockHeight
}
immerState.evm[block.network.chainID].blocks[block.blockHeight] = block
},
},
})

export const { blockSeen } = networksSlice.actions

export default networksSlice.reducer
9 changes: 4 additions & 5 deletions background/redux-slices/selectors/activitiesSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import { RootState } from ".."

export const selectCurrentAccountActivitiesWithTimestamps = createSelector(
(state: RootState) => {
const currentAccountAddress = selectCurrentAccount(state).address
const currentAccount = selectCurrentAccount(state)
const { address, network } = currentAccount

return {
currentAccountActivities:
typeof currentAccountAddress !== "undefined"
? state.activities[currentAccountAddress]
: undefined,
blocks: state.account.blocks,
typeof address !== "undefined" ? state.activities[address] : undefined,
blocks: state.networks.evm[network.chainID]?.blocks ?? {},
}
},
({ currentAccountActivities, blocks }) => {
Expand Down
5 changes: 4 additions & 1 deletion background/redux-slices/selectors/uiSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const hardcodedMainCurrencySymbol = "USD"
export const selectShowingActivityDetail = createSelector(
(state: RootState) => state.activities,
(state: RootState) => state.ui.showingActivityDetailID,
(state: RootState) => state.account.blocks,
(state: RootState) => {
const { network } = state.ui.selectedAccount
return state.networks.evm[network.chainID].blocks
},
(activities, showingActivityDetailID, blocks) => {
return showingActivityDetailID === null
? null
Expand Down
Loading

0 comments on commit 1ae6b2f

Please sign in to comment.