reactjsblockchainsolidityweb3jserc721

Error while related to big number in react for input in smart contract erc721


I want to integrate my smart contract with reactjs. I have this method called set_price() where i enter the value of token ID (in uint256) and price (uint256) . TokenID is basically a hash of some other values so it was of type byte32 and i have typecasted it into uint256. Now that when i enter the value from react it shows the following error: enter image description here

I have also added this line of code where I am trying to convert into big number enter image description here

and now i get this error: enter image description here

I am also adding my file where I have instantiated web3. The method that I am trying to solve is set_price.

import Web3 from 'web3';
import TokenContract from './token.json';
let selectedAccount;
let tokenid;
let tokenContract;
let isInitialized = false;
export const init = async () => {
  let provider = window.ethereum; //
  if (typeof provider !== 'undefined') {
    //metamask is installed
    provider
      .request({ method: 'eth_requestAccounts' })
      .then((accounts) => {
        selectedAccount = accounts[0];
        console.log(`selected account is ${selectedAccount}`);
      })
      .catch((err) => {
        console.log(err);
        return;
      });
    window.ethereum.on('accountsChanged', function (accounts) {
      selectedAccount = accounts[0];
      console.log(`Selected account changed to ${selectedAccount}`);
    });
  }
  const web3 = new Web3(provider);
  tokenid =
    web3.utils.BN(
      4116505640912284550723191986264393474293570820512791522119157812802259305428
    );
  const networkId = await web3.eth.net.getId();
  tokenContract = new web3.eth.Contract(
    TokenContract.abi,
    TokenContract.networks[networkId].address
  );
  isInitialized = true;
};
export const itembyS = async () => {
  if (!isInitialized) {
    await init();
  }
  // console.log(nftContract.methods.name.call());
  return tokenContract.methods
    .itemBySupplier(4, 1, 1, 1)
    .send({ from: selectedAccount });
};
export const set_price = async () => {
  // var tokenid= BigNumber.from(4116505640912284550723191986264393474293570820512791522119157812802259305428);
  if (!isInitialized) {
    await init();
  }
  // console.log(nftContract.methods.name.call());
  return tokenContract.methods
    .itemForSale(tokenid, 200)
    .send({ from: selectedAccount });
};
export const showNum = async () => {
  if (!isInitialized) {
    await init();
  }
  // const number = nftContract.methods.myFunction().call();
  // console.log(number);
  return tokenContract.methods.enternum(1).send({ from: selectedAccount });
};


Solution

  • The reason why we use big numbers is that JS can't directly handle big numbers...so you can't pass the "big number" as the whole number. The BN object expects the big number as a string. Like:

    tokenid =
        web3.utils.BN(
          "4116505640912284550723191986264393474293570820512791522119157812802259305428"
        );
    

    That would solve the assertion error.