Home > Mobile >  How specific Cost Value to ethersJs in React
How specific Cost Value to ethersJs in React

Time:01-20

I need to implement in the code below "value" the cost in ETH because in Solidity I require msg.value and in order to be able to lie I have to put a parameter "value" in the code in js, but I don't know how, you can it helps? I also attached the Smart Contract part with mint.

React JS file

  async function handleMint() {
    if (mintFinish === true) {
      setMintFinish(false);
    } else {
      setMintFinish(true);
    }

    if (window.ethereum) {
      const provider = new ethers.providers.Web3Provider(window.ethereum);
      const signer = provider.getSigner();
      const contract = new ethers.Contract(
        mintExampleAddress,
        mintExampleABI.abi,
        signer
      );
      console.log(signer)
      try {
        // console.log(contract)
        const response = await contract.mint(mint);
        console.log(response);
      } catch (err) {
        console.log(err);
      }
    }
  }

Solidity file

  function mint(uint256 _mintAmount) public payable mintCompliance(_mintAmount) {
    require(!paused, "The contract is paused!");
    require(msg.value >= cost * _mintAmount, "Insufficient funds!");

    _mintLoop(msg.sender, _mintAmount);
  }

CodePudding user response:

how you are using etherjs and seeing the smart contract code inside the mint function, i suppose that cost is a public variable and can be read from outside the contract, you get the cost and multiply the value by the mint amount parameter, something like this

const cost = await contract.cost();
const response = await contract.mint(mint,{value:cost.mul(mint)});

you have to remember to use bignumbers and that all return values that are numbers will be returned as one, what i'm doing here is getting the cost from the contract and multiply that for mint and pass that to value as the last parameter in the function call

  •  Tags:  
  • Related