Payment Transfer Issue in NFT Minting Contract
I’m working on a Solidity NFT contract that has two different minting mechanisms. One mint requires ownership of a specific soulbound token, and the other is a paid mint where users pay in ETH or MATIC. The soulbound minting works fine, but I’m having trouble with the paid version.
Here’s my paid minting function:
function mintPaidNft() public payable whenNotPaused {
require(
tokenPrice > 0, "Token price not configured"
);
require(
keccak256(bytes(approvedUsers[msg.sender].username)) !=
keccak256(""),
"User not in approved list"
);
require(
keccak256(bytes(approvedUsers[msg.sender].account_type)) == keccak256(bytes("purchaser")),
"Only purchasers allowed to mint"
);
uint256 userBalance = address(msg.sender).balance;
require(
userBalance >= tokenPrice,
"insufficient funds for NFT purchase"
);
uint256 newTokenId = _idCounter.current();
_idCounter.increment();
tokenOwners[newTokenId] = msg.sender;
address payable contractWallet = payable(address(this));
contractWallet.transfer(tokenPrice);
_safeMint(msg.sender, newTokenId);
}
My test setup looks like this:
describe("User has sufficient balance for minting", async function() {
beforeEach(async function() {
await this.nftContract.setTokenPrice(ethers.utils.parseEther("0.1"));
});
it("should successfully mint token", async function() {
const transaction = await this.nftContract.connect(this.testUser).mintPaidNft();
const receipt = await transaction.wait();
expect(receipt.status).to.equal(1);
});
});
The test account has plenty of ETH (999+ from Hardhat), and the price is set to 0.1 ETH. But when the test runs, I get a revert error at the contractWallet.transfer(tokenPrice); line.
What could be causing this transfer to fail? The user has enough balance and the amount seems correct.