1

I am using "web3": "^1.5.1", and "web3-eth-contract": "^1.5.2".

I have the following solidity function in my contract:

    function mint(string memory _linkURL) public payable returns (uint) {
        uint256 supply = totalSupply();
        uint256 tokenID = supply + 1;

        if (msg.sender != owner()) {
            require(msg.value >= cost);
        }

        emit Logging(_linkURL);
        setLinkURL(_linkURL);

        _safeMint(msg.sender, tokenID);

        return tokenID;
    }

As you can see I am writing an url to the function before minting the token. In my app I do this the following way:

    const claimNFTs = () => {
        let cost = CONFIG.WEI_COST; // how much does the rNFT service cost
        let gasLimit = CONFIG.GAS_LIMIT;
        let totalCostWei = String(cost * mintAmount);
        let totalGasLimit = String(gasLimit * mintAmount);
        let currentUrl = window.location.href;
        console.log("Current URL: " + currentUrl);
        console.log("Cost: ", totalCostWei);
        console.log("Gas limit: ", totalGasLimit);
        setFeedback(`Minting your ${CONFIG.NFT_NAME}...`);
        setClaimingNft(true);
        blockchain.smartContract.methods
            .mint(currentUrl)
            .send({
                gasLimit: String(totalGasLimit),
                to: CONFIG.CONTRACT_ADDRESS,
                from: blockchain.account,
                value: totalCostWei,
            })
            .once("error", (err) => {
                console.log(err);
                setFeedback("Sorry, something went wrong please try again later.");
                setClaimingNft(false);
            })
            .then((receipt) => {
                console.log(receipt);
                setFeedback(
                    `WOW, the ${CONFIG.NFT_NAME} is yours! go visit Opensea.io to view it.`
                );
                setClaimingNft(false);
                dispatch(fetchData(blockchain.account));
            });
    };

Any suggestions how to get the tokenID as a return value from the contract? Is there another parameter that I should get back via the promise?

I appreciate your replies!

2
  • 2
    You can listen to this specific event for this specific user
    – Adam Boudj
    Mar 27, 2022 at 12:32
  • @AdamBoudjemaa Thx for your reply! How would this look like? Please add this as an answer so I can accept it.
    – Carol.Kar
    Mar 27, 2022 at 12:38

1 Answer 1

0

I'm missing some details, like which events are emitted, but in a nutshell it should look like this. First of all, you need set your filter to get the event for your specific event.

let options = {
    filter: {
        (your indexed params),
        receiver: 0xabc... (example)

    },
    fromBlock: 0 (better to start from the block where you've deploy your contract, better to have a `rpc node` with `archive mode`)
};


smartContract.events.Mint(options)
    .on('data', event => console.log(event))
    .on('changed', changed => console.log(changed))
    .on('error', err => throw err)
    .on('connected', str => console.log(str))

similar questions: Web3.js Script exits while listening to CreatedPairs

Default options for getPastEvents in web3js

web3 doc for events: https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#id36

https://web3js.readthedocs.io/en/v1.2.11/web3-eth-subscribe.html

2
  • Thx. The event is called Logging
    – Carol.Kar
    Mar 27, 2022 at 17:16
  • ok replace Mint with Logging, and add all the required indexed filters
    – Adam Boudj
    Mar 27, 2022 at 17:37

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.