Skip to content

Commit

Permalink
feat!: update getBlock to support versioned transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry committed Aug 29, 2022
1 parent eb86b12 commit bca68f0
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 112 deletions.
52 changes: 47 additions & 5 deletions web3.js/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1024,12 +1024,14 @@ export type BlockResponse = {
/** The transaction */
transaction: {
/** The transaction message */
message: Message;
message: VersionedMessage;
/** The transaction signatures */
signatures: string[];
};
/** Metadata produced from the transaction */
meta: ConfirmedTransactionMeta | null;
/** The transaction version */
version?: TransactionVersion;
}>;
/** Vector of block rewards */
rewards?: Array<{
Expand Down Expand Up @@ -1906,6 +1908,8 @@ const ParsedConfirmedTransactionMetaResult = pick({
loadedAddresses: optional(LoadedAddressesResult),
});

const TransactionVersionStruct = union([literal(0), literal('legacy')]);

/**
* Expected JSON RPC response for the "getBlock" message
*/
Expand All @@ -1919,6 +1923,7 @@ const GetBlockRpcResult = jsonRpcResult(
pick({
transaction: ConfirmedTransactionResult,
meta: nullable(ConfirmedTransactionMetaResult),
version: optional(TransactionVersionStruct),
}),
),
rewards: optional(
Expand Down Expand Up @@ -1984,8 +1989,6 @@ const GetBlockSignaturesRpcResult = jsonRpcResult(
),
);

const TransactionVersionStruct = union([literal(0), literal('legacy')]);

/**
* Expected JSON RPC response for the "getTransaction" message
*/
Expand Down Expand Up @@ -3685,6 +3688,10 @@ export class Connection {
slot: number,
rawConfig?: GetBlockConfig,
): Promise<BlockResponse | null> {
if (rawConfig && !('maxSupportedTransactionVersion' in rawConfig)) {
rawConfig.maxSupportedTransactionVersion = 0;
}

const {commitment, config} = extractCommitmentFromConfig(rawConfig);
const args = this._buildArgsAtLeastConfirmed(
[slot],
Expand All @@ -3704,14 +3711,33 @@ export class Connection {

return {
...result,
transactions: result.transactions.map(({transaction, meta}) => {
const message = new Message(transaction.message);
transactions: result.transactions.map(({transaction, meta, version}) => {
const messageResponse = transaction.message;
let message: VersionedMessage;
if (version === 0) {
message = new MessageV0({
header: messageResponse.header,
staticAccountKeys: messageResponse.accountKeys.map(
address => new PublicKey(address),
),
recentBlockhash: messageResponse.recentBlockhash,
compiledInstructions: messageResponse.instructions.map(ix => ({
programIdIndex: ix.programIdIndex,
accountKeyIndexes: ix.accounts,
data: bs58.decode(ix.data),
})),
addressTableLookups: messageResponse.addressTableLookups!,
});
} else {
message = new Message(messageResponse);
}
return {
meta,
transaction: {
...transaction,
message,
},
version,
};
}),
};
Expand Down Expand Up @@ -3836,6 +3862,14 @@ export class Connection {
signature: TransactionSignature,
commitmentOrConfig?: GetTransactionConfig | Finality,
): Promise<ParsedTransactionWithMeta | null> {
if (
commitmentOrConfig &&
typeof commitmentOrConfig === 'object' &&
!('maxSupportedTransactionVersion' in commitmentOrConfig)
) {
commitmentOrConfig.maxSupportedTransactionVersion = 0;
}

const {commitment, config} =
extractCommitmentFromConfig(commitmentOrConfig);
const args = this._buildArgsAtLeastConfirmed(
Expand Down Expand Up @@ -3894,6 +3928,14 @@ export class Connection {
signatures: TransactionSignature[],
commitmentOrConfig?: GetTransactionConfig | Finality,
): Promise<(TransactionResponse | null)[]> {
if (
commitmentOrConfig &&
typeof commitmentOrConfig === 'object' &&
!('maxSupportedTransactionVersion' in commitmentOrConfig)
) {
commitmentOrConfig.maxSupportedTransactionVersion = 0;
}

const {commitment, config} =
extractCommitmentFromConfig(commitmentOrConfig);
const batch = signatures.map(signature => {
Expand Down
Loading

0 comments on commit bca68f0

Please sign in to comment.