@leofcoin/endpoint-clients 0.2.7 → 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/direct.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { formatUnits, BigNumber } from '@leofcoin/utils';
2
+
1
3
  class Client {
2
4
  url;
3
5
  networkVersion;
@@ -8,15 +10,17 @@ class Client {
8
10
  }
9
11
  get pubsub() {
10
12
  return {
11
- publish: peernet.pubsub.publish,
12
- subscribe: peernet.pubsub.subscribe
13
+ publish: globalThis.pubsub.publish,
14
+ subscribe: globalThis.pubsub.subscribe
13
15
  };
14
16
  }
15
17
  balances() {
16
18
  return chain.balances;
17
19
  }
18
- balanceOf(address, format) {
19
- return chain.balanceOf(address, format);
20
+ async balanceOf(address, format) {
21
+ const balances = await chain.balances;
22
+ const balance = balances[address];
23
+ return format ? formatUnits(balance) : balance;
20
24
  }
21
25
  selectedAccount() {
22
26
  return chain.selectedAccount;
@@ -28,22 +32,22 @@ class Client {
28
32
  return chain.accounts;
29
33
  }
30
34
  hasTransactionToHandle() {
31
- return chain.hasTransactionToHandle;
35
+ return chain.hasTransactionToHandle();
32
36
  }
33
37
  getBlock(index) {
34
38
  return chain.getBlock(index);
35
39
  }
36
40
  blocks(amount) {
37
- return chain.blocks(amount);
41
+ return chain.blocks.slice(amount);
38
42
  }
39
43
  sendTransaction(transaction) {
40
44
  return chain.sendTransaction(transaction);
41
45
  }
42
46
  peerId() {
43
- return chain.peerId;
47
+ return peernet.peerId;
44
48
  }
45
49
  peers() {
46
- return chain.peers;
50
+ return peernet.peers;
47
51
  }
48
52
  validators() {
49
53
  return chain.validators;
@@ -76,16 +80,16 @@ class Client {
76
80
  return chain.totalTransactions;
77
81
  }
78
82
  poolTransactions() {
79
- return chain.poolTransactions;
83
+ return transactionPoolStore.get();
80
84
  }
81
85
  transactionsInPool() {
82
- return chain.transactionsInPool;
86
+ return transactionPoolStore.length();
83
87
  }
84
88
  transactionPoolSize() {
85
- return chain.transactionPoolSize;
89
+ return transactionPoolStore.size();
86
90
  }
87
91
  totalBlocks() {
88
- return chain.totalBlocks;
92
+ return chain.blocks.length;
89
93
  }
90
94
  nativeCalls() {
91
95
  return chain.nativeCalls;
@@ -105,12 +109,38 @@ class Client {
105
109
  network() {
106
110
  return chain.network;
107
111
  }
108
- networkStats() {
109
- return chain.networkStats;
112
+ async networkStats() {
113
+ let accountsHolding = 0;
114
+ let accountsHoldingAmount = BigNumber.from(0);
115
+ let topHolders = [];
116
+ const balances = Object.entries(await chain.balances)
117
+ .map(([holder, amount]) => {
118
+ amount = BigNumber.from(amount);
119
+ return { holder, amount };
120
+ })
121
+ .sort((a, b) => formatUnits(b.amount.sub(a.amount)));
122
+ for (let { holder, amount } of balances) {
123
+ if (amount.gt(0)) {
124
+ accountsHoldingAmount = accountsHoldingAmount.add(amount);
125
+ accountsHolding += 1;
126
+ topHolders.length < 100 && topHolders.push({ holder, amount: formatUnits(amount) });
127
+ }
128
+ }
129
+ return {
130
+ version: peernet.networkVersion,
131
+ peers: peernet.peers.map(([id, peer]) => id),
132
+ accounts: await accountsStore.length(),
133
+ accountsHolding,
134
+ accountsHoldingAmount: formatUnits(accountsHoldingAmount).toString(),
135
+ topHolders
136
+ };
110
137
  }
111
138
  getNonce(address) {
112
139
  return chain.getNonce(address);
113
140
  }
141
+ lastBlock() {
142
+ return chain.lastBlock;
143
+ }
114
144
  }
115
145
 
116
146
  export { Client as default };
package/exports/http.js CHANGED
@@ -121,6 +121,8 @@ class Client {
121
121
  getNonce(address) {
122
122
  return this._fetchNumber('getNonce', { address });
123
123
  }
124
+ lastBlock() {
125
+ }
124
126
  }
125
127
 
126
128
  export { Client as default };
package/exports/index.js CHANGED
@@ -1,3 +1,5 @@
1
1
  export { default as ws } from './ws.js';
2
2
  export { default as http } from './http.js';
3
+ export { default as direct } from './direct.js';
3
4
  import 'socket-request-client';
5
+ import '@leofcoin/utils';
@@ -8,7 +8,7 @@ export default class Client {
8
8
  subscribe: any;
9
9
  };
10
10
  balances(): any;
11
- balanceOf(address: string, format: boolean): any;
11
+ balanceOf(address: string, format: boolean): Promise<any>;
12
12
  selectedAccount(): any;
13
13
  selectAccount(address: any): any;
14
14
  accounts(): any;
@@ -38,11 +38,14 @@ export default class Client {
38
38
  createContractAddress(owner: string, code: string, params: {}): {};
39
39
  deployContract(code: string, params: {}): {};
40
40
  network(): any;
41
- networkStats(): {
41
+ networkStats(): Promise<{
42
42
  version: string;
43
43
  peers: {}[];
44
44
  accounts: number;
45
45
  accountsHolding: number;
46
- };
46
+ accountsHoldingAmount: number;
47
+ topHolders: any[];
48
+ }>;
47
49
  getNonce(address: string): any;
50
+ lastBlock(): any;
48
51
  }
@@ -47,5 +47,6 @@ export default class Client {
47
47
  accountsHolding: number;
48
48
  }>;
49
49
  getNonce(address: string): Promise<number>;
50
+ lastBlock(): void;
50
51
  }
51
52
  export {};
@@ -1,2 +1,3 @@
1
1
  export { default as ws } from './ws.js';
2
2
  export { default as http } from './http.js';
3
+ export { default as direct } from './direct.js';
@@ -1,8 +1,8 @@
1
- import socketRequestClient from 'socket-request-client';
1
+ import SocketRequestClient from 'socket-request-client';
2
2
  export default class Client {
3
3
  url: string;
4
4
  networkVersion: string;
5
- client: socketRequestClient;
5
+ client: SocketRequestClient;
6
6
  constructor(url?: string, networkVersion?: string);
7
7
  init(): Promise<void>;
8
8
  get pubsub(): {
package/exports/ws.js CHANGED
@@ -1,4 +1,4 @@
1
- import socketRequestClient from 'socket-request-client';
1
+ import SocketRequestClient from 'socket-request-client';
2
2
 
3
3
  class Client {
4
4
  url;
@@ -9,7 +9,7 @@ class Client {
9
9
  this.networkVersion = networkVersion;
10
10
  }
11
11
  async init() {
12
- this.client = await socketRequestClient(this.url, this.networkVersion);
12
+ this.client = await new SocketRequestClient(this.url, this.networkVersion);
13
13
  }
14
14
  get pubsub() {
15
15
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/endpoint-clients",
3
- "version": "0.2.7",
3
+ "version": "0.2.8",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": {
@@ -46,6 +46,7 @@
46
46
  "tslib": "^2.5.0"
47
47
  },
48
48
  "dependencies": {
49
- "socket-request-client": "^1.7.3"
49
+ "@leofcoin/utils": "^1.1.7",
50
+ "socket-request-client": "^1.7.5"
50
51
  }
51
52
  }
package/src/direct.ts CHANGED
@@ -1,5 +1,9 @@
1
+ import {formatUnits, BigNumber} from '@leofcoin/utils'
2
+
1
3
  declare var chain
2
4
  declare var peernet
5
+ declare var transactionPoolStore
6
+ declare var accountsStore
3
7
 
4
8
  export default class Client {
5
9
  url: string
@@ -13,8 +17,8 @@ export default class Client {
13
17
 
14
18
  get pubsub() {
15
19
  return {
16
- publish: peernet.pubsub.publish,
17
- subscribe: peernet.pubsub.subscribe
20
+ publish: globalThis.pubsub.publish,
21
+ subscribe: globalThis.pubsub.subscribe
18
22
  }
19
23
  }
20
24
 
@@ -22,8 +26,10 @@ export default class Client {
22
26
  return chain.balances
23
27
  }
24
28
 
25
- balanceOf(address: string, format: boolean) {
26
- return chain.balanceOf(address, format)
29
+ async balanceOf(address: string, format: boolean) {
30
+ const balances = await chain.balances
31
+ const balance = balances[address]
32
+ return format ? formatUnits(balance) : balance
27
33
  }
28
34
 
29
35
  selectedAccount() {
@@ -39,24 +45,24 @@ export default class Client {
39
45
  }
40
46
 
41
47
  hasTransactionToHandle() {
42
- return chain.hasTransactionToHandle
48
+ return chain.hasTransactionToHandle()
43
49
  }
44
50
 
45
51
  getBlock(index: number) {
46
52
  return chain.getBlock(index)
47
53
  }
48
54
  blocks(amount: number) {
49
- return chain.blocks(amount)
55
+ return chain.blocks.slice(amount)
50
56
  }
51
57
 
52
58
  sendTransaction(transaction) {
53
59
  return chain.sendTransaction(transaction)
54
60
  }
55
61
  peerId() {
56
- return chain.peerId
62
+ return peernet.peerId
57
63
  }
58
64
  peers() {
59
- return chain.peers
65
+ return peernet.peers
60
66
  }
61
67
  validators() {
62
68
  return chain.validators
@@ -90,16 +96,16 @@ export default class Client {
90
96
  return chain.totalTransactions
91
97
  }
92
98
  poolTransactions() {
93
- return chain.poolTransactions
99
+ return transactionPoolStore.get()
94
100
  }
95
101
  transactionsInPool() {
96
- return chain.transactionsInPool
102
+ return transactionPoolStore.length()
97
103
  }
98
104
  transactionPoolSize() {
99
- return chain.transactionPoolSize
105
+ return transactionPoolStore.size()
100
106
  }
101
107
  totalBlocks(): number {
102
- return chain.totalBlocks
108
+ return chain.blocks.length
103
109
  }
104
110
  nativeCalls(): number {
105
111
  return chain.nativeCalls
@@ -120,11 +126,40 @@ export default class Client {
120
126
  return chain.network
121
127
  }
122
128
 
123
- networkStats(): { version: string; peers: {}[]; accounts: number; accountsHolding: number } {
124
- return chain.networkStats
129
+ async networkStats(): Promise<{ version: string; peers: {}[]; accounts: number; accountsHolding: number, accountsHoldingAmount: number, topHolders: any[] }> {
130
+ let accountsHolding = 0
131
+ let accountsHoldingAmount = BigNumber.from(0)
132
+ let topHolders = []
133
+ const balances = Object.entries(await chain.balances)
134
+ .map(([holder, amount]) => {
135
+ amount = BigNumber.from(amount)
136
+ return {holder, amount}
137
+ })
138
+ .sort((a, b) => formatUnits(b.amount.sub(a.amount)))
139
+
140
+ for (let {holder, amount} of balances) {
141
+ if (amount.gt(0)) {
142
+ accountsHoldingAmount = accountsHoldingAmount.add(amount)
143
+ accountsHolding += 1
144
+ topHolders.length < 100 && topHolders.push({holder, amount: formatUnits(amount)})
145
+ }
146
+ }
147
+
148
+ return {
149
+ version: peernet.networkVersion,
150
+ peers: peernet.peers.map(([id, peer]) => id),
151
+ accounts: await accountsStore.length(),
152
+ accountsHolding,
153
+ accountsHoldingAmount: formatUnits(accountsHoldingAmount).toString(),
154
+ topHolders
155
+ }
125
156
  }
126
157
 
127
158
  getNonce(address: string) {
128
159
  return chain.getNonce(address)
129
160
  }
161
+
162
+ lastBlock() {
163
+ return chain.lastBlock
164
+ }
130
165
  }
package/src/http.ts CHANGED
@@ -145,4 +145,8 @@ export default class Client {
145
145
  getNonce(address: string) {
146
146
  return this._fetchNumber('getNonce', { address })
147
147
  }
148
+
149
+ lastBlock() {
150
+
151
+ }
148
152
  }
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export {default as ws} from './ws.js'
2
- export {default as http} from './http.js'
2
+ export {default as http} from './http.js'
3
+ export {default as direct} from './direct.js'
package/src/ws.ts CHANGED
@@ -1,9 +1,9 @@
1
- import socketRequestClient from 'socket-request-client'
1
+ import SocketRequestClient from 'socket-request-client'
2
2
 
3
3
  export default class Client {
4
4
  url: string
5
5
  networkVersion: string
6
- client: socketRequestClient
6
+ client: SocketRequestClient
7
7
 
8
8
  constructor(url = 'ws://localhost:4040', networkVersion = 'leofcoin-peach') {
9
9
  this.url = url
@@ -11,7 +11,7 @@ export default class Client {
11
11
  }
12
12
 
13
13
  async init() {
14
- this.client = await socketRequestClient(this.url, this.networkVersion)
14
+ this.client = await new SocketRequestClient(this.url, this.networkVersion)
15
15
  }
16
16
 
17
17
  get pubsub() {
package/test/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { needsImplementations } from './spec.js';
2
2
  import WsClient from './../exports/ws.js'
3
3
  import HttpClient from './../exports/http.js'
4
+ import DirectClient from './../exports/direct.js'
4
5
 
5
6
  try {
6
7
  const wsNeeds = needsImplementations(new WsClient())
@@ -8,6 +9,9 @@ try {
8
9
 
9
10
  const httpNeeds = needsImplementations(new HttpClient())
10
11
  if (httpNeeds.length > 0) throw new Error(`http: missing implementations ${httpNeeds.length > 2 ? httpNeeds.join(', ') : httpNeeds.join(' & ')}`)
12
+
13
+ const directNeeds = needsImplementations(new DirectClient())
14
+ if (directNeeds.length > 0) throw new Error(`direct: missing implementations ${directNeeds.length > 2 ? directNeeds.join(', ') : directNeeds.join(' & ')}`)
11
15
  } catch (error) {
12
16
  console.error(error);
13
17
  }
package/test/spec.js CHANGED
@@ -26,7 +26,8 @@ export const implementations = [
26
26
  'totalBlocks',
27
27
  'totalSize',
28
28
  'totalTransactions',
29
- 'validators'
29
+ 'validators',
30
+ 'lastBlock'
30
31
  ]
31
32
 
32
33
  export const needsImplementations = (client) => {
@@ -39,5 +40,6 @@ export const needsImplementations = (client) => {
39
40
  }
40
41
 
41
42
  }
43
+ console.log(needs);
42
44
  return needs
43
45
  }
package/tsconfig.json CHANGED
@@ -12,6 +12,5 @@
12
12
  },
13
13
  "include": [
14
14
  "./src/**/*"
15
- ],
16
- "exclude": ["./node_modules"]
15
+ ]
17
16
  }