@leofcoin/endpoint-clients 0.3.7 → 0.3.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/src/direct.ts CHANGED
@@ -1,177 +1,185 @@
1
- import {formatUnits, BigNumber} from '@leofcoin/utils'
2
-
3
- declare var chain
4
- declare var peernet
5
- declare var transactionPoolStore
6
- declare var accountsStore
7
-
8
- export default class Client {
9
- url: string
10
- networkVersion: string
11
- client;
12
-
13
- constructor(url = 'ws://localhost:4040', networkVersion = 'leofcoin-peach') {
14
- this.url = url
15
- this.networkVersion = networkVersion
16
- }
17
-
18
- get pubsub() {
19
- return {
20
- publish: globalThis.pubsub.publish,
21
- subscribe: globalThis.pubsub.subscribe
22
- }
23
- }
24
-
25
- balances() {
26
- return chain.balances
27
- }
28
-
29
- async balanceOf(address: string, format: boolean) {
30
- const balances = await chain.balances
31
- const balance = balances[address]
32
- return format ? formatUnits(balance) : balance
33
- }
34
-
35
- selectedAccount() {
36
- return peernet.selectedAccount
37
- }
38
-
39
- async selectAccount(address: any) {
40
- try {
41
- await globalThis.walletStore.put('selected-account', address)
42
- } catch (error) {
43
- throw new Error(`couldn't set selected account`)
44
- }
45
- }
46
-
47
- accounts() {
48
- return chain.accounts
49
- }
50
-
51
- hasTransactionToHandle() {
52
- return chain.hasTransactionToHandle()
53
- }
54
-
55
- getBlock(index: number) {
56
- return chain.blocks[index - 1]
57
- }
58
- blocks(amount: number) {
59
- return chain.blocks.slice(amount)
60
- }
61
-
62
- sendTransaction(transaction) {
63
- return chain.sendTransaction(transaction)
64
- }
65
- peerId() {
66
- return peernet.peerId
67
- }
68
- peers() {
69
- return peernet.peers
70
- }
71
- validators() {
72
- return chain.validators
73
- }
74
- lookup(name: string) {
75
- return chain.lookup(name)
76
- }
77
-
78
- staticCall(contract: string, method: string, params: {}) {
79
- return chain.staticCall(contract, method, params)
80
- }
81
- nativeBurns(): number {
82
- return chain.nativeBurns
83
- }
84
- contracts(): number {
85
- return chain.contracts
86
- }
87
- nativeMints(): number {
88
- return chain.nativeMints
89
- }
90
- nativeToken(): string {
91
- return chain.nativeToken
92
- }
93
- nativeTransfers(): number {
94
- return chain.nativeTransfers
95
- }
96
- totalSize(): number {
97
- return chain.totalSize
98
- }
99
- totalTransactions(): number {
100
- return chain.totalTransactions
101
- }
102
- poolTransactions() {
103
- return transactionPoolStore.get()
104
- }
105
- transactionsInPool() {
106
- return transactionPoolStore.length()
107
- }
108
- transactionPoolSize() {
109
- return transactionPoolStore.size()
110
- }
111
- totalBlocks(): number {
112
- return chain.blocks.length
113
- }
114
- nativeCalls(): number {
115
- return chain.nativeCalls
116
- }
117
- participating(): boolean {
118
- return chain.participating
119
- }
120
- participate(address: string): {} {
121
- return chain.participate(address)
122
- }
123
- createContractAddress(owner: string, code: string, params: {}): {} {
124
- return chain.createContractAddress(owner, code, params)
125
- }
126
- deployContract(code: string, params: {}): {} {
127
- return chain.deployContract(code, params)
128
- }
129
- network() {
130
- return chain.network
131
- }
132
-
133
- async networkStats(): Promise<{ version: string; peers: {}[]; accounts: number; accountsHolding: number, accountsHoldingAmount: number, topHolders: any[] }> {
134
- let accountsHolding = 0
135
- let accountsHoldingAmount = BigNumber.from(0)
136
- let topHolders = []
137
- const balances = Object.entries(await chain.balances)
138
- .map(([holder, amount]): {holder: string, amount: BigNumber} => {
139
- amount = BigNumber.from(amount)
140
- return {holder, amount}
141
- })
142
- .sort((a, b) => formatUnits(b.amount.sub(a.amount)))
143
-
144
- for (let {holder, amount} of balances) {
145
- if (amount.gt(0)) {
146
- accountsHoldingAmount = accountsHoldingAmount.add(amount)
147
- accountsHolding += 1
148
- topHolders.length < 100 && topHolders.push({holder, amount: formatUnits(amount)})
149
- }
150
- }
151
-
152
- return {
153
- version: peernet.networkVersion,
154
- peers: peernet.peers.map(([id, peer]) => id),
155
- accounts: await accountsStore.length(),
156
- accountsHolding,
157
- accountsHoldingAmount: formatUnits(accountsHoldingAmount).toString(),
158
- topHolders
159
- }
160
- }
161
-
162
- getNonce(address: string) {
163
- return chain.getNonce(address)
164
- }
165
-
166
- lastBlock() {
167
- return chain.lastBlock
168
- }
169
-
170
- blockHashMap() {
171
- return chain.blockHashMap
172
- }
173
-
174
- bootstrap() {
175
- return chain.blockHashMap
176
- }
177
- }
1
+ import { formatUnits, BigNumber } from '@leofcoin/utils'
2
+
3
+ declare var chain
4
+ declare var peernet
5
+ declare var transactionPoolStore
6
+ declare var accountsStore
7
+
8
+ export default class Client {
9
+ url: string
10
+ networkVersion: string
11
+ client
12
+
13
+ constructor(url = 'ws://localhost:4040', networkVersion = 'leofcoin-peach') {
14
+ this.url = url
15
+ this.networkVersion = networkVersion
16
+ }
17
+
18
+ get pubsub() {
19
+ return {
20
+ publish: globalThis.pubsub.publish,
21
+ subscribe: globalThis.pubsub.subscribe
22
+ }
23
+ }
24
+
25
+ balances() {
26
+ return chain.balances
27
+ }
28
+
29
+ async balanceOf(address: string, format: boolean) {
30
+ const balances = await chain.balances
31
+ const balance = balances[address]
32
+ return format ? formatUnits(balance) : balance
33
+ }
34
+
35
+ selectedAccount() {
36
+ return peernet.selectedAccount
37
+ }
38
+
39
+ async selectAccount(address: any) {
40
+ try {
41
+ await globalThis.walletStore.put('selected-account', address)
42
+ } catch (error) {
43
+ throw new Error(`couldn't set selected account`)
44
+ }
45
+ }
46
+
47
+ accounts() {
48
+ return chain.accounts
49
+ }
50
+
51
+ hasTransactionToHandle() {
52
+ return chain.hasTransactionToHandle()
53
+ }
54
+
55
+ getBlock(index: number) {
56
+ return chain.getBlock(index)
57
+ }
58
+
59
+ blocks(amount: number) {
60
+ return chain.getBlocks(amount)
61
+ }
62
+
63
+ sendTransaction(transaction) {
64
+ return chain.sendTransaction(transaction)
65
+ }
66
+ peerId() {
67
+ return peernet.peerId
68
+ }
69
+ peers() {
70
+ return peernet.peers
71
+ }
72
+ validators() {
73
+ return chain.validators
74
+ }
75
+ lookup(name: string) {
76
+ return chain.lookup(name)
77
+ }
78
+
79
+ staticCall(contract: string, method: string, params: {}) {
80
+ return chain.staticCall(contract, method, params)
81
+ }
82
+ nativeBurns(): number {
83
+ return chain.nativeBurns
84
+ }
85
+ contracts(): number {
86
+ return chain.contracts
87
+ }
88
+ nativeMints(): number {
89
+ return chain.nativeMints
90
+ }
91
+ nativeToken(): string {
92
+ return chain.nativeToken
93
+ }
94
+ nativeTransfers(): number {
95
+ return chain.nativeTransfers
96
+ }
97
+ totalSize(): number {
98
+ return chain.totalSize
99
+ }
100
+ totalTransactions(): number {
101
+ return chain.totalTransactions
102
+ }
103
+ poolTransactions() {
104
+ return transactionPoolStore.get()
105
+ }
106
+ transactionsInPool() {
107
+ return transactionPoolStore.length()
108
+ }
109
+ transactionPoolSize() {
110
+ return transactionPoolStore.size()
111
+ }
112
+ totalBlocks(): number {
113
+ return chain.totalBlocks
114
+ }
115
+ nativeCalls(): number {
116
+ return chain.nativeCalls
117
+ }
118
+ participating(): boolean {
119
+ return chain.participating
120
+ }
121
+ participate(address: string): {} {
122
+ return chain.participate(address)
123
+ }
124
+ createContractAddress(owner: string, code: string, params: {}): {} {
125
+ return chain.createContractAddress(owner, code, params)
126
+ }
127
+ deployContract(code: string, params: {}): {} {
128
+ return chain.deployContract(code, params)
129
+ }
130
+ network() {
131
+ return chain.network
132
+ }
133
+
134
+ async networkStats(): Promise<{
135
+ version: string
136
+ peers: {}[]
137
+ accounts: number
138
+ accountsHolding: number
139
+ accountsHoldingAmount: number
140
+ topHolders: any[]
141
+ }> {
142
+ let accountsHolding = 0
143
+ let accountsHoldingAmount = BigNumber.from(0)
144
+ let topHolders = []
145
+ const balances = Object.entries(await chain.balances)
146
+ .map(([holder, amount]): { holder: string; amount: BigNumber } => {
147
+ amount = BigNumber.from(amount)
148
+ return { holder, amount }
149
+ })
150
+ .sort((a, b) => formatUnits(b.amount.sub(a.amount)))
151
+
152
+ for (let { holder, amount } of balances) {
153
+ if (amount.gt(0)) {
154
+ accountsHoldingAmount = accountsHoldingAmount.add(amount)
155
+ accountsHolding += 1
156
+ topHolders.length < 100 && topHolders.push({ holder, amount: formatUnits(amount) })
157
+ }
158
+ }
159
+
160
+ return {
161
+ version: peernet.networkVersion,
162
+ peers: peernet.peers.map(([id, peer]) => id),
163
+ accounts: await accountsStore.length(),
164
+ accountsHolding,
165
+ accountsHoldingAmount: formatUnits(accountsHoldingAmount).toString(),
166
+ topHolders
167
+ }
168
+ }
169
+
170
+ getNonce(address: string) {
171
+ return chain.getNonce(address)
172
+ }
173
+
174
+ lastBlock() {
175
+ return chain.lastBlock
176
+ }
177
+
178
+ blockHashMap() {
179
+ return chain.blockHashMap
180
+ }
181
+
182
+ bootstrap() {
183
+ return chain.blockHashMap
184
+ }
185
+ }