Skip to content

Commit

Permalink
feat: add getParsedBlock method to Connection (#28345)
Browse files Browse the repository at this point in the history
* feat: add getParsedBlock method to Connection

* Update web3.js/src/connection.ts

Co-authored-by: Yihau Chen <a122092487@gmail.com>

Co-authored-by: Yihau Chen <a122092487@gmail.com>
  • Loading branch information
jstarry and yihau committed Oct 11, 2022
1 parent 928730a commit 33a328e
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
90 changes: 90 additions & 0 deletions web3.js/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,42 @@ export type BlockResponse = {
blockTime: number | null;
};

/**
* A block with parsed transactions
*/
export type ParsedBlockResponse = {
/** Blockhash of this block */
blockhash: Blockhash;
/** Blockhash of this block's parent */
previousBlockhash: Blockhash;
/** Slot index of this block's parent */
parentSlot: number;
/** Vector of transactions with status meta and original message */
transactions: Array<{
/** The details of the transaction */
transaction: ParsedTransaction;
/** Metadata produced from the transaction */
meta: ParsedTransactionMeta | null;
/** The transaction version */
version?: TransactionVersion;
}>;
/** Vector of block rewards */
rewards?: Array<{
/** Public key of reward recipient */
pubkey: string;
/** Reward value in lamports */
lamports: number;
/** Account balance after reward is applied */
postBalance: number | null;
/** Type of reward received */
rewardType: string | null;
}>;
/** The unix timestamp of when the block was processed */
blockTime: number | null;
/** The number of blocks beneath this block */
blockHeight: number | null;
};

/**
* A processed block fetched from the RPC API
*/
Expand Down Expand Up @@ -2081,6 +2117,38 @@ const GetBlockRpcResult = jsonRpcResult(
),
);

/**
* Expected parsed JSON RPC response for the "getBlock" message
*/
const GetParsedBlockRpcResult = jsonRpcResult(
nullable(
pick({
blockhash: string(),
previousBlockhash: string(),
parentSlot: number(),
transactions: array(
pick({
transaction: ParsedConfirmedTransactionResult,
meta: nullable(ParsedConfirmedTransactionMetaResult),
version: optional(TransactionVersionStruct),
}),
),
rewards: optional(
array(
pick({
pubkey: string(),
lamports: number(),
postBalance: nullable(number()),
rewardType: nullable(string()),
}),
),
),
blockTime: nullable(number()),
blockHeight: nullable(number()),
}),
),
);

/**
* Expected JSON RPC response for the "getConfirmedBlock" message
*
Expand Down Expand Up @@ -3878,6 +3946,28 @@ export class Connection {
};
}

/**
* Fetch parsed transaction details for a confirmed or finalized block
*/
async getParsedBlock(
slot: number,
rawConfig?: GetVersionedBlockConfig,
): Promise<ParsedBlockResponse | null> {
const {commitment, config} = extractCommitmentFromConfig(rawConfig);
const args = this._buildArgsAtLeastConfirmed(
[slot],
commitment as Finality,
'jsonParsed',
config,
);
const unsafeRes = await this._rpcRequest('getBlock', args);
const res = create(unsafeRes, GetParsedBlockRpcResult);
if ('error' in res) {
throw new SolanaJSONRPCError(res.error, 'failed to get block');
}
return res.result;
}

/*
* Returns the current block height of the node
*/
Expand Down
18 changes: 18 additions & 0 deletions web3.js/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4621,6 +4621,24 @@ describe('Connection', function () {
}
expect(foundTx).to.be.true;
});

it('getParsedBlock', async () => {
const block = await connection.getParsedBlock(transactionSlot, {
maxSupportedTransactionVersion: 0,
commitment: 'confirmed',
});
expect(block).to.not.be.null;
if (block === null) throw new Error(); // unreachable

let foundTx = false;
for (const tx of block.transactions) {
if (tx.transaction.signatures[0] === signature) {
foundTx = true;
expect(tx.version).to.eq(0);
}
}
expect(foundTx).to.be.true;
});
}).timeout(5 * 1000);
}
});

0 comments on commit 33a328e

Please sign in to comment.