@leofcoin/endpoints 0.3.1 → 0.3.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/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 - 1])
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;