-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
368 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Loadable } from './loadable'; | ||
|
||
export function mapLoadableData<T, R>(load: Loadable<T>, map: (obj: T) => R): Loadable<R> { | ||
return { | ||
...load, | ||
data: load.data != null ? map(load.data) : undefined, | ||
} as Loadable<R>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { timeUnits } from '@/constants/time'; | ||
|
||
import { type RootStore } from '@/state/_store'; | ||
import { setIndexerHeightRaw, setValidatorHeightRaw } from '@/state/raw'; | ||
|
||
import { loadableIdle } from '../lib/loadable'; | ||
import { mapLoadableData } from '../lib/mapLoadable'; | ||
import { | ||
createIndexerQueryStoreEffect, | ||
createValidatorQueryStoreEffect, | ||
} from './lib/indexerQueryStoreEffect'; | ||
import { queryResultToLoadable } from './lib/queryResultToLoadable'; | ||
|
||
export function setUpIndexerHeightQuery(store: RootStore) { | ||
const cleanupEffect = createIndexerQueryStoreEffect(store, { | ||
selector: () => true, | ||
getQueryKey: () => ['height'], | ||
getQueryFn: (indexerClient) => { | ||
return () => indexerClient.utility.getHeight(); | ||
}, | ||
onResult: (height) => { | ||
store.dispatch(setIndexerHeightRaw(queryResultToLoadable(height))); | ||
}, | ||
onNoQuery: () => store.dispatch(setIndexerHeightRaw(loadableIdle())), | ||
refetchInterval: timeUnits.second * 10, | ||
staleTime: timeUnits.second * 10, | ||
}); | ||
return () => { | ||
cleanupEffect(); | ||
store.dispatch(setIndexerHeightRaw(loadableIdle())); | ||
}; | ||
} | ||
|
||
export function setUpValidatorHeightQuery(store: RootStore) { | ||
const cleanupEffect = createValidatorQueryStoreEffect(store, { | ||
selector: () => true, | ||
getQueryKey: () => ['height'], | ||
getQueryFn: (compositeClient) => { | ||
return () => compositeClient.validatorClient.get.latestBlock(); | ||
}, | ||
onResult: (height) => { | ||
store.dispatch( | ||
setValidatorHeightRaw( | ||
mapLoadableData(queryResultToLoadable(height), (d) => ({ | ||
height: d.header.height, | ||
time: d.header.time, | ||
})) | ||
) | ||
); | ||
}, | ||
onNoQuery: () => store.dispatch(setValidatorHeightRaw(loadableIdle())), | ||
refetchInterval: timeUnits.second * 10, | ||
staleTime: timeUnits.second * 10, | ||
}); | ||
return () => { | ||
cleanupEffect(); | ||
store.dispatch(setValidatorHeightRaw(loadableIdle())); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import { createStoreEffect } from '@/abacus-ts/lib/createStoreEffect'; | ||
import { ResourceCacheManager } from '@/abacus-ts/lib/resourceCacheManager'; | ||
import { | ||
CompositeClient, | ||
IndexerClient, | ||
IndexerConfig, | ||
Network, | ||
NetworkOptimizer, | ||
ValidatorConfig, | ||
} from '@dydxprotocol/v4-client-js'; | ||
|
||
import { DEFAULT_TRANSACTION_MEMO } from '@/constants/analytics'; | ||
import { | ||
DydxChainId, | ||
DydxNetwork, | ||
ENVIRONMENT_CONFIG_MAP, | ||
TOKEN_CONFIG_MAP, | ||
} from '@/constants/networks'; | ||
|
||
import { type RootStore } from '@/state/_store'; | ||
import { getSelectedNetwork } from '@/state/appSelectors'; | ||
import { setNetworkStateRaw } from '@/state/raw'; | ||
|
||
import { getStatsigConfigAsync } from '@/lib/statsig'; | ||
|
||
type CompositeClientWrapper = { | ||
dead?: boolean; | ||
compositeClient?: CompositeClient; | ||
indexer?: IndexerClient; | ||
tearDown: () => void; | ||
}; | ||
|
||
function makeCompositeClient({ | ||
network, | ||
store, | ||
}: { | ||
network: DydxNetwork; | ||
store: RootStore; | ||
}): CompositeClientWrapper { | ||
const networkConfig = ENVIRONMENT_CONFIG_MAP[network]; | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (!networkConfig) { | ||
throw new Error(`Unknown network: ${network}`); | ||
} | ||
const chainId = networkConfig.dydxChainId as DydxChainId; | ||
const tokens = TOKEN_CONFIG_MAP[chainId]; | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (tokens == null) { | ||
throw new Error(`Unknown chain id: ${chainId}`); | ||
} | ||
|
||
const clientWrapper: CompositeClientWrapper = { | ||
tearDown: () => { | ||
clientWrapper.dead = true; | ||
store.dispatch( | ||
setNetworkStateRaw({ | ||
networkId: network, | ||
stateToMerge: { compositeClientReady: false, indexerClientReady: false }, | ||
}) | ||
); | ||
}, | ||
}; | ||
|
||
store.dispatch( | ||
setNetworkStateRaw({ | ||
networkId: network, | ||
stateToMerge: { compositeClientReady: false, indexerClientReady: false }, | ||
}) | ||
); | ||
|
||
(async () => { | ||
const networkOptimizer = new NetworkOptimizer(); | ||
const indexerUrl = networkConfig.endpoints.indexers[0]; | ||
if (indexerUrl == null) { | ||
throw new Error('No indexer urls found'); | ||
} | ||
const validatorUrl = await networkOptimizer.findOptimalNode( | ||
networkConfig.endpoints.validators, | ||
chainId | ||
); | ||
if (clientWrapper.dead) { | ||
return; | ||
} | ||
const indexerConfig = new IndexerConfig(indexerUrl.api, indexerUrl.socket); | ||
clientWrapper.indexer = new IndexerClient(indexerConfig); | ||
store.dispatch( | ||
setNetworkStateRaw({ | ||
networkId: network, | ||
stateToMerge: { indexerClientReady: true }, | ||
}) | ||
); | ||
const statsigFlags = await getStatsigConfigAsync(); | ||
const compositeClient = await CompositeClient.connect( | ||
new Network( | ||
chainId, | ||
indexerConfig, | ||
new ValidatorConfig( | ||
validatorUrl, | ||
chainId, | ||
{ | ||
USDC_DENOM: tokens.usdc.denom, | ||
USDC_DECIMALS: tokens.usdc.decimals, | ||
USDC_GAS_DENOM: tokens.usdc.gasDenom, | ||
CHAINTOKEN_DENOM: tokens.chain.denom, | ||
CHAINTOKEN_DECIMALS: tokens.chain.decimals, | ||
}, | ||
{ | ||
broadcastPollIntervalMs: 3_000, | ||
broadcastTimeoutMs: 60_000, | ||
}, | ||
DEFAULT_TRANSACTION_MEMO, | ||
statsigFlags.ff_enable_timestamp_nonce | ||
) | ||
) | ||
); | ||
// this shouldn't be necessary - can actually be false | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (clientWrapper.dead) { | ||
return; | ||
} | ||
clientWrapper.compositeClient = compositeClient; | ||
store.dispatch( | ||
setNetworkStateRaw({ | ||
networkId: network, | ||
stateToMerge: { compositeClientReady: true }, | ||
}) | ||
); | ||
})(); | ||
return clientWrapper; | ||
} | ||
|
||
export const CompositeClientManager = new ResourceCacheManager({ | ||
constructor: (config: { network: DydxNetwork; store: RootStore }) => makeCompositeClient(config), | ||
destroyer: (instance) => { | ||
instance.tearDown(); | ||
}, | ||
// store not part of serialization, assumed immutable | ||
keySerializer: ({ network }) => network, | ||
}); | ||
|
||
// this just makes things simpler | ||
export function alwaysUseCurrentNetworkClient(store: RootStore) { | ||
return createStoreEffect(store, getSelectedNetwork, (network) => { | ||
CompositeClientManager.use({ network, store }); | ||
return () => { | ||
CompositeClientManager.markDone({ network, store }); | ||
}; | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Loadable } from '@/abacus-ts/lib/loadable'; | ||
import { QueryObserverResult } from '@tanstack/react-query'; | ||
|
||
export function queryResultToLoadable<T>(arg: QueryObserverResult<T>): Loadable<T> { | ||
return { | ||
status: arg.status, | ||
data: arg.data, | ||
error: arg.error, | ||
} as Loadable<T>; | ||
} |
Oops, something went wrong.