Get current timestamp

There is a helper that does wallet.get_provider().latest_bloick_height . Is there nothing like that for timestamp?

During the office hours I was presented with two ways, none of which work:

let pagination = PaginationRequest {
        cursor: None,
        results: 1,
        direction: PageDirection::Backward,
    };
let blocks: Vec<Block> = wallet.get_provider().unwrap().get_blocks(pagination).await?.results;
let time = blocks[0].header.time;

This says no field "results" on type Result<PaginatedResult<fuels::types::block::Block, std::string::String>, fuels::signers::provider::ProviderError>

wallet.get_provider().chain_info().await.unwrap().latest_block.header.time.unwrap();

This says panicked at called Option::unwrap() on a None value

2 Likes

IIRC, chain_info() returns a Result and time is an Option, so it looks like it’s the unwrap on the latter that is causing this, suggesting that the time is None.

Have you tried logging out latest_block or header to see what its properties are?

My guess is maybe you’re trying to use this before any blocks have been produced (i.e. before you have sent any transactions, if using a local node).

Nit: time isn’t an Option, it is strictly a Tai64Timestamp. Here’s how I made it work (using fuels-rs latest):

I’m pasting an image so you can see the type hints next to each return.

The output is, in this case, Tai64Timestamp(Tai64(4611686018427387914)).

2 Likes

@Simon I had deployed a contract and logged block_height which was 1.

@digorithm sure but that still doesn’t explain why time is None even though the block height is 1. I even tried to run fuel node locally via fuel-core run --db-type in-memory

Yeah… I’m gonna need a more reproducible piece of code so I can further debug this. I can’t seem to reproduce this locally; time is never None for me.

Sure - appreciate you debugging with me!

use fuels::{
    prelude::*, 
    client::{PageDirection, PaginationRequest},
    types::{block::Block, message::Message},
    tx::ContractId};

// Load abi from json
abigen!(MyContract, "out/debug/counter_contract-abi.json");

struct Fixture {
    wallet: WalletUnlocked,
    contract_instance: MyContract,
    contract_id: ContractId,
    more_wallets: Vec<WalletUnlocked>,
}

pub async fn get_wallets() -> Vec<WalletUnlocked> {
    let config = Config {
        manual_blocks_enabled: true, // Necessary so the `produce_blocks` API can be used locally
        ..Config::local_node()
    };
    let num_wallets = 3;
    let num_coins = 1;
    let initial_amount = 10_000_000_000_000;
    let wallets_config = WalletsConfig::new(Some(num_wallets), Some(num_coins), Some(initial_amount));

    let wallets = launch_custom_provider_and_get_wallets(wallets_config, Some(config), None).await;

    wallets
}

async fn setup() -> Fixture {
    let wallets = get_wallets().await;
    let wallet = wallets.get(0).unwrap().clone();
    
    let id = Contract::deploy(
        "./out/debug/counter_contract.bin",
        &wallet,
        TxParameters::default(),
        StorageConfiguration::with_storage_path(Some(
            "./out/debug/counter_contract-storage_slots.json".to_string(),
        )),
    )
    .await
    .unwrap();

    let instance = MyContract::new(id.clone(), wallet.clone());

    let block_header = wallet.get_provider().unwrap().chain_info().await.unwrap().latest_block.header;
    println!("{:?}", block_header.height);  // 1
    println!("{:?}", block_header.time);      // None```

Even when I run a local node I get something similar

This is because the timestamp of the genesis block is currently None (although this has been since changed and will make its way downstream soon). What you are observing is a time of None/0 is being returned because you are querying the time of the Genesis block, however you should be able to the correct time from the header of non-genesis blocks currently, I would recommend using the .block method on the client to select those blocks as well rather than chain_info

1 Like