Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/three-nights-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@solana/transaction-messages': minor
'@solana/instruction-plans': minor
'@solana/transactions': minor
'@solana/signers': minor
'@solana/kit': minor
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flagging this - I think this is debatable on minor or major. TransactionMessage is compatible with BaseTransactionMessage, so app code isn't going to break. But if you have functions/types that use BaseTransactionMessage you may need to change them to use TransactionMessage in order to be compatible with our helper functions.

---

Return more precise types from transaction message functions

Deprecate `BaseTransactionMessage` in favour of `TransactionMessage`
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// [DESCRIBE] A scenario testing the types of transaction-message functionality
// Based on discussion/examples from https://github.com/anza-xyz/kit/pull/1103

import { Address } from '@solana/addresses';
import { pipe } from '@solana/functional';
import { Instruction } from '@solana/instructions';
import { GetMultipleAccountsApi, Rpc } from '@solana/rpc';
import { setTransactionMessageFeePayerSigner, TransactionSigner } from '@solana/signers';
import {
appendTransactionMessageInstruction,
appendTransactionMessageInstructions,
BlockhashLifetimeConstraint,
CompiledTransactionMessage,
CompiledTransactionMessageWithLifetime,
prependTransactionMessageInstruction,
prependTransactionMessageInstructions,
setTransactionMessageFeePayer,
setTransactionMessageLifetimeUsingBlockhash,
setTransactionMessageLifetimeUsingDurableNonce,
TransactionMessage,
} from '@solana/transaction-messages';

import { decompileTransactionMessageFetchingLookupTables } from '../../decompile-transaction-message-fetching-lookup-tables';

const compiledTransactionMessage = null as unknown as CompiledTransactionMessage &
CompiledTransactionMessageWithLifetime;
const rpc = null as unknown as Rpc<GetMultipleAccountsApi>;

type TransactionMessageNotLegacy = Exclude<TransactionMessage, { version: 'legacy' }>;

void (async () => {
const transactionMessage = await decompileTransactionMessageFetchingLookupTables(compiledTransactionMessage, rpc);

// @ts-expect-error Transaction has an unknown version
transactionMessage satisfies TransactionMessageNotLegacy;
if (transactionMessage.version === 0) {
// It typechecks when the transaction message is known to be v0
transactionMessage satisfies TransactionMessageNotLegacy;
}

// We update the transaction message using update functions to ensure the types flow correctly
const blockhash = null as unknown as BlockhashLifetimeConstraint;
const durableNonce = null as unknown as Parameters<typeof setTransactionMessageLifetimeUsingDurableNonce>[0];
const feePayer = null as unknown as Address;
const instruction = null as unknown as Instruction;
const signer = null as unknown as TransactionSigner;

const updatedMessage = pipe(
transactionMessage,
m => setTransactionMessageLifetimeUsingBlockhash(blockhash, m),
m => setTransactionMessageLifetimeUsingDurableNonce(durableNonce, m),
m => setTransactionMessageFeePayer(feePayer, m),
m => appendTransactionMessageInstruction(instruction, m),
m => appendTransactionMessageInstructions([instruction], m),
m => prependTransactionMessageInstruction(instruction, m),
m => prependTransactionMessageInstructions([instruction], m),
m => setTransactionMessageFeePayerSigner(signer, m),
);

// @ts-expect-error Transaction has an unknown version
updatedMessage satisfies TransactionMessageNotLegacy;
if (updatedMessage.version === 0) {
// It typechecks when the transaction message is known to be v0
updatedMessage satisfies TransactionMessageNotLegacy;
}
})();