FYI, I'm a newbie. My smart contract method is below called buyToken. I'm using a Javascript code with web3 to execute the method.

/// Requires the amount of Ether be at least or more of the currentPrice
  /// @dev Creates an instance of an token and mints it to the purchaser
  /// @param _type The token type as an integer
  /// @param _title The short title of the token
  /// @param _description Description of the token
  function buyToken (
    uint256 _type,
    string _title,
    string _description
  ) external payable {
    bytes memory _titleBytes = bytes(_title);
    require(_titleBytes.length >= TITLE_MIN_LENGTH, "Title is too short");
    require(_titleBytes.length <= TITLE_MAX_LENGTH, "Title is too long");

    bytes memory _descriptionBytes = bytes(_description);
    require(_descriptionBytes.length >= DESCRIPTION_MIN_LENGTH, "Description is too short");
    require(_descriptionBytes.length <= DESCRIPTION_MAX_LENGTH, "Description is too long");
    require(msg.value >= currentPrice, "Amount of Ether sent too small");

    uint256 index = allTokens.length + 1;

    _mint(msg.sender, index);

    tokenTypes[index] = _type;
    tokenTitles[index] = _title;
    tokenDescription[index] = _description;

    emit BoughtToken(msg.sender, index);

The return values I received are as below where it shows "tokenId": "3" which is shown my Remix IDE. This is the value that I wish retrieve in my javascript code. How do I grab it?

.... ...

        "from": "0x580938fa8d19d39771893ac9e9c28cb3a1d399c4",
        "topic": "0x75424253909c2f4460f8a59099700e980f5b484608c4fdd79f600f5162ac88a5",
        "event": "BoughtToken",
        "args": {
            "0": "0x77b8042e431fF76439721738237DAFD78199ac60",
            "1": "3",
            "buyer": "0x77b8042e431fF76439721738237DAFD78199ac60",
            "tokenId": "3",
            "length": 2

Here are some of my Javascript codes that call the buyToken method.

const query = smartContract.methods.buyToken(type, title, desc);
const encodedABI = query.encodeABI();
const tx = {
  from: walletAddress,
  to: contractAddress,
  gas: 2000000,
  data: encodedABI,

const account = web3.eth.accounts.privateKeyToAccount(privateKey);

web3.eth.accounts.signTransaction(tx, privateKey).then(signed => {
  const tran = web3.eth
    .on('confirmation', (confirmationNumber, receipt) => {
      console.log('=> confirmation: ' + confirmationNumber);
    .on('transactionHash', hash => {
      console.log('=> hash');
    .on('receipt', receipt => {
      console.log('=> reciept');
    .on('error', console.error);
  • you can get it in receipt.logs[0].args.tokenId – Iftifar Taz May 10 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.