0

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. **

1
  • 1
    Caculate and check the price before calling the buyFunction, your price is less than the 2ETH Commented Jan 25, 2022 at 6:28

1 Answer 1

0

I think error is here:

{from: accounts[0],Value: 2000000000000000000})

Value is capital letter. should be {value: 2000000000000000000})

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.