Skip to content

Commit

Permalink
refactor: add block height, parachain status and locked tokens hooks (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsimao authored Jul 26, 2023
1 parent ca68f9e commit 5336dc7
Show file tree
Hide file tree
Showing 19 changed files with 222 additions and 342 deletions.
59 changes: 3 additions & 56 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import './i18n';

import { FaucetClient, SecurityStatusCode } from '@interlay/interbtc-api';
import { FaucetClient } from '@interlay/interbtc-api';
import { Keyring } from '@polkadot/keyring';
import * as React from 'react';
import { useErrorHandler, withErrorBoundary } from 'react-error-boundary';
import { useQuery } from 'react-query';
import { useDispatch, useSelector } from 'react-redux';
import { Redirect, Route, Switch } from 'react-router-dom';

import { initGeneralDataAction, isFaucetLoaded, isVaultClientLoaded } from '@/common/actions/general.actions';
import { ParachainStatus, StoreType } from '@/common/types/util.types';
import { GOVERNANCE_TOKEN, RELAY_CHAIN_NATIVE_TOKEN, WRAPPED_TOKEN } from '@/config/relay-chains';
import { isFaucetLoaded, isVaultClientLoaded } from '@/common/actions/general.actions';
import { StoreType } from '@/common/types/util.types';
import ErrorFallback from '@/legacy-components/ErrorFallback';
import FullLoadingSpinner from '@/legacy-components/FullLoadingSpinner';
import { useSubstrate, useSubstrateSecureState } from '@/lib/substrate';
Expand Down Expand Up @@ -91,58 +90,6 @@ const App = (): JSX.Element => {
);
useErrorHandler(vaultsError);

// Initializes data on app bootstrap
React.useEffect(() => {
if (!dispatch) return;
if (!bridgeLoaded) return;

(async () => {
try {
const [
totalWrappedTokenAmount,
totalLockedCollateralTokenAmount,
totalGovernanceTokenAmount,
btcRelayHeight,
bitcoinHeight,
state
] = await Promise.all([
window.bridge.tokens.total(WRAPPED_TOKEN),
window.bridge.tokens.total(RELAY_CHAIN_NATIVE_TOKEN),
window.bridge.tokens.total(GOVERNANCE_TOKEN),
window.bridge.btcRelay.getLatestBlockHeight(),
window.bridge.electrsAPI.getLatestBlockHeight(),
window.bridge.system.getStatusCode()
]);

const parachainStatus = (state: SecurityStatusCode) => {
if (state.isError) {
return ParachainStatus.Error;
} else if (state.isRunning) {
return ParachainStatus.Running;
} else if (state.isShutdown) {
return ParachainStatus.Shutdown;
} else {
return ParachainStatus.Loading;
}
};

dispatch(
initGeneralDataAction(
totalWrappedTokenAmount,
totalLockedCollateralTokenAmount,
totalGovernanceTokenAmount,
btcRelayHeight,
bitcoinHeight,
parachainStatus(state)
)
);
} catch (error) {
// TODO: should add error handling
console.log('[App React.useEffect 2] error.message => ', error.message);
}
})();
}, [dispatch, bridgeLoaded]);

React.useEffect(() => {
if (!setSelectedAccount) return;

Expand Down
45 changes: 1 addition & 44 deletions src/common/actions/general.actions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { CollateralCurrencyExt } from '@interlay/interbtc-api';
import { BitcoinAmount, MonetaryAmount } from '@interlay/monetary-js';

import { GovernanceTokenMonetaryAmount } from '@/config/relay-chains';

import {
ADD_NOTIFICATION,
AddNotification,
INIT_GENERAL_DATA_ACTION,
InitGeneralDataAction,
IS_BRIDGE_LOADED,
IS_FAUCET_LOADED,
IS_VAULT_CLIENT_LOADED,
Expand All @@ -20,14 +13,10 @@ import {
ShowAccountModal,
ShowBuyModal,
ShowSignTermsModal,
UPDATE_HEIGHTS,
UPDATE_TOTALS,
UPDATE_TRANSACTION_MODAL_STATUS,
UpdateHeights,
UpdateTotals,
UpdateTransactionModal
} from '../types/actions.types';
import { Notification, ParachainStatus, TransactionModalData } from '../types/util.types';
import { Notification, TransactionModalData } from '../types/util.types';

export const isBridgeLoaded = (isLoaded = false): IsBridgeLoaded => ({
type: IS_BRIDGE_LOADED,
Expand All @@ -44,23 +33,6 @@ export const isVaultClientLoaded = (isLoaded = false): IsVaultClientLoaded => ({
isLoaded
});

export const initGeneralDataAction = (
totalWrappedTokenAmount: BitcoinAmount,
totalLockedCollateralTokenAmount: MonetaryAmount<CollateralCurrencyExt>,
totalGovernanceTokenAmount: GovernanceTokenMonetaryAmount,
btcRelayHeight: number,
bitcoinHeight: number,
parachainStatus: ParachainStatus
): InitGeneralDataAction => ({
type: INIT_GENERAL_DATA_ACTION,
btcRelayHeight,
bitcoinHeight,
totalWrappedTokenAmount,
totalLockedCollateralTokenAmount,
totalGovernanceTokenAmount,
parachainStatus
});

export const showAccountModalAction = (showAccountModal: boolean): ShowAccountModal => ({
type: SHOW_ACCOUNT_MODAL,
showAccountModal
Expand All @@ -76,21 +48,6 @@ export const showBuyModal = (isBuyModalOpen: boolean): ShowBuyModal => ({
isBuyModalOpen
});

export const updateHeightsAction = (btcRelayHeight: number, bitcoinHeight: number): UpdateHeights => ({
type: UPDATE_HEIGHTS,
btcRelayHeight,
bitcoinHeight
});

export const updateTotalsAction = (
totalLockedCollateralTokenAmount: MonetaryAmount<CollateralCurrencyExt>,
totalWrappedTokenAmount: BitcoinAmount
): UpdateTotals => ({
type: UPDATE_TOTALS,
totalLockedCollateralTokenAmount,
totalWrappedTokenAmount
});

export const addNotification = (accountAddress: string, notification: Notification): AddNotification => ({
type: ADD_NOTIFICATION,
accountAddress,
Expand Down
22 changes: 0 additions & 22 deletions src/common/live-data/block-height-watcher.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/common/live-data/live-data.ts

This file was deleted.

29 changes: 0 additions & 29 deletions src/common/live-data/totals-watcher.ts

This file was deleted.

31 changes: 1 addition & 30 deletions src/common/reducers/general.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import { newMonetaryAmount } from '@interlay/interbtc-api';
import { BitcoinAmount } from '@interlay/monetary-js';

import { RELAY_CHAIN_NATIVE_TOKEN } from '@/config/relay-chains';
import { TransactionStatus } from '@/hooks/transaction/types';

import {
ADD_NOTIFICATION,
GeneralActions,
INIT_GENERAL_DATA_ACTION,
IS_BRIDGE_LOADED,
IS_VAULT_CLIENT_LOADED,
SHOW_ACCOUNT_MODAL,
SHOW_BUY_MODAL,
SHOW_SIGN_TERMS_MODAL,
UPDATE_HEIGHTS,
UPDATE_TOTALS,
UPDATE_TRANSACTION_MODAL_STATUS
} from '../types/actions.types';
import { GeneralState, ParachainStatus } from '../types/util.types';
import { GeneralState } from '../types/util.types';

const initialState = {
bridgeLoaded: false,
Expand All @@ -26,11 +19,6 @@ const initialState = {
showAccountModal: false,
isBuyModalOpen: false,
isSignTermsModalOpen: false,
totalWrappedTokenAmount: BitcoinAmount.zero(),
totalLockedCollateralTokenAmount: newMonetaryAmount(0, RELAY_CHAIN_NATIVE_TOKEN),
btcRelayHeight: 0,
bitcoinHeight: 0,
parachainStatus: ParachainStatus.Loading,
prices: {
bitcoin: { usd: 0 },
relayChainNativeToken: { usd: 0 },
Expand All @@ -46,25 +34,8 @@ const initialState = {

export const generalReducer = (state: GeneralState = initialState, action: GeneralActions): GeneralState => {
switch (action.type) {
case UPDATE_TOTALS:
return {
...state,
totalWrappedTokenAmount: action.totalWrappedTokenAmount,
totalLockedCollateralTokenAmount: action.totalLockedCollateralTokenAmount
};
case UPDATE_HEIGHTS:
return { ...state, btcRelayHeight: action.btcRelayHeight, bitcoinHeight: action.bitcoinHeight };
case IS_BRIDGE_LOADED:
return { ...state, bridgeLoaded: action.isLoaded };
case INIT_GENERAL_DATA_ACTION:
return {
...state,
totalLockedCollateralTokenAmount: action.totalLockedCollateralTokenAmount,
totalWrappedTokenAmount: action.totalWrappedTokenAmount,
btcRelayHeight: action.btcRelayHeight,
bitcoinHeight: action.bitcoinHeight,
parachainStatus: action.parachainStatus
};
case IS_VAULT_CLIENT_LOADED:
return { ...state, vaultClientLoaded: action.isLoaded };
case SHOW_ACCOUNT_MODAL:
Expand Down
32 changes: 1 addition & 31 deletions src/common/types/actions.types.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,24 @@
import { CollateralCurrencyExt } from '@interlay/interbtc-api';
import { BitcoinAmount, MonetaryAmount } from '@interlay/monetary-js';

import { GovernanceTokenMonetaryAmount } from '@/config/relay-chains';

import { Notification, ParachainStatus, StoreType, TransactionModalData } from './util.types';
import { Notification, StoreType, TransactionModalData } from './util.types';

// GENERAL ACTIONS
export const IS_BRIDGE_LOADED = 'IS_BRIDGE_LOADED';
export const IS_FAUCET_LOADED = 'IS_FAUCET_LOADED';
export const IS_VAULT_CLIENT_LOADED = 'IS_VAULT_CLIENT_LOADED';
export const INIT_STATE = 'INIT_STATE';
export const INIT_GENERAL_DATA_ACTION = 'INIT_GENERAL_DATA_ACTION';
export const UPDATE_BALANCE_POLKA_BTC = 'UPDATE_BALANCE_POLKA_BTC';
export const UPDATE_WRAPPED_TOKEN_TRANSFERABLE_BALANCE = 'UPDATE_WRAPPED_TOKEN_TRANSFERABLE_BALANCE';
export const UPDATE_COLLATERAL_TOKEN_BALANCE = 'UPDATE_COLLATERAL_TOKEN_BALANCE';
export const UPDATE_COLLATERAL_TOKEN_TRANSFERABLE_BALANCE = 'UPDATE_COLLATERAL_TOKEN_TRANSFERABLE_BALANCE';
export const SHOW_ACCOUNT_MODAL = 'SHOW_ACCOUNT_MODAL';
export const SHOW_SIGN_TERMS_MODAL = 'SHOW_SIGN_TERMS_MODAL';
export const UPDATE_HEIGHTS = 'UPDATE_HEIGHTS';
export const UPDATE_TOTALS = 'UPDATE_TOTALS';
export const SHOW_BUY_MODAL = 'SHOW_BUY_MODAL';
export const ADD_NOTIFICATION = 'ADD_NOTIFICATION';
export const SHOW_TRANSACTION_MODAL = 'SHOW_TRANSACTION_MODAL';
export const UPDATE_TRANSACTION_MODAL_STATUS = 'UPDATE_TRANSACTION_MODAL_STATUS';

export interface UpdateTotals {
type: typeof UPDATE_TOTALS;
totalLockedCollateralTokenAmount: MonetaryAmount<CollateralCurrencyExt>;
totalWrappedTokenAmount: BitcoinAmount;
}

export interface UpdateHeights {
type: typeof UPDATE_HEIGHTS;
btcRelayHeight: number;
bitcoinHeight: number;
}

export interface IsBridgeLoaded {
type: typeof IS_BRIDGE_LOADED;
isLoaded: boolean;
Expand All @@ -56,16 +39,6 @@ export interface InitState {
state: StoreType;
}

export interface InitGeneralDataAction {
type: typeof INIT_GENERAL_DATA_ACTION;
totalWrappedTokenAmount: BitcoinAmount;
totalLockedCollateralTokenAmount: MonetaryAmount<CollateralCurrencyExt>;
totalGovernanceTokenAmount: GovernanceTokenMonetaryAmount;
btcRelayHeight: number;
bitcoinHeight: number;
parachainStatus: ParachainStatus;
}

export interface UpdateBalancePolkaBTC {
type: typeof UPDATE_BALANCE_POLKA_BTC;
wrappedTokenBalance: BitcoinAmount;
Expand Down Expand Up @@ -115,15 +88,12 @@ export interface UpdateTransactionModal {

export type GeneralActions =
| IsBridgeLoaded
| InitGeneralDataAction
| IsVaultClientLoaded
| UpdateBalancePolkaBTC
| UpdateWrappedTokenTransferableBalance
| UpdateCollateralTokenBalance
| UpdateCollateralTokenTransferableBalance
| ShowAccountModal
| UpdateHeights
| UpdateTotals
| ShowBuyModal
| ShowSignTermsModal
| AddNotification
Expand Down
Loading

2 comments on commit 5336dc7

@vercel
Copy link

@vercel vercel bot commented on 5336dc7 Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 5336dc7 Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.