fix browser issues for good hopefully
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { PaymentTx_T, ParameterizedFunction, Payload, RJSVM, RJSVM_Config, RJSVM_Implementations, Function_Map, Endpoints_Of, State_Of, Generic_Ctor_ReturnType, RJSVM_Endpoint } from "./types"
|
||||
import { AccountTxTransaction, Client as Xrpl } from 'xrpl';
|
||||
import { Client as Xrpl } from 'xrpl';
|
||||
import { xrpIO } from 'xrpio';
|
||||
import { DataParser } from "../../util/dataparser";
|
||||
import { XRP_ADDRESS } from "../../util/protocol.constants";
|
||||
@@ -13,8 +13,8 @@ export abstract class RJSVM_Builder {
|
||||
Impl extends RJSVM_Implementations<any, Endpoints_T>,
|
||||
Endpoints_T extends Function_Map = Endpoints_Of<Generic_Ctor_ReturnType<Base_Ctor>>,
|
||||
State_T = State_Of<Generic_Ctor_ReturnType<Base_Ctor>>
|
||||
>(Base: Base_Ctor, defs: Impl): (new (config: RJSVM_Config) => RJSVM<State_T, Endpoints_T>){
|
||||
return <any> class RJSVM_Runnable extends (Base as any){
|
||||
>(Base: Base_Ctor, defs: Impl): (new (config: RJSVM_Config) => RJSVM<State_T, Endpoints_T>) {
|
||||
return <any>class RJSVM_Runnable extends (Base as any) {
|
||||
|
||||
sync_block_height = -1
|
||||
|
||||
@@ -30,13 +30,13 @@ export abstract class RJSVM_Builder {
|
||||
public readonly config: RJSVM_Config
|
||||
) {
|
||||
super()
|
||||
|
||||
if(!XRP_ADDRESS.test(this.owner)){
|
||||
|
||||
if (!XRP_ADDRESS.test(this.owner)) {
|
||||
const err = new Error(`Inavlid owner address ${this.owner}`)
|
||||
this.emit('error', err)
|
||||
throw err
|
||||
}
|
||||
|
||||
|
||||
this.definitions = Object.freeze(defs)
|
||||
Object.entries(this.definitions).forEach(([k, v]) => {
|
||||
this[k] = (env: PaymentTx_T, ...args: any) => (v.implementation as ParameterizedFunction).apply(this as any, [env, ...args])
|
||||
@@ -47,7 +47,7 @@ export abstract class RJSVM_Builder {
|
||||
this.rippleApi = new Xrpl(this.config.rippleNode)
|
||||
await this.rippleApi.connect()
|
||||
|
||||
this.xrpIO = new xrpIO(this.config.rippleNode);
|
||||
this.xrpIO = new xrpIO(this.config.rippleNode, { readFreshApi: false });
|
||||
await this.xrpIO.connect()
|
||||
await this.sync()
|
||||
|
||||
@@ -71,15 +71,15 @@ export abstract class RJSVM_Builder {
|
||||
}
|
||||
|
||||
public disconnect = async () => {
|
||||
if(this.syncTimeout){
|
||||
if (this.syncTimeout) {
|
||||
clearTimeout(this.syncTimeout)
|
||||
this.syncTimeout = undefined
|
||||
}
|
||||
if(this.xrpIO){
|
||||
if (this.xrpIO) {
|
||||
await this.xrpIO.disconnect()
|
||||
this.xrpIO = undefined
|
||||
}
|
||||
if(this.rippleApi){
|
||||
if (this.rippleApi) {
|
||||
await this.rippleApi.disconnect()
|
||||
this.rippleApi = undefined
|
||||
}
|
||||
@@ -90,10 +90,10 @@ export abstract class RJSVM_Builder {
|
||||
|
||||
public on = (event: string, handler: ParameterizedFunction) => {
|
||||
const availableEvents = ['error', ...Object.keys(this.definitions)]
|
||||
if(!availableEvents.includes(event))
|
||||
if (!availableEvents.includes(event))
|
||||
return
|
||||
|
||||
if(!this.subscribers[event])
|
||||
|
||||
if (!this.subscribers[event])
|
||||
this.subscribers[event] = []
|
||||
|
||||
this.subscribers[event].push(handler)
|
||||
@@ -101,20 +101,20 @@ export abstract class RJSVM_Builder {
|
||||
|
||||
public once = (event: string, handler: ParameterizedFunction) => {
|
||||
const availableEvents = ['error', ...Object.keys(this.definitions)]
|
||||
if(!availableEvents.includes(event))
|
||||
if (!availableEvents.includes(event))
|
||||
return
|
||||
|
||||
if(!this.onceSubscribers[event])
|
||||
|
||||
if (!this.onceSubscribers[event])
|
||||
this.onceSubscribers[event] = []
|
||||
|
||||
this.onceSubscribers[event].push(handler)
|
||||
}
|
||||
|
||||
private emit = (event: string, payload: any) => {
|
||||
if(this.subscribers[event])
|
||||
if (this.subscribers[event])
|
||||
this.subscribers[event].forEach(handler => handler(payload))
|
||||
|
||||
if(this.onceSubscribers[event]){
|
||||
|
||||
if (this.onceSubscribers[event]) {
|
||||
this.onceSubscribers[event].forEach(handler => handler(payload))
|
||||
this.onceSubscribers[event] = []
|
||||
}
|
||||
@@ -127,18 +127,18 @@ export abstract class RJSVM_Builder {
|
||||
|
||||
const endpointDef: RJSVM_Endpoint<RJSVM, any> = this.definitions[payload.endpoint]
|
||||
|
||||
if(endpointDef.visibility === 'owner' && tx.Account !== this.owner){
|
||||
if (endpointDef.visibility === 'owner' && tx.Account !== this.owner) {
|
||||
const err = new RestrictedAccessError(payload.endpoint, tx.hash, tx.Account, this.owner)
|
||||
this.emit('error', err)
|
||||
return
|
||||
}
|
||||
|
||||
if(endpointDef.fee && Number(tx.Amount) < endpointDef.fee){
|
||||
if (endpointDef.fee && Number(tx.Amount) < endpointDef.fee) {
|
||||
const err = new InsufficientFeeError(tx.hash, Number(endpointDef.fee), Number(tx.Amount))
|
||||
this.emit('error', err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
const data = await this.xrpIO.treeRead([payload.data], tx.Account)
|
||||
const jsonData = JSON.parse(data)
|
||||
@@ -151,7 +151,7 @@ export abstract class RJSVM_Builder {
|
||||
}
|
||||
}
|
||||
|
||||
private parseMemos = async (tx:PaymentTx_T) => {
|
||||
private parseMemos = async (tx: PaymentTx_T) => {
|
||||
const memos = tx.Memos
|
||||
memos
|
||||
.map((memo: any) => {
|
||||
@@ -186,43 +186,36 @@ export abstract class RJSVM_Builder {
|
||||
const resp = await this.rippleApi.request({
|
||||
command: "account_tx",
|
||||
account: this.config.listeningAddress,
|
||||
forward: true,
|
||||
forward: true,
|
||||
ledger_index_min: this.sync_block_height,
|
||||
marker: marker,
|
||||
})
|
||||
|
||||
//results may not be ordered, so sort them
|
||||
const raw_txs = await Promise.all(
|
||||
resp.result.transactions
|
||||
.filter(tx => tx.tx_json.TransactionType === "Payment")
|
||||
.sort((a,b) => a.tx_json.ledger_index - b.tx_json.ledger_index)
|
||||
.map((tx) => this.xrpIO.getTransaction(tx.hash))
|
||||
const full_txs = resp.result.transactions
|
||||
.filter(tx => !!tx.tx_json.Memos)
|
||||
.map(tx => {
|
||||
return ({
|
||||
...tx.tx_json,
|
||||
hash: tx.hash,
|
||||
Amount: tx.tx_json["DeliverMax"],
|
||||
inLedger: tx.tx_json['ledger_index']
|
||||
} as PaymentTx_T)
|
||||
})
|
||||
|
||||
await Promise.all(
|
||||
full_txs
|
||||
.map(tx => this.parseMemos(tx))
|
||||
)
|
||||
|
||||
//XRPL API update adaption
|
||||
await Promise.all(
|
||||
raw_txs
|
||||
.filter(tx => !!tx.result.tx_json.Memos)
|
||||
.map(tx => {
|
||||
return({
|
||||
...tx.result.tx_json,
|
||||
hash: tx.result.hash,
|
||||
Amount: tx.result.tx_json["DeliverMax"],
|
||||
inLedger: tx.result.tx_json['ledger_index']
|
||||
} as PaymentTx_T)
|
||||
})
|
||||
.map(tx => this.parseMemos(tx))
|
||||
)
|
||||
|
||||
//if marker is present the result is paginated.
|
||||
//re-run the same request with the marker included
|
||||
if(resp.result.marker){
|
||||
if (resp.result.marker) {
|
||||
await this.sync(resp.result.marker)
|
||||
}else{
|
||||
} else {
|
||||
//presence of no marker means we caught up to current block height
|
||||
this.sync_block_height = resp.result.ledger_index_max + 1
|
||||
//schedule the next sync
|
||||
this.syncTimeout = setTimeout(this.sync, 10000)
|
||||
this.syncTimeout = setTimeout(this.sync, 5000)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,4 +1,5 @@
|
||||
export * from "./RJSVM/framework/rjsvm"
|
||||
export * from "./RJSVM/datawriter/datawriter"
|
||||
export * from "./RJSVM/framework/types"
|
||||
export * from "./util/dataparser"
|
||||
export * from "./util/dataparser"
|
||||
export * from "zod"
|
||||
Reference in New Issue
Block a user