|
@@ -1,5 +1,5 @@
|
1
|
1
|
import { defaultOptions, Memo, Options } from '../util/types'
|
2
|
|
-import { Client, Payment, TxResponse, Wallet } from 'xrpl'
|
|
2
|
+import { Client, Payment, RippledError, TxResponse, Wallet } from 'xrpl'
|
3
|
3
|
|
4
|
4
|
import * as zlib from 'zlib'
|
5
|
5
|
import * as util from 'util'
|
|
@@ -29,6 +29,7 @@ export class xrpIO {
|
29
|
29
|
this.options.connectionTimeout = options.connectionTimeout ? Number(options.connectionTimeout) : defaultOptions.connectionTimeout
|
30
|
30
|
this.options.readMaxRetry = options.readMaxRetry ? Number(options.readMaxRetry) : defaultOptions.readMaxRetry
|
31
|
31
|
this.options.readRetryTimeout = options.readRetryTimeout ? Number(options.readRetryTimeout) : defaultOptions.readRetryTimeout
|
|
32
|
+ this.options.readFreshApi = options.readFreshApi ? Boolean(options.readFreshApi) : defaultOptions.readFreshApi
|
32
|
33
|
|
33
|
34
|
this.api = new Client(server, {
|
34
|
35
|
connectionTimeout: this.options.connectionTimeout
|
|
@@ -67,36 +68,36 @@ export class xrpIO {
|
67
|
68
|
}
|
68
|
69
|
}
|
69
|
70
|
|
70
|
|
- private async sendPayment(data: Memo, to: string, secret: string, sequence?: number, amount: string = "1"): Promise<TxResponse> {
|
|
71
|
+ public async sendPayment(data: Memo, to: string, secret: string, sequence?: number, amount: string = "1"): Promise<TxResponse> {
|
71
|
72
|
const wallet = Wallet.fromSecret(secret)
|
72
|
73
|
this.dbg("Sending payment", wallet.address, '->', to)
|
73
|
74
|
|
74
|
75
|
const _api = await this.cloneApi()
|
75
|
|
- try {
|
76
|
|
- const payment: Payment = await _api.autofill({
|
77
|
|
- TransactionType: 'Payment',
|
78
|
|
- Account: wallet.address,
|
79
|
|
- Destination: to,
|
80
|
|
- Sequence: sequence,
|
81
|
|
- Amount: amount,
|
82
|
|
- Memos: [{
|
83
|
|
- Memo: {
|
84
|
|
- MemoData: hexEncode(data.data || ""),
|
85
|
|
- MemoFormat: hexEncode(data.format || ""),
|
86
|
|
- MemoType: hexEncode(data.type || "")
|
87
|
|
- }
|
88
|
|
- }]
|
89
|
|
- })
|
90
|
|
-
|
|
76
|
+ const payment: Payment = await _api.autofill({
|
|
77
|
+ TransactionType: 'Payment',
|
|
78
|
+ Account: wallet.address,
|
|
79
|
+ Destination: to,
|
|
80
|
+ Sequence: sequence,
|
|
81
|
+ Amount: amount,
|
|
82
|
+ Memos: [{
|
|
83
|
+ Memo: {
|
|
84
|
+ MemoData: hexEncode(data.data || ""),
|
|
85
|
+ MemoFormat: hexEncode(data.format || ""),
|
|
86
|
+ MemoType: hexEncode(data.type || "")
|
|
87
|
+ }
|
|
88
|
+ }]
|
|
89
|
+ })
|
91
|
90
|
|
|
91
|
+ try {
|
92
|
92
|
const response = await _api.submitAndWait(payment, { wallet })
|
93
|
|
- await _api.disconnect()
|
94
|
93
|
this.dbg("Tx finalized", response.result.hash, response.result.Sequence)
|
95
|
94
|
return response
|
96
|
95
|
} catch (error: any) {
|
97
|
96
|
this.dbg("SENDPAYMENT ERROR", error)
|
98
|
|
- await _api.disconnect()
|
|
97
|
+ console.log("SENDPAYMENT ERROR", error)
|
99
|
98
|
throw error
|
|
99
|
+ }finally{
|
|
100
|
+ await _api.disconnect()
|
100
|
101
|
}
|
101
|
102
|
}
|
102
|
103
|
|
|
@@ -106,22 +107,57 @@ export class xrpIO {
|
106
|
107
|
return tx.result.hash
|
107
|
108
|
}
|
108
|
109
|
|
109
|
|
- private async getTransaction(hash: string, retry = 0): Promise<TxResponse> {
|
|
110
|
+ public async getTransaction(hash: string, retry = 0): Promise<TxResponse> {
|
|
111
|
+ if (!NON_ZERO_TX_HASH.test(hash)) {
|
|
112
|
+ throw ERR_BAD_TX_HASH(hash)
|
|
113
|
+ }
|
|
114
|
+
|
110
|
115
|
this.dbg("Getting Tx", hash)
|
|
116
|
+
|
|
117
|
+ const _api = this.options.readFreshApi ? await this.cloneApi() : this.api
|
|
118
|
+
|
111
|
119
|
try {
|
112
|
|
- return await this.api.request({
|
|
120
|
+ return await _api.request({
|
113
|
121
|
command: 'tx',
|
114
|
122
|
transaction: hash,
|
115
|
123
|
})
|
116
|
|
- } catch (e) {
|
117
|
|
- this.dbg(e)
|
118
|
|
- if (this.options.readMaxRetry != -1) {
|
119
|
|
- if (retry >= this.options.readMaxRetry)
|
120
|
|
- console.error("Retry limit exceeded for", hash, ". This is an irrecoverable error")
|
|
124
|
+ } catch (e: any) {
|
|
125
|
+ this.dbg("getTransaction err", e)
|
|
126
|
+
|
|
127
|
+ if(e.data){
|
|
128
|
+ switch(e.data.error){
|
|
129
|
+
|
|
130
|
+ case 'amendmentBlocked':
|
|
131
|
+ case 'invalid_API_version':
|
|
132
|
+ case 'jsonInvalid':
|
|
133
|
+ case 'missingCommand':
|
|
134
|
+ case 'noClosed':
|
|
135
|
+ case 'txnNotFound':
|
|
136
|
+ case 'unknownCmd':
|
|
137
|
+ case 'wsTextRequired':
|
|
138
|
+ case 'invalidParams':
|
|
139
|
+ case 'excessiveLgrRange':
|
|
140
|
+ case 'invalidLgrRange':
|
|
141
|
+ throw e
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+ case 'failedToForward':
|
|
145
|
+ case 'noCurrent':
|
|
146
|
+ case 'noNetwork':
|
|
147
|
+ case 'tooBusy':
|
|
148
|
+ default:
|
|
149
|
+
|
|
150
|
+ }
|
|
151
|
+ }
|
|
152
|
+
|
|
153
|
+ if (this.options.readMaxRetry != -1 && retry >= this.options.readMaxRetry) {
|
121
|
154
|
throw e
|
|
155
|
+ }else{
|
|
156
|
+ await new Promise(res => setTimeout(res, this.options.readRetryTimeout))
|
|
157
|
+ return await this.getTransaction(hash, retry + 1)
|
122
|
158
|
}
|
123
|
|
- await new Promise(res => setTimeout(res, this.options.readRetryTimeout))
|
124
|
|
- return await this.getTransaction(hash, retry + 1)
|
|
159
|
+ }finally{
|
|
160
|
+ if(this.options.readFreshApi) await _api.disconnect()
|
125
|
161
|
}
|
126
|
162
|
}
|
127
|
163
|
|