forked from jimmychu0807/substrate-front-end-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Balances.js
87 lines (82 loc) · 2.67 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import React, { useEffect, useState } from 'react'
import { Table, Grid, Button, Label } from 'semantic-ui-react'
import { CopyToClipboard } from 'react-copy-to-clipboard'
import { useSubstrateState } from './substrate-lib'
export default function Main(props) {
const { api, keyring } = useSubstrateState()
const accounts = keyring.getPairs()
const [balances, setBalances] = useState({})
useEffect(() => {
const addresses = keyring.getPairs().map(account => account.address)
let unsubscribeAll = null
api.query.system.account
.multi(addresses, balances => {
const balancesMap = addresses.reduce(
(acc, address, index) => ({
...acc,
[address]: balances[index].data.free.toHuman(),
}),
{}
)
setBalances(balancesMap)
})
.then(unsub => {
unsubscribeAll = unsub
})
.catch(console.error)
return () => unsubscribeAll && unsubscribeAll()
}, [api, keyring, setBalances])
return (
<Grid.Column>
<h1>Balances</h1>
{accounts.length === 0 ? (
<Label basic color="yellow">
No accounts to be shown
</Label>
) : (
<Table celled striped size="small">
<Table.Body>
<Table.Row>
<Table.Cell width={3} textAlign="right">
<strong>Name</strong>
</Table.Cell>
<Table.Cell width={10}>
<strong>Address</strong>
</Table.Cell>
<Table.Cell width={3}>
<strong>Balance</strong>
</Table.Cell>
</Table.Row>
{accounts.map(account => (
<Table.Row key={account.address}>
<Table.Cell width={3} textAlign="right">
{account.meta.name}
</Table.Cell>
<Table.Cell width={10}>
<span style={{ display: 'inline-block', minWidth: '31em' }}>
{account.address}
</span>
<CopyToClipboard text={account.address}>
<Button
basic
circular
compact
size="mini"
color="blue"
icon="copy outline"
/>
</CopyToClipboard>
</Table.Cell>
<Table.Cell width={3}>
{balances &&
balances[account.address] &&
balances[account.address]}
</Table.Cell>
</Table.Row>
))}
</Table.Body>
</Table>
)}
</Grid.Column>
)
}