@leofcoin/endpoints 0.3.0 → 0.3.2

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.
@@ -0,0 +1,6 @@
1
+ /**
2
+ *
3
+ * ensure remote is set to false when serving on a server
4
+ */
5
+ declare const _default: (chain: any, port: any, networkVersion: any, remote?: boolean) => void;
6
+ export default _default;
package/exports/http.js CHANGED
@@ -1,193 +1,144 @@
1
- import koa from 'koa'
2
- import cors from '@koa/cors'
3
- import Router from '@koa/router'
4
- import {formatUnits} from '@leofcoin/utils'
5
- import {readFile} from 'fs/promises'
6
- import Showdown from 'showdown'
7
- import shared from './shared.js'
8
- import { dirname, join} from 'path'
9
- import { fileURLToPath } from 'url'
10
-
1
+ import koa from 'koa';
2
+ import cors from '@koa/cors';
3
+ import Router from '@koa/router';
4
+ import { formatUnits } from '@leofcoin/utils';
5
+ import { readFile } from 'fs/promises';
6
+ import Showdown from 'showdown';
7
+ import shared from './shared.js';
8
+ import { dirname, join } from 'path';
9
+ import { fileURLToPath } from 'url';
11
10
  const __filename = fileURLToPath(import.meta.url);
12
11
  const __dirname = dirname(__filename);
13
-
14
- const api = new koa()
15
- const router = new Router()
16
-
17
- const converter = new Showdown.Converter()
18
- const apiFile = await readFile(join(__dirname, './api.md'))
19
-
20
- export default (chain, port, networkVersion) => {
21
- // api routes
22
- router.get('/', ctx => {
23
- ctx.body = converter.makeHtml(apiFile.toString())
24
- })
25
-
26
- router.get('/network', ctx => {
27
- ctx.body = {
28
- version: networkVersion
29
- }
30
- })
31
-
32
- router.get('/networkStats', async ctx => {
33
- ctx.body = await shared.networkStats(chain, networkVersion)
34
- })
35
-
36
- router.get('/balances', async ctx => {
37
- ctx.body = await chain.balances
38
- })
39
-
40
- router.get('/balanceOf', async ctx => {
41
- const balance = (await chain.balances)[ctx.query.address]
42
- if (balance) ctx.body = ctx.query.format ? formatUnits(balance) : balance
43
- else ctx.body = 0
44
- })
45
-
46
- router.get('/selectedAccount', ctx => ctx.body = peernet.selectedAccount)
47
-
48
- router.get('/selectAccount', async ctx => {
49
- try {
50
- await walletStore.put('selected-account', address)
51
- } catch (error) {
52
- }
53
- return
54
- })
55
-
56
- router.get('/accounts', async ctx => {
57
- ctx.body = JSON.parse(new TextDecoder().decode((await walletStore.get('accounts'))))
58
- })
59
-
60
- router.get('/hasTransactionToHandle', async ctx => {
61
- ctx.body = await chain.hasTransactionToHandle()
62
- })
63
-
64
- router.get('/getNonce', async ctx => {
65
- ctx.body = await chain.getNonce(ctx.query.address)
66
- })
67
-
68
- router.get('/getBlock', ctx => ctx.body = chain.blocks[ctx.query.index])
69
-
70
- router.get('/blocks', ctx => {
71
- ctx.body = chain.blocks.slice(ctx.query.amount)
72
- })
73
-
74
- router.get('/sendTransaction', async ctx => {
75
- try {
76
- const tx = await chain.sendTransaction(ctx.query.transaction)
77
- await tx.wait()
78
- delete tx.wait
79
- ctx.body = tx
80
- } catch (error) {
81
- // ctx(202)
82
- }
83
- })
84
-
85
- router.get('/peerId', ctx =>
86
- ctx.body = peernet.peerId
87
- )
88
-
89
- router.get('/validators', ctx =>
90
- ctx.body = chain.validators
91
- )
92
-
93
- router.get('/peers', ctx =>
94
- ctx.body = peernet.peers.map(([id, peer]) => id)
95
- )
96
-
97
- router.get('/lookup', async ctx => {
98
- ctx.body = await chain.lookup(ctx.query.name)
99
- })
100
-
101
- router.get('/staticCall', async ctx =>
102
- ctx.body = await chain.staticCall(ctx.query.contract, ctx.query.method, ctx.query.params)
103
- )
104
-
105
- router.get('/nativeBurns', ctx =>
106
- ctx.body = chain.nativeBurns
107
- )
108
-
109
- router.get('/contracts', ctx =>
110
- ctx.body = chain.contracts
111
- )
112
-
113
- router.get('/nativeMints', ctx =>
114
- ctx.body = chain.nativeMints
115
- )
116
-
117
- router.get('/nativeToken', ctx =>
118
- ctx.body = chain.nativeToken
119
- )
120
-
121
- router.get('/nativeTransfers', ctx =>
122
- ctx.body = chain.nativeTransfers
123
- )
124
-
125
- router.get('/totalSize', ctx =>
126
- ctx.body = chain.totalSize
127
- )
128
-
129
- router.get('/totalTransactions', ctx =>
130
- ctx.body = chain.totalTransactions
131
- )
132
-
133
- router.get('/poolTransactions', async ctx =>
134
- ctx.body = await transactionPoolStore.get()
135
- )
136
- router.get('/transactionsInPool', async ctx =>
137
- ctx.body = await transactionPoolStore.length()
138
- )
139
- router.get('/transactionPoolSize', async ctx =>
140
- ctx.body = await transactionPoolStore.size()
141
- )
142
-
143
- router.get('/totalBlocks', ctx =>
144
- ctx.body = chain.blocks.length
145
- )
146
-
147
- router.get('/nativeCalls', ctx =>
148
- ctx.body = chain.nativeCalls
149
- )
150
-
151
- router.get('/participating', ctx =>
152
- ctx.body = chain.participating
153
- )
154
-
155
- router.get('/participate', async ctx =>
156
- ctx.body = await chain.participate(ctx.query.address)
157
- )
158
-
159
- router.get('/deployContract', async ctx => {
160
- try {
161
- const tx = await chain.deployContract(ctx.query.code, ctx.query.params)
162
- await tx.wait()
163
- delete tx.wait
164
- ctx.body = JSON.stringify(tx)
165
- } catch (error) {
166
-
167
- }
168
- })
169
-
170
- router.get('/createContractAddress', async ctx => {
171
- try {
172
- const address = await chain.createContractAddress(ctx.query.owner, ctx.query.code, ctx.query.params)
173
- ctx.body = address
174
- } catch (error) {
175
-
176
- }
177
- })
178
-
179
- router.get('/lastBlock', async ctx => ctx.body = chain.lastBlock)
180
-
181
- router.get('/blockHashMap', async ctx => ctx.body = chain.blockHashMap)
182
-
183
- router.get('/bootstrap', async ctx => {
184
- ctx.body = (await shared.bootstrap()).map(value => Array.from(value.toString()))
185
- })
186
-
187
- // todo finish ...
188
- api.use(cors('*'))
189
- api.use(router.routes())
190
- api.listen(port)
191
- }
192
-
193
-
12
+ const api = new koa();
13
+ const router = new Router();
14
+ const converter = new Showdown.Converter();
15
+ const apiFile = await readFile(join(__dirname, './api.md'));
16
+ /**
17
+ *
18
+ * ensure remote is set to false when serving on a server
19
+ */
20
+ export default (chain, port, networkVersion, remote = false) => {
21
+ // api routes
22
+ router.get('/', (ctx) => {
23
+ ctx.body = converter.makeHtml(apiFile.toString());
24
+ });
25
+ router.get('/network', (ctx) => {
26
+ ctx.body = {
27
+ version: networkVersion
28
+ };
29
+ });
30
+ router.get('/networkStats', async (ctx) => {
31
+ ctx.body = await shared.networkStats(chain, networkVersion);
32
+ });
33
+ router.get('/balances', async (ctx) => {
34
+ ctx.body = await chain.balances;
35
+ });
36
+ router.get('/balanceOf', async (ctx) => {
37
+ // @ts-ignore
38
+ const balance = (await chain.balances)[ctx.query.address];
39
+ if (balance)
40
+ ctx.body = ctx.query.format ? formatUnits(balance) : balance;
41
+ else
42
+ ctx.body = 0;
43
+ });
44
+ router.get('/selectedAccount', (ctx) => {
45
+ if (remote)
46
+ ctx.body = globalThis.peernet.selectedAccount;
47
+ else
48
+ ctx.status = 405;
49
+ });
50
+ router.get('/selectAccount', async (ctx) => {
51
+ if (remote) {
52
+ globalThis.peernet.selectedAccount = ctx.query.address;
53
+ try {
54
+ await globalThis.walletStore.put('selected-account', ctx.query.address);
55
+ }
56
+ catch (error) {
57
+ ctx.body = error.message;
58
+ }
59
+ }
60
+ else {
61
+ ctx.status = 405;
62
+ }
63
+ });
64
+ router.get('/accounts', async (ctx) => {
65
+ if (remote)
66
+ ctx.body = JSON.parse(new TextDecoder().decode(await globalThis.walletStore.get('accounts')));
67
+ else
68
+ ctx.status = 405;
69
+ });
70
+ router.get('/hasTransactionToHandle', async (ctx) => {
71
+ ctx.body = await chain.hasTransactionToHandle();
72
+ });
73
+ router.get('/getNonce', async (ctx) => {
74
+ ctx.body = await chain.getNonce(ctx.query.address);
75
+ });
76
+ router.get('/getBlock', async (ctx) => (ctx.body = await chain.getBlock(ctx.query.index)));
77
+ router.get('/blocks', async (ctx) => {
78
+ ctx.body = chain.getBlocks(ctx.query.amount);
79
+ });
80
+ router.get('/sendTransaction', async (ctx) => {
81
+ try {
82
+ const tx = await chain.sendTransaction(ctx.query.transaction);
83
+ await tx.wait();
84
+ delete tx.wait;
85
+ ctx.body = tx;
86
+ }
87
+ catch (error) {
88
+ // ctx(202)
89
+ }
90
+ });
91
+ router.get('/peerId', (ctx) => (ctx.body = globalThis.peernet.peerId));
92
+ router.get('/validators', (ctx) => (ctx.body = chain.validators));
93
+ router.get('/peers', (ctx) => (ctx.body = globalThis.peernet.peers.map(([id, peer]) => id)));
94
+ router.get('/lookup', async (ctx) => {
95
+ ctx.body = await chain.lookup(ctx.query.name);
96
+ });
97
+ router.get('/staticCall', async (ctx) => (ctx.body = await chain.staticCall(ctx.query.contract, ctx.query.method, ctx.query.params)));
98
+ router.get('/nativeBurns', async (ctx) => (ctx.body = await chain.nativeBurns));
99
+ router.get('/contracts', async (ctx) => (ctx.body = await chain.contracts));
100
+ router.get('/nativeMints', async (ctx) => (ctx.body = await chain.nativeMints));
101
+ router.get('/nativeToken', async (ctx) => (ctx.body = await chain.nativeToken));
102
+ router.get('/nativeTransfers', async (ctx) => (ctx.body = await chain.nativeTransfers));
103
+ router.get('/totalSize', async (ctx) => (ctx.body = await chain.totalSize));
104
+ router.get('/totalTransactions', async (ctx) => (ctx.body = await chain.totalTransactions));
105
+ router.get('/totalBlocks', async (ctx) => (ctx.body = await chain.totalBlocks));
106
+ router.get('/nativeCalls', async (ctx) => (ctx.body = await chain.nativeCalls));
107
+ router.get('/lastBlock', async (ctx) => (ctx.body = await chain.lastBlock));
108
+ router.get('/poolTransactions', async (ctx) => (ctx.body = await globalThis.transactionPoolStore.get()));
109
+ router.get('/transactionsInPool', async (ctx) => (ctx.body = await globalThis.transactionPoolStore.length()));
110
+ router.get('/transactionPoolSize', async (ctx) => (ctx.body = await globalThis.transactionPoolStore.size()));
111
+ router.get('/participating', (ctx) => (ctx.body = chain.participating));
112
+ router.get('/participate', async (ctx) => {
113
+ if (remote)
114
+ ctx.body = await chain.participate(ctx.query.address);
115
+ else
116
+ ctx.status = 405;
117
+ });
118
+ router.get('/deployContract', async (ctx) => {
119
+ try {
120
+ const tx = await chain.deployContract(ctx.query.contract, ctx.query.transaction);
121
+ await tx.wait();
122
+ delete tx.wait;
123
+ ctx.body = JSON.stringify(tx);
124
+ }
125
+ catch (error) {
126
+ ctx.body = error.mesage;
127
+ }
128
+ });
129
+ router.get('/createContractAddress', async (ctx) => {
130
+ try {
131
+ const address = await chain.createContractAddress(ctx.query.owner, ctx.query.code, ctx.query.params);
132
+ ctx.body = address;
133
+ }
134
+ catch (error) { }
135
+ });
136
+ router.get('/blockHashMap', async (ctx) => (ctx.body = chain.blockHashMap));
137
+ router.get('/bootstrap', async (ctx) => {
138
+ ctx.body = (await shared.bootstrap()).map((value) => Array.from(value.toString()));
139
+ });
140
+ // todo finish ...
141
+ api.use(cors({ origin: '*' }));
142
+ api.use(router.routes());
143
+ api.listen(port);
144
+ };
@@ -0,0 +1,20 @@
1
+ export declare const networkStats: (chain: any, networkVersion: any) => Promise<{
2
+ version: any;
3
+ peers: any;
4
+ accounts: any;
5
+ accountsHolding: number;
6
+ accountsHoldingAmount: string;
7
+ topHolders: any[];
8
+ }>;
9
+ declare const _default: {
10
+ networkStats: (chain: any, networkVersion: any) => Promise<{
11
+ version: any;
12
+ peers: any;
13
+ accounts: any;
14
+ accountsHolding: number;
15
+ accountsHoldingAmount: string;
16
+ topHolders: any[];
17
+ }>;
18
+ bootstrap: () => Promise<any>;
19
+ };
20
+ export default _default;
package/exports/shared.js CHANGED
@@ -1,39 +1,38 @@
1
- import {formatUnits, BigNumber} from '@leofcoin/utils'
2
-
1
+ import { formatUnits, BigNumber } from '@leofcoin/utils';
3
2
  export const networkStats = async (chain, networkVersion) => {
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}
3
+ let accountsHolding = 0;
4
+ let accountsHoldingAmount = BigNumber.from(0);
5
+ let topHolders = [];
6
+ const balances = Object.entries(await chain.balances)
7
+ .map(([holder, amount]) => {
8
+ amount = BigNumber.from(amount);
9
+ return { holder, amount };
11
10
  })
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)})
11
+ // @ts-ignore
12
+ .sort((a, b) => formatUnits(b.amount.sub(a.amount)));
13
+ for (let { holder, amount } of balances) {
14
+ // @ts-ignore
15
+ if (amount.gt(0)) {
16
+ // @ts-ignore
17
+ accountsHoldingAmount = accountsHoldingAmount.add(amount);
18
+ accountsHolding += 1;
19
+ // @ts-ignore
20
+ topHolders.length < 100 && topHolders.push({ holder, amount: formatUnits(amount) });
21
+ }
19
22
  }
20
- }
21
-
22
- return {
23
- version: networkVersion,
24
- peers: peernet.peers.map(([id, peer]) => id),
25
- accounts: await accountsStore.length(),
26
- accountsHolding,
27
- accountsHoldingAmount: formatUnits(accountsHoldingAmount).toString(),
28
- topHolders
29
- }
30
- }
31
-
23
+ return {
24
+ version: networkVersion,
25
+ peers: globalThis.peernet.peers.map(([id, peer]) => id),
26
+ accounts: await globalThis.accountsStore.length(),
27
+ accountsHolding,
28
+ accountsHoldingAmount: formatUnits(accountsHoldingAmount).toString(),
29
+ topHolders
30
+ };
31
+ };
32
32
  const bootstrap = async () => {
33
- return blockStore.values()
34
- }
35
-
33
+ return globalThis.blockStore.values();
34
+ };
36
35
  export default {
37
- networkStats,
38
- bootstrap
39
- }
36
+ networkStats,
37
+ bootstrap
38
+ };
@@ -0,0 +1,5 @@
1
+ declare const _default: (chain: any, port: any, networkVersion: any, remote?: boolean) => Promise<{
2
+ close: () => any;
3
+ connections: any[];
4
+ }>;
5
+ export default _default;
package/exports/ws.js CHANGED
@@ -1,114 +1,111 @@
1
- import Server from 'socket-request-server'
2
- import {formatUnits} from '@leofcoin/utils'
3
- import shared from './shared.js'
4
-
5
- export default (chain, port, networkVersion) => {
6
- return Server({ port, protocol: networkVersion }, {
7
- network: async () => {
8
- send({version: networkVersion})
9
- },
10
- networkStats: async ({send}) => {
11
- send(await shared.networkStats(chain, networkVersion))
12
- },
13
- balances: async ({send}) =>
14
- send(await chain.balances),
15
- balanceOf: async ({address, format = false}, {send}) => {
16
- const balance = (await chain.balances)[address]
17
- if (balance) send(format ? formatUnits(balance) : balance)
18
- else send(0)
19
- },
20
- getNonce: async ({address}, {send}) => send(await chain.getNonce(address)),
21
- selectedAccount: ({send}) => send(peernet.selectedAccount),
22
- selectAccount: async ({address}, {send}) => {
23
- try {
24
- await walletStore.put('selected-account', address)
25
- send(200)
26
- } catch (error) {
27
- send(404)
28
- }
29
- },
30
- accounts: async ({send}) =>
31
- send(JSON.parse(new TextDecoder().decode((await walletStore.get('accounts'))))),
32
- hasTransactionToHandle: async ({send}) =>
33
- send(await chain.hasTransactionToHandle()),
34
- getBlock: ({index}, {send}) =>
35
- send(chain.blocks[index]),
36
- blocks:({amount}, {send}) => {
37
- send(chain.blocks.slice(amount))},
38
- sendTransaction: async (transaction, {send}) => {
39
- try {
40
- const tx = await chain.sendTransaction(transaction)
41
- await tx.wait()
42
- delete tx.wait
43
- send(tx)
44
- } catch (error) {
45
- send(202)
46
- }
47
- },
48
- peerId: ({send}) =>
49
- send(peernet.peerId),
50
- peers: ({send}) =>
51
- send(peernet.peers.map(([id, peer]) => id)),
52
- validators: ({send}) =>
53
- send(chain.validators),
54
- lookup: async ({name}, {send}) =>
55
- send(await chain.lookup(name)),
56
- staticCall: async({contract, method, params}, {send}) =>
57
- send(await chain.staticCall(contract, method, params)),
58
- nativeBurns: ({send}) =>
59
- send(chain.nativeBurns),
60
- contracts: ({send}) =>
61
- send(chain.contracts),
62
- nativeMints: ({send}) =>
63
- send(chain.nativeMints),
64
- nativeToken: ({send}) =>
65
- send(chain.nativeToken),
66
- nativeTransfers: ({send}) =>
67
- send(chain.nativeTransfers),
68
- totalSize: ({send}) =>
69
- send(chain.totalSize),
70
- totalTransactions: ({send}) =>
71
- send(chain.totalTransactions),
72
- poolTransactions: async ({send}) =>
73
- send(await transactionPoolStore.get()),
74
- transactionsInPool: async ({send}) =>
75
- send(await transactionPoolStore.length()),
76
- transactionPoolSize: async ({send}) =>
77
- send(await transactionPoolStore.size()),
78
- totalBlocks: ({send}) =>
79
- send(chain.blocks.length),
80
- nativeCalls: ({send}) =>
81
- send(chain.nativeCalls),
82
- participating: ({send}) =>
83
- send(chain.participating),
84
- participate: async ({address}, {send}) =>
85
- send(await chain.participate(address)),
86
- deployContract: async ({code, params}, {send}) => {
87
- try {
88
- const tx = await chain.deployContract(code, params)
89
- await tx.wait()
90
- delete tx.wait
91
- send(tx)
92
- } catch (error) {
93
- send(error.message)
94
- }
95
- },
96
- createContractAddress: async ({owner, code, params}, {send}) => {
97
- try {
98
- const address = await chain.createContractAddress(owner, code, params)
99
- send(address)
100
- } catch (error) {
101
- send(error)
102
- }
103
- },
104
-
105
- lastBlock: async ctx => ctx.body = chain.lastBlock,
106
-
107
- blockHashMap: async ctx => ctx.body = chain.blockHashMap,
108
-
109
- bootstrap: async ({send}) => {
110
- send(await shared.bootstrap())
111
- }
112
- })
113
- }
114
-
1
+ import Server from 'socket-request-server';
2
+ import { formatUnits } from '@leofcoin/utils';
3
+ import shared from './shared.js';
4
+ export default (chain, port, networkVersion, remote = false) => {
5
+ return Server({ port, protocol: networkVersion }, {
6
+ network: async ({ send }) => {
7
+ send({ version: networkVersion });
8
+ },
9
+ networkStats: async ({ send }) => {
10
+ send(await shared.networkStats(chain, networkVersion));
11
+ },
12
+ balances: async ({ send }) => send(await chain.balances),
13
+ balanceOf: async ({ address, format = false }, { send }) => {
14
+ const balance = (await chain.balances)[address];
15
+ if (balance)
16
+ send(format ? formatUnits(balance) : balance);
17
+ else
18
+ send(0);
19
+ },
20
+ getNonce: async ({ address }, { send }) => send(await chain.getNonce(address)),
21
+ selectedAccount: ({ send }) => {
22
+ if (remote)
23
+ send(globalThis.peernet.selectedAccount);
24
+ else
25
+ send(405);
26
+ },
27
+ selectAccount: async ({ address }, { send }) => {
28
+ if (remote)
29
+ try {
30
+ await globalThis.walletStore.put('selected-account', address);
31
+ send(200);
32
+ }
33
+ catch (error) {
34
+ send(404);
35
+ }
36
+ else
37
+ send(405);
38
+ },
39
+ accounts: async ({ send }) => {
40
+ if (remote)
41
+ send(JSON.parse(new TextDecoder().decode(await globalThis.walletStore.get('accounts'))));
42
+ else
43
+ send(405);
44
+ },
45
+ hasTransactionToHandle: async ({ send }) => send(await chain.hasTransactionToHandle()),
46
+ getBlock: async ({ index }, { send }) => send(await chain.getBlock(index)),
47
+ blocks: async ({ amount }, { send }) => {
48
+ send(await chain.getBlocks(amount));
49
+ },
50
+ sendTransaction: async (transaction, { send }) => {
51
+ try {
52
+ const tx = await chain.sendTransaction(transaction);
53
+ await tx.wait();
54
+ delete tx.wait;
55
+ send(tx);
56
+ }
57
+ catch (error) {
58
+ send(202);
59
+ }
60
+ },
61
+ peerId: ({ send }) => send(globalThis.peernet.peerId),
62
+ peers: ({ send }) => send(globalThis.peernet.peers.map(([id, peer]) => id)),
63
+ validators: ({ send }) => send(chain.validators),
64
+ lookup: async ({ name }, { send }) => send(await chain.lookup(name)),
65
+ staticCall: async ({ contract, method, params }, { send }) => send(await chain.staticCall(contract, method, params)),
66
+ nativeBurns: async ({ send }) => send(await chain.nativeBurns),
67
+ contracts: async ({ send }) => send(await chain.contracts),
68
+ nativeMints: async ({ send }) => send(await chain.nativeMints),
69
+ nativeToken: async ({ send }) => send(await chain.nativeToken),
70
+ nativeTransfers: async ({ send }) => send(await chain.nativeTransfers),
71
+ totalSize: async ({ send }) => send(await chain.totalSize),
72
+ totalTransactions: async ({ send }) => send(await chain.totalTransactions),
73
+ totalBlocks: async ({ send }) => send(chain.totalBlocks),
74
+ lastBlock: async (ctx) => (ctx.body = await chain.lastBlock),
75
+ nativeCalls: async ({ send }) => send(await chain.nativeCalls),
76
+ participating: async ({ send }) => send(await chain.participating),
77
+ poolTransactions: async ({ send }) => send(await globalThis.transactionPoolStore.get()),
78
+ transactionsInPool: async ({ send }) => send(await globalThis.transactionPoolStore.length()),
79
+ transactionPoolSize: async ({ send }) => send(await globalThis.transactionPoolStore.size()),
80
+ participate: async ({ address }, { send }) => {
81
+ if (remote)
82
+ send(await chain.participate(address));
83
+ else
84
+ send(405);
85
+ },
86
+ deployContract: async ({ transaction, contract }, { send }) => {
87
+ try {
88
+ const tx = await chain.deployContract(contract, transaction);
89
+ await tx.wait();
90
+ delete tx.wait;
91
+ send(tx);
92
+ }
93
+ catch (error) {
94
+ send(error.message);
95
+ }
96
+ },
97
+ createContractAddress: async ({ owner, code, params }, { send }) => {
98
+ try {
99
+ const address = await chain.createContractAddress(owner, code, params);
100
+ send(address);
101
+ }
102
+ catch (error) {
103
+ send(error);
104
+ }
105
+ },
106
+ blockHashMap: async (ctx) => (ctx.body = chain.blockHashMap),
107
+ bootstrap: async ({ send }) => {
108
+ send(await shared.bootstrap());
109
+ }
110
+ });
111
+ };
package/package.json CHANGED
@@ -1,33 +1,44 @@
1
- {
2
- "name": "@leofcoin/endpoints",
3
- "version": "0.3.0",
4
- "description": "",
5
- "type": "module",
6
- "exports": {
7
- "./api": "./exports/api.md",
8
- "./http": "./exports/http.js",
9
- "./ws": "./exports/ws.js"
10
- },
11
- "scripts": {
12
- "test": "echo \"Error: no test specified\" && exit 1"
13
- },
14
- "keywords": [],
15
- "author": "",
16
- "license": "MIT",
17
- "repository": {
18
- "type": "git",
19
- "url": "git+https://github.com/leofcoin/endpoints.git"
20
- },
21
- "bugs": {
22
- "url": "https://github.com/leofcoin/endpoints/issues"
23
- },
24
- "homepage": "https://github.com/leofcoin/endpoints#readme",
25
- "dependencies": {
26
- "@koa/cors": "^4.0.0",
27
- "@koa/router": "^12.0.0",
28
- "@leofcoin/utils": "^1.1.5",
29
- "koa": "^2.14.1",
30
- "showdown": "^2.1.0",
31
- "socket-request-server": "^1.6.3"
32
- }
33
- }
1
+ {
2
+ "name": "@leofcoin/endpoints",
3
+ "version": "0.3.2",
4
+ "description": "",
5
+ "type": "module",
6
+ "exports": {
7
+ "./api": "./exports/api.md",
8
+ "./http": "./exports/http.js",
9
+ "./ws": "./exports/ws.js"
10
+ },
11
+ "files": [
12
+ "./exports"
13
+ ],
14
+ "scripts": {
15
+ "build": "npx tsc",
16
+ "test": "echo \"Error: no test specified\" && exit 1"
17
+ },
18
+ "keywords": [],
19
+ "author": "",
20
+ "license": "MIT",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/leofcoin/endpoints.git"
24
+ },
25
+ "bugs": {
26
+ "url": "https://github.com/leofcoin/endpoints/issues"
27
+ },
28
+ "homepage": "https://github.com/leofcoin/endpoints#readme",
29
+ "dependencies": {
30
+ "@koa/cors": "^5.0.0",
31
+ "@koa/router": "^12.0.0",
32
+ "@leofcoin/utils": "^1.1.10",
33
+ "koa": "^2.14.1",
34
+ "showdown": "^2.1.0",
35
+ "socket-request-server": "^1.6.3"
36
+ },
37
+ "devDependencies": {
38
+ "@types/koa__cors": "^4.0.1",
39
+ "@types/koa__router": "^12.0.1",
40
+ "@types/node": "^20.8.6",
41
+ "@types/showdown": "^2.0.2",
42
+ "typescript": "^5.2.2"
43
+ }
44
+ }
package/.gitattributes DELETED
@@ -1,2 +0,0 @@
1
- # Auto detect text files and perform LF normalization
2
- * text=auto
package/exports/api.md DELETED
@@ -1,251 +0,0 @@
1
- #### [selectAccount(address: `string`)](https://remote.leofcoin.org/select-account?address=)
2
- `address`: desired address
3
- > set default account to given address
4
- ```js
5
- await client.selectAccount(address)
6
- ```
7
- <br>
8
-
9
- #### [getBlock(index: `number`)](https://remote.leofcoin.org/getblock?index=0)
10
- `index`: index of the block to fetch<br>
11
- ```js
12
- await client.getBlock(10)
13
- ```
14
- <br>
15
-
16
- #### [blocks(amount)](https://remote.leofcoin.org/blocks)
17
- `amount`: the amount to slice
18
- ```js
19
- await client.blocks(-25)
20
- ```
21
- <br>
22
-
23
- #### createTransactionFrom(from: `string`, to: `string`, method: `string`, parameters: `array`, nonce: `number` | `undefined`)
24
-
25
- `from`:<br>
26
- `to`: <br>
27
- `method`: <br>
28
- `parameters`: <br>
29
- `nonce`: <br>
30
- ```js
31
- await client.createTransactionFrom(from, to, method, parameters, nonce)
32
- ```
33
- <br>
34
-
35
- #### [lookup(name: `string`)](https://remote.leofcoin.org/lookup?name=ArtOnlineContractFactory)
36
- `name`: name of the contract to lookup the address and owner of
37
- ```js
38
- await client.lookup('contractName')
39
- ```
40
- <br>
41
-
42
- #### participate(address: `string`)
43
- `address`: <br>
44
-
45
- ```js
46
- await client.participate(address)
47
- ```
48
- <br>
49
-
50
- #### staticCall(contract: `string`, method: `string`, params: `array`)
51
- `contract`: contract address todo the call on
52
- `method`: method/function to call
53
- `params`: array containing params needed to call the desired method
54
- ```js
55
- await client.staticCall(contract, method, params)
56
- ```
57
- <br>
58
-
59
- #### createContractAddress(owner: `string`, code: `string`, params: `array`)
60
- `owner`: address of the deployer<br>
61
- `code`: contract as a string<br>
62
- `params`: contract constructor params<br>
63
-
64
- ```js
65
- const address = await client.createContractAddress(owner, code, params)
66
- ```
67
- <br>
68
-
69
- #### deployContract(code: `string`, params: `array`)
70
- `code`: contract as a string<br>
71
- `params`: contract constructor params<br>
72
-
73
- ```js
74
- const tx = await client.deployContract(code, params)
75
- ```
76
- <br>
77
-
78
- #### [accounts()](https://remote.leofcoin.org/accounts)
79
- ```js
80
- await client.accounts()
81
- ```
82
- <br>
83
-
84
- #### [hasTransactionToHandle()](https://remote.leofcoin.org/hasTransactionToHandle)
85
- ```js
86
- await client.hasTransactionToHandle()
87
- ```
88
- <br>
89
-
90
- #### [balances()](https://remote.leofcoin.org/balances)
91
- > returns all balances
92
- ```js
93
- await client.balances()
94
- ```
95
- <br>
96
-
97
- #### [balancOf(address: `string`, format: `boolean`)](https://remote.leofcoin.org/balanceOf?address=YTqxKshWH4Qo7KprGYxQVCNYutedaE6YGoUdUPnUdFUVBPrTzgsrP&format=true)
98
- `address`: The address to check<br>
99
- `format`: Wether or not to format<br>
100
- > returns the balance of given address as BigNumber or string (when format is true)
101
- ```js
102
- await client.balanceOf(address, true)
103
- ```
104
- <br>
105
-
106
- #### [getNonce(address: `string`)](https://remote.leofcoin.org/getNonce?address=YTqxKshWH4Qo7KprGYxQVCNYutedaE6YGoUdUPnUdFUVBPrTzgsrP)
107
- `address`: The address to check<br>
108
- `format`: Wether or not to format<br>
109
- > returns the balance of given address as BigNumber or string (when format is true)
110
- ```js
111
- await client.getNonce(address)
112
- ```
113
- <br>
114
-
115
- #### [selectedAccount()](https://remote.leofcoin.org/selectedAccount)
116
- ```js
117
- await client.selectedAccount()
118
- ```
119
- <br>
120
-
121
- #### [peerId()](https://remote.leofcoin.org/peerId)
122
- ```js
123
- await client.peerId()
124
- ```
125
- <br>
126
-
127
- #### [peers()](https://remote.leofcoin.org/peers)
128
- ```js
129
- await client.peers()
130
- ```
131
- <br>
132
-
133
- #### [validators()](https://remote.leofcoin.org/validators)
134
- ```js
135
- await client.validators()
136
- ```
137
- <br>
138
-
139
- #### [nativeBurns()](https://remote.leofcoin.org/nativeBurns)
140
- ```js
141
- await client.nativeBurns()
142
- ```
143
- <br>
144
-
145
- #### [contracts()](https://remote.leofcoin.org/contracts)
146
- ```js
147
- await client.contracts()
148
- ```
149
- <br>
150
-
151
- #### [nativeMints()](https://remote.leofcoin.org/nativeMints)
152
- ```js
153
- await client.nativeMints()
154
- ```
155
- <br>
156
-
157
- #### [nativeToken()](https://remote.leofcoin.org/nativeToken)
158
- ```js
159
- await client.nativeToken()
160
- ```
161
- <br>
162
-
163
- #### [nativeTransfers()](https://remote.leofcoin.org/nativeTransfers)
164
- ```js
165
- await client. nativeTransfers()
166
- ```
167
- <br>
168
-
169
- #### [totalSize()](https://remote.leofcoin.org/totalSize)
170
- ```js
171
- await client.totalSize()
172
- ```
173
- <br>
174
-
175
- #### [totalTransactions()](https://remote.leofcoin.org/totalTransactions)
176
- ```js
177
- await client.totalTransactions()
178
- ```
179
- <br>
180
-
181
- #### [poolTransactions()](https://remote.leofcoin.org/poolTransactions)
182
- ```js
183
- await client.poolTransactions()
184
- ```
185
- <br>
186
-
187
- #### [transactionsInPool()](https://remote.leofcoin.org/transactionsInPool)
188
- ```js
189
- await client.transactionsInPool()
190
- ```
191
- <br>
192
-
193
- #### [transactionPoolSize()](https://remote.leofcoin.org/transactionPoolSize)
194
- ```js
195
- await client.transactionPoolSize()
196
- ```
197
- <br>
198
-
199
- #### [totalBlocks()](https://remote.leofcoin.org/totalBlocks)
200
- ```js
201
- await client.totalBlocks()
202
- ```
203
- <br>
204
-
205
- #### [nativeCalls()](https://remote.leofcoin.org/nativeCalls)
206
- ```js
207
- await client.nativeCalls()
208
- ```
209
- <br>
210
-
211
- #### [participating()](https://remote.leofcoin.org/participating)
212
- ```js
213
- await client.participating()
214
- ```
215
- <br>
216
-
217
- #### [network()](https://remote.leofcoin.org/network)
218
- ```js
219
- await client.network()
220
- ```
221
- <br>
222
-
223
- #### [networkStats()](https://remote.leofcoin.org/networkStats)
224
- ```js
225
- await client.networkStats()
226
- ```
227
- <br>
228
-
229
- #### [networkStats()](https://remote.leofcoin.org/networkStats)
230
- ```js
231
- await client.networkStats()
232
- ```
233
- <br>
234
-
235
- #### [lastBlock()](https://remote.leofcoin.org/lastBlock)
236
- ```js
237
- await client.lastBlock()
238
- ```
239
- <br>
240
-
241
- #### [blockHashMap()](https://remote.leofcoin.org/blockHashMap)
242
- ```js
243
- await client.blockHashMap()
244
- ```
245
- <br>
246
-
247
- #### [bootstrap()](https://remote.leofcoin.org/bootstrap)
248
- ```js
249
- await client.bootstrap()
250
- ```
251
- <br>