forked from jnordberg/dsteem
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathclient.ts
More file actions
302 lines (273 loc) · 9.55 KB
/
client.ts
File metadata and controls
302 lines (273 loc) · 9.55 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/**
* @file Steem RPC client implementation.
* @author Johan Nordberg <code@johan-nordberg.com>
* @license
* Copyright (c) 2017 Johan Nordberg. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* You acknowledge that this software is not designed, licensed or intended for use
* in the design, construction, operation or maintenance of any military facility.
*/
import * as assert from 'assert'
import {VError} from 'verror'
import packageVersion from './version'
import {Blockchain} from './helpers/blockchain'
import {BroadcastAPI} from './helpers/broadcast'
import {DatabaseAPI} from './helpers/database'
import {copy, retryingFetch, waitForEvent} from './utils'
/**
* Library version.
*/
export const VERSION = packageVersion
/**
* Main steem network chain id.
*/
export const DEFAULT_CHAIN_ID = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
/**
* Main steem network address prefix.
*/
export const DEFAULT_ADDRESS_PREFIX = 'STM'
interface RPCRequest {
/**
* Request sequence number.
*/
id: number
/**
* RPC method.
*/
method: 'call' | 'notice' | 'callback'
/**
* Array of parameters to pass to the method.
*/
jsonrpc: '2.0'
params: any[]
}
interface RPCCall extends RPCRequest {
method: 'call'
/**
* 1. API to call, you can pass either the numerical id of the API you get
* from calling 'get_api_by_name' or the name directly as a string.
* 2. Method to call on that API.
* 3. Arguments to pass to the method.
*/
params: [number|string, string, any[]]
}
interface RPCError {
code: number
message: string
data?: any
}
interface RPCResponse {
/**
* Response sequence number, corresponding to request sequence number.
*/
id: number
error?: RPCError
result?: any
}
interface PendingRequest {
request: RPCRequest,
timer: NodeJS.Timer | undefined
resolve: (response: any) => void
reject: (error: Error) => void
}
/**
* RPC Client options
* ------------------
*/
export interface ClientOptions {
/**
* Steem chain id. Defaults to main steem network:
* `0000000000000000000000000000000000000000000000000000000000000000`
*/
chainId?: string
/**
* Steem address prefix. Defaults to main steem network:
* `STM`
*/
addressPrefix?: string
/**
* Send timeout, how long to wait in milliseconds before giving
* up on a rpc call. Note that this is not an exact timeout,
* no in-flight requests will be aborted, they will just not
* be retried any more past the timeout.
* Can be set to 0 to retry forever. Defaults to 60 * 1000 ms.
*/
timeout?: number
/**
* Retry backoff function, returns milliseconds. Default = {@link defaultBackoff}.
*/
backoff?: (tries: number) => number
/**
* Node.js http(s) agent, use if you want http keep-alive.
* Defaults to using https.globalAgent.
* @see https://nodejs.org/api/http.html#http_new_agent_options.
*/
agent?: any // https.Agent
}
/**
* RPC Client
* ----------
* Can be used in both node.js and the browser. Also see {@link ClientOptions}.
*/
export class Client {
/**
* Create a new client instance configured for the testnet.
*/
public static testnet(options?: ClientOptions) {
let opts: ClientOptions = {}
if (options) {
opts = copy(options)
opts.agent = options.agent
}
opts.addressPrefix = 'TST'
opts.chainId = '46d82ab7d8db682eb1959aed0ada039a6d49afa1602491f93dde9cac3e8e6c32'
return new Client('https://testnet.steemitdev.com', opts)
}
/**
* Client options, *read-only*.
*/
public readonly options: ClientOptions
/**
* Address to Steem RPC server, *read-only*.
*/
public readonly address: string
/**
* Database API helper.
*/
public readonly database: DatabaseAPI
/**
* Broadcast API helper.
*/
public readonly broadcast: BroadcastAPI
/**
* Blockchain helper.
*/
public readonly blockchain: Blockchain
/**
* Chain ID for current network.
*/
public readonly chainId: Buffer
/**
* Address prefix for current network.
*/
public readonly addressPrefix: string
private seqNo: number = 0
private timeout: number
private backoff: typeof defaultBackoff
/**
* @param address The address to the Steem RPC server, e.g. `https://api.steemit.com`.
* @param options Client options.
*/
constructor(address: string, options: ClientOptions = {}) {
this.address = address
this.options = options
this.chainId = options.chainId ? Buffer.from(options.chainId, 'hex') : DEFAULT_CHAIN_ID
assert.equal(this.chainId.length, 32, 'invalid chain id')
this.addressPrefix = options.addressPrefix || DEFAULT_ADDRESS_PREFIX
this.timeout = options.timeout || 60 * 1000
this.backoff = options.backoff || defaultBackoff
this.database = new DatabaseAPI(this)
this.broadcast = new BroadcastAPI(this)
this.blockchain = new Blockchain(this)
}
/**
* Make a RPC call to the server.
*
* @param api The API to call, e.g. `database_api`.
* @param method The API method, e.g. `get_dynamic_global_properties`.
* @param params Array of parameters to pass to the method, optional.
*
*/
public async call(api: string, method: string, params: any = []): Promise<any> {
const request: RPCCall = {
id: ++this.seqNo,
jsonrpc: '2.0',
method: 'call',
params: [api, method, params],
}
const body = JSON.stringify(request, (key, value) => {
// encode Buffers as hex strings instead of an array of bytes
if (typeof value === 'object' && value.type === 'Buffer') {
return Buffer.from(value.data).toString('hex')
}
return value
})
const opts: any = {
body,
cache: 'no-cache',
headers: {'User-Agent': `dsteem/${ packageVersion }`},
method: 'POST',
mode: 'cors',
}
if (this.options.agent) {
opts.agent = this.options.agent
}
let fetchTimeout: any
if (api !== 'network_broadcast_api' && method.substring(0, 21) !== 'broadcast_transaction') {
// bit of a hack to work around some nodes high error rates
// only effective in node.js (until timeout spec lands in browsers)
fetchTimeout = (tries) => (tries + 1) * 500
}
const response: RPCResponse = await retryingFetch(
this.address, opts, this.timeout, this.backoff, fetchTimeout
)
if (response.error) {
const {data} = response.error
let {message} = response.error
if (data && data.stack && data.stack.length > 0) {
const top = data.stack[0]
const topData = copy(top.data)
message = top.format.replace(/\$\{([a-z_]+)\}/gi, (match: string, key: string) => {
let rv = match
if (topData[key]) {
rv = topData[key]
delete topData[key]
}
return rv
})
const unformattedData = Object.keys(topData)
.map((key) => ({key, value: topData[key]}))
.filter((item) => typeof item.value === 'string')
.map((item) => `${ item.key }=${ item.value}`)
if (unformattedData.length > 0) {
message += ' ' + unformattedData.join(' ')
}
}
throw new VError({info: data, name: 'RPCError'}, message)
}
assert.equal(response.id, request.id, 'got invalid response id')
return response.result
}
}
/**
* Default backoff function.
* ```min(tries*10^2, 10 seconds)```
*/
const defaultBackoff = (tries: number): number => {
return Math.min(Math.pow(tries * 10, 2), 10 * 1000)
}