2

I want to get the block number based on the transactionhash from filter,however, when I add the following codes to my project,I found it strange because sometimes it automatically transfer the money twice and sometimes I have to transfer a second time in order to see it returning the transaction hash.

besides,I can't get the block number back in the filter. What's wrong ?

function TransferTest() {
    alert("START TO TRANSFER")
    var Web3 = require('web3');
    var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
    var abiArray=[
        {"constant":true,"inputs":[],"name":"minter","outputs":
            [{"name":"","type":"address"}],"payable":false,"type":"function"},
        {"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balances","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},
        {"constant":false,"inputs":[{"name":"receiver","type":"address"},{"name":"amount","type":"uint256"}],"name":"mint","outputs":[],"payable":false,"type":"function"},
        {"constant":false,"inputs":[{"name":"receiver","type":"address"},{"name":"amount","type":"uint256"}],"name":"send","outputs":[],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"from","type":"address"},{"indexed":false,"name":"to","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Sent","type":"event"}
    ];
    var object=web3.eth.getTransactionReceipt("0x327fa3ecb7f9312119201995714c6c94a92accfa1643810b2bd3ee6c4f3a038e");
    var conAddress=object.contractAddress;
    var Mycontract=web3.eth.contract(abiArray);
    var MyConIns=Mycontract.at(conAddress);
    var uAddress=document.getElementById("uAddress").value;
    var tarAddress=document.getElementById("tarAddress").value;
    var AKamount=document.getElementById("AKamount").value;
    var success=web3.personal.unlockAccount(uAddress,"carochan123");
    alert(success);
    MyConIns.send(tarAddress,AKamount,{from:uAddress});var filter=web3.eth.filter("pending");
    filter.watch(function(error, result){
        if (!error)
            alert("monitor");
            alert(result);
        var block = web3.eth.getBlock(result, true);
        alert(block.number);
    });

    alert(MyConIns.balances(tarAddress));//50,150,170,260,270,300,320,330,360,370,380,390,410,420
    alert(MyConIns.balances(uAddress));
}
  • What is alert()? Is that the correct account password? – Noel Maersk Apr 8 '17 at 22:10
2

Partial answer: it may be that you can't get a block number with

var block = web3.eth.getBlock(result, true);
alert(block.number);

because the block is "pending", in which case its number will be null (see web3.eth.getBlock return value description).

0

First, to avoid missing your transaction, it is better to intiate your filter watch before running the transaction.

Also, for thegetBlock() you might want to make it asynchronous, to ensure it has a value returned.

Hope this helps.

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.