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).
@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
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