1

I'm playing around with ERC721 tokens.

I have this mint function in my contract that is supposed to return a value, but in my client app (using Web3), the result I get from this function call only contains the transaction id.

Here is the function :

    function mint() public returns (uint) {
        uint newId = currentTokenId + 1;
        _mint(msg.sender, newId);
        currentTokenId = newId;
        return currentTokenId;
    }

The point of this is just making an interface to issue/mint new tokens out of this smart contract. Here is how I call it in my client app :

this.contract.methods.mint().send({from: this.account}, function(error, result) {
    console.log(result) // 0x08e...00d0
})

Am I missing something? Am I misusing the Web3 framework? Or maybe I just misunderstood the return statement in Solidity?

1 Answer 1

3

Turns out I can only get the transaction hash for the _mint call, but I was able to recover the data (currentTokenId) by checking the transaction event by it's hash.

I'm not sure it's the best solution but here is how I've done it :

this.mint = function(callback) {
    var mintTxHash = null
    this.contract.methods.mint().send({from: this.account}, function(error, result) {
        mintTxHash = result
    })
    this.contract.events.allEvents((error, event) => {
        if (event != null && mintTxHash != null) {
            if (event.transactionHash == mintTxHash) {
                callback(event.returnValues.tokenId)
            }
        }
    })
}

Not sure it will help anyone but ¯_(ツ)_/¯ If you have a better solution, please don't hesitate to post!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.