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));
}
陈明艳
  • 41
  • 2

2 Answers2

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).

Noel Maersk
  • 586
  • 3
  • 15
  • useful to know the getBlock function. I have a question of my own in regards to filters and the .get() function, would you happen to know the answer? https://ethereum.stackexchange.com/questions/22954/mistake-when-using-web3-eth-filter-and-filter-get – Webeng Jul 24 '17 at 22:40
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.

Adibas03
  • 388
  • 2
  • 7