1

I am completing series of tutorials on chainshot. Now I am on events 4. - Sale and I am getting an error

"before all" hook for "should allow a purchase"

and I don't have an idea how to fix it.

Task: Create an event called Purchase which takes two arguments: a uint for the purchase amount, and an address for the buyer. Create an external, payable function purchase which allows a buyer to purchase the collectible at the asking price.

To make this purchase happen you'll need to do 3 things: Transfer the msg.value to the seller. Transfer the ownership to the buyer. Mark the collectible as not for sale any longer.

My code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Collectible 
{
    address owner;
    uint price;
    bool purchased = false;

    event Deployed(address _address);
    event Transfer(address owner, address newOwner);
    event ForSale(uint price, uint blockTimestamp);
    event Purchase(uint purchaseAmount, address buyerAddress);

    constructor()
    {
        owner = msg.sender;
        emit Deployed(msg.sender);
    }

    function transfer(address _recipient) external
    {
        if(msg.sender == owner)
        {
            emit Transfer(owner, _recipient);
            owner = _recipient;
        }
        else
        {
            revert();
        }
    }

    function markPrice(uint _askingPrice) external
    {
        if(msg.sender == owner)
        {
            emit ForSale(_askingPrice, block.timestamp);
            price = _askingPrice;
        }
        else
        {
            revert();
        }
    }

    function purchase() external payable
    {
        if(msg.value >= price && msg.value != 0 && purchased == false)
        {
            emit Purchase(msg.value, msg.sender);
            payable(owner).transfer(msg.value);
            price = 0;
            purchased = true;
        }
        else
        {
            revert();
        }

    }
}

console output:

Collectible
    ✔ should revert if a purchase is attempted before the item is marked for sale (100ms)
    after marking a price
      ✔ should revert if a purchase is attempted with less money than the price
    after purchasing
      ✔ should emit a Purchase event
      ✔ should pay the owner
      ✔ should fail on the next purchase attempt
      after marking a new price
        1) "before all" hook for "should allow a purchase"


  5 passing (478ms)
  1 failing

  1) Collectible
       after purchasing
         after marking a new price
           "before all" hook for "should allow a purchase":
     Error: Transaction reverted without a reason string
    at Collectible.markPrice (contracts/collectible.sol:43)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at runNextTicks (node:internal/process/task_queues:65:3)
    at listOnTimeout (node:internal/timers:528:9)
    at processTimers (node:internal/timers:502:7)
    at HardhatNode._mineBlockWithPendingTxs (/usr/local/node-16.15.1/bin/node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:1815:23)
    at HardhatNode.mineBlock (/usr/local/node-16.15.1/bin/node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:504:16)
    at EthModule._sendTransactionAndReturnHash (/usr/local/node-16.15.1/bin/node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:1522:18)

I also have test.js file if it could help.

1 Answer 1

0

in function purchase you need to update de owner by the new one this is my version of function purchase `

    if(price!=msg.value || price==0 ){revert();} 
    payable(owner).transfer(msg.value);
      emit Purchase(price ,msg.sender);
      owner=msg.sender;
      price=0;
`

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.