I've got this weird issue with my smart contract on the Goerli testnet. I minted an NFT, but the contract's balance isn't going up. What gives?
Here's a snippet of my contract code:
```solidity
function mintNFT(address buyer, uint256 quantity) public payable {
require(totalMinted < MAX_SUPPLY, "Sold out");
require(quantity > 0 && quantity <= MAX_PER_TRANSACTION, "Invalid quantity");
require(msg.value == PRICE * quantity, "Incorrect payment");
for (uint256 i = 0; i < quantity; i++) {
totalMinted++;
uint256 newTokenId = totalMinted;
_mint(buyer, newTokenId);
}
bool sent = payable(owner()).send(msg.value);
require(sent, "Payment failed");
}
I’m using Hardhat to test the minting process. Any ideas why the contract balance isn’t increasing? Is there something I’m missing in my code or setup?
hey hugo, seems ur code sends funds immediately to the owner, so the contract balance never increases. try keeping the funds in the contract or use a withdrawal function instead. hope that helps!
I’ve encountered a similar issue before. The problem lies in the immediate transfer of funds to the owner. This action prevents the contract’s balance from accumulating. To resolve this, consider implementing a separate withdrawal function. This approach allows the contract to retain funds temporarily, making it easier to track balances and transactions. Additionally, it’s more gas-efficient and aligns with best practices for contract security. Remember to thoroughly test any changes on the testnet before deploying to mainnet. If you’re still facing issues after modifying your code, it might be worth reviewing your testing methodology to ensure you’re accurately capturing the contract’s state changes.
hmm interesting issue, hugo!
have you considered that maybe the balance isnt updating cuz of how quick the funds are moving? like, they go in and out so fast the contract doesnt even have time to show a balance change. what if you tried adding a delay or somethin before sending to the owner? or maybe use a separate function for withdrawing? just thinkin out loud here. btw, how are you checkin the balance? through etherscan or in your code? might be worth double-checking that too. oh, and have you tried minting multiple NFTs at once to see if it makes a difference? just curious how that might play out. lemme know what you find out!