@leofcoin/endpoint-clients 0.2.1 → 0.2.3

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 vandeurenglenn
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ MIT License
2
+
3
+ Copyright (c) 2022 vandeurenglenn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
package/README.md CHANGED
@@ -10,14 +10,15 @@ npm i @leofcoin/endpoint-clients
10
10
  import WsClient from '@leofcoin/endpoint-clients/ws'
11
11
  import HttpClient from '@leofcoin/endpoint-clients/http'
12
12
 
13
- const networkVersion = 'leofcoin-peach'
13
+ const networkVersion = 'peach'
14
14
 
15
15
  const ws = new WsClient('ws://localhost:4040', networkVersion)
16
16
  const http = new HttpClient('http://localhost:8080', networkVersion)
17
17
  ```
18
18
 
19
19
  ## api
20
- ### selectAccount(address: `string`)
20
+ ### new Client(url, networkVersion)
21
+ #### selectAccount(address: `string`)
21
22
  `address`: desired address
22
23
  > set default account to given address
23
24
  ```js
@@ -25,21 +26,21 @@ await client.selectAccount(address)
25
26
  ```
26
27
  <br>
27
28
 
28
- ### getBlock(index: `number`)
29
+ #### getBlock(index: `number`)
29
30
  `index`: index of the block to fetch<br>
30
31
  ```js
31
32
  await client.getBlock(10)
32
33
  ```
33
34
  <br>
34
35
 
35
- ### blocks(amount)
36
+ #### blocks(amount)
36
37
  `amount`: the amount to slice
37
38
  ```js
38
39
  await client.blocks(-25)
39
40
  ```
40
41
  <br>
41
42
 
42
- ### createTransactionFrom(from: `string`, to: `string`, method: `string`, parameters: `array`, nonce: `number` | `undefined`)
43
+ #### createTransactionFrom(from: `string`, to: `string`, method: `string`, parameters: `array`, nonce: `number` | `undefined`)
43
44
 
44
45
  `from`:<br>
45
46
  `to`: <br>
@@ -51,14 +52,14 @@ await client.createTransactionFrom(from, to, method, parameters, nonce)
51
52
  ```
52
53
  <br>
53
54
 
54
- ### lookup(name: `string`)
55
+ #### lookup(name: `string`)
55
56
  `name`: name of the contract to lookup the address and owner of
56
57
  ```js
57
58
  await client.lookup('contractName')
58
59
  ```
59
60
  <br>
60
61
 
61
- ### participate(address: `string`)
62
+ #### participate(address: `string`)
62
63
  `address`: <br>
63
64
 
64
65
  ```js
@@ -66,7 +67,7 @@ await client.participate(address)
66
67
  ```
67
68
  <br>
68
69
 
69
- ### staticCall(contract: `string`, method: `string`, params: `array`)
70
+ #### staticCall(contract: `string`, method: `string`, params: `array`)
70
71
  `contract`: contract address todo the call on
71
72
  `method`: method/function to call
72
73
  `params`: array containing params needed to call the desired method
@@ -75,7 +76,7 @@ await client.staticCall(contract, method, params)
75
76
  ```
76
77
  <br>
77
78
 
78
- ### createContractAddress(owner: `string`, code: `string`, params: `array`)
79
+ #### createContractAddress(owner: `string`, code: `string`, params: `array`)
79
80
  `owner`: address of the deployer<br>
80
81
  `code`: contract as a string<br>
81
82
  `params`: contract constructor params<br>
@@ -85,7 +86,7 @@ const address = await client.createContractAddress(owner, code, params)
85
86
  ```
86
87
  <br>
87
88
 
88
- ### deployContract(code: `string`, params: `array`)
89
+ #### deployContract(code: `string`, params: `array`)
89
90
  `code`: contract as a string<br>
90
91
  `params`: contract constructor params<br>
91
92
 
@@ -94,26 +95,26 @@ const tx = await client.deployContract(code, params)
94
95
  ```
95
96
  <br>
96
97
 
97
- ### accounts()
98
+ #### accounts()
98
99
  ```js
99
100
  await client.accounts()
100
101
  ```
101
102
  <br>
102
103
 
103
- ### hasTransactionToHandle()
104
+ #### hasTransactionToHandle()
104
105
  ```js
105
106
  await client.hasTransactionToHandle()
106
107
  ```
107
108
  <br>
108
109
 
109
- ### balances()
110
+ #### balances()
110
111
  > returns all balances
111
112
  ```js
112
113
  await client.balances()
113
114
  ```
114
115
  <br>
115
116
 
116
- ### balancOf(address: `string`, format: `boolean`)
117
+ #### balancOf(address: `string`, format: `boolean`)
117
118
  `address`: The address to check<br>
118
119
  `format`: Wether or not to format<br>
119
120
  > returns the balance of given address as BigNumber or string (when format is true)
@@ -122,86 +123,86 @@ await client.balanceOf(address, true)
122
123
  ```
123
124
  <br>
124
125
 
125
- ### selectedAccount()
126
+ #### selectedAccount()
126
127
  ```js
127
128
  await client.selectedAccount()
128
129
  ```
129
130
  <br>
130
131
 
131
- ### peerId()
132
+ #### peerId()
132
133
  ```js
133
134
  await client.peerId()
134
135
  ```
135
136
  <br>
136
137
 
137
- ### peers()
138
+ #### peers()
138
139
  ```js
139
140
  await client.peers()
140
141
  ```
141
142
  <br>
142
143
 
143
- ### validators()
144
+ #### validators()
144
145
  ```js
145
146
  await client.validators()
146
147
  ```
147
148
  <br>
148
149
 
149
- ### nativeBurns()
150
+ #### nativeBurns()
150
151
  ```js
151
152
  await client.nativeBurns()
152
153
  ```
153
154
  <br>
154
155
 
155
- ### contracts()
156
+ #### contracts()
156
157
  ```js
157
158
  await client.contracts()
158
159
  ```
159
160
  <br>
160
161
 
161
- ### nativeMints()
162
+ #### nativeMints()
162
163
  ```js
163
164
  await client.nativeMints()
164
165
  ```
165
166
  <br>
166
167
 
167
- ### nativeToken()
168
+ #### nativeToken()
168
169
  ```js
169
170
  await client.nativeToken()
170
171
  ```
171
172
  <br>
172
173
 
173
- ### nativeTransfers()
174
+ #### nativeTransfers()
174
175
  ```js
175
176
  await client. nativeTransfers()
176
177
  ```
177
178
  <br>
178
179
 
179
- ### totalSize()
180
+ #### totalSize()
180
181
  ```js
181
182
  await client.totalSize()
182
183
  ```
183
184
  <br>
184
185
 
185
- ### totalTransactions()
186
+ #### totalTransactions()
186
187
  ```js
187
188
  await client.totalTransactions()
188
189
  ```
189
190
  <br>
190
191
 
191
- ### totalBlocks()
192
+ #### totalBlocks()
192
193
  ```js
193
194
  await client.totalBlocks()
194
195
  ```
195
196
  <br>
196
197
 
197
- ### nativeCalls()
198
+ #### nativeCalls()
198
199
  ```js
199
200
  await client.nativeCalls()
200
201
  ```
201
202
  <br>
202
203
 
203
- ### participating()
204
+ #### participating()
204
205
  ```js
205
206
  await client.participating()
206
207
  ```
207
- <br>
208
+ <br>
package/exports/http.js CHANGED
@@ -1,106 +1,108 @@
1
- export default class {
2
- get isHttpClient() {
3
- return true
4
- }
5
- constructor(url, networkVersion) {
6
- this.url = url
7
- this.networkVersion = networkVersion
8
- }
9
-
10
- async _fetch(method, params) {
11
- params = new URLSearchParams(params).toString()
12
- const response = await fetch(`${this.url}/${method}?${params}`)
13
- return response.json()
14
- }
15
-
16
- balances() {
17
- return this._fetch('balances')
18
- }
19
-
20
- balanceOf(address, format) {
21
- return this._fetch(`balanceOf`, {address, format})
22
- }
23
-
24
- selectedAccount() {
25
- return this._fetch('selectedAccount')
26
- }
27
-
28
- selectAccount(address) {
29
- return this._fetch('selectAccount', {address})
30
- }
31
-
32
- accounts() {
33
- return this._fetch('accounts')
34
- }
35
-
36
- hasTransactionToHandle() {
37
- return this._fetch('hasTransactionToHandle')
38
- }
39
-
40
- getBlock(index) {
41
- return this._fetch('getBlock', { index })
42
- }
43
- blocks(amount) {
44
- return this._fetch('blocks', {amount})
45
- }
46
-
47
- createTransactionFrom(from, to, method, parameters, nonce) {
48
- return this._fetch('createTransactionFrom', {from, to, method, parameters, nonce})
49
- }
50
- peerId() {
51
- return this._fetch('peerId')
52
- }
53
- peers() {
54
- return this._fetch('peers')
55
- }
56
- validators() {
57
- return this._fetch('validators')
58
- }
59
- lookup(name) {
60
- return this._fetch('lookup', { name })
61
- }
62
-
63
- staticCall(contract, method, params) {
64
- return this._fetch('staticCall', {contract, method, params})
65
- }
66
- nativeBurns() {
67
- return this._fetch('nativeBurns')
68
- }
69
- contracts() {
70
- return this._fetch('contracts')
71
- }
72
- nativeMints() {
73
- return this._fetch('nativeMints')
74
- }
75
- nativeToken() {
76
- return this._fetch('nativeToken')
77
- }
78
- nativeTransfers() {
79
- return this._fetch('nativeTransfers')
80
- }
81
- totalSize() {
82
- return this._fetch('totalSize')
83
- }
84
- totalTransactions() {
85
- return this._fetch('totalTransactions')
86
- }
87
- totalBlocks() {
88
- return this._fetch('totalBlocks')
89
- }
90
- nativeCalls() {
91
- return this._fetch('nativeCalls')
92
- }
93
- participating() {
94
- return this._fetch('participating')
95
- }
96
- participate(address) {
97
- return this._fetch('participate', { address })
98
- }
99
-
100
- createContractAddress(owner, code, params) {
101
- return this._fetch('createContractAddress', { owner, code, params })
102
- }
103
- deployContract(code, params) {
104
- return this._fetch('deployContract', { code, params })
105
- }
106
- }
1
+ class http {
2
+ url;
3
+ networkVersion;
4
+ get isHttpClient() {
5
+ return true;
6
+ }
7
+ constructor(url, networkVersion) {
8
+ this.url = url;
9
+ this.networkVersion = networkVersion;
10
+ }
11
+ async _fetch(method, params, type = 'json') {
12
+ params = new URLSearchParams(params).toString();
13
+ const response = await fetch(`${this.url}/${method}?${params}`);
14
+ return response[type]();
15
+ }
16
+ async _fetchString(method, params) {
17
+ return String(await this._fetch(method, params, 'text'));
18
+ }
19
+ async _fetchNumber(method, params) {
20
+ return Number(await this._fetchString(method, params));
21
+ }
22
+ async _fetchBoolean(method, params) {
23
+ return Boolean((await this._fetchString(method, params)) === 'true');
24
+ }
25
+ balances() {
26
+ return this._fetch('balances');
27
+ }
28
+ balanceOf(address, format) {
29
+ return this._fetch(`balanceOf`, { address, format });
30
+ }
31
+ selectedAccount() {
32
+ return this._fetchString('selectedAccount');
33
+ }
34
+ selectAccount(address) {
35
+ return this._fetch('selectAccount', { address });
36
+ }
37
+ accounts() {
38
+ return this._fetch('accounts');
39
+ }
40
+ hasTransactionToHandle() {
41
+ return this._fetchBoolean('hasTransactionToHandle');
42
+ }
43
+ getBlock(index) {
44
+ return this._fetch('getBlock', { index });
45
+ }
46
+ blocks(amount) {
47
+ return this._fetch('blocks', { amount });
48
+ }
49
+ sendTransaction(transaction) {
50
+ return this._fetch('sendTransaction', transaction);
51
+ }
52
+ peerId() {
53
+ return this._fetchString('peerId');
54
+ }
55
+ peers() {
56
+ return this._fetch('peers');
57
+ }
58
+ validators() {
59
+ return this._fetch('validators');
60
+ }
61
+ lookup(name) {
62
+ return this._fetch('lookup', { name });
63
+ }
64
+ staticCall(contract, method, params) {
65
+ return this._fetch('staticCall', { contract, method, params });
66
+ }
67
+ nativeBurns() {
68
+ return this._fetchNumber('nativeBurns');
69
+ }
70
+ contracts() {
71
+ return this._fetch('contracts');
72
+ }
73
+ nativeMints() {
74
+ return this._fetchNumber('nativeMints');
75
+ }
76
+ nativeToken() {
77
+ return this._fetchString('nativeToken');
78
+ }
79
+ nativeTransfers() {
80
+ return this._fetchNumber('nativeTransfers');
81
+ }
82
+ totalSize() {
83
+ return this._fetchNumber('totalSize');
84
+ }
85
+ totalTransactions() {
86
+ return this._fetchNumber('totalTransactions');
87
+ }
88
+ totalBlocks() {
89
+ return this._fetchNumber('totalBlocks');
90
+ }
91
+ nativeCalls() {
92
+ return this._fetchNumber('nativeCalls');
93
+ }
94
+ participating() {
95
+ return this._fetchBoolean('participating');
96
+ }
97
+ participate(address) {
98
+ return this._fetch('participate', { address });
99
+ }
100
+ createContractAddress(owner, code, params) {
101
+ return this._fetch('createContractAddress', { owner, code, params });
102
+ }
103
+ deployContract(code, params) {
104
+ return this._fetch('deployContract', { code, params });
105
+ }
106
+ }
107
+
108
+ export { http as default };
@@ -0,0 +1,3 @@
1
+ export { default as ws } from './ws.js';
2
+ export { default as http } from './http.js';
3
+ import 'socket-request-client';
@@ -0,0 +1,40 @@
1
+ type responseType = 'json' | 'text' | 'arrayBuffer' | 'clone' | 'formData' | 'blob';
2
+ type networkVersion = 'peach' | string;
3
+ export default class {
4
+ url: URL;
5
+ networkVersion: networkVersion;
6
+ get isHttpClient(): boolean;
7
+ constructor(url: any, networkVersion: any);
8
+ _fetch(method: any, params?: any, type?: responseType): Promise<any>;
9
+ _fetchString(method: string, params?: any): Promise<string>;
10
+ _fetchNumber(method: string, params?: any): Promise<number>;
11
+ _fetchBoolean(method: string, params?: any): Promise<boolean>;
12
+ balances(): Promise<any>;
13
+ balanceOf(address: any, format: any): Promise<any>;
14
+ selectedAccount(): Promise<string>;
15
+ selectAccount(address: any): Promise<any>;
16
+ accounts(): Promise<any>;
17
+ hasTransactionToHandle(): Promise<boolean>;
18
+ getBlock(index: any): Promise<any>;
19
+ blocks(amount: any): Promise<any>;
20
+ sendTransaction(transaction: any): Promise<any>;
21
+ peerId(): Promise<string>;
22
+ peers(): Promise<any>;
23
+ validators(): Promise<any>;
24
+ lookup(name: any): Promise<any>;
25
+ staticCall(contract: any, method: any, params: any): Promise<any>;
26
+ nativeBurns(): Promise<number>;
27
+ contracts(): Promise<any>;
28
+ nativeMints(): Promise<number>;
29
+ nativeToken(): Promise<string>;
30
+ nativeTransfers(): Promise<number>;
31
+ totalSize(): Promise<number>;
32
+ totalTransactions(): Promise<number>;
33
+ totalBlocks(): Promise<number>;
34
+ nativeCalls(): Promise<number>;
35
+ participating(): Promise<boolean>;
36
+ participate(address: any): Promise<any>;
37
+ createContractAddress(owner: any, code: any, params: any): Promise<any>;
38
+ deployContract(code: any, params: any): Promise<any>;
39
+ }
40
+ export {};
@@ -0,0 +1,2 @@
1
+ export { default as ws } from './ws.js';
2
+ export { default as http } from './http.js';
@@ -0,0 +1,40 @@
1
+ import socketRequestClient from 'socket-request-client';
2
+ export default class Client {
3
+ url: string;
4
+ networkVersion: string;
5
+ client: socketRequestClient;
6
+ constructor(url?: string, networkVersion?: string);
7
+ init(): Promise<void>;
8
+ get pubsub(): {
9
+ publish: any;
10
+ subscribe: any;
11
+ };
12
+ request(url: string, params?: {}): any;
13
+ balances(): any;
14
+ balanceOf(address: string, format: boolean): any;
15
+ selectedAccount(): any;
16
+ selectAccount(address: any): any;
17
+ accounts(): any;
18
+ hasTransactionToHandle(): any;
19
+ getBlock(index: number): any;
20
+ blocks(amount: number): any;
21
+ sendTransaction(transaction: any): any;
22
+ peerId(): any;
23
+ peers(): any;
24
+ validators(): any;
25
+ lookup(name: string): any;
26
+ staticCall(contract: string, method: string, params: {}): any;
27
+ nativeBurns(): number;
28
+ contracts(): number;
29
+ nativeMints(): number;
30
+ nativeToken(): number;
31
+ nativeTransfers(): number;
32
+ totalSize(): number;
33
+ totalTransactions(): number;
34
+ totalBlocks(): number;
35
+ nativeCalls(): number;
36
+ participating(): boolean;
37
+ participate(address: string): {};
38
+ createContractAddress(owner: string, code: string, params: {}): {};
39
+ deployContract(code: string, params: {}): {};
40
+ }
package/exports/ws.js CHANGED
@@ -1,114 +1,106 @@
1
- import socketRequestClient from 'socket-request-client'
2
-
3
- export default class Client {
4
- constructor(url = 'ws://localhost:4040', networkVersion = 'leofcoin-peach') {
5
- this.url = url
6
- this.networkVersion = networkVersion
7
- }
8
-
9
- async init() {
10
- this.client = await socketRequestClient(this.url, this.networkVersion)
11
- globalThis.client = this
12
- }
13
-
14
- get pubsub() {
15
- return {
16
- publish: this.client.pubsub.publish,
17
- subscribe: this.client.pubsub.subscribe
18
- }
19
- }
20
-
21
- request(url, params) {
22
- return this.client.request({url, params})
23
- }
24
-
25
- balances() {
26
- return this.request('balances')
27
- }
28
-
29
- balanceOf(address, format) {
30
- return this.request('balanceOf', {address, format})
31
- }
32
-
33
- selectedAccount() {
34
- return this.request('selectedAccount')
35
- }
36
-
37
- selectAccount(address) {
38
- return this.request('selectAccount', {address})
39
- }
40
-
41
- accounts() {
42
- return this.request('accounts')
43
- }
44
-
45
- hasTransactionToHandle() {
46
- return this.request('hasTransactionToHandle')
47
- }
48
-
49
- getBlock(index) {
50
- return this.request('getBlock', { index })
51
- }
52
- blocks(amount) {
53
- return this.request('blocks', {amount})
54
- }
55
-
56
- createTransactionFrom(from, to, method, parameters, nonce) {
57
- return this.request('createTransactionFrom', {from, to, method, parameters, nonce})
58
- }
59
- peerId() {
60
- return this.request('peerId')
61
- }
62
- peers() {
63
- return this.request('peers')
64
- }
65
- validators() {
66
- return this.request('validators')
67
- }
68
- lookup(name) {
69
- return this.request('lookup', { name })
70
- }
71
-
72
- staticCall(contract, method, params) {
73
- return this.request('staticCall', {contract, method, params})
74
- }
75
- nativeBurns() {
76
- return this.request('nativeBurns')
77
- }
78
- contracts() {
79
- return this.request('contracts')
80
- }
81
- nativeMints() {
82
- return this.request('nativeMints')
83
- }
84
- nativeToken() {
85
- return this.request('nativeToken')
86
- }
87
- nativeTransfers() {
88
- return this.request('nativeTransfers')
89
- }
90
- totalSize() {
91
- return this.request('totalSize')
92
- }
93
- totalTransactions() {
94
- return this.request('totalTransactions')
95
- }
96
- totalBlocks() {
97
- return this.request('totalBlocks')
98
- }
99
- nativeCalls() {
100
- return this.request('nativeCalls')
101
- }
102
- participating() {
103
- return this.request('participating')
104
- }
105
- participate(address) {
106
- return this.request('participate', { address })
107
- }
108
- createContractAddress(owner, code, params) {
109
- return this.request('createContractAddress', { owner, code, params })
110
- }
111
- deployContract(code, params) {
112
- return this.request('deployContract', { code, params })
113
- }
114
- }
1
+ import socketRequestClient from 'socket-request-client';
2
+
3
+ class Client {
4
+ url;
5
+ networkVersion;
6
+ client;
7
+ constructor(url = 'ws://localhost:4040', networkVersion = 'leofcoin-peach') {
8
+ this.url = url;
9
+ this.networkVersion = networkVersion;
10
+ }
11
+ async init() {
12
+ this.client = await socketRequestClient(this.url, this.networkVersion);
13
+ }
14
+ get pubsub() {
15
+ return {
16
+ publish: this.client.pubsub.publish,
17
+ subscribe: this.client.pubsub.subscribe
18
+ };
19
+ }
20
+ request(url, params) {
21
+ return this.client.request({ url, params });
22
+ }
23
+ balances() {
24
+ return this.request('balances');
25
+ }
26
+ balanceOf(address, format) {
27
+ return this.request('balanceOf', { address, format });
28
+ }
29
+ selectedAccount() {
30
+ return this.request('selectedAccount');
31
+ }
32
+ selectAccount(address) {
33
+ return this.request('selectAccount', { address });
34
+ }
35
+ accounts() {
36
+ return this.request('accounts');
37
+ }
38
+ hasTransactionToHandle() {
39
+ return this.request('hasTransactionToHandle');
40
+ }
41
+ getBlock(index) {
42
+ return this.request('getBlock', { index });
43
+ }
44
+ blocks(amount) {
45
+ return this.request('blocks', { amount });
46
+ }
47
+ sendTransaction(transaction) {
48
+ return this.request('sendTransaction', transaction);
49
+ }
50
+ peerId() {
51
+ return this.request('peerId');
52
+ }
53
+ peers() {
54
+ return this.request('peers');
55
+ }
56
+ validators() {
57
+ return this.request('validators');
58
+ }
59
+ lookup(name) {
60
+ return this.request('lookup', { name });
61
+ }
62
+ staticCall(contract, method, params) {
63
+ return this.request('staticCall', { contract, method, params });
64
+ }
65
+ nativeBurns() {
66
+ return this.request('nativeBurns');
67
+ }
68
+ contracts() {
69
+ return this.request('contracts');
70
+ }
71
+ nativeMints() {
72
+ return this.request('nativeMints');
73
+ }
74
+ nativeToken() {
75
+ return this.request('nativeToken');
76
+ }
77
+ nativeTransfers() {
78
+ return this.request('nativeTransfers');
79
+ }
80
+ totalSize() {
81
+ return this.request('totalSize');
82
+ }
83
+ totalTransactions() {
84
+ return this.request('totalTransactions');
85
+ }
86
+ totalBlocks() {
87
+ return this.request('totalBlocks');
88
+ }
89
+ nativeCalls() {
90
+ return this.request('nativeCalls');
91
+ }
92
+ participating() {
93
+ return this.request('participating');
94
+ }
95
+ participate(address) {
96
+ return this.request('participate', { address });
97
+ }
98
+ createContractAddress(owner, code, params) {
99
+ return this.request('createContractAddress', { owner, code, params });
100
+ }
101
+ deployContract(code, params) {
102
+ return this.request('deployContract', { code, params });
103
+ }
104
+ }
105
+
106
+ export { Client as default };
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "@leofcoin/endpoint-clients",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "",
5
5
  "exports": {
6
6
  "./ws": "./exports/ws.js",
7
7
  "./http": "./exports/http.js"
8
8
  },
9
+ "types": "./exports/types/index.d.ts",
9
10
  "type": "module",
10
11
  "scripts": {
12
+ "build": "rollup -c",
11
13
  "test": "echo \"Error: no test specified\" && exit 1"
12
14
  },
13
15
  "repository": {
@@ -20,5 +22,13 @@
20
22
  "bugs": {
21
23
  "url": "https://github.com/leofcoin/endpoint-clients/issues"
22
24
  },
23
- "homepage": "https://github.com/leofcoin/endpoint-clients#readme"
25
+ "homepage": "https://github.com/leofcoin/endpoint-clients#readme",
26
+ "devDependencies": {
27
+ "@rollup/plugin-typescript": "^11.0.0",
28
+ "rollup": "^3.14.0",
29
+ "tslib": "^2.5.0"
30
+ },
31
+ "dependencies": {
32
+ "socket-request-client": "^1.7.3"
33
+ }
24
34
  }
@@ -0,0 +1,15 @@
1
+ import typescript from '@rollup/plugin-typescript'
2
+ import tsConfig from './tsconfig.json' assert { type: 'json'}
3
+ export default [{
4
+ input: ['./src/index.ts', './src/http.ts', './src/ws.ts'],
5
+ output: {
6
+ dir: './exports',
7
+ format: 'es'
8
+ },
9
+ plugins: [
10
+ typescript(tsConfig)
11
+ ],
12
+ external: [
13
+ './identity.js'
14
+ ]
15
+ }]
package/src/http.ts ADDED
@@ -0,0 +1,126 @@
1
+
2
+ // see https://developer.mozilla.org/en-US/docs/Web/API/Response#instance_methods
3
+ type responseType = 'json' | 'text' | 'arrayBuffer' | 'clone' | 'formData' | 'blob';
4
+ type networkVersion = 'peach' | string;
5
+
6
+ export default class {
7
+ url: URL;
8
+ networkVersion: networkVersion;
9
+ get isHttpClient() {
10
+ return true
11
+ }
12
+
13
+ constructor(url, networkVersion) {
14
+ this.url = url
15
+ this.networkVersion = networkVersion
16
+ }
17
+
18
+ async _fetch(method, params?, type: responseType = 'json') {
19
+ params = new URLSearchParams(params).toString()
20
+ const response = await fetch(`${this.url}/${method}?${params}`)
21
+ return response[type]()
22
+ }
23
+
24
+ async _fetchString(method: string, params?) {
25
+ return String(await this._fetch(method, params, 'text'))
26
+ }
27
+
28
+ async _fetchNumber(method: string, params?) {
29
+ return Number(await this._fetchString(method, params))
30
+ }
31
+
32
+ async _fetchBoolean(method: string, params?) {
33
+ return Boolean((await this._fetchString(method, params)) === 'true')
34
+ }
35
+
36
+ balances() {
37
+ return this._fetch('balances')
38
+ }
39
+
40
+ balanceOf(address, format) {
41
+ return this._fetch(`balanceOf`, {address, format})
42
+ }
43
+
44
+ selectedAccount() {
45
+ return this._fetchString('selectedAccount')
46
+ }
47
+
48
+ selectAccount(address) {
49
+ return this._fetch('selectAccount', {address})
50
+ }
51
+
52
+ accounts() {
53
+ return this._fetch('accounts')
54
+ }
55
+
56
+ hasTransactionToHandle() {
57
+ return this._fetchBoolean('hasTransactionToHandle')
58
+ }
59
+
60
+ getBlock(index) {
61
+ return this._fetch('getBlock', { index })
62
+ }
63
+ blocks(amount) {
64
+ return this._fetch('blocks', {amount})
65
+ }
66
+
67
+ sendTransaction(transaction) {
68
+ return this._fetch('sendTransaction', transaction)
69
+ }
70
+ peerId() {
71
+ return this._fetchString('peerId')
72
+ }
73
+ peers() {
74
+ return this._fetch('peers')
75
+ }
76
+ validators() {
77
+ return this._fetch('validators')
78
+ }
79
+ lookup(name) {
80
+ return this._fetch('lookup', { name })
81
+ }
82
+
83
+ staticCall(contract, method, params) {
84
+ return this._fetch('staticCall', {contract, method, params})
85
+ }
86
+ nativeBurns() {
87
+ return this._fetchNumber('nativeBurns')
88
+ }
89
+ contracts() {
90
+ return this._fetch('contracts')
91
+ }
92
+ nativeMints() {
93
+ return this._fetchNumber('nativeMints')
94
+ }
95
+ nativeToken() {
96
+ return this._fetchString('nativeToken')
97
+ }
98
+ nativeTransfers() {
99
+ return this._fetchNumber('nativeTransfers')
100
+ }
101
+ totalSize() {
102
+ return this._fetchNumber('totalSize')
103
+ }
104
+ totalTransactions() {
105
+ return this._fetchNumber('totalTransactions')
106
+ }
107
+ totalBlocks() {
108
+ return this._fetchNumber('totalBlocks')
109
+ }
110
+ nativeCalls() {
111
+ return this._fetchNumber('nativeCalls')
112
+ }
113
+ participating() {
114
+ return this._fetchBoolean('participating')
115
+ }
116
+ participate(address) {
117
+ return this._fetch('participate', { address })
118
+ }
119
+
120
+ createContractAddress(owner, code, params) {
121
+ return this._fetch('createContractAddress', { owner, code, params })
122
+ }
123
+ deployContract(code, params) {
124
+ return this._fetch('deployContract', { code, params })
125
+ }
126
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export {default as ws} from './ws.js'
2
+ export {default as http} from './http.js'
package/src/ws.ts ADDED
@@ -0,0 +1,117 @@
1
+ import socketRequestClient from 'socket-request-client'
2
+
3
+ export default class Client {
4
+ url: string
5
+ networkVersion: string
6
+ client: socketRequestClient
7
+
8
+ constructor(url = 'ws://localhost:4040', networkVersion = 'leofcoin-peach') {
9
+ this.url = url
10
+ this.networkVersion = networkVersion
11
+ }
12
+
13
+ async init() {
14
+ this.client = await socketRequestClient(this.url, this.networkVersion)
15
+ }
16
+
17
+ get pubsub() {
18
+ return {
19
+ publish: this.client.pubsub.publish,
20
+ subscribe: this.client.pubsub.subscribe
21
+ }
22
+ }
23
+
24
+ request(url: string, params?: {}) {
25
+ return this.client.request({url, params})
26
+ }
27
+
28
+ balances() {
29
+ return this.request('balances')
30
+ }
31
+
32
+ balanceOf(address: string, format: boolean) {
33
+ return this.request('balanceOf', {address, format})
34
+ }
35
+
36
+ selectedAccount() {
37
+ return this.request('selectedAccount')
38
+ }
39
+
40
+ selectAccount(address: any) {
41
+ return this.request('selectAccount', {address})
42
+ }
43
+
44
+ accounts() {
45
+ return this.request('accounts')
46
+ }
47
+
48
+ hasTransactionToHandle() {
49
+ return this.request('hasTransactionToHandle')
50
+ }
51
+
52
+ getBlock(index: number) {
53
+ return this.request('getBlock', { index })
54
+ }
55
+ blocks(amount: number) {
56
+ return this.request('blocks', {amount})
57
+ }
58
+
59
+ sendTransaction(transaction) {
60
+ return this.request('sendTransaction', transaction)
61
+ }
62
+ peerId() {
63
+ return this.request('peerId')
64
+ }
65
+ peers() {
66
+ return this.request('peers')
67
+ }
68
+ validators() {
69
+ return this.request('validators')
70
+ }
71
+ lookup(name: string) {
72
+ return this.request('lookup', { name })
73
+ }
74
+
75
+ staticCall(contract: string, method: string, params: {}) {
76
+ return this.request('staticCall', {contract, method, params})
77
+ }
78
+ nativeBurns(): number {
79
+ return this.request('nativeBurns')
80
+ }
81
+ contracts(): number {
82
+ return this.request('contracts')
83
+ }
84
+ nativeMints(): number {
85
+ return this.request('nativeMints')
86
+ }
87
+ nativeToken(): number {
88
+ return this.request('nativeToken')
89
+ }
90
+ nativeTransfers(): number {
91
+ return this.request('nativeTransfers')
92
+ }
93
+ totalSize(): number {
94
+ return this.request('totalSize')
95
+ }
96
+ totalTransactions(): number {
97
+ return this.request('totalTransactions')
98
+ }
99
+ totalBlocks(): number {
100
+ return this.request('totalBlocks')
101
+ }
102
+ nativeCalls(): number {
103
+ return this.request('nativeCalls')
104
+ }
105
+ participating(): boolean {
106
+ return this.request('participating')
107
+ }
108
+ participate(address: string): {} {
109
+ return this.request('participate', { address })
110
+ }
111
+ createContractAddress(owner: string, code: string, params: {}): {} {
112
+ return this.request('createContractAddress', { owner, code, params })
113
+ }
114
+ deployContract(code: string, params: {}): {} {
115
+ return this.request('deployContract', { code, params })
116
+ }
117
+ }
package/test/index.js CHANGED
@@ -1,13 +1,13 @@
1
- import { needsImplementations } from './spec.js';
2
- import WsClient from './../exports/ws.js'
3
- import HttpClient from './../exports/http.js'
4
-
5
- try {
6
- const wsNeeds = needsImplementations(new WsClient())
7
- if (wsNeeds.length > 0) throw new Error(`ws: missing implementations ${wsNeeds.length > 2 ? wsNeeds.join(', ') : wsNeeds.join(' & ')}`)
8
-
9
- const httpNeeds = needsImplementations(new HttpClient())
10
- if (httpNeeds.length > 0) throw new Error(`http: missing implementations ${httpNeeds.length > 2 ? httpNeeds.join(', ') : httpNeeds.join(' & ')}`)
11
- } catch (error) {
12
- console.error(error);
1
+ import { needsImplementations } from './spec.js';
2
+ import WsClient from './../exports/ws.js'
3
+ import HttpClient from './../exports/http.js'
4
+
5
+ try {
6
+ const wsNeeds = needsImplementations(new WsClient())
7
+ if (wsNeeds.length > 0) throw new Error(`ws: missing implementations ${wsNeeds.length > 2 ? wsNeeds.join(', ') : wsNeeds.join(' & ')}`)
8
+
9
+ const httpNeeds = needsImplementations(new HttpClient())
10
+ if (httpNeeds.length > 0) throw new Error(`http: missing implementations ${httpNeeds.length > 2 ? httpNeeds.join(', ') : httpNeeds.join(' & ')}`)
11
+ } catch (error) {
12
+ console.error(error);
13
13
  }
package/test/spec.js CHANGED
@@ -1,43 +1,43 @@
1
- export const implementations = [
2
- 'accounts',
3
- 'balanceOf',
4
- 'balances',
5
- 'blocks',
6
- 'contracts',
7
- 'createContractAddress',
8
- 'createTransactionFrom',
9
- 'deployContract',
10
- 'getBlock',
11
- 'hasTransactionToHandle',
12
- 'lookup',
13
- 'nativeBurns',
14
- 'nativeCalls',
15
- 'nativeMints',
16
- 'nativeToken',
17
- 'nativeTransfers',
18
- 'participate',
19
- 'participating',
20
- 'peerId',
21
- 'peers',
22
- 'pubsub',
23
- 'selectAccount',
24
- 'selectedAccount',
25
- 'staticCall',
26
- 'totalBlocks',
27
- 'totalSize',
28
- 'totalTransactions',
29
- 'validators'
30
- ]
31
-
32
- export const needsImplementations = (client) => {
33
- const needs = []
34
- for (const key of implementations) {
35
- try {
36
- if (!client[key] && key === 'pubsub' && !client.isHttpClient) needs.push(key)
37
- } catch (error) {
38
- if (error.message !== "Cannot read properties of undefined (reading 'pubsub')") throw new Error(error)
39
- }
40
-
41
- }
42
- return needs
1
+ export const implementations = [
2
+ 'accounts',
3
+ 'balanceOf',
4
+ 'balances',
5
+ 'blocks',
6
+ 'contracts',
7
+ 'createContractAddress',
8
+ 'createTransactionFrom',
9
+ 'deployContract',
10
+ 'getBlock',
11
+ 'hasTransactionToHandle',
12
+ 'lookup',
13
+ 'nativeBurns',
14
+ 'nativeCalls',
15
+ 'nativeMints',
16
+ 'nativeToken',
17
+ 'nativeTransfers',
18
+ 'participate',
19
+ 'participating',
20
+ 'peerId',
21
+ 'peers',
22
+ 'pubsub',
23
+ 'selectAccount',
24
+ 'selectedAccount',
25
+ 'staticCall',
26
+ 'totalBlocks',
27
+ 'totalSize',
28
+ 'totalTransactions',
29
+ 'validators'
30
+ ]
31
+
32
+ export const needsImplementations = (client) => {
33
+ const needs = []
34
+ for (const key of implementations) {
35
+ try {
36
+ if (!client[key] && key === 'pubsub' && !client.isHttpClient) needs.push(key)
37
+ } catch (error) {
38
+ if (error.message !== "Cannot read properties of undefined (reading 'pubsub')") throw new Error(error)
39
+ }
40
+
41
+ }
42
+ return needs
43
43
  }
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "es2022",
4
+ "target": "es2022",
5
+ "outDir": "./exports",
6
+ "moduleResolution":"NodeNext",
7
+ "allowJs": true,
8
+ "declaration": true,
9
+ "declarationDir": "./exports/types",
10
+ "allowSyntheticDefaultImports": true,
11
+ "resolveJsonModule": true
12
+ },
13
+ "include": [
14
+ "./src/**/*"
15
+ ],
16
+ "exclude": ["./node_modules"]
17
+ }