hopefully fix websockets closing from over-use

This commit is contained in:
nitowa
2022-02-12 20:01:03 +01:00
parent de6d1da3c1
commit 2674141716
6 changed files with 250 additions and 148 deletions
+45 -15
View File
@@ -12,9 +12,16 @@ const PAYLOAD_SIZE = 925
const debug = false
const cloneApi = async (api: RippleAPI): Promise<RippleAPI> => {
const subApi = new RippleAPI({ server: api.connection['_url'] })
await subApi.connect()
return subApi
try{
const subApi = new RippleAPI({ server: api.connection['_url'] })
await subApi.connect()
return subApi
}catch(e){
if(debug){
console.log("CLONEAPI ERR", e)
}
return await cloneApi(api)
}
}
export const getLatestSequence = async (api: RippleAPI, accountAddress: string): Promise<number> => {
@@ -31,7 +38,7 @@ const sendReliably = (api: RippleAPI, signed: any, preparedPayment,): Promise<an
let status
try {
status = await api.getTransaction(signed.id, {
minLedgerVersion: 20368096
minLedgerVersion: 25235454
})
} catch (e) {
@@ -85,23 +92,26 @@ export const sendPayment = async (api: RippleAPI, data: Memo[], from: string, to
},
memos: data,
};
const _api = await cloneApi(api)
try {
const _api = await cloneApi(api)
const prepared = await _api.preparePayment(from, payment, options)
const signed = _api.sign(prepared.txJSON, secret);
const txHash = await _api.submit(signed.signedTransaction)
if(debug) console.log("Transaction submitted", txHash)
//if(debug) console.log("Transaction submitted", txHash)
await sendReliably(_api, signed, prepared)
_api.disconnect()
return txHash
} catch (error) {
//console.log("SENDPAYMENT ERROR", error)
if(debug)
console.log("SENDPAYMENT ERROR", error)
throw error
}finally{
_api.disconnect()
}
}
export const getTransactions = async (api: RippleAPI, address: string, minLedgerVersion: number = 19832467): Promise<any[]> => {
export const getTransactions = async (api: RippleAPI, address: string, minLedgerVersion: number = 25235454): Promise<any[]> => {
const txs = await api.getTransactions(address, {
minLedgerVersion: minLedgerVersion,
earliestFirst: true,
@@ -110,7 +120,8 @@ export const getTransactions = async (api: RippleAPI, address: string, minLedger
return txs
}
export const writeRaw = async (api: RippleAPI, data: Memo, from: string, to: string, secret: string, sequence?: number): Promise<string> => {
export const writeRaw = async (api: RippleAPI, data: Memo, from:
string, to: string, secret: string, sequence?: number): Promise<string> => {
//if (memoSize(data) > 1000) throw new Error("data length exceeds capacity")
try {
if (!sequence) {
@@ -121,19 +132,38 @@ export const writeRaw = async (api: RippleAPI, data: Memo, from: string, to: str
const resp = await sendPayment(api, [data], from, to, secret, sequence)
return resp['tx_json'].hash
} catch (error) {
console.log("WRITERAW ERR", error);
if(debug){
console.log("WRITERAW ERR", error);
}
throw error
}
}
export const readRaw = async (api: RippleAPI, hash: string): Promise<Memo> => {
const tx = await api.getTransaction(hash, {
minLedgerVersion: 16392480
})
let _api
if(!api.isConnected()){
_api = await cloneApi(api)
}else{
_api = api
}
let tx
try{
tx = await _api.getTransaction(hash, {
minLedgerVersion: 25235454
})
}catch(e){
if(debug){
console.log("READRAW ERR", e)
throw e
}
}finally{
await _api.disconnect()
}
if (!tx || !tx.specification || !tx.specification['memos'] || !tx.specification['memos'][0]) {
throw new Error('Invalid Transaction ' + hash)
}
return tx.specification['memos'][0]
}