Skip to content

Commit

Permalink
MASTER - Release/4.2.0 (#181)
Browse files Browse the repository at this point in the history
4.2.0 Release
  • Loading branch information
julianapeace authored Apr 5, 2022
1 parent b2a4794 commit 4499d2f
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bnc-sdk",
"version": "4.1.0",
"version": "4.2.0",
"description": "SDK to connect to the blocknative backend via a websocket connection",
"keywords": [
"ethereum",
Expand Down Expand Up @@ -46,6 +46,7 @@
},
"dependencies": {
"crypto-es": "^1.2.2",
"nanoid": "^3.3.1",
"rxjs": "^6.6.3",
"sturdy-websocket": "^0.1.12"
}
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CryptoEs from 'crypto-es'
import transaction from './transaction'
import account from './account'
import event from './event'
import simulate from './simulate'
import unsubscribe from './unsubscribe'
import configuration from './configuration'

Expand All @@ -26,6 +27,7 @@ import {
Account,
Event,
Unsubscribe,
Simulate,
Destroy,
Configuration,
SDKError,
Expand Down Expand Up @@ -66,6 +68,7 @@ class Blocknative {
public transaction: Transaction
public account: Account
public event: Event
public simulate: Simulate
public unsubscribe: Unsubscribe
public destroy: Destroy
public configuration: Configuration
Expand Down Expand Up @@ -160,6 +163,7 @@ class Blocknative {
this.transaction = transaction.bind(this)
this.account = account.bind(this)
this.event = event.bind(this)
this.simulate = simulate.bind(this)
this.unsubscribe = unsubscribe.bind(this)
this.configuration = configuration.bind(this)
this.destroy = () => {
Expand Down
48 changes: 48 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export type Status =
| 'cancel'
| 'failed'
| 'dropped'
| 'simulated'

export interface InputOutput {
address: string
Expand Down Expand Up @@ -257,6 +258,8 @@ export interface EthereumTransactionLog extends BaseTransactionLog {
value?: number | string
gas?: string
gasPrice?: string
maxPriorityFeePerGas?: string
maxFeePerGas?: string
nonce?: number
}

Expand All @@ -267,6 +270,50 @@ export interface BitcoinTransactionLog extends BaseTransactionLog {

export type TransactionEventLog = EthereumTransactionLog | BitcoinTransactionLog

export interface SimulationTransaction {
id: string
from: string
to: string
value: number
gas: number
input: string,
gasPrice?: number
maxPriorityFeePerGas?: number
maxFeePerGas?: number
}

export interface SimDetails {
blockNumber: number
e2eMs: number
performanceProfile: any
}

export interface SimulationTransactionOutput {
id: string
from: string
to: string
value: number
gas: number
gasPrice: string
input: string
type: number
gasUsed: string
internalTransactions?: InternalTransaction[]
netBalanceChanges?: BalanceChange[]
serverVersion: string
simulatedBlockNumber: number
simDetails: SimDetails
status: Status
system: System
network: Network
error?: any
contractCall: ContractCall
}

export interface Simulate {
(system: System, network: Network, transaction: SimulationTransaction): Promise<SimulationTransactionOutput>
}

export interface EventObject {
eventCode: string
categoryCode: string
Expand Down Expand Up @@ -341,6 +388,7 @@ export interface API {
transaction: Transaction
account: Account
event: Event
simulate: Simulate
unsubscribe: Unsubscribe
destroy: Destroy
config: Configuration
Expand Down
14 changes: 14 additions & 0 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { version } from '../package.json'
import { Ac, Tx, Emitter, EventObject, TransactionHandler } from './interfaces'
import { DEFAULT_RATE_LIMIT_RULES, QUEUE_LIMIT } from './defaults'
import { simulations$ } from './streams'

export function sendMessage(this: any, msg: EventObject) {
if (this._queuedMessages.length > QUEUE_LIMIT) {
Expand Down Expand Up @@ -85,6 +86,11 @@ export function handleMessage(this: any, msg: { data: string }): void {
return
}

if (event.categoryCode === 'simulate') {
simulations$.error(event)
return
}

if (reason.includes('not a valid API key')) {
if (this._onerror) {
this._onerror({ message: reason })
Expand Down Expand Up @@ -200,6 +206,7 @@ export function handleMessage(this: any, msg: { data: string }): void {
}
}


if (event && event.transaction) {
const {
transaction,
Expand Down Expand Up @@ -265,6 +272,13 @@ 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)
return
}

const watchedAddress =
transaction.watchedAddress && this._system === 'ethereum'
? transaction.watchedAddress.toLowerCase()
Expand Down
33 changes: 33 additions & 0 deletions src/simulate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { SimulationTransaction, SimulationTransactionOutput } from './interfaces'
import { simulations$ } from './streams'
import { take, filter } from 'rxjs/operators'
import { nanoid } from 'nanoid'


function simulate(this: any, system: string, network: string, transaction: SimulationTransaction): Promise<SimulationTransactionOutput> {
if (this._destroyed)
throw new Error(
'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()

// send payload to server
this._sendMessage({
categoryCode: 'simulate',
eventCode: 'txSimulation',
transaction: transaction
})

return new Promise((resolve, reject) => {
simulations$.pipe(filter(({ id }) => {
return id === transaction.id
}), take(1)).subscribe({
next: (transaction) => resolve(transaction),
error: (event) => reject(event.error.message)
})
})
}

export default simulate
4 changes: 4 additions & 0 deletions src/streams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Subject } from 'rxjs'
import { EthereumTransactionData, SimulationTransactionOutput, SimulationTransaction } from './interfaces'

export const simulations$ = new Subject<SimulationTransactionOutput>()
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2259,6 +2259,11 @@ ms@^2.1.1:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==

nanoid@^3.3.1:
version "3.3.1"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.1.tgz#6347a18cac88af88f58af0b3594b723d5e99bb35"
integrity sha512-n6Vs/3KGyxPQd6uO0eH4Bv0ojGSUvuLlIHtC3Y0kEO23YRge8H9x1GCzLn28YX0H66pMkxuaeESFq4tKISKwdw==

nanomatch@^1.2.9:
version "1.2.13"
resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119"
Expand Down

0 comments on commit 4499d2f

Please sign in to comment.