1

Is there any way to obtain the time when you have received a transaction? I would like to know when is a Smart contract included in my node. (local testnet)

2
  • 1
    Hi there. Do you mean when a transaction arrives in your node's transaction pool (i.e. before the transaction is part of a block), or when your node syncs a block containing a particular transaction (i.e. after the transaction is part of a block)? Commented Oct 14, 2018 at 20:01
  • I mean the second case, although the first one could be also useful to know. thanks Commented Oct 14, 2018 at 20:09

1 Answer 1

1

When blocks get added to you local blockchain, they contain a bunch of metadata include the block timestamp, which is the time when the block got mined including all of the transactions in that block.

Here is the latest block at the time of writing this post:

https://etherscan.io/block/6521553

Height: 6521553  
TimeStamp: 56 secs ago (Oct-15-2018 07:55:37 PM +UTC)       <--- here
Transactions: 104 transactions and 23 contract Internal Transactions in this Block
Hash: 0x6995e8a668ddc3743e806e99a3c860b8a57d4a7c8de47664489e0c39749069bb
...

However, that timestamp does not necessarily reflect the time that all nodes became aware of that block, since it takes time to propagate that information across the network. It simply says when the block creator claims to have created that block.

Furthermore, the block timestamp is not a perfect science since it is a claim from the block creator, and can be adjusted by the block creator within some rules. See here:

Can a contract safely rely on block.timestamp?

You can access the block timestamp really easily using any Web3 API like the one provided by Web3.js:

getBlock

web3.eth.getBlock(blockHashOrBlockNumber [, returnTransactionObjects] [, callback])

For example:

web3.eth.getBlock(6521553).then(console.log);
{
    difficulty: "3220937666879710"   ​
    extraData: "0x66726131"  ​
    gasLimit: 8000029  ​
    gasUsed: 7982786
    hash: "0x6995e8a668ddc3743e806e99a3c860b8a57d4a7c8de47664489e0c39749069bb"
    logsBloom: "0x420542890060082225300245402122f1002aa0004221201100005100566155ea0d8210b04c821040472440169190308898314a00010881a6880891464f2e0ce02188cd40200669c908b0060ac8002480d824080cd03048524a5461080680606a0609d09302495802e460259814040c41282801600c5c00020058201599486031206482383cc2007034420ac0848020083810222c055000862a8111212a4060008a0c0b5242671860864480c12411e531141004080404803842018004d20100280081832251959a9619126c07400800d68c196805250a2087ec1cd000c0512000021031019212a800a009a0040818ec22a0402e86120a05134d7c87180d94e204"
    miner: "0xb2930B35844a230f00E51431aCAe96Fe543a0347"
    mixHash: "0x31f52f83ea88a0201aa44e3e70abe03d097fbe2fed54d9b32bc268194b0b6ae3"
    nonce: "0x297eb26407ff9ca4"
    number: 6521553
    parentHash: "0x8516a9c81830a6b8d953083af31aad2497f33108d5df184b20098003103e56d8"
    receiptsRoot: "0x4c95c9233bcc119ab84bf347a371229b516aacb13aa55bdbf785dfb635e0a719"
    sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
    size: 21086
    stateRoot: "0xcfd2357bcdc9497cf7289e45fd142860ecf933becb7b2060f70a1bf00a885ae0"
    timestamp: 1539633337           <--------- here
    totalDifficulty: "7243332583953312312387"
    transactions: Array(104) [...]
    transactionsRoot: "0x1e2248a45cf7e0f3500781a8293eadbaf12235dd5dc78b4f00b87b2559e75c58"
    uncles: Array []
}

You can then turn this into a human readable time using the JavaScript Date() function:

Date(1539633337 * 1000)

"Mon Oct 15 2018 13:08:39 GMT-0700 (Pacific Daylight Time)"

4
  • Thanks, and is there any way to obtain when this block was added to my node? Not just when it was created @Shawn Tabrizi Commented Oct 15, 2018 at 20:16
  • You might be able to look at the logs for your Ethereum client to see approximately when the block got synced to your node, but you might need to elaborate more on how you are going to use this data to get a better answer. Commented Oct 15, 2018 at 20:21
  • I am using Geth, I guess is the "elapsed" argument the time to sync the block. But I would like to obtain the time from a smart contract was migrated to the network to it is added in my node or, in other words, the time that this tx takes from it is sent to it is received. Commented Oct 15, 2018 at 20:45
  • You may be able to use Web3.js 1.0 and WebSockets to get notified when your node gets a new block: subscribe(“newBlockHeaders”), however I feel as though this entire train of thought is flawed. You need to think about Ethereum as a distributed network. What does it mean to time your specific node? You might be interested in creating an experience which triggers when a transaction completes, but you should use something like events. Commented Oct 15, 2018 at 20:59

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.