diff --git a/package.json b/package.json index 81bc326..7092f5a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bnc-sdk", - "version": "4.4.1", + "version": "4.4.1-0.1.0", "description": "SDK to connect to the blocknative backend via a websocket connection", "keywords": [ "ethereum", diff --git a/src/index.ts b/src/index.ts index ffcfff2..02dcdef 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,7 @@ import transaction from './transaction' import account from './account' import event from './event' import simulate from './simulate' +import multiSim from './multi-sim' import unsubscribe from './unsubscribe' import configuration from './configuration' import { DEFAULT_RATE_LIMIT_RULES } from './defaults' @@ -67,6 +68,7 @@ class SDK { public account: Account public event: Event public simulate: Simulate + public multiSim: typeof multiSim public unsubscribe: Unsubscribe public destroy: Destroy public configuration: Configuration @@ -165,6 +167,7 @@ class SDK { this.account = account.bind(this) this.event = event.bind(this) this.simulate = simulate.bind(this) + this.multiSim = multiSim.bind(this) this.unsubscribe = unsubscribe.bind(this) this.configuration = configuration.bind(this) this.destroy = () => { diff --git a/src/messages.ts b/src/messages.ts index 1f5388b..4bf138e 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -180,7 +180,10 @@ export function handleMessage(this: any, msg: { data: string }): void { } if (event && event.categoryCode === 'simulate') { - simulations$.error(event) + simulations$.error({ + eventId: event.eventId, + error: { message: reason } + }) return } @@ -219,6 +222,7 @@ export function handleMessage(this: any, msg: { data: string }): void { if (event && event.transaction) { const { + eventId, transaction, eventCode, contractCall, @@ -285,7 +289,7 @@ export function handleMessage(this: any, msg: { data: string }): void { if (event && event.categoryCode === 'simulate') { newState.contractCall = event.transaction.contractCall delete newState.dispatchTimestamp - simulations$.next(newState) + simulations$.next({ eventId, transaction: newState }) return } diff --git a/src/multi-sim.ts b/src/multi-sim.ts new file mode 100644 index 0000000..6c742b0 --- /dev/null +++ b/src/multi-sim.ts @@ -0,0 +1,42 @@ +import { take, filter } from 'rxjs/operators' +import { nanoid } from 'nanoid' +import { SimulationTransaction, SimulationTransactionOutput } from './types' +import { simulations$ } from './streams' +import SDK from '.' + +function multiSim( + this: SDK, + transactions: SimulationTransaction[] +): Promise { + if (this._destroyed) + throw new Error( + 'The WebSocket instance has been destroyed, re-initialize to continue making requests.' + ) + + const id = nanoid() + + // send payload to server + this._sendMessage({ + categoryCode: 'simulate', + eventCode: 'txSimulation', + eventId: id, + transaction: transactions + }) + + return new Promise((resolve, reject) => { + simulations$ + .pipe( + filter(({ eventId }) => { + return eventId === id + }), + take(1) + ) + .subscribe({ + next: ({ transaction }) => + resolve(transaction as SimulationTransactionOutput[]), + error: ({ error }) => reject(error.message) + }) + }) +} + +export default multiSim diff --git a/src/simulate.ts b/src/simulate.ts index 03ce764..2820ce0 100644 --- a/src/simulate.ts +++ b/src/simulate.ts @@ -15,27 +15,28 @@ function simulate( 'The WebSocket instance has been destroyed, re-initialize to continue making requests.' ) - // generate a nano ID, add into transaction object, instead of filtering() below, just match the nano id - transaction.id = nanoid() + const id = nanoid() // send payload to server this._sendMessage({ categoryCode: 'simulate', eventCode: 'txSimulation', + eventId: id, transaction: transaction }) return new Promise((resolve, reject) => { simulations$ .pipe( - filter(({ id }) => { - return id === transaction.id + filter(({ eventId }) => { + return eventId === id }), take(1) ) .subscribe({ - next: transaction => resolve(transaction), - error: event => reject(event.error.message) + next: ({ transaction }) => + resolve(transaction as SimulationTransactionOutput), + error: ({ error }) => reject(error.message) }) }) } diff --git a/src/streams.ts b/src/streams.ts index 9059794..dbfb639 100644 --- a/src/streams.ts +++ b/src/streams.ts @@ -1,4 +1,7 @@ import { Subject } from 'rxjs' import { SimulationTransactionOutput } from './types' -export const simulations$ = new Subject() +export const simulations$ = new Subject<{ + eventId: string + transaction: SimulationTransactionOutput | SimulationTransactionOutput[] +}>() diff --git a/src/types.ts b/src/types.ts index 72ccb2b..86ea917 100644 --- a/src/types.ts +++ b/src/types.ts @@ -275,7 +275,6 @@ export interface BitcoinTransactionLog extends BaseTransactionLog { export type TransactionEventLog = EthereumTransactionLog | BitcoinTransactionLog export interface SimulationTransaction { - id: string from: string to: string value: number @@ -293,7 +292,7 @@ export interface SimDetails { } export interface SimulationTransactionOutput { - id: string + id?: string from: string to: string value: number @@ -324,6 +323,7 @@ export interface Simulate { export type BaseEventObject = { eventCode: string + eventId?: string categoryCode: string } @@ -346,6 +346,7 @@ export type TransactionEventObject = BaseEventObject & { | BitcoinTransactionEventObject | EthereumTransactionEventObject | SimulationTransaction + | SimulationTransaction[] } export type WalletEventObject = BaseEventObject & {