Skip to content

Commit

Permalink
Added handling for signature time measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
lukecaan committed Jan 12, 2024
1 parent ef7d2ec commit ff535bb
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 19 deletions.
4 changes: 4 additions & 0 deletions sdk/src/accounts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export interface DriftClientAccountEvents {
error: (e: Error) => void;
}

export interface DriftClientMiscEvents {
txSigned: void;
}

export interface DriftClientAccountSubscriber {
eventEmitter: StrictEventEmitter<EventEmitter, DriftClientAccountEvents>;
isSubscribed: boolean;
Expand Down
24 changes: 19 additions & 5 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -150,6 +151,7 @@ export class DriftClient {
userStatsAccountSubscriptionConfig: UserStatsSubscriptionConfig;
accountSubscriber: DriftClientAccountSubscriber;
eventEmitter: StrictEventEmitter<EventEmitter, DriftClientAccountEvents>;
miscEventEmitter: StrictEventEmitter<EventEmitter, DriftClientMiscEvents>;
_isSubscribed = false;
txSender: TxSender;
perpMarketLastSlotCache = new Map<number, number>();
Expand Down Expand Up @@ -288,6 +290,7 @@ export class DriftClient {
);
}
this.eventEmitter = this.accountSubscriber.eventEmitter;
this.miscEventEmitter = new EventEmitter();
this.txSender =
config.txSender ??
new RetryTxSender({
Expand Down Expand Up @@ -711,7 +714,7 @@ export class DriftClient {
public async initializeUserAccount(
subAccountId = 0,
name?: string,
referrerInfo?: ReferrerInfo
referrerInfo?: ReferrerInfo,
): Promise<[TransactionSignature, PublicKey]> {
const initializeIxs = [];

Expand Down Expand Up @@ -6322,25 +6325,36 @@ export class DriftClient {
return undefined;
}

private handleSignedTransaction() {
this.miscEventEmitter.emit("txSigned");
}


sendTransaction(
tx: Transaction | VersionedTransaction,
additionalSigners?: Array<Signer>,
opts?: ConfirmOptions,
preSigned?: boolean
preSigned?: boolean,
): Promise<TxSigAndSlot> {
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
);
}
}
Expand Down
7 changes: 4 additions & 3 deletions sdk/src/phoenix/phoenixFulfillmentConfigMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export class PhoenixFulfillmentConfigMap {
marketIndex: number,
phoenixMarketAddress: PublicKey
): Promise<void> {
const account = await this.driftClient.getPhoenixV1FulfillmentConfig(
phoenixMarketAddress
);
const account =
await this.driftClient.getPhoenixV1FulfillmentConfig(
phoenixMarketAddress
);
this.map.set(marketIndex, account);
}

Expand Down
5 changes: 2 additions & 3 deletions sdk/src/serum/serumFulfillmentConfigMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ export class SerumFulfillmentConfigMap {
marketIndex: number,
serumMarketAddress: PublicKey
): Promise<void> {
const account = await this.driftClient.getSerumV3FulfillmentConfig(
serumMarketAddress
);
const account =
await this.driftClient.getSerumV3FulfillmentConfig(serumMarketAddress);
this.map.set(marketIndex, account);
}

Expand Down
26 changes: 20 additions & 6 deletions sdk/src/tx/baseTxSender.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ConfirmationStrategy, TxSender, TxSigAndSlot } from './types';
import {
ConfirmationStrategy,
ExtraConfirmationOptions,
TxSender,
TxSigAndSlot,
} from './types';
import {
Commitment,
ConfirmOptions,
Expand Down Expand Up @@ -57,7 +62,8 @@ export abstract class BaseTxSender implements TxSender {
tx: Transaction,
additionalSigners?: Array<Signer>,
opts?: ConfirmOptions,
preSigned?: boolean
preSigned?: boolean,
extraConfirmationOptions?: ExtraConfirmationOptions
): Promise<TxSigAndSlot> {
if (additionalSigners === undefined) {
additionalSigners = [];
Expand All @@ -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);
}

Expand Down Expand Up @@ -124,7 +134,8 @@ export abstract class BaseTxSender implements TxSender {
tx: VersionedTransaction,
additionalSigners?: Array<Signer>,
opts?: ConfirmOptions,
preSigned?: boolean
preSigned?: boolean,
extraConfirmationOptions?: ExtraConfirmationOptions
): Promise<TxSigAndSlot> {
let signedTx;
if (preSigned) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions sdk/src/tx/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,27 @@ export type TxSigAndSlot = {
slot: number;
};

export type ExtraConfirmationOptions = {
onSignedCb: () => void;
};

export interface TxSender {
wallet: IWallet;

send(
tx: Transaction,
additionalSigners?: Array<Signer>,
opts?: ConfirmOptions,
preSigned?: boolean
preSigned?: boolean,
extraConfirmationOptions?: ExtraConfirmationOptions
): Promise<TxSigAndSlot>;

sendVersionedTransaction(
tx: VersionedTransaction,
additionalSigners?: Array<Signer>,
opts?: ConfirmOptions,
preSigned?: boolean
preSigned?: boolean,
extraConfirmationOptions?: ExtraConfirmationOptions
): Promise<TxSigAndSlot>;

getVersionedTransaction(
Expand Down

0 comments on commit ff535bb

Please sign in to comment.