@exodus/fiat-balances
This Exodus SDK feature tracks fiat-denominated balances for all enabled wallet accounts, based on the application’s locale and user’s preferred currency. For crypto-denominated balances, check out @exodus/balances.
Install
npm i @exodus/fiat-balancesUsage
This feature is designed to be used together with @exodus/headless. See using the sdk.
API Side
See using the sdk for more details on how features plug into the SDK. Note that the balances feature currently doesn’t provide a top level API and is meant to be used purely through selectors.
If you’re building a feature that requires balances, add a dependency on the fiatBalancesAtom as below.
const myModuleDefinition = {
id: 'myModule',
type: 'module',
factory: ({ fiatBalancesAtom }) => {
const doSomethingSpecial = async () => {
const { balances } = await fiatBalancesAtom.get();
console.log(
'>>> total wallet balance',
balances.totals.balance.toDefaultString({ unit: true })
);
};
return {
doSomethingSpecial,
};
},
dependencies: ['fiatBalancesAtom'],
};UI Side
See using the sdk for more details on basic UI-side setup.
import { selectors } from '~/ui/flux';
const MyComponent = () => {
const totalWalletBalance = useSelector(
selectors.fiatBalances.optimisticTotalBalance(store.getState())
); // returns a NumberUnit
// create a function to convert ethereum to the user's preferred currency
const ethToFiat = useSelector(selectors.fiatBalances.createAssetConversion('ethereum'));
const ethBalanceInFiat = ethToFiat(ethereum.currency.defaultUnit(1));
};To read fiat balances aggregated by asset across all enabled wallet accounts, use selectors.fiatBalances.byAsset. The result respects the user’s preferred currency set via exodus.locale.setCurrency:
const BalanceList = () => {
const balancesByAsset = useSelector(selectors.fiatBalances.byAsset);
// { ethereum: NumberUnit, bitcoin: NumberUnit, ... }
// each NumberUnit formats in the user's preferred currency,
// e.g. balancesByAsset.ethereum.toDefaultString({ unit: true }) -> "123.45 USD"
};See tests for additional selector usage examples.
