Skip to content

getTransactionByBlockIdAndIndex

Action for getting a Starknet transaction using block identifier and index.

Import

import { getTransactionByBlockIdAndIndex } from "starkweb/core";

Usage

index.ts
import { getTransactionByBlockIdAndIndex } from "starkweb/core";
import { config } from "./config";
 
const transaction = await getTransactionByBlockIdAndIndex(config, {
  blockIdentifier: "latest", 
  index: 0, 
});

Parameters

import { type GetTransactionByBlockIdAndIndexParameters } from "starkweb/core";

blockIdentifier

BlockIdentifier

The block identifier (number, hash, or 'latest').

index

number

The index of the transaction in the block.

index.ts
import { getTransactionByBlockIdAndIndex } from "starkweb/core";
import { config } from "./config";
 
const transaction = await getTransactionByBlockIdAndIndex(config, {
  blockIdentifier: "latest", 
  index: 0, 
});

chainId (optional)

Hex | undefined

The chain ID to get the transaction from. If not provided, uses the current chain.

index.ts
import { getTransactionByBlockIdAndIndex } from "starkweb/core";
import { mainnet } from "starkweb/chains"; 
import { config } from "./config";
 
const transaction = await getTransactionByBlockIdAndIndex(config, {
  blockIdentifier: "latest",
  index: 0,
  chainId: mainnet.chain_id, 
});

Return Type

import { type GetTransactionByBlockIdAndIndexReturnType } from "starkweb/core";

Returns the transaction information including:

chainId

Hex

The chain ID of the network.

transaction_hash

Hex

The hash of the transaction.

type

TransactionType

The type of the transaction.

block_number

bigint

The block number containing the transaction.

Example

example.ts
import { getTransactionByBlockIdAndIndex } from "starkweb/core";
import { mainnet } from "starkweb/chains"; 
import { config } from "./config";
 
// Get first transaction from latest block
const transaction = await getTransactionByBlockIdAndIndex(config, {
  blockIdentifier: "latest", 
  index: 0, 
});
console.log("Transaction hash:", transaction.transaction_hash);
 
// Get transaction from specific block on mainnet
const mainnetTransaction = await getTransactionByBlockIdAndIndex(config, {
  blockIdentifier: 123456n,
  index: 1,
  chainId: mainnet.chain_id, 
});

Error

import { type GetTransactionByBlockIdAndIndexErrorType } from "starkweb/core";