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});
});
})
}