Search code examples
rustblockchainunix-timestampsolana

How can a Solana Rust smart contract get block height or Unix time?


I know how to pass the current Unix time from the frontend:

web3.js:

anchor.web3.SYSVAR_CLOCK_PUBKEY

Rust:

let current_time = ctx.accounts.clock.unix_timestamp;

I do not want that. I need the smart contract itself to get the current time.


Solution

  • I'm assuming you're using anchor

    let now_ts = Clock::get().unwrap().unix_timestamp;
    

    You will need to pass in the system program account