0

is it possible to check for a contract if a new block is created without calling a function? Means an automatically getter. A Live-update from the chain.

"function () internal" maybe. Somthing like this.

uint public a;
 function (){
    a = block.number;
 }

Thanks.

2 Answers 2

1

There is no likely callback function when a new block created. You must constantly request to confirm that a new block generated. there are two ways, as follows:

Using contract

pragma solidity ^0.4.0;
contract Test {

    uint lastBlockNum;

    function hasNewBlock() constant returns (bool) {
        if (lastBlockNum < block.number) {
            return true;
        }
        return false;
    }

    function updateBlockNum() {
        lastBlockNum = block.number;
    }
}

you should constantly call hasNewBlock function, if returning true, then you must call updateBlockNum function. This way requires to deploy contract and updateBlockNum function would cost ether.

Ethereum Developer APIs From the official documentation:

eth_blockNumber
Returns the number of most recent block
https://api.etherscan.io/api?module=proxy&action=eth_blockNumber&apikey=YourApiKeyToken

So you can constantly request it to find a new block using javascript or python, etc.

Hope it helps~

2
  • Thanks so far. No, it´s not an awnser I receive with pleasure :) Sure, this would be the way and it costs a lot to check every block... How you would automize to execute this function?
    – kornholio
    Sep 13, 2017 at 15:02
  • you must call it in the outside. for example, a javascript loop. Sep 13, 2017 at 15:18
0

If you are talking from the perspective of :

A Live-udate from the chain.

Few things has to be kept in mind:

  1. Block Number:The transactions we make are included in the block being mined so before a Hash is given to the block we can find the height of the block by block.number

  2. As already told Hash number is given to the block at the end after it is mined by the miner so its not good to calculate it but if you run the command before your transaction is included in a block you might be wrong as the block may/may not include your transaction...could be.

1
  • Thanks so far. It´s not dealing with a transaction. The question deals about "just" ticking the number of the latest block automatically.
    – kornholio
    Sep 13, 2017 at 12:45

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.