Skip to content

Commit

Permalink
4.4.1-0.1.0: [feature] - Multi Sim (#199)
Browse files Browse the repository at this point in the history
* Start refactor to use eventId

* Adds multisim method

* Fix type

* Add multiSim method to class

* Increment version

* Fix error messages

* Fixes type
  • Loading branch information
lnbc1QWFyb24 authored Jun 26, 2022
1 parent c0f0432 commit 0d8d26c
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = () => {
Expand Down
8 changes: 6 additions & 2 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -219,6 +222,7 @@ export function handleMessage(this: any, msg: { data: string }): void {

if (event && event.transaction) {
const {
eventId,
transaction,
eventCode,
contractCall,
Expand Down Expand Up @@ -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
}

Expand Down
42 changes: 42 additions & 0 deletions src/multi-sim.ts
Original file line number Diff line number Diff line change
@@ -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<SimulationTransactionOutput[]> {
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
13 changes: 7 additions & 6 deletions src/simulate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
}
Expand Down
5 changes: 4 additions & 1 deletion src/streams.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Subject } from 'rxjs'
import { SimulationTransactionOutput } from './types'

export const simulations$ = new Subject<SimulationTransactionOutput>()
export const simulations$ = new Subject<{
eventId: string
transaction: SimulationTransactionOutput | SimulationTransactionOutput[]
}>()
5 changes: 3 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -293,7 +292,7 @@ export interface SimDetails {
}

export interface SimulationTransactionOutput {
id: string
id?: string
from: string
to: string
value: number
Expand Down Expand Up @@ -324,6 +323,7 @@ export interface Simulate {

export type BaseEventObject = {
eventCode: string
eventId?: string
categoryCode: string
}

Expand All @@ -346,6 +346,7 @@ export type TransactionEventObject = BaseEventObject & {
| BitcoinTransactionEventObject
| EthereumTransactionEventObject
| SimulationTransaction
| SimulationTransaction[]
}

export type WalletEventObject = BaseEventObject & {
Expand Down

0 comments on commit 0d8d26c

Please sign in to comment.