Skip to content

Commit 15b58a6

Browse files
authored
Merge pull request #200 from blocknative/release/4.5.0
Release 4.5.0 (main)
2 parents 80f9a11 + 3982840 commit 15b58a6

File tree

7 files changed

+66
-12
lines changed

7 files changed

+66
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bnc-sdk",
3-
"version": "4.4.1",
3+
"version": "4.5.0",
44
"description": "SDK to connect to the blocknative backend via a websocket connection",
55
"keywords": [
66
"ethereum",

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import transaction from './transaction'
55
import account from './account'
66
import event from './event'
77
import simulate from './simulate'
8+
import multiSim from './multi-sim'
89
import unsubscribe from './unsubscribe'
910
import configuration from './configuration'
1011
import { DEFAULT_RATE_LIMIT_RULES } from './defaults'
@@ -67,6 +68,7 @@ class SDK {
6768
public account: Account
6869
public event: Event
6970
public simulate: Simulate
71+
public multiSim: typeof multiSim
7072
public unsubscribe: Unsubscribe
7173
public destroy: Destroy
7274
public configuration: Configuration
@@ -165,6 +167,7 @@ class SDK {
165167
this.account = account.bind(this)
166168
this.event = event.bind(this)
167169
this.simulate = simulate.bind(this)
170+
this.multiSim = multiSim.bind(this)
168171
this.unsubscribe = unsubscribe.bind(this)
169172
this.configuration = configuration.bind(this)
170173
this.destroy = () => {

src/messages.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ export function handleMessage(this: any, msg: { data: string }): void {
180180
}
181181

182182
if (event && event.categoryCode === 'simulate') {
183-
simulations$.error(event)
183+
simulations$.error({
184+
eventId: event.eventId,
185+
error: { message: reason }
186+
})
184187
return
185188
}
186189

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

220223
if (event && event.transaction) {
221224
const {
225+
eventId,
222226
transaction,
223227
eventCode,
224228
contractCall,
@@ -285,7 +289,7 @@ export function handleMessage(this: any, msg: { data: string }): void {
285289
if (event && event.categoryCode === 'simulate') {
286290
newState.contractCall = event.transaction.contractCall
287291
delete newState.dispatchTimestamp
288-
simulations$.next(newState)
292+
simulations$.next({ eventId, transaction: newState })
289293
return
290294
}
291295

src/multi-sim.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { take, filter } from 'rxjs/operators'
2+
import { nanoid } from 'nanoid'
3+
import { SimulationTransaction, SimulationTransactionOutput } from './types'
4+
import { simulations$ } from './streams'
5+
import SDK from '.'
6+
7+
function multiSim(
8+
this: SDK,
9+
transactions: SimulationTransaction[]
10+
): Promise<SimulationTransactionOutput[]> {
11+
if (this._destroyed)
12+
throw new Error(
13+
'The WebSocket instance has been destroyed, re-initialize to continue making requests.'
14+
)
15+
16+
const id = nanoid()
17+
18+
// send payload to server
19+
this._sendMessage({
20+
categoryCode: 'simulate',
21+
eventCode: 'txSimulation',
22+
eventId: id,
23+
transaction: transactions
24+
})
25+
26+
return new Promise((resolve, reject) => {
27+
simulations$
28+
.pipe(
29+
filter(({ eventId }) => {
30+
return eventId === id
31+
}),
32+
take(1)
33+
)
34+
.subscribe({
35+
next: ({ transaction }) =>
36+
resolve(transaction as SimulationTransactionOutput[]),
37+
error: ({ error }) => reject(error.message)
38+
})
39+
})
40+
}
41+
42+
export default multiSim

src/simulate.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,28 @@ function simulate(
1515
'The WebSocket instance has been destroyed, re-initialize to continue making requests.'
1616
)
1717

18-
// generate a nano ID, add into transaction object, instead of filtering() below, just match the nano id
19-
transaction.id = nanoid()
18+
const id = nanoid()
2019

2120
// send payload to server
2221
this._sendMessage({
2322
categoryCode: 'simulate',
2423
eventCode: 'txSimulation',
24+
eventId: id,
2525
transaction: transaction
2626
})
2727

2828
return new Promise((resolve, reject) => {
2929
simulations$
3030
.pipe(
31-
filter(({ id }) => {
32-
return id === transaction.id
31+
filter(({ eventId }) => {
32+
return eventId === id
3333
}),
3434
take(1)
3535
)
3636
.subscribe({
37-
next: transaction => resolve(transaction),
38-
error: event => reject(event.error.message)
37+
next: ({ transaction }) =>
38+
resolve(transaction as SimulationTransactionOutput),
39+
error: ({ error }) => reject(error.message)
3940
})
4041
})
4142
}

src/streams.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { Subject } from 'rxjs'
22
import { SimulationTransactionOutput } from './types'
33

4-
export const simulations$ = new Subject<SimulationTransactionOutput>()
4+
export const simulations$ = new Subject<{
5+
eventId: string
6+
transaction: SimulationTransactionOutput | SimulationTransactionOutput[]
7+
}>()

src/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ export interface BitcoinTransactionLog extends BaseTransactionLog {
275275
export type TransactionEventLog = EthereumTransactionLog | BitcoinTransactionLog
276276

277277
export interface SimulationTransaction {
278-
id: string
279278
from: string
280279
to: string
281280
value: number
@@ -293,7 +292,7 @@ export interface SimDetails {
293292
}
294293

295294
export interface SimulationTransactionOutput {
296-
id: string
295+
id?: string
297296
from: string
298297
to: string
299298
value: number
@@ -324,6 +323,7 @@ export interface Simulate {
324323

325324
export type BaseEventObject = {
326325
eventCode: string
326+
eventId?: string
327327
categoryCode: string
328328
}
329329

@@ -346,6 +346,7 @@ export type TransactionEventObject = BaseEventObject & {
346346
| BitcoinTransactionEventObject
347347
| EthereumTransactionEventObject
348348
| SimulationTransaction
349+
| SimulationTransaction[]
349350
}
350351

351352
export type WalletEventObject = BaseEventObject & {

0 commit comments

Comments
 (0)