@leofcoin/endpoints 0.2.6 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/exports/http.js +2 -9
- package/exports/shared.js +34 -0
- package/exports/ws.js +5 -12
- package/package.json +1 -1
package/exports/http.js
CHANGED
|
@@ -4,6 +4,7 @@ import Router from '@koa/router'
|
|
|
4
4
|
import {formatUnits} from '@leofcoin/utils'
|
|
5
5
|
import {readFile} from 'fs/promises'
|
|
6
6
|
import Showdown from 'showdown'
|
|
7
|
+
import shared from './shared.js'
|
|
7
8
|
import { dirname, join} from 'path'
|
|
8
9
|
import { fileURLToPath } from 'url'
|
|
9
10
|
|
|
@@ -29,15 +30,7 @@ export default (chain, port, networkVersion) => {
|
|
|
29
30
|
})
|
|
30
31
|
|
|
31
32
|
router.get('/networkStats', async () => {
|
|
32
|
-
ctx.body =
|
|
33
|
-
version: networkVersion,
|
|
34
|
-
peers: peernet.peers,
|
|
35
|
-
accounts: await accountsStore.length(),
|
|
36
|
-
accountsHolding: Object.values(await chain.balances).reduce((previous, current) => {
|
|
37
|
-
const amount = Number(formatUnits(current)) || 0
|
|
38
|
-
return previous + isNaN(amount) ? 0 : amount
|
|
39
|
-
}, 0)
|
|
40
|
-
}
|
|
33
|
+
ctx.body = await shared.networkStats(chain)
|
|
41
34
|
})
|
|
42
35
|
|
|
43
36
|
router.get('/balances', async ctx => {
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {formatUnits, BigNumber} from '@leofcoin/utils'
|
|
2
|
+
|
|
3
|
+
export const networkStats = async () => {
|
|
4
|
+
let accountsHolding = 0
|
|
5
|
+
let accountsHoldingAmount = BigNumber.from(0)
|
|
6
|
+
let topHolders = []
|
|
7
|
+
const balances = Object.entries(await chain.balances)
|
|
8
|
+
.map(([holder, amount]) => {
|
|
9
|
+
amount = BigNumber.from(amount)
|
|
10
|
+
return {holder, amount}
|
|
11
|
+
})
|
|
12
|
+
.sort((a, b) => formatUnits(b.amount.sub(a.amount)))
|
|
13
|
+
|
|
14
|
+
for (let {holder, amount} of balances) {
|
|
15
|
+
if (amount.gt(0)) {
|
|
16
|
+
accountsHoldingAmount = accountsHoldingAmount.add(amount)
|
|
17
|
+
accountsHolding += 1
|
|
18
|
+
topHolders.length < 100 && topHolders.push({holder, amount: formatUnits(amount)})
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
version: networkVersion,
|
|
24
|
+
peers: peernet.peers,
|
|
25
|
+
accounts: await accountsStore.length(),
|
|
26
|
+
accountsHolding,
|
|
27
|
+
accountsHoldingAmount: formatUnits(accountsHoldingAmount).toString(),
|
|
28
|
+
topHolders
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export default {
|
|
33
|
+
networkStats
|
|
34
|
+
}
|
package/exports/ws.js
CHANGED
|
@@ -1,21 +1,14 @@
|
|
|
1
1
|
import Server from 'socket-request-server'
|
|
2
2
|
import {formatUnits} from '@leofcoin/utils'
|
|
3
|
+
import shared from './shared.js'
|
|
3
4
|
|
|
4
5
|
export default (chain, port, networkVersion) => {
|
|
5
6
|
return Server({ port, protocol: networkVersion }, {
|
|
6
7
|
network: async () => {
|
|
7
8
|
send({version: networkVersion})
|
|
8
9
|
},
|
|
9
|
-
networkStats: async () => {
|
|
10
|
-
send(
|
|
11
|
-
version: networkVersion,
|
|
12
|
-
peers: peernet.peers,
|
|
13
|
-
accounts: await accountsStore.length(),
|
|
14
|
-
accountsHolding: Object.values(await chain.balances).reduce((previous, current) => {
|
|
15
|
-
const amount = Number(formatUnits(current)) || 0
|
|
16
|
-
return previous + isNaN(amount) ? 0 : amount
|
|
17
|
-
}, 0)
|
|
18
|
-
})
|
|
10
|
+
networkStats: async ({send}) => {
|
|
11
|
+
send(await shared.networkStats(chain))
|
|
19
12
|
},
|
|
20
13
|
balances: async ({send}) =>
|
|
21
14
|
send(await chain.balances),
|
|
@@ -36,8 +29,8 @@ export default (chain, port, networkVersion) => {
|
|
|
36
29
|
},
|
|
37
30
|
accounts: async ({send}) =>
|
|
38
31
|
send(JSON.parse(new TextDecoder().decode((await walletStore.get('accounts'))))),
|
|
39
|
-
hasTransactionToHandle: ({send}) =>
|
|
40
|
-
send(chain.hasTransactionToHandle()),
|
|
32
|
+
hasTransactionToHandle: async ({send}) =>
|
|
33
|
+
send(await chain.hasTransactionToHandle()),
|
|
41
34
|
getBlock: ({index}, {send}) =>
|
|
42
35
|
send(chain.blocks[index]),
|
|
43
36
|
blocks:({amount}, {send}) => {
|