Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
Refactor transaction-confirmation to use new transaction lifetimes (#…
Browse files Browse the repository at this point in the history
…2485)

* add lastValidBlockHeight to transaction blockhash lifetime

* add nonceAccountAddress to Transaction durable nonce lifetime

* refactor transaction-confirmation to use new lifetimes
  • Loading branch information
mcintyre94 authored Apr 18, 2024
1 parent 028bdcc commit 1344fa7
Show file tree
Hide file tree
Showing 17 changed files with 218 additions and 135 deletions.
36 changes: 16 additions & 20 deletions packages/library/src/__tests__/send-transaction-internal-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import type { Rpc, SendTransactionApi } from '@solana/rpc';
import type { Commitment } from '@solana/rpc-types';
import {
Base64EncodedWireTransaction,
BaseTransaction,
getBase64EncodedWireTransaction,
IDurableNonceTransaction,
IFullySignedTransaction,
ITransactionWithBlockhashLifetime,
ITransactionWithFeePayer,
FullySignedTransaction,
newGetBase64EncodedWireTransaction,
TransactionBlockhashLifetime,
TransactionDurableNonceLifetime,
} from '@solana/transactions';

import {
Expand All @@ -23,10 +21,9 @@ const FOREVER_PROMISE = new Promise(() => {
});

describe('sendAndConfirmTransaction', () => {
const MOCK_TRANSACTION = {} as unknown as BaseTransaction &
IFullySignedTransaction &
ITransactionWithBlockhashLifetime &
ITransactionWithFeePayer;
const MOCK_TRANSACTION = {} as unknown as FullySignedTransaction & {
lifetimeConstraint: TransactionBlockhashLifetime;
};
let confirmRecentTransaction: jest.Mock;
let createPendingRequest: jest.Mock;
let rpc: Rpc<SendTransactionApi>;
Expand All @@ -39,7 +36,7 @@ describe('sendAndConfirmTransaction', () => {
rpc = {
sendTransaction: createPendingRequest,
};
jest.mocked(getBase64EncodedWireTransaction).mockReturnValue(
jest.mocked(newGetBase64EncodedWireTransaction).mockReturnValue(
'MOCK_WIRE_TRANSACTION' as Base64EncodedWireTransaction,
);
});
Expand All @@ -51,7 +48,7 @@ describe('sendAndConfirmTransaction', () => {
rpc,
transaction: MOCK_TRANSACTION,
});
expect(getBase64EncodedWireTransaction).toHaveBeenCalledWith(MOCK_TRANSACTION);
expect(newGetBase64EncodedWireTransaction).toHaveBeenCalledWith(MOCK_TRANSACTION);
expect(createPendingRequest).toHaveBeenCalledWith('MOCK_WIRE_TRANSACTION', expect.anything());
});
it('calls `sendTransaction` with the expected inputs', () => {
Expand All @@ -69,7 +66,7 @@ describe('sendAndConfirmTransaction', () => {
rpc,
transaction: MOCK_TRANSACTION,
});
expect(getBase64EncodedWireTransaction).toHaveBeenCalledWith(MOCK_TRANSACTION);
expect(newGetBase64EncodedWireTransaction).toHaveBeenCalledWith(MOCK_TRANSACTION);
expect(createPendingRequest).toHaveBeenCalledWith('MOCK_WIRE_TRANSACTION', {
...sendTransactionConfig,
encoding: 'base64',
Expand Down Expand Up @@ -180,10 +177,9 @@ describe('sendAndConfirmTransaction', () => {
});

describe('sendAndConfirmDurableNonceTransaction', () => {
const MOCK_DURABLE_NONCE_TRANSACTION = {} as unknown as BaseTransaction &
IDurableNonceTransaction &
IFullySignedTransaction &
ITransactionWithFeePayer;
const MOCK_DURABLE_NONCE_TRANSACTION = {} as unknown as FullySignedTransaction & {
lifetimeConstraint: TransactionDurableNonceLifetime;
};
let confirmDurableNonceTransaction: jest.Mock;
let createPendingRequest: jest.Mock;
let rpc: Rpc<SendTransactionApi>;
Expand All @@ -196,7 +192,7 @@ describe('sendAndConfirmDurableNonceTransaction', () => {
rpc = {
sendTransaction: createPendingRequest,
};
jest.mocked(getBase64EncodedWireTransaction).mockReturnValue(
jest.mocked(newGetBase64EncodedWireTransaction).mockReturnValue(
'MOCK_WIRE_TRANSACTION' as Base64EncodedWireTransaction,
);
});
Expand All @@ -208,7 +204,7 @@ describe('sendAndConfirmDurableNonceTransaction', () => {
rpc,
transaction: MOCK_DURABLE_NONCE_TRANSACTION,
});
expect(getBase64EncodedWireTransaction).toHaveBeenCalledWith(MOCK_DURABLE_NONCE_TRANSACTION);
expect(newGetBase64EncodedWireTransaction).toHaveBeenCalledWith(MOCK_DURABLE_NONCE_TRANSACTION);
expect(createPendingRequest).toHaveBeenCalledWith('MOCK_WIRE_TRANSACTION', expect.anything());
});
it('calls `sendTransaction` with the expected inputs', () => {
Expand All @@ -226,7 +222,7 @@ describe('sendAndConfirmDurableNonceTransaction', () => {
rpc,
transaction: MOCK_DURABLE_NONCE_TRANSACTION,
});
expect(getBase64EncodedWireTransaction).toHaveBeenCalledWith(MOCK_DURABLE_NONCE_TRANSACTION);
expect(newGetBase64EncodedWireTransaction).toHaveBeenCalledWith(MOCK_DURABLE_NONCE_TRANSACTION);
expect(createPendingRequest).toHaveBeenCalledWith('MOCK_WIRE_TRANSACTION', {
...sendTransactionConfig,
encoding: 'base64',
Expand Down
15 changes: 10 additions & 5 deletions packages/library/src/send-transaction-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import {
} from '@solana/transaction-confirmation';
import {
BaseTransaction,
getBase64EncodedWireTransaction,
FullySignedTransaction,
IDurableNonceTransaction,
IFullySignedTransaction,
ITransactionWithBlockhashLifetime,
ITransactionWithFeePayer,
newGetBase64EncodedWireTransaction,
} from '@solana/transactions';
import {
TransactionBlockhashLifetime,
TransactionDurableNonceLifetime,
} from '@solana/transactions/dist/types/lifetime';

interface SendAndConfirmDurableNonceTransactionConfig
extends SendTransactionBaseConfig,
Expand All @@ -23,7 +28,7 @@ interface SendAndConfirmDurableNonceTransactionConfig
'getNonceInvalidationPromise' | 'getRecentSignatureConfirmationPromise'
>,
) => Promise<void>;
transaction: IDurableNonceTransaction & SendableTransaction;
transaction: FullySignedTransaction & { lifetimeConstraint: TransactionDurableNonceLifetime };
}

interface SendAndConfirmTransactionWithBlockhashLifetimeConfig
Expand All @@ -35,14 +40,14 @@ interface SendAndConfirmTransactionWithBlockhashLifetimeConfig
'getBlockHeightExceedencePromise' | 'getRecentSignatureConfirmationPromise'
>,
) => Promise<void>;
transaction: ITransactionWithBlockhashLifetime & SendableTransaction;
transaction: FullySignedTransaction & { lifetimeConstraint: TransactionBlockhashLifetime };
}

interface SendTransactionBaseConfig extends SendTransactionConfigWithoutEncoding {
abortSignal?: AbortSignal;
commitment: Commitment;
rpc: Rpc<SendTransactionApi>;
transaction: SendableTransaction;
transaction: FullySignedTransaction;
}

interface SendTransactionConfigWithoutEncoding
Expand Down Expand Up @@ -84,7 +89,7 @@ export async function sendTransaction_INTERNAL_ONLY_DO_NOT_EXPORT({
transaction,
...sendTransactionConfig
}: SendTransactionBaseConfig): Promise<Signature> {
const base64EncodedWireTransaction = getBase64EncodedWireTransaction(transaction);
const base64EncodedWireTransaction = newGetBase64EncodedWireTransaction(transaction);
return await rpc
.sendTransaction(base64EncodedWireTransaction, {
...getSendTransactionConfigWithAdjustedPreflightCommitment(commitment, sendTransactionConfig),
Expand Down
15 changes: 6 additions & 9 deletions packages/library/src/send-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ import {
waitForRecentTransactionConfirmation,
} from '@solana/transaction-confirmation';
import {
BaseTransaction,
IDurableNonceTransaction,
IFullySignedTransaction,
ITransactionWithBlockhashLifetime,
ITransactionWithFeePayer,
FullySignedTransaction,
TransactionBlockhashLifetime,
TransactionDurableNonceLifetime,
} from '@solana/transactions';

import {
SendableTransaction,
sendAndConfirmDurableNonceTransaction_INTERNAL_ONLY_DO_NOT_EXPORT,
sendAndConfirmTransactionWithBlockhashLifetime_INTERNAL_ONLY_DO_NOT_EXPORT,
sendTransaction_INTERNAL_ONLY_DO_NOT_EXPORT,
Expand All @@ -42,23 +39,23 @@ interface SendTransactionWithoutConfirmingFactoryConfig {
}

type SendAndConfirmTransactionWithBlockhashLifetimeFunction = (
transaction: ITransactionWithBlockhashLifetime & SendableTransaction,
transaction: FullySignedTransaction & { lifetimeConstraint: TransactionBlockhashLifetime },
config: Omit<
Parameters<typeof sendAndConfirmTransactionWithBlockhashLifetime_INTERNAL_ONLY_DO_NOT_EXPORT>[0],
'confirmRecentTransaction' | 'rpc' | 'transaction'
>,
) => Promise<void>;

type SendAndConfirmDurableNonceTransactionFunction = (
transaction: BaseTransaction & IDurableNonceTransaction & IFullySignedTransaction & ITransactionWithFeePayer,
transaction: FullySignedTransaction & { lifetimeConstraint: TransactionDurableNonceLifetime },
config: Omit<
Parameters<typeof sendAndConfirmDurableNonceTransaction_INTERNAL_ONLY_DO_NOT_EXPORT>[0],
'confirmDurableNonceTransaction' | 'rpc' | 'transaction'
>,
) => Promise<void>;

type SendTransactionWithoutConfirmingFunction = (
transaction: SendableTransaction,
transaction: FullySignedTransaction,
config: Omit<Parameters<typeof sendTransaction_INTERNAL_ONLY_DO_NOT_EXPORT>[0], 'rpc' | 'transaction'>,
) => Promise<void>;

Expand Down
Loading

0 comments on commit 1344fa7

Please # to comment.