0

Can I write if statements in geth console - for example if I would like to check that allowance for a ERC20 token is greater than the amount to be transferred , then execute the transferFrom else not or do I have to load and execute a js file for this?

1 Answer 1

0

You can run javascript in the geth console, even stupid stuff like this

setInterval(function() {console.log(eth.blockNumber)}, 1000)

Another example having global variables and a bit more complex logic

n = 0;

setInterval(
    function () {
        newBlockNumber = eth.syncing.currentBlock;

        if (n == newBlockNumber) {
            return;
        }

        n = newBlockNumber;

        if (n % 2 == 0) {
            console.log(n, "is even");
        } else {
            console.log(n, "is odd");
        }
    },
    1000
)

While it is updating will output

3837231 is odd
3838010 is even
3838298 is even
3838609 is odd
3839011 is odd
3839292 is even
3839970 is even
3840727 is odd
3842836 is even
3843830 is even
3845195 is odd
3845628 is even
3846997 is odd
3848227 is odd
3848658 is even
3850673 is odd
2
  • What about a if statement?
    – Webdev
    Apr 25, 2018 at 11:15
  • I've updated the answer with a more complex example Apr 25, 2018 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.