diff --git a/src/renderer/contexts/ThorchainContext.tsx b/src/renderer/contexts/ThorchainContext.tsx index 637d4cec5..c6d69436b 100644 --- a/src/renderer/contexts/ThorchainContext.tsx +++ b/src/renderer/contexts/ThorchainContext.tsx @@ -25,6 +25,8 @@ import { reloadMimir, inboundAddressesShared$, reloadInboundAddresses, + reloadThorchainConstants, + thorchainConstantsState$, getLiquidityProviders, reloadLiquidityProviders } from '../services/thorchain' @@ -54,6 +56,8 @@ export type ThorchainContextValue = { reloadMimir: typeof reloadMimir inboundAddressesShared$: typeof inboundAddressesShared$ reloadInboundAddresses: typeof reloadInboundAddresses + thorchainConstantsState$: typeof thorchainConstantsState$ + reloadThorchainConstants: typeof reloadThorchainConstants getLiquidityProviders: typeof getLiquidityProviders reloadLiquidityProviders: typeof reloadLiquidityProviders } @@ -83,6 +87,8 @@ const initialContext: ThorchainContextValue = { reloadMimir, inboundAddressesShared$, reloadInboundAddresses, + reloadThorchainConstants, + thorchainConstantsState$, getLiquidityProviders, reloadLiquidityProviders } diff --git a/src/renderer/hooks/usePoolCycle.ts b/src/renderer/hooks/usePoolCycle.ts index 7b6bbdea3..0caf61716 100644 --- a/src/renderer/hooks/usePoolCycle.ts +++ b/src/renderer/hooks/usePoolCycle.ts @@ -8,7 +8,6 @@ import { useObservableState } from 'observable-hooks' import * as Rx from 'rxjs' import * as RxOp from 'rxjs/operators' -import { useMidgardContext } from '../contexts/MidgardContext' import { useThorchainContext } from '../contexts/ThorchainContext' import { eqApiError } from '../helpers/fp/eq' import { LiveData, liveData } from '../helpers/rx/liveData' @@ -24,16 +23,21 @@ export const usePoolCycle = (): { reloadPoolCycle: FP.Lazy } => { const { mimir$, reloadMimir } = useThorchainContext() - const { - service: { thorchainConstantsState$, reloadThorchainConstants } - } = useMidgardContext() + const { thorchainConstantsState$, reloadThorchainConstants } = useThorchainContext() const midgardConstantsPoolCycle$: PoolCycleLD = useMemo( () => FP.pipe( thorchainConstantsState$, - liveData.map(({ int_64_values }) => int_64_values.PoolCycle), - liveData.mapLeft(() => ({ errorId: ErrorId.GET_POOL_CYCLE, msg: 'Unable to load pool cycle from Midgard' })) + liveData.map(({ int64_values }) => Number(int64_values?.PoolCycle)), + liveData.chain((poolCycle) => + // validation -> value needs to be a number + liveData.fromPredicate( + () => !isNaN(poolCycle), + () => Error(`Invalid value of constant 'PoolCycle' ${poolCycle} `) + )(poolCycle) + ), + liveData.mapLeft(() => ({ errorId: ErrorId.GET_POOL_CYCLE, msg: 'Unable to get constant of PoolCycle' })) ), [thorchainConstantsState$] ) diff --git a/src/renderer/services/midgard/service.ts b/src/renderer/services/midgard/service.ts index 8967daa4a..e46537d67 100644 --- a/src/renderer/services/midgard/service.ts +++ b/src/renderer/services/midgard/service.ts @@ -1,5 +1,4 @@ import * as RD from '@devexperts/remote-data-ts' -import { baseAmount } from '@xchainjs/xchain-util' import * as FP from 'fp-ts/function' import * as O from 'fp-ts/Option' import { IntlShape } from 'react-intl' @@ -8,7 +7,6 @@ import * as RxOp from 'rxjs/operators' import { ApiUrls, Network } from '../../../shared/api/types' import { DEFAULT_MIDGARD_URLS } from '../../../shared/midgard/const' -import { THORCHAIN_DECIMAL } from '../../helpers/assetHelper' import { eqApiUrls } from '../../helpers/fp/eq' import { liveData } from '../../helpers/rx/liveData' import { triggerStream, TriggerStream$ } from '../../helpers/stateHelper' @@ -25,10 +23,8 @@ import { createSharesService } from './shares' import { NetworkInfoRD, NetworkInfoLD, - ThorchainConstantsLD, MidgardUrlLD, ThorchainLastblockLD, - NativeFeeLD, HealthLD, ValidateNodeLD, CheckMidgardUrlHandler, @@ -124,44 +120,6 @@ const thorchainLastblockState$: ThorchainLastblockLD = FP.pipe( RxOp.shareReplay(1) ) -/** - * Get `ThorchainConstants` data from Midgard - */ -const apiGetThorchainConstants$ = FP.pipe( - midgardUrl$, - liveData.chain((endpoint) => - FP.pipe( - getMidgardDefaultApi(endpoint).getProxiedConstants(), - RxOp.map(RD.success), - RxOp.catchError((e: Error) => Rx.of(RD.failure(e))) - ) - ) -) - -const { stream$: reloadThorchainConstants$, trigger: reloadThorchainConstants } = triggerStream() - -/** - * Provides data of `ThorchainConstants` - */ -const thorchainConstantsState$: ThorchainConstantsLD = FP.pipe( - reloadThorchainConstants$, - RxOp.debounceTime(300), - RxOp.switchMap(() => apiGetThorchainConstants$), - RxOp.startWith(RD.pending), - RxOp.retry(MIDGARD_MAX_RETRY), - RxOp.shareReplay(1), - RxOp.catchError(() => Rx.of(RD.failure(Error('Failed to load Thorchain constants')))) -) - -const nativeTxFee$: NativeFeeLD = thorchainConstantsState$.pipe( - liveData.map((constants) => - FP.pipe( - O.fromNullable(constants.int_64_values?.NativeTransactionFee), - O.map((value) => baseAmount(value, THORCHAIN_DECIMAL)) - ) - ) -) - /** * Loads data of `NetworkInfo` */ @@ -234,10 +192,7 @@ export const checkMidgardUrl$: CheckMidgardUrlHandler = (url: string, intl: Intl export type MidgardService = { networkInfo$: NetworkInfoLD reloadNetworkInfo: FP.Lazy - reloadThorchainConstants: FP.Lazy - thorchainConstantsState$: ThorchainConstantsLD thorchainLastblockState$: ThorchainLastblockLD - nativeTxFee$: NativeFeeLD reloadThorchainLastblock: FP.Lazy setSelectedPoolAsset: (p: SelectedPoolAsset) => void selectedPoolAsset$: Rx.Observable @@ -258,10 +213,7 @@ export type MidgardService = { export const service: MidgardService = { networkInfo$, reloadNetworkInfo, - reloadThorchainConstants, - thorchainConstantsState$, thorchainLastblockState$, - nativeTxFee$, reloadThorchainLastblock, reloadChartDataUI, reloadChartDataUI$, diff --git a/src/renderer/services/midgard/types.ts b/src/renderer/services/midgard/types.ts index ef0f09552..1e749ee3a 100644 --- a/src/renderer/services/midgard/types.ts +++ b/src/renderer/services/midgard/types.ts @@ -12,7 +12,6 @@ import { LiveData } from '../../helpers/rx/liveData' import { AssetWithAmount, DepositType } from '../../types/asgardex' import { Network as NetworkInfo, - Constants as ThorchainConstants, LastblockItem, PoolDetail, Health, @@ -99,13 +98,6 @@ export type LastblockItems = LastblockItem[] export type ThorchainLastblockRD = RD.RemoteData export type ThorchainLastblockLD = LiveData -export type ThorchainConstantsRD = RD.RemoteData -export type ThorchainConstantsLD = LiveData - -export type NativeFee = O.Option -export type NativeFeeRD = RD.RemoteData -export type NativeFeeLD = LiveData - export type HaltedChainsRD = RD.RemoteData export type HaltedChainsLD = LiveData diff --git a/src/renderer/services/thorchain/index.ts b/src/renderer/services/thorchain/index.ts index 62e234b6b..3e160d7a5 100644 --- a/src/renderer/services/thorchain/index.ts +++ b/src/renderer/services/thorchain/index.ts @@ -23,6 +23,8 @@ const { reloadThornodeUrl, getNodeInfos$, reloadNodeInfos, + reloadThorchainConstants, + thorchainConstantsState$, inboundAddressesShared$, loadInboundAddresses$, reloadInboundAddresses, @@ -73,6 +75,8 @@ export { interact$, getNodeInfos$, reloadNodeInfos, + reloadThorchainConstants, + thorchainConstantsState$, mimir$, reloadMimir, getLiquidityProviders, diff --git a/src/renderer/services/thorchain/thornode.ts b/src/renderer/services/thorchain/thornode.ts index db649689a..d6c790c16 100644 --- a/src/renderer/services/thorchain/thornode.ts +++ b/src/renderer/services/thorchain/thornode.ts @@ -24,7 +24,8 @@ import { NodeInfosLD, NodeInfos, ClientUrl$, - InboundAddressesLD + InboundAddressesLD, + ThorchainConstantsLD } from './types' export const createThornodeService$ = (network$: Network$, clientUrl$: ClientUrl$) => { @@ -96,6 +97,34 @@ export const createThornodeService$ = (network$: Network$, clientUrl$: ClientUrl RxOp.shareReplay(1) ) + /** + * Get `ThorchainConstants` data from Midgard + */ + const apiGetThorchainConstants$ = FP.pipe( + thornodeUrl$, + liveData.chain((basePath) => + FP.pipe( + new NetworkApi(new Configuration({ basePath })).constants({}), + RxOp.map(RD.success), + RxOp.catchError((e: Error) => Rx.of(RD.failure(e))) + ) + ) + ) + + const { stream$: reloadThorchainConstants$, trigger: reloadThorchainConstants } = triggerStream() + + /** + * Provides data of `ThorchainConstants` + */ + const thorchainConstantsState$: ThorchainConstantsLD = FP.pipe( + reloadThorchainConstants$, + RxOp.debounceTime(300), + RxOp.switchMap(() => apiGetThorchainConstants$), + RxOp.startWith(RD.pending), + RxOp.shareReplay(1), + RxOp.catchError(() => Rx.of(RD.failure(Error('Failed to load THORChain constants')))) + ) + const { stream$: reloadNodeInfos$, trigger: reloadNodeInfos } = triggerStream() const getNodeInfos$: NodeInfosLD = FP.pipe( @@ -202,6 +231,8 @@ export const createThornodeService$ = (network$: Network$, clientUrl$: ClientUrl reloadThornodeUrl, getNodeInfos$, reloadNodeInfos, + reloadThorchainConstants, + thorchainConstantsState$, inboundAddressesShared$, reloadInboundAddresses, loadInboundAddresses$, diff --git a/src/renderer/services/thorchain/types.ts b/src/renderer/services/thorchain/types.ts index ef5a6bf05..c837a5aa8 100644 --- a/src/renderer/services/thorchain/types.ts +++ b/src/renderer/services/thorchain/types.ts @@ -12,6 +12,7 @@ import { HDMode, WalletType } from '../../../shared/wallet/types' import { LiveData } from '../../helpers/rx/liveData' import { AssetsWithAmount1e8, AssetWithAmount1e8 } from '../../types/asgardex' import * as TN from '../../types/generated/thornode' +import { ConstantsResponse } from '../../types/generated/thornode' import * as C from '../clients' import { TxHashLD, TxHashRD } from '../wallet/types' @@ -29,6 +30,13 @@ export type InboundAddressRD = RD.RemoteData export type InboundAddresses = InboundAddress[] export type InboundAddressesLD = LiveData +export type ThorchainConstantsRD = RD.RemoteData +export type ThorchainConstantsLD = LiveData + +export type NativeFee = O.Option +export type NativeFeeRD = RD.RemoteData +export type NativeFeeLD = LiveData + export type Client$ = C.Client$ export type ClientState = C.ClientState