Function for buying NFT
function buy(address nftContract, uint256 itemId)
public
payable
nonReentrant
{
uint256 price = idToMarketItem[itemId].price;
uint256 tokenId = idToMarketItem[itemId].tokenId;
require(
msg.value >= price,
"Please submit the asking price in order to complete the purchase"
);
idToMarketItem[itemId].seller.transfer(msg.value);
fandomToken(nftContract).transfer(msg.sender, tokenId, 1);
idToMarketItem[itemId].owner = payable(msg.sender);
idToMarketItem[itemId].sold = true;
_itemsSold.increment();
}
Test case for the above code
describe('buy', async() => {
it('buy functionality check ', async()=>{
const nftContract = '0x8CdaF0CD259887258Bc13a92C0a6dA92698644C0'
console.log("ACCOUNT 2",accounts[2])
const result_nftContract = await contract_nFTMarket.buy(nftContract,1, {from: accounts[0],Value: 2000000000000000000})
console.log("---->>>>",result_nftContract)
const eventMinted = await result_nftContract.logs[0].args
// assert.equal(eventMinted.category,'video')
// assert.equal(eventMinted.nftContract,contract_address)
})
})
but still, I am getting below error
**Error: Returned error: VM Exception while processing transaction: revert Please submit the asking price in order to complete the purchase -- Reason given: Please submit the asking price in order to complete the purchase. **