Get timestamp of latest block

Hi guys! I’m building a time-sensitive smart contract with sophia and currently I’m writing unit tests for it. In short - I need to get the unix timestamp of the latest block. With the JS SDK I was only able to get the block height, is there a way to calculate the lates blocks’ timestamp from it?

In Ethereum, we have:

const getTimestamp = async txHash => {
const tx = await web3.eth.getTransaction(txHash);
const blockNum = tx.blockNumber;
const blockInfo = await web3.eth.getBlock(blockNum);
currentTimestamp = blockInfo.timestamp;
return currentTimestamp;

Is there any sort of analog in AE?

Help would be much appreciated.


@philipp.chain any ideas? :grin:

Sorry to be witty here, but what have you tried out so far?

(Hint: get the top block and look at the timestamp attribute)

1 Like

To get specific block timestamp by tx hash you can query a middleware instance or any known node with the transaction hash at this endpoint:


You’ll get back a json object, from which you can get the block number which the tx was included in:

    "block_height": 193952,
    "block_hash": "mh_2BXn9dpuKKfMcn5me5Vwx4mb1DtJ38rjK8At2mqQHmqhxpPyWQ",
    "hash": "th_2BGm5bbsvum3hDuVP7RvLkdYLbp4aAQZT6qDjq1Ah8bB43Djzu",
    "signatures": ["sg_SgxLS3D8YbM1ZYQCPT5nZcFX7CS9xGv9Vydh95BQkhmyVQhLe2QfHwXa9dtgymEFX6jXUwR16XAgxDFy6DMp73YswNXee"],
    "tx": {
        "amount": 6e+22,
        "fee": 16860000000000,
        "nonce": 18,
        "payload": "ba_Xfbg4g==",
        "recipient_id": "ak_Tmwf23kcVVXvJwb79G68wBtyEL9iTSGQRzZz4DJxwTxJWbM1",
        "sender_id": "ak_3jgongnfUibKiXBUVCtwXMWbM8hzifRm6C1E4Fp3mKbAT7ZQ9",
        "type": "SpendTx",
        "version": 1

After getting the block_height field you can query again the node at /v2/key-blocks/height/{block_height} to get the timestamp of this key-block:

    "beneficiary": "ak_nv5B93FPzRHrGNmMdTDfGdd5xGZvep3MVSpJqzcQmMp59bBCv",
    "hash": "kh_dUa6zZYwZj3yxSoF3PkuJXTqiPFKu8z3tPF3R1nmksJ993Tog",
    "height": 193952,
    "info": "cb_AAAAAj0XPPM=",
    "miner": "ak_2jAhFrNdd5RAFd7XguSs9my5DbXUBNCm9mfUGZWmcBo4z3quiR",
    "nonce": 7995623650721608000,
    "pow": [ ... ],
    "prev_hash": "mh_yV6aUhubotT4iuVA1XqwYoVfCBAQdKKw2HiQML95QT3WX5fDn",
    "prev_key_hash": "kh_ZZqnCFQ9Y7e9iGzMrh4udNdVNhXDvggpZ8HyMFLufLhjCX6Rn",
    "state_hash": "bs_F1GcGpEQpn3pWTo1t58LFCMdETwyMCdaZGZ9FfTYc9TqJGFAX",
    "target": 504641518,
    "time": 1578378213434,
    "version": 4

Or as @philipp.chain suggested by the time I wrote this you can also query the /v2/key-blocks/current endpoint to get the latest block and get the time.


1 Like

Thanks a lot guys, turns out it’s even simpler than for ethereum

1 Like

@philipp.chain @milenradkov.chain

Real quick - now the problem is it returns a completely different number than the Chain.timestamp, which is used in the contract for checking the time.

For example, I ran both just now and Chain.timestamp returns 1578402773881, while querying the endpoint returns 1578402184357

Why is that? :sleepy:

One looks at the last micro-block and one looks at the last key-block?

1 Like

What is the endpoint for the micro blocks? :>

v2/blocks/top gives you the current top block - can be either a key-block or a micro-block.