Search code examples
ethereumsolidity

waitOnReceipt function doesn't work in solidity, running on SuperBlocks


Running on https://studio.superblocks.com/ trying to make a a fun game in Solidity. the problem happens when I call waitfor receipt inside the "addKity" function. Any ideas why? I suspect it could be because of a technical issue in superblocks but could very well be because of a coding error I have.

It reaches the "heres" alert but doesn't reach alert("4");

Please let me know your thoughts!

waitForReceipt(txHash, function(receipt){

Javascript File

(function (Contract) {
    var web3_instance;
    var instance;
    var accounts;

    function init(cb) {
        web3_instance = new Web3(
            (window.web3 && window.web3.currentProvider) ||
            new Web3.providers.HttpProvider(Contract.endpoint));

        accounts = web3.eth.accounts;


        var contract_interface = web3_instance.eth.contract(Contract.abi);


        instance = contract_interface.at(Contract.address);
        cb();
    }

function waitForReceipt(hash, cb) {
web3.eth.getTransactionReceipt(hash, function (err, receipt) {
    if (err) {
    error(err);
    }

    if (receipt !== null) {
    // Transaction went through
    if (cb) {
        cb(receipt);
    }
    } else {
    // Try again in 1 second
    window.setTimeout(function () {
        waitForReceipt(hash, cb);
    }, 1000);
    }
});
}





    function addKitty(){

    //  var KittyName = "he"; //$("#kittyName").val();
    //  var KittyPrice = 5;//parseInt($("#kittyPrice").val());


const transactionObject = {
from: "0x95c2332b26bb22153a689ae619d81a6c59e0a804",
gas: "1000000",
gasPrice: "1000000",
value:"1000"
};

    instance.addNewKitty.sendTransaction("sdad",5, transactionObject, (error, result) => { 

    if(error){
        alert(error);
        alert("2");
        }
        else{
            alert("heres");


            waitForReceipt(txHash, function(receipt){
                alert("4");
            //   if(receipt.status === "0x1"){

            //     alert("got receipt");
            //   }
            //   else{
            //       alert("5");
            //     alert("receipt status fail");
            //   }
            });
        }
    })

    }









    // function getMessage(cb) {
    //     instance.message(function (error, result) {
    //         cb(error, result);
    //     });
    // }

    $(document).ready(function () {

        init(function () {
            // getMessage(function (error, result) {
            //     if (error) {
            //         console.error("Could not get article:", error);
            //         return;
            //     }
            //     $('#message').append(result);
            // });
            $("#addKitty").click(function(){

            addKitty();
        })

        });
    });
})(Contracts['CryptoKitties']);

Solidity file

    pragma solidity ^0.4.21;


contract CryptoKitties  {

    address public owner;

    struct CKitties{
        string name;
        uint price;
    }


function CryptoKitties() public {
    owner = msg.sender;
}

modifier onlyOwner() {
    require(msg.sender == owner);
    _;
}




    mapping (address => uint) kittytoUser;  
    CKitties[] kitties;

    event NewCryptoKitty(address owner, string name, uint price); 

    modifier cost (uint minCost){
        require(msg.value>= minCost && minCost >=0);
        _;
    }

function addNewKitty(string newName, uint newPrice) public payable cost(5000) {
    address sender = msg.sender;
    uint place =  kitties.push(CKitties(newName,newPrice));
    kittytoUser[sender] = place;
    emit NewCryptoKitty(sender, newName, newPrice);

}

function kill() public  onlyOwner{
    selfdestruct(owner);
}



function getName() public view returns (string){
        address sender = msg.sender;
        uint index = kittytoUser[sender]-1;
        return kitties[index].name;
}

function setName(string newName) public{
    address sender = msg.sender;
        uint index = kittytoUser[sender]-1;
        kitties[index].name = newName;
}

function getBalance() public view returns (uint){
    return address(this).balance;
}




}

Solution

  • First, the waitForReceipt function is getting called passing an undefined value txHash. In that context, it should be result instead (the value coming from the callback that activated that call, triggered by sendTransaction).
    That change will make waitForReceipt call work.

    The other problem is that you have the web3 variable named as web3_instance, but you still refer to it as web3. Considering that, inside waitForReceipt, you are still referencing to it as web3.eth..., while the correct call in that context would be: web3_instance.eth.getTransactionReceipt....
    With that fixed, alert("4") will get called as intended.