Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Move handling of Constants from Midgard to TN (#2418)
Browse files Browse the repository at this point in the history
- [x] Move `thorchainConstantsState$`, `reloadThorchainConstants` from `services/midgard` to `services/thorchain/thornode`
- [x] Remove `nativeFee$` (native fees are handled in `xchain-thorchain`)
- [x] Update `usePoolCycle` hook to validate data of `PoolCycle` properly
  • Loading branch information
veado authored and sergey.soltys committed Nov 2, 2022
1 parent 30f8b52 commit ef2f0fe
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 63 deletions.
6 changes: 6 additions & 0 deletions src/renderer/contexts/ThorchainContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
reloadMimir,
inboundAddressesShared$,
reloadInboundAddresses,
reloadThorchainConstants,
thorchainConstantsState$,
getLiquidityProviders,
reloadLiquidityProviders
} from '../services/thorchain'
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -83,6 +87,8 @@ const initialContext: ThorchainContextValue = {
reloadMimir,
inboundAddressesShared$,
reloadInboundAddresses,
reloadThorchainConstants,
thorchainConstantsState$,
getLiquidityProviders,
reloadLiquidityProviders
}
Expand Down
16 changes: 10 additions & 6 deletions src/renderer/hooks/usePoolCycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -24,16 +23,21 @@ export const usePoolCycle = (): {
reloadPoolCycle: FP.Lazy<void>
} => {
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<Error, number>(
() => !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$]
)
Expand Down
48 changes: 0 additions & 48 deletions src/renderer/services/midgard/service.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'
Expand All @@ -25,10 +23,8 @@ import { createSharesService } from './shares'
import {
NetworkInfoRD,
NetworkInfoLD,
ThorchainConstantsLD,
MidgardUrlLD,
ThorchainLastblockLD,
NativeFeeLD,
HealthLD,
ValidateNodeLD,
CheckMidgardUrlHandler,
Expand Down Expand Up @@ -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`
*/
Expand Down Expand Up @@ -234,10 +192,7 @@ export const checkMidgardUrl$: CheckMidgardUrlHandler = (url: string, intl: Intl
export type MidgardService = {
networkInfo$: NetworkInfoLD
reloadNetworkInfo: FP.Lazy<void>
reloadThorchainConstants: FP.Lazy<void>
thorchainConstantsState$: ThorchainConstantsLD
thorchainLastblockState$: ThorchainLastblockLD
nativeTxFee$: NativeFeeLD
reloadThorchainLastblock: FP.Lazy<void>
setSelectedPoolAsset: (p: SelectedPoolAsset) => void
selectedPoolAsset$: Rx.Observable<SelectedPoolAsset>
Expand All @@ -258,10 +213,7 @@ export type MidgardService = {
export const service: MidgardService = {
networkInfo$,
reloadNetworkInfo,
reloadThorchainConstants,
thorchainConstantsState$,
thorchainLastblockState$,
nativeTxFee$,
reloadThorchainLastblock,
reloadChartDataUI,
reloadChartDataUI$,
Expand Down
8 changes: 0 additions & 8 deletions src/renderer/services/midgard/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -99,13 +98,6 @@ export type LastblockItems = LastblockItem[]
export type ThorchainLastblockRD = RD.RemoteData<Error, LastblockItems>
export type ThorchainLastblockLD = LiveData<Error, LastblockItems>

export type ThorchainConstantsRD = RD.RemoteData<Error, ThorchainConstants>
export type ThorchainConstantsLD = LiveData<Error, ThorchainConstants>

export type NativeFee = O.Option<BaseAmount>
export type NativeFeeRD = RD.RemoteData<Error, NativeFee>
export type NativeFeeLD = LiveData<Error, NativeFee>

export type HaltedChainsRD = RD.RemoteData<Error, Chain[]>
export type HaltedChainsLD = LiveData<Error, Chain[]>

Expand Down
4 changes: 4 additions & 0 deletions src/renderer/services/thorchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const {
reloadThornodeUrl,
getNodeInfos$,
reloadNodeInfos,
reloadThorchainConstants,
thorchainConstantsState$,
inboundAddressesShared$,
loadInboundAddresses$,
reloadInboundAddresses,
Expand Down Expand Up @@ -73,6 +75,8 @@ export {
interact$,
getNodeInfos$,
reloadNodeInfos,
reloadThorchainConstants,
thorchainConstantsState$,
mimir$,
reloadMimir,
getLiquidityProviders,
Expand Down
33 changes: 32 additions & 1 deletion src/renderer/services/thorchain/thornode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {
NodeInfosLD,
NodeInfos,
ClientUrl$,
InboundAddressesLD
InboundAddressesLD,
ThorchainConstantsLD
} from './types'

export const createThornodeService$ = (network$: Network$, clientUrl$: ClientUrl$) => {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -202,6 +231,8 @@ export const createThornodeService$ = (network$: Network$, clientUrl$: ClientUrl
reloadThornodeUrl,
getNodeInfos$,
reloadNodeInfos,
reloadThorchainConstants,
thorchainConstantsState$,
inboundAddressesShared$,
reloadInboundAddresses,
loadInboundAddresses$,
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/services/thorchain/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -29,6 +30,13 @@ export type InboundAddressRD = RD.RemoteData<Error, InboundAddresses>
export type InboundAddresses = InboundAddress[]
export type InboundAddressesLD = LiveData<Error, InboundAddresses>

export type ThorchainConstantsRD = RD.RemoteData<Error, ConstantsResponse>
export type ThorchainConstantsLD = LiveData<Error, ConstantsResponse>

export type NativeFee = O.Option<BaseAmount>
export type NativeFeeRD = RD.RemoteData<Error, NativeFee>
export type NativeFeeLD = LiveData<Error, NativeFee>

export type Client$ = C.Client$<Client>

export type ClientState = C.ClientState<Client>
Expand Down

0 comments on commit ef2f0fe

Please sign in to comment.