1

I have smart contract server function and that function including emit and event.

event TokenMinted(
        address indexed _recipient, 
        uint256 indexed _tokenId
        );

 function mintToken(address to, address[] memory bundle) public {
        // Mint new token
        uint256 tokenId = _tokenIdCounter.current();
        .........
        // Emit TokenMinted event
        emit TokenMinted(to, tokenId);
        // Increase the token counter
        _tokenIdCounter.increment();
    }

Not I'm going to call this function and hope to get tokenId.

But In my React frontend code, I can't find method to get tokenId.

export const mintToken = async (contract, to, address) => {
  let tokenId = await contract.mintToken(to, address);
  console.log('contracts', contract)
  // console.log('contract.events', contract.filters.TokenMinted())
  //get the latest blocknumber

  return tokenId;
}

The contract generated like this.

new ethers.Contract(ROCI_MINTER_ADDR, ROCI_MINTER_ABI, library.getSigner());

How can I get this value correctly? Thanks for attention.

1
  • I would recommend adding a getBalance view function which returns an array of ids that the caller owns. Then you can use this function to see if there are any changes.
    – trizin
    Commented Sep 16, 2021 at 21:43

2 Answers 2

0

that is not neither a view or a pure function, so you probably are looking for something like this (check here for documentation):

let tx = await contract.mintToken(to, address).send({from: '0x123123123123'});

let events = tx.receipt.events
console.log(events)
3
  • But I got error 'not found send function'. Commented Sep 16, 2021 at 22:50
  • @BackendandFrontendDeveloper are you using the basic web3.js library?
    – anon
    Commented Sep 16, 2021 at 23:25
  • "web3-react": "^5.0.5" "@web3-react/core": "^6.1.9", "@web3-react/injected-connector": "^6.0.7", This is my package.json file Commented Sep 16, 2021 at 23:32
0

The latest docs says to do the following:

contract.events.TokenMinted().on('data', this.contractEventHandler )

it depends on the version of web3 you have. Here are the docs. https://web3js.readthedocs.io/en/v1.5.2/web3-eth-contract.html#id50

Note; I was having errors when I tried to locally copy and manipulate a react state variable in the contractEventHandler, however, doing the following worked for me setState({tokens:[...this.state.tokens,token]});

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.