Newer
Older
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import abi from '@parity/shared/lib/contracts/abi/eip20';
import { empty } from 'rxjs';
import {
defaultAccount$,
isNullOrLoading,
makeContract$,
myBalance$
} from '@parity/light.js';
import { filter, map, switchMap } from 'rxjs/operators';
import { fromWei } from '@parity/api/lib/util/wei';
import light from 'light-hoc';
/**
* A HOC on light.js to get the current balance.
*
* @example
* @withBalance
* class MyComponent extends React.Component{
*
* }
*/
export default (propsSelector = ({ token }) => token) =>
light({
balance: ownProps => {
// Find our token object in the props
const token = propsSelector(ownProps);
if (!token.address) {
return empty();
}
return token.address === 'ETH'
? myBalance$().pipe(
map(value => (isNullOrLoading(value) ? null : value)), // Transform loading state to null
map(value => value && fromWei(value))
)
: defaultAccount$().pipe(
filter(x => x),
switchMap(defaultAccount =>
makeContract$(token.address, abi).balanceOf$(defaultAccount)
),
map(value => (isNullOrLoading(value) ? null : value)), // Transform loading state to null
map(value => value && value.div(10 ** token.decimals))
);
}
});