-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.mjs
173 lines (160 loc) · 3.72 KB
/
common.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import got from 'got'
import { fileURLToPath } from 'url'
import constants from './constants.mjs'
import config from '#config'
export const isMain = (path) => process.argv[1] === fileURLToPath(path)
export const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
const POST = (data) => ({
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
})
const MAX_RETRIES = 6
const RETRY_BACKOFF = 5000
export const rpc_request = async ({ url, retries = 0, ...options }) => {
try {
if (retries > 0) {
console.log(`retrying ${url}, retries: ${retries}`)
}
return got(url, {
...options,
timeout: {
request: 50000
}
}).json()
} catch (error) {
if (retries < MAX_RETRIES) {
const wait_time = RETRY_BACKOFF * Math.pow(2, retries)
await wait(wait_time)
return rpc_request({ url, retries: retries + 1, ...options })
} else {
throw error
}
}
}
const rpc_request_options = (data) => {
return { url: config.rpcAddress, ...POST(data) }
}
export const getFrontierCount = () => {
const data = {
action: 'frontier_count'
}
const options = rpc_request_options(data)
return rpc_request(options)
}
/* eslint-disable camelcase */
export const getLedger = ({
account = constants.BURN_ACCOUNT,
count = 1,
threshold = 100000000000000000,
modified_since,
sorting
}) => {
const data = {
action: 'ledger',
pending: true,
representative: true,
weight: true,
account,
threshold,
count,
modified_since,
sorting
}
const options = rpc_request_options(data)
return rpc_request(options)
}
/* eslint-enable camelcase */
export const getBlocksInfo = ({ hashes }) => {
const data = {
action: 'blocks_info',
include_not_found: true,
source: true,
json_block: true,
hashes
}
const options = rpc_request_options(data)
return rpc_request(options)
}
export const getAccountInfo = ({ account }) => {
const data = {
action: 'account_info',
account,
representative: true,
weight: true,
pending: true,
include_confirmed: true
}
const options = rpc_request_options(data)
return rpc_request(options)
}
export const getChain = ({ block, count }) => {
const data = {
action: 'chain',
block,
count
}
const options = rpc_request_options(data)
return rpc_request(options)
}
/* eslint-disable camelcase */
export const formatAccountInfo = ({
frontier,
open_block,
representative_block,
balance,
modified_timestamp,
block_count,
representative,
weight,
pending,
confirmed_height,
confirmed_frontier
}) => ({
frontier,
open_block,
representative_block,
balance,
modified_timestamp,
block_count,
representative,
weight,
pending,
confirmation_height: confirmed_height,
confirmation_height_frontier: confirmed_frontier
})
export const formatBlockInfo = ({
amount,
balance,
block_account,
height,
local_timestamp,
confirmed,
contents,
subtype,
source_account
}) => ({
amount,
balance,
height,
local_timestamp,
confirmed: confirmed === 'true',
account: contents.account || block_account,
previous: contents.previous || null,
representative: contents.representative,
link: contents.link || contents.destination || contents.source,
link_account:
(source_account !== '0' && source_account) || // receive
(contents.link_as_account !== constants.BURN_ACCOUNT &&
contents.link_as_account) || // send
contents.destination || // send
contents.representative || // change
null,
signature: contents.signature,
work: contents.work,
type: constants.blockType[contents.type],
subtype: constants.blockSubType[subtype] || null
})
/* eslint-enable camelcase */