curl --request POST \
--url https://nd-418-459-126.p2pify.com/8763cb5a211e1d4345acd51bde484c00/ext/bc/C/rpc \
--header 'Content-Type: application/json' \
--data '
{
"jsonrpc": "2.0",
"method": "eth_newPendingTransactionFilter",
"params": [],
"id": 1
}
'{
"jsonrpc": "<string>",
"id": 123,
"result": [
"<string>"
]
}curl --request POST \
--url https://nd-418-459-126.p2pify.com/8763cb5a211e1d4345acd51bde484c00/ext/bc/C/rpc \
--header 'Content-Type: application/json' \
--data '
{
"jsonrpc": "2.0",
"method": "eth_newPendingTransactionFilter",
"params": [],
"id": 1
}
'{
"jsonrpc": "<string>",
"id": 123,
"result": [
"<string>"
]
}eth_newBlockFilter method is useful for developers who must be notified of new blocks on the blockchain in real time.
noneresult — a hexadecimal string representing the ID of the newly created filtereth_newPendingTransactionFilter code examplesweb3.eth.filter methods have been deprecated and replaced with the web3.eth.subscribe in web3.js. See web3.js subscriptions.const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);
const createFilter = async () => {
try {
const filterId = await provider.send('eth_newPendingTransactionFilter', []);
console.log(filterId); // the filter ID returned by eth_newFilter
return filterId
} catch (error) {
console.log(error);
}
};
createFilter();
eth_newPendingTransactionFilter method is to listen for new pending transactions at predefined intervals and extract specific data from them. For instance, a decentralized application might check for pending transactions every second and identify those that transfer a value greater than a certain amount of the MATIC token. This could be useful for monitoring high-value transactions or detecting potential fraud or security threats in real time.
Here is an implementation of this concept using ethers.js:
const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);
// Create a filter using eth_newPendingTransactionFilter
const createFilter = async () => {
try {
const filterId = await provider.send('eth_newPendingTransactionFilter', []);
console.log(filterId); // the filter ID returned by eth_newFilter
return filterId
} catch (error) {
console.log(error);
}
};
// Use the filter to extract the value from each transaction
async function getValue(filter) {
try {
// Retrieve the list of new pending transactions
const transactions = await provider.send('eth_getFilterChanges', [filter]);
// Loop through the list of transactions and process each one
for (const hash of transactions) {
const receipt = await provider.send("eth_getTransactionByHash", [hash]);
// Check that the receipt is not null and has a non-null value field
if (receipt && receipt.value != null) {
const value = receipt.value;
const decimalValue = BigInt(value).toString();
const convertedValue = ethers.utils.formatEther(decimalValue);
// Check if the transferred value is greater than or equal to 100 AVAX
if (convertedValue >= 100) {
console.log(`This transaction is sending more than 100 AVAX`);
console.log(`Transaction Hash: ${hash}`);
console.log(`Value transferred: ${convertedValue} \n`);
}
}
}
} catch (error) {
console.error(error); // Handle errors that may occur
}
}
// Main program setting an interval to call the `getValue` function at regular intervals
async function main() {
const filterId = await createFilter();
setInterval(getValue, 1000, filterId);
}
main()
createFilter, getValue, and main. Here’s an overview of how each function works:
The createFilter function sets up a new filter using the eth_newPendingTransactionFilter method. It returns the ID of the filter, which can be used to retrieve data from the filter later.
The getValue function retrieves the list of new pending transactions using the eth_getFilterChanges method and loops through each transaction. For each transaction, it retrieves the transaction data using the eth_getTransactionByHash method and checks if the transaction value is greater than or equal to 100 MATIC. If the value is above this threshold, the function logs information about the transaction to the console.
The main function sets up the program by first calling the createFilter function to create a new filter and retrieve its ID. It then sets an interval to call the getValue function every 1 second, passing the filter ID as an argument to retrieve the latest transaction data at regular intervals.Was this page helpful?