generated from jimmychu0807/substrate-front-end-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBalances.js
35 lines (30 loc) · 1.04 KB
/
Balances.js
1
2
3
4
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
import React, {useEffect, useState} from 'react'
import {useSubstrateState} from './substrate-lib'
import { formatBalance } from '@polkadot/util';
const acctAddr = acct => (acct ? acct.address : '')
export default function Main(props) {
const {api, currentAccount} = useSubstrateState()
const [accountBalance, setAccountBalance] = useState('none')
formatBalance.setDefaults({
decimals: 10,
unit: 'PAS'
});
// When account address changes, update subscriptions
useEffect(() => {
let unsubscribe
// If the user has selected an address, create a new subscription
currentAccount &&
api.query.system
.account(acctAddr(currentAccount), balance =>
setAccountBalance(formatBalance(balance.data.free))
)
.then(unsub => (unsubscribe = unsub))
.catch(console.error)
return () => unsubscribe && unsubscribe()
}, [api, currentAccount])
return (
<div>
My L1 Balance: {accountBalance}
</div>
)
}