@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/LICENSE +20 -20
- package/exports/api.md +336 -251
- package/exports/http.d.ts +6 -0
- package/exports/http.js +142 -191
- package/exports/shared.d.ts +20 -0
- package/exports/shared.js +33 -34
- package/exports/ws.d.ts +5 -0
- package/exports/ws.js +111 -114
- package/package.json +44 -33
- package/.gitattributes +0 -2
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
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
ctx.body = peernet.peers.map(([id, peer]) => id)
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
ctx.body = await chain.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
ctx.body = await chain.
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
ctx.body = chain.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
ctx.body =
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
ctx.body =
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
33
|
+
return globalThis.blockStore.values();
|
|
34
|
+
};
|
|
36
35
|
export default {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
36
|
+
networkStats,
|
|
37
|
+
bootstrap
|
|
38
|
+
};
|