0

Good morning! My Smart Contract has a function to update the price once totalSupply reach 2000 tokens minted. I added a function in the front end but i think is not clean at all. Works well on my Dapp (with React) but... have you got any idea to make it cleaner? I created a function inside the mint function to replicate the Updateprice from the Smart Contract, because i couldn't call the Smart Contract function directly from the contract.methods.... Any suggestion? As I say is working already, but i'm pretty sure that i can get a cleaner code. Thank you so much in advice!
Smart contract:

  uint128 public publicSalePrice = 0.01 ether;

 //Function to update publicSalePrice once 2.000 have been minted

  function updatePrice() internal returns (uint256) {
  return publicSalePrice = totalSupply() < 2000 ? 0.01 ether : 0.03 ether;
  }
         
 //Function Public Mint.

 function publicSaleMint(uint256 _quantity) external payable callerIsUser {
    require(sellingStep == Step.PublicSale, "Public sale is not  activated");
    require(totalSupply() + _quantity <= MAX_PUBLIC, "Max supply exceeded");
    require(totalSupply() + _quantity <= MAX_SUPPLY, "Max supply exceeded");
    require(msg.value >= updatePrice() * _quantity, "Not enought funds");
    _safeMint(msg.sender, _quantity);
}

NOW the Frontend(function setprice is where mintrate is calculated). I also created a variable nuevoprice to get the totalSupply for the Smart Contract:

async function mint() {
var _mintAmount = Number(document.querySelector("[name=amount]").value);
var nuevoprice = Number(await contract.methods.totalSupply().call());
var maxsupply = 10000;
function setprice() {
 if(nuevoprice < 2000){
    return Web3.utils.toWei('0.01', 'ether');}
 else if(nuevoprice <= maxsupply){
  return Web3.utils.toWei('0.03', 'ether');}
   } 
var mintRate = Number(setprice());
var totalAmount = mintRate * _mintAmount;
await Web3Alc.eth.getMaxPriorityFeePerGas().then((tip) => {
    Web3Alc.eth.getBlock('pending').then((block) => {
      var baseFee = Number(block.baseFeePerGas);
      var maxPriority = Number(tip);
      var maxFee = baseFee + maxPriority;
contract.methods.publicSaleMint(_mintAmount)
.send({from: account, 
    value: String(totalAmount),
    maxFeePerGas: maxFee,
    maxPriorityFeePerGas: maxPriority});
  });
})
}

1 Answer 1

0

Couple of points to note but nothing major.

    require(totalSupply() + _quantity <= MAX_PUBLIC, "Max supply exceeded");
    require(totalSupply() + _quantity <= MAX_SUPPLY, "Max supply exceeded");

If one of MAX_PUBLIC and MAX_SUPPLY is always larger than the other, you can remove one statement.

There is a chance setprice will return undefined when the sale has concluded, breaking further calculations.

Consider removing gas fee estimations from your request. Metamask and other providers will internally calculate these values when requesting a transaction signature.

1
  • Thank you so much Milk! I will update with your advices. Thank you :D
    – edis3
    Aug 30, 2022 at 15:09

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.