1

I am trying to program an erc20 event listener using Rust programming language and trying to get events from a particular block to the latest block. But even though I have specified from the block it just gives an output from the latest block, not from the specified block. Below is my code:

use hex_literal::hex;
use std::time;
use web3::{
    contract::{Contract, Options},
    futures::{future, StreamExt},
    types::{FilterBuilder, Address},
};
use std::str::FromStr;
use web3::types::{BlockNumber, U64};

#[tokio::main]
async fn main() -> web3::contract::Result<()> {
    let web3 = web3::Web3::new(web3::transports::WebSocket::new("wss:").await?);


    let filter = FilterBuilder::default()
        // this is BSC: TokenHub
        .from_block(BlockNumber::Number(U64::from(15255440)))
        .address(vec![Address::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").unwrap()])


       /* .topics(
            // this is 'Transfer (index_topic_1 address from, index_topic_2 address to, uint256 value)' event
            // use https://emn178.github.io/online-tools/keccak_256.html, and type in 'Transfer(address,address,uint256)'
            // it will return the resulting hash as used in the next line
            Some(vec![hex!("d282f389399565f3671145f5916e51652b60eee8e5c759293a2f5771b8ddfd2e").into()]),
            None,
            None,
            None,
        )*/
        .build();

    let sub = web3.eth_subscribe().subscribe_logs(filter).await?;

    sub.for_each(|log| {
        println!("{:?}", log);
        future::ready(())
    }).await;



    Ok(())
}

Output is like:

Ok(Log { address: 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2, topics: [0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef, 0x000000000000000000000000007933790a4f00000099e9001629d9fe7775b800, 0x000000000000000000000000938625591adb4e865b882377e2c965f9f9b85e34], 
data: Bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 65, 133, 173, 92, 182, 64, 0]), 
block_hash: Some(0xa076bddbfd69560de59c9be11cf5ae6f8782ace08d96c4945f3d15d05e91100f), block_number: Some(15256705), 
transaction_hash:Some(0xdd603b88c60e5b5a1aaebedb53d38df626b72d6ec2a11530cf50a8957082f404), transaction_index: Some(0), 
log_index: Some(0), 
transaction_log_index: None, 
log_type: None, 
removed: Some(false) })

Ok(Log { address: 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2, topics: [0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef, 0x0000000000000000000000002dfa8a0413255ecca71
3b6d1c1e28e634e021478, 0x000000000000000000000000938625591adb4e865b882377e2c965f9f9b85e34], 
data: Bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 76,81, 115, 63, 131, 0, 0]), 
block_hash: Some(0xa076bddbfd69560de59c9be11cf5ae6f8782ace08d96c4945f3d15d05e91100f), block_number: Some(15256705), 
transaction_hash:Some(0xcf4b91c9c465e8c2b22126ad580e92988162bf51be7520e5cc6dfe9f878faa06), transaction_index: Some(1), 
log_index: Some(4), 
transaction_log_index: None, 
log_type: None, 
removed: Some(false) })

Ok(Log { address: 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2, 
topics: [0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef, 0x000000000000000000000000938625591adb4e865b882377e2c965f9f9b85e34, 0x000000000000000000000000007933790a4f00000099e9001629d9fe7775b800], 
data: Bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 170, 83, 197, 89, 150, 0, 0]), 
block_hash: Some(0xa076bddbfd69560de59c9be11cf5ae6f8782ace08d96c4945f3d15d05e91100f), block_number: Some(15256705), 
transaction_hash:Some(0xd45f5b511cf832b407182751c451c32b27a203d8ab60136f9719cf61523b0e9a), transaction_index: Some(2), 
log_index: Some(9), 
transaction_log_index: None, 
log_type: None, removed: Some(false) })

Ok(Log { address: 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2, 
topics: [0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef, 0x000000000000000000000000000000000035b5e5ad9019092c665357240f594e, 0x0000000000000000000000002f8ac927aa94293461c75406e90ec0ccfb2748d9], 
data: Bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 90, 
119, 187, 148, 242, 21, 156]), 
block_hash: Some(0xa076bddbfd69560de59c9be11cf5ae6f8782ace08d96c4945f3d15d05e91100f), block_number: Some(15256705), 
transaction_hash: Some(0xd36df58ffe146202ffb9ef2e29e64380991a674c820bb8a1f6a776b9468baa86), transaction_index: Some(3), 
log_index: Some(12), 
transaction_log_index: None, 
log_type: None, 
removed: Some(false) })

1 Answer 1

0

I got to the conclusion that the above code gives the output of the event that will happen. So when we add from_block: 15255440, the event has already happened. And in that particular block, no other event is going to happen. So you will get the output from the latest block. To test this in code add this:

let filter = FilterBuilder::default()
        // this is BSC: TokenHub
        .from_block(BlockNumber::Number(U64::from(15257125)))
        .to_block(BlockNumber::Number(U64::from(15257130)))
        .address(vec![Address::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").unwrap()])
.build();

Keep in mind to replace 15257125(i.e from_block) and 15257130(i.e. to_block) with the future block which has not been mined yet. And when that block will be mined you will get events between that two blocks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.