【问题标题】:waitOnReceipt function doesn't work in solidity, running on SuperBlockswaitOnReceipt 函数在 SuperBlocks 上运行时无法稳定运行
【发布时间】:2018-08-12 10:21:50
【问题描述】:

https://studio.superblocks.com/ 上运行,试图在 Solidity 中制作一款有趣的游戏。当我在“addKity”函数中调用 waitfor 收据时,就会出现问题。任何想法为什么?我怀疑这可能是因为超级块中的技术问题,但很可能是因为我的编码错误。

到达“here”警报但未到达 alert("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;
}




}

【问题讨论】:

    标签: ethereum solidity


    【解决方案1】:

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

    另一个问题是您有一个名为web3_instanceweb3 变量,但您仍将其称为web3。 考虑到在waitForReceipt 内部,您仍然将其称为web3.eth...,而在该上下文中正确的调用是:web3_instance.eth.getTransactionReceipt...
    修复后,alert("4") 将按预期被调用。

    【讨论】:

    猜你喜欢
    • 2015-07-01
    • 2022-12-19
    • 2019-03-10
    • 1970-01-01
    • 2022-06-15
    • 2023-01-17
    • 2014-06-28
    • 2016-11-05
    • 1970-01-01
    相关资源
    最近更新 更多