withBalance.js 1.5 KiB
Newer Older
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
Amaury Martiny's avatar
Amaury Martiny committed
// SPDX-License-Identifier: BSD-3-Clause
Amaury Martiny's avatar
Amaury Martiny committed
import abi from '@parity/contracts/lib/abi/eip20';
import { balanceOf$, makeContract } from '@parity/light.js';
Axel Chalon's avatar
Axel Chalon committed
import branch from 'recompose/branch';
import compose from 'recompose/compose';
import { fromWei } from '@parity/api/lib/util/wei';
import light from '@parity/light.js-react';
import { map, startWith } from 'rxjs/operators';
Axel Chalon's avatar
Axel Chalon committed
import withProps from 'recompose/withProps';

export const withErc20Balance = light({
  erc20Balance: ({ token, account: { address } }) =>
Axel Chalon's avatar
Axel Chalon committed
    makeContract(token.address, abi)
      .balanceOf$(address)
      .pipe(
        map(value => value && value.div(10 ** token.decimals)),
        startWith(undefined)
      )
});

export const withEthBalance = light({
  ethBalance: ({ account: { address } }) =>
    balanceOf$(address).pipe(
      map(value => value && fromWei(value)),
      startWith(undefined)
    )
 * A HOC on light.js to get the current balance of the account.
 *
 * The component needs to receive a `token` prop as well as an
 * `account: {address}` prop (i.e. needs to be decorated with withAccount).
 *
 * @example
 * @withAccount
 * @withBalance
 * class MyComponent extends React.Component{
 *
 * }
 */
export default compose(
  branch(
Axel Chalon's avatar
Axel Chalon committed
    ({ token }) => token && token.address && token.address !== 'ETH',
    withErc20Balance,
    withEthBalance
  ),
  withProps(props => ({
    balance: props.erc20Balance || props.ethBalance
  }))
);