const { Web3 } = require("web3");
const url =
"YOUR_CHAINSTACK_NODE"; // Replace with your Chainstack Ethereum node endpoint
const web3 = new Web3(new Web3.providers.HttpProvider(url));
// ABI for the Bored Ape Yacht Club contract, only including the Transfer event
const BAYC_ABI = [
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: "address",
name: "from",
type: "address",
},
{
indexed: true,
internalType: "address",
name: "to",
type: "address",
},
{
indexed: true,
internalType: "uint256",
name: "tokenId",
type: "uint256",
},
],
name: "Transfer",
type: "event",
},
];
const BAYC_CONTRACT_ADDRESS = "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D";
const contract = new web3.eth.Contract(BAYC_ABI, BAYC_CONTRACT_ADDRESS);
async function fetchTransfersForTokenId(tokenId) {
try {
// Fetch the latest block number and calculate the target block
const latestBlock = await web3.eth.getBlockNumber();
const target = Number(latestBlock) - 10000;
// Fetch Transfer events for the given token ID
const events = await contract.getPastEvents("Transfer", {
filter: { tokenId: tokenId },
fromBlock: target,
toBlock: "latest",
});
console.log(
`Total transfers for Bored Ape ${tokenId}: ${events.length} transfers`
);
// Iterate through the events and log details
for (let event of events) {
console.log(
`From: ${event.returnValues.from} To: ${event.returnValues.to} at block: ${event.blockNumber}`
);
}
} catch (error) {
console.error(`Error fetching transfers for token ${tokenId}:`, error);
}
}
// Fetch bids for BAYC NFT ID 7924
fetchTransfersForTokenId(7924);