@exodus/balances
Install
yarn add @exodus/balances
Usage
Like all Exodus SDK features, the balances
feature consists of:
- A feature that plugs into the Exodus SDK in your logical or actual background process. This provides the
balancesAtom
to other features. - A redux module that plugs into the Exodus SDK in the UI process. This provides selectors for getting various balances.
Setup
In the background process, plug balances
into the SDK:
import balances from '@exodus/balances'
sdk.use(balances())
In the UI process, plug the balances
redux module into the SDK:
import { setupRedux } from '@exodus/redux-dependency-injection'
import balances from '@exodus/balances/redux'
const { selectors, createHandleEvent, reducers, initialState } = setupRedux({
dependencies: [
balances,
],
})
const handleEvent = createHandleEvent(store)
Connect the background process to the UI process. In a multi-process app, you will need a transport layer:
sdk.subscribe(({ type, payload }) => handleEvent(type, payload))
Consuming Balances
In your feature you can now depend on the balancesAtom
const myModuleFactory = ({ balancesAtom }) => {
const doSomethingSpecial = async () => {
const { balances } = await balancesAtom.get()
}
return {
doSomethingSpecial,
}
}
const myModuleDefinition = {
id: 'myModule',
type: 'module',
factory: myModuleFactory,
dependencies: ['balancesAtom'],
}
const MyComponent = () => {
const bitcoinBalance = useSelector(
selectors.balances.createSpendable({
assetName: 'bitcoin',
walletAccount: 'exodus_0',
})
)
}
Playground
Try the balances selectors in the sdk-playground.
In this repo root, run:
yarn --cwd apps/sdk-playground dev
Open http://localhost:8008
in the browser and run the following in the dev tools console:
await exodus.debug.addressProvider.mockAddress({
assetName: 'ethereum',
walletAccount: 'exodus_0',
address: '0x1111111111111111111111111111111111111111',
})
await exodus.txLogMonitors.update({ assetName: 'ethereum', refresh: true })
selectors.balances.createTotal({ assetName: 'ethereum', walletAccount: 'exodus_0' })(
store.getState()
)
Contributing
WARNING: no asset specifics!
If you introduce any asset specifics like if (assetName === 'dogicorn') balance = balance.mul(2)
, you will be fired immediately. This is a safety measure to save you from @feri42's wrath. All asset-specifics belong in asset.api.getBalances
or other APIs inside the asset libraries.