Ethereum WaitonReceive函数不使用';在超级积木上运行时,不能稳定工作

Ethereum WaitonReceive函数不使用';在超级积木上运行时,不能稳定工作,ethereum,solidity,Ethereum,Solidity,继续努力在Solidity中制作一个有趣的游戏。当我在“addKity”函数中调用waitforreceipt时,就会出现问题。你知道为什么吗?我怀疑这可能是因为超级块的技术问题,但很可能是因为我的一个编码错误 达到“heres”警报,但未达到警报(“4”) 请让我知道你的想法 waitForReceipt(txHash, function(receipt){ Javascript文件 (function (Contract) { var web3_instance; var

继续努力在Solidity中制作一个有趣的游戏。当我在“addKity”函数中调用waitforreceipt时,就会出现问题。你知道为什么吗?我怀疑这可能是因为超级块的技术问题,但很可能是因为我的一个编码错误

达到“heres”警报,但未达到警报(“4”)

请让我知道你的想法

waitForReceipt(txHash, function(receipt){
Javascript文件

(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']);
坚固锉刀

    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;
}




}

首先,调用
waitforreceive
函数传递未定义的值
txHash
。在这种情况下,它应该是
result
(该值来自激活该调用的回调,由
sendTransaction
触发)。
该更改将使
waitforreceive
呼叫工作

另一个问题是,将
web3
变量命名为
web3\u instance
,但仍将其称为
web3
。 考虑到在
waitforreceive
中,您仍然将其引用为
web3.eth…
,而在该上下文中正确的调用应该是:
web3\u instance.eth.getTransactionReceive…


有了这个修正,
警报(“4”)
将按预期调用。

完美!非常感谢你。我仍然是一个初学者,虽然现在我遇到了另一个问题,收据返回空。请看这里的问题