@leofcoin/endpoints 0.2.4 → 0.2.5

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
@@ -4,12 +4,17 @@ import Router from '@koa/router'
4
4
  import {formatUnits} from '@leofcoin/utils'
5
5
  import {readFile} from 'fs/promises'
6
6
  import Showdown from 'showdown'
7
+ import { dirname, join} from 'path'
8
+ import { fileURLToPath } from 'url'
9
+
10
+ const __filename = fileURLToPath(import.meta.url);
11
+ const __dirname = dirname(__filename);
7
12
 
8
13
  const api = new koa()
9
14
  const router = new Router()
10
15
 
11
16
  const converter = new Showdown.Converter()
12
- const apiFile = await readFile('./api.md')
17
+ const apiFile = await readFile(join(__dirname, './api.md'))
13
18
 
14
19
  export default (chain, port, networkVersion) => {
15
20
  // api routes
@@ -26,8 +31,9 @@ export default (chain, port, networkVersion) => {
26
31
  })
27
32
 
28
33
  router.get('/balanceOf', async ctx => {
29
- const balance = (await chain.balances)[ctx.query.address]
30
- ctx.body = ctx.query.format ? formatUnits(balance) : balance
34
+ const balance = (await chain.balances)[ctx.query.address]
35
+ if (balance) ctx.body = ctx.query.format ? formatUnits(balance) : balance
36
+ else ctx.body = 0
31
37
  })
32
38
 
33
39
  router.get('/selectedAccount', ctx => ctx.body = peernet.selectedAccount)
@@ -58,9 +64,9 @@ export default (chain, port, networkVersion) => {
58
64
  ctx.body = chain.blocks.slice(ctx.query.amount)
59
65
  })
60
66
 
61
- router.get('/createTransactionFrom', async ctx => {
62
- try {
63
- const tx = await chain.createTransactionFrom(ctx.query.from, ctx.query.to, ctx.query.method, ctx.query.parameters, ctx.query.nonce)
67
+ router.get('/sendTransaction', async ctx => {
68
+ try {
69
+ const tx = await chain.sendTransaction(ctx.query.transaction)
64
70
  await tx.wait()
65
71
  send(tx)
66
72
  } catch (error) {
@@ -76,6 +82,10 @@ export default (chain, port, networkVersion) => {
76
82
  ctx.body = chain.validators
77
83
  )
78
84
 
85
+ router.get('/peers', ctx =>
86
+ ctx.body = peernet.peers
87
+ )
88
+
79
89
  router.get('/lookup', async ctx => {
80
90
  ctx.body = await chain.lookup(ctx.query.name)
81
91
  })
package/exports/ws.js CHANGED
@@ -6,8 +6,9 @@ export default (chain, port, protocol) => {
6
6
  balances: async ({send}) =>
7
7
  send(await chain.balances),
8
8
  balanceOf: async ({address, format = false}, {send}) => {
9
- const balance = (await chain.balances)[address]
10
- send(format ? formatUnits(balance) : balance)
9
+ const balance = (await chain.balances)[address]
10
+ if (balance) send(format ? formatUnits(balance) : balance)
11
+ else send(0)
11
12
  },
12
13
  getNonce: async ({address}, {send}) => send((await chain.balances)[address]),
13
14
  selectedAccount: ({send}) => send(peernet.selectedAccount),
@@ -27,9 +28,9 @@ export default (chain, port, protocol) => {
27
28
  send(chain.blocks[index]),
28
29
  blocks:({amount}, {send}) => {
29
30
  send(chain.blocks.slice(amount))},
30
- createTransactionFrom: async ({from, to, method, parameters, nonce}, {send}) => {
31
+ sendTransaction: async (transaction, {send}) => {
31
32
  try {
32
- const tx = await chain.createTransactionFrom(from, to, method, parameters, nonce)
33
+ const tx = await chain.sendTransaction(transaction)
33
34
  await tx.wait()
34
35
  send(tx)
35
36
  } catch (error) {
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@leofcoin/endpoints",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
7
+ "./api": "./exportys/api.md",
7
8
  "./http": "./exports/http.js",
8
9
  "./ws": "./exports/ws.js"
9
10
  },