From ff535bb9080c4af8d9cb9f780ed43fe12a86c507 Mon Sep 17 00:00:00 2001 From: Luke Steyn Date: Fri, 12 Jan 2024 13:43:31 +0300 Subject: [PATCH] Added handling for signature time measurement --- sdk/src/accounts/types.ts | 4 +++ sdk/src/driftClient.ts | 24 +++++++++++++---- .../phoenix/phoenixFulfillmentConfigMap.ts | 7 ++--- sdk/src/serum/serumFulfillmentConfigMap.ts | 5 ++-- sdk/src/tx/baseTxSender.ts | 26 ++++++++++++++----- sdk/src/tx/types.ts | 10 +++++-- 6 files changed, 57 insertions(+), 19 deletions(-) diff --git a/sdk/src/accounts/types.ts b/sdk/src/accounts/types.ts index 440148927..643d4fb5e 100644 --- a/sdk/src/accounts/types.ts +++ b/sdk/src/accounts/types.ts @@ -43,6 +43,10 @@ export interface DriftClientAccountEvents { error: (e: Error) => void; } +export interface DriftClientMiscEvents { + txSigned: void; +} + export interface DriftClientAccountSubscriber { eventEmitter: StrictEventEmitter; isSubscribed: boolean; diff --git a/sdk/src/driftClient.ts b/sdk/src/driftClient.ts index adcc374c0..bd76e0b8b 100644 --- a/sdk/src/driftClient.ts +++ b/sdk/src/driftClient.ts @@ -86,8 +86,9 @@ import { DriftClientAccountSubscriber, DriftClientAccountEvents, DataAndSlot, + DriftClientMiscEvents, } from './accounts/types'; -import { TxSender, TxSigAndSlot } from './tx/types'; +import { ExtraConfirmationOptions, TxSender, TxSigAndSlot } from './tx/types'; import { getSignedTransactionMap, wrapInTx } from './tx/utils'; import { BASE_PRECISION, @@ -150,6 +151,7 @@ export class DriftClient { userStatsAccountSubscriptionConfig: UserStatsSubscriptionConfig; accountSubscriber: DriftClientAccountSubscriber; eventEmitter: StrictEventEmitter; + miscEventEmitter: StrictEventEmitter; _isSubscribed = false; txSender: TxSender; perpMarketLastSlotCache = new Map(); @@ -288,6 +290,7 @@ export class DriftClient { ); } this.eventEmitter = this.accountSubscriber.eventEmitter; + this.miscEventEmitter = new EventEmitter(); this.txSender = config.txSender ?? new RetryTxSender({ @@ -711,7 +714,7 @@ export class DriftClient { public async initializeUserAccount( subAccountId = 0, name?: string, - referrerInfo?: ReferrerInfo + referrerInfo?: ReferrerInfo, ): Promise<[TransactionSignature, PublicKey]> { const initializeIxs = []; @@ -6322,25 +6325,36 @@ export class DriftClient { return undefined; } + private handleSignedTransaction() { + this.miscEventEmitter.emit("txSigned"); + } + + sendTransaction( tx: Transaction | VersionedTransaction, additionalSigners?: Array, opts?: ConfirmOptions, - preSigned?: boolean + preSigned?: boolean, ): Promise { + const extraConfirmationOptions : ExtraConfirmationOptions = { + onSignedCb: this.handleSignedTransaction.bind(this), + }; + if (tx instanceof VersionedTransaction) { return this.txSender.sendVersionedTransaction( tx as VersionedTransaction, additionalSigners, opts, - preSigned + preSigned, + extraConfirmationOptions ); } else { return this.txSender.send( tx as Transaction, additionalSigners, opts, - preSigned + preSigned, + extraConfirmationOptions ); } } diff --git a/sdk/src/phoenix/phoenixFulfillmentConfigMap.ts b/sdk/src/phoenix/phoenixFulfillmentConfigMap.ts index 0708cc196..34b8540f9 100644 --- a/sdk/src/phoenix/phoenixFulfillmentConfigMap.ts +++ b/sdk/src/phoenix/phoenixFulfillmentConfigMap.ts @@ -14,9 +14,10 @@ export class PhoenixFulfillmentConfigMap { marketIndex: number, phoenixMarketAddress: PublicKey ): Promise { - const account = await this.driftClient.getPhoenixV1FulfillmentConfig( - phoenixMarketAddress - ); + const account = + await this.driftClient.getPhoenixV1FulfillmentConfig( + phoenixMarketAddress + ); this.map.set(marketIndex, account); } diff --git a/sdk/src/serum/serumFulfillmentConfigMap.ts b/sdk/src/serum/serumFulfillmentConfigMap.ts index 4d57f40a2..23b3562a1 100644 --- a/sdk/src/serum/serumFulfillmentConfigMap.ts +++ b/sdk/src/serum/serumFulfillmentConfigMap.ts @@ -14,9 +14,8 @@ export class SerumFulfillmentConfigMap { marketIndex: number, serumMarketAddress: PublicKey ): Promise { - const account = await this.driftClient.getSerumV3FulfillmentConfig( - serumMarketAddress - ); + const account = + await this.driftClient.getSerumV3FulfillmentConfig(serumMarketAddress); this.map.set(marketIndex, account); } diff --git a/sdk/src/tx/baseTxSender.ts b/sdk/src/tx/baseTxSender.ts index e1f5f905d..c4697aa94 100644 --- a/sdk/src/tx/baseTxSender.ts +++ b/sdk/src/tx/baseTxSender.ts @@ -1,4 +1,9 @@ -import { ConfirmationStrategy, TxSender, TxSigAndSlot } from './types'; +import { + ConfirmationStrategy, + ExtraConfirmationOptions, + TxSender, + TxSigAndSlot, +} from './types'; import { Commitment, ConfirmOptions, @@ -57,7 +62,8 @@ export abstract class BaseTxSender implements TxSender { tx: Transaction, additionalSigners?: Array, opts?: ConfirmOptions, - preSigned?: boolean + preSigned?: boolean, + extraConfirmationOptions?: ExtraConfirmationOptions ): Promise { if (additionalSigners === undefined) { additionalSigners = []; @@ -70,6 +76,10 @@ export abstract class BaseTxSender implements TxSender { ? tx : await this.prepareTx(tx, additionalSigners, opts); + if (extraConfirmationOptions?.onSignedCb) { + extraConfirmationOptions.onSignedCb(); + } + return this.sendRawTransaction(signedTx.serialize(), opts); } @@ -124,7 +134,8 @@ export abstract class BaseTxSender implements TxSender { tx: VersionedTransaction, additionalSigners?: Array, opts?: ConfirmOptions, - preSigned?: boolean + preSigned?: boolean, + extraConfirmationOptions?: ExtraConfirmationOptions ): Promise { let signedTx; if (preSigned) { @@ -144,6 +155,10 @@ export abstract class BaseTxSender implements TxSender { signedTx = await this.wallet.signTransaction(tx); } + if (extraConfirmationOptions?.onSignedCb) { + extraConfirmationOptions.onSignedCb(); + } + if (opts === undefined) { opts = this.opts; } @@ -216,9 +231,8 @@ export abstract class BaseTxSender implements TxSender { if (response === null) { if (this.confirmationStrategy === ConfirmationStrategy.Combo) { try { - const rpcResponse = await this.connection.getSignatureStatus( - signature - ); + const rpcResponse = + await this.connection.getSignatureStatus(signature); if (rpcResponse?.value?.confirmationStatus) { response = { context: rpcResponse.context, diff --git a/sdk/src/tx/types.ts b/sdk/src/tx/types.ts index a76c04fe6..360699685 100644 --- a/sdk/src/tx/types.ts +++ b/sdk/src/tx/types.ts @@ -20,6 +20,10 @@ export type TxSigAndSlot = { slot: number; }; +export type ExtraConfirmationOptions = { + onSignedCb: () => void; +}; + export interface TxSender { wallet: IWallet; @@ -27,14 +31,16 @@ export interface TxSender { tx: Transaction, additionalSigners?: Array, opts?: ConfirmOptions, - preSigned?: boolean + preSigned?: boolean, + extraConfirmationOptions?: ExtraConfirmationOptions ): Promise; sendVersionedTransaction( tx: VersionedTransaction, additionalSigners?: Array, opts?: ConfirmOptions, - preSigned?: boolean + preSigned?: boolean, + extraConfirmationOptions?: ExtraConfirmationOptions ): Promise; getVersionedTransaction(