Skip to content

Commit

Permalink
fix: solve glitching balance bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasgabrielgsp committed Nov 6, 2024
1 parent 0f21527 commit 7c94e61
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
18 changes: 17 additions & 1 deletion source/scripts/Background/controllers/MainController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class MainController extends KeyringManager {

this.bindMethods();
}

public setAutolockTimer(minutes: number) {
store.dispatch(setTimer(minutes));
}
Expand Down Expand Up @@ -1206,18 +1207,30 @@ class MainController extends KeyringManager {
isBitcoinBased,
activeNetwork,
activeAccount,
isPolling,
}: {
activeAccount: {
id: number;
type: KeyringAccountType;
};
activeNetwork: INetwork;
isBitcoinBased: boolean;
isPolling?: boolean;
}) {
const { accounts } = store.getState().vault;

const currentAccount = accounts[activeAccount.type][activeAccount.id];

let internalProvider: CustomJsonRpcProvider | undefined;

if (isPolling) {
const abortController = new AbortController();
internalProvider = new CustomJsonRpcProvider(
abortController.signal,
activeNetwork.url
);
}

const { currentPromise: balancePromise, cancel } =
this.cancellablePromises.createCancellablePromise<void>(
async (resolve, reject) => {
Expand All @@ -1226,7 +1239,8 @@ class MainController extends KeyringManager {
await this.balancesManager.utils.getBalanceUpdatedForAccount(
currentAccount,
isBitcoinBased,
activeNetwork.url
activeNetwork.url,
internalProvider
);

const actualUserBalance = isBitcoinBased
Expand Down Expand Up @@ -1372,6 +1386,7 @@ class MainController extends KeyringManager {
store.dispatch(setIsNetworkChanging(false));
store.dispatch(setIsLoadingBalances(false));
};

private async configureNetwork(
network: INetwork,
chain: string
Expand Down Expand Up @@ -1437,6 +1452,7 @@ class MainController extends KeyringManager {
wallet,
});
}

private bindMethods() {
const proto = Object.getPrototypeOf(this);
for (const key of Object.getOwnPropertyNames(proto)) {
Expand Down
8 changes: 5 additions & 3 deletions source/scripts/Background/controllers/balances/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const BalancesManager = (
const getBalanceUpdatedForAccount = async (
currentAccount: IPaliAccount,
isBitcoinBased: boolean,
networkUrl: string
networkUrl: string,
provider?: CustomJsonRpcProvider
) => {
switch (isBitcoinBased) {
case true:
Expand All @@ -30,8 +31,9 @@ const BalancesManager = (
}
case false:
try {
const getEvmBalance =
await evmBalanceController.getEvmBalanceForAccount(currentAccount);
const getEvmBalance = await EvmBalanceController(
provider || web3Provider
).getEvmBalanceForAccount(currentAccount);

return getEvmBalance;
} catch (evmBalanceError) {
Expand Down
5 changes: 4 additions & 1 deletion source/scripts/Background/controllers/balances/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CustomJsonRpcProvider } from '@pollum-io/sysweb3-keyring';

import { IPaliAccount } from 'state/vault/types';

export interface IEvmBalanceController {
Expand All @@ -15,7 +17,8 @@ export interface IBalancesManagerUtils {
getBalanceUpdatedForAccount: (
currentAccount: IPaliAccount,
isBitcoinBased: boolean,
networkUrl: string
networkUrl: string,
provider?: CustomJsonRpcProvider
) => Promise<string>;
}

Expand Down
2 changes: 2 additions & 0 deletions source/scripts/Background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ async function createOffscreen() {
})
.catch(() => {});

Check warning on line 74 in source/scripts/Background/index.ts

View workflow job for this annotation

GitHub Actions / yarn test-all

Unexpected empty arrow function
}

chrome.runtime.onStartup.addListener(createOffscreen);
self.onmessage = () => null; // keepAlive
createOffscreen();
Expand Down Expand Up @@ -314,6 +315,7 @@ async function checkForUpdates() {
walletMethods?.updateUserTransactionsState
) {
walletMethods.updateUserNativeBalance({
isPolling: true,
isBitcoinBased,
activeNetwork,
activeAccount,
Expand Down
6 changes: 5 additions & 1 deletion source/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Store,
} from '@reduxjs/toolkit';
import isEqual from 'lodash/isEqual';
import logger from 'redux-logger';
import { thunk } from 'redux-thunk';

import dapp from './dapp';
Expand All @@ -27,9 +28,12 @@ const middleware: any = [
];

middleware.push(thunk);

const nodeEnv = process.env.NODE_ENV;

if (nodeEnv !== 'production' && nodeEnv !== 'test') {
middleware.push(logger as never);
}

const store: Store<{
dapp: IDAppState;
price: IPriceState;
Expand Down

0 comments on commit 7c94e61

Please sign in to comment.