@exodus/multi-account-redux
Selectors and utilities for managing state with the following shape:
{
[yourStateSliceName]: {
[walletAccount]: {
error: null,
loaded: true,
data: {
[assetName]: {}, // TxSet/UtxoCollection/etc.
},
},
}
}
Install
pnpm add @exodus/multi-account-redux
Usage
import { TxSet } from '@exodus/models'
import { createReduxModuleHelper } from '@exodus/multi-account-redux'
const helper = createReduxModuleHelper({
slice: 'someAssetSourceRelatedData',
createInitialPerAssetData: () => TxSet.EMPTY,
})
const myReduxModule = {
id: 'someAssetSourceRelatedData',
initialState: helper.createInitialState(),
eventReducers: {
someAssetSourceRelatedData: (state, data) => {
// check source code for other utils for merging/resetting state
return helper.setAccounts(state, data)
},
},
selectorDefinitions: [...helper.selectorDefinitions, ...customSelectorDefinitions],
}
TypeScript projection inference
Let the helper infer per-asset data from the initializer. Projection parameters, selector names, and return types are then inferred without annotating each selector:
const helper = createReduxModuleHelper({
slice: 'balances',
createInitialPerAssetData: () => ({ balance: 0 }),
assetSourceDataSelectors: [
{ name: 'balance', selector: (data) => data?.balance ?? 0 },
{ name: 'label', selector: (data) => String(data?.balance ?? 0) },
],
})
Projections receive null or undefined when an asset is unknown, or when no
createInitialPerAssetData is given and the asset has no stored data. Handle that case in each
selector.
When supplying explicit data or asset types, use two calls so projection types can still be inferred:
type Balance = { balance: number }
type Asset = { name: string; initialBalance: number }
const helper = createReduxModuleHelper<Balance, Asset>()({
slice: 'balances',
createInitialPerAssetData: ({ asset }) => ({ balance: asset.initialBalance }),
assetSourceDataSelectors: [{ name: 'balance', selector: (data) => data?.balance ?? 0 }],
})
The original createReduxModuleHelper<Balance>({...}) form remains supported. With projections,
it preserves the data type but widens projection names to strings and results to unknown.
Use the two-call form for precise projection types.