0

I have a supply chain contract and the basic idea is: in itemManger contract:

  1. create an item where input string and uint. For example, "Iphone", 123.
  2. input 0 into item function

0: address: _item 0x5C9eb5D6a6C2c1B3EFc52255C0b356f116f6f66D 1: string: _identifier Iphone 2: uint256: _itemPrice 123 3: uint8: _state 0

In item contract: there is a payment function where I input 123 so that i paid all the price and could trigger the delivery back to itemManager contract.

The error I got is when I pay 123 wei:

from: 0x5B3...eddC4to: Item.(receive) 0x5C9...6f66Dvalue: 123 weidata: 0xlogs: 0hash: 0xb9f...3ff87 transact to Item.(receive) errored: VM error: revert. revert The transaction has been reverted to the initial state. Reason provided by the contract: "The transaction wasn't successful, canceling". Debug the transaction to get more information.

The code is :

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

contract Item {
    uint public priceInWei;
    uint public pricePaid;
    uint public index;
    
    ItemManager parentContract;
    
    constructor(ItemManager _parentContract, uint _priceInWei, uint _index) public {
        priceInWei = _priceInWei;
        index = _index;
        parentContract = _parentContract;
    }
    
    receive() external payable {
        require(pricePaid == 0, "Item is paid already");
        require(priceInWei == msg.value, "Only full payments allowed");
        pricePaid += msg.value;
        (bool success, ) = address(parentContract).call{value:msg.value}(abi.encodeWithSignature("triggerPayment(uint256)"));
        require(success, "The transaction wasn't successful, canceling");
        
    }
    
    fallback() external payable {
        
    }
}

contract ItemManager {
    
    enum SupplyChainState{Created, Paid, Delivered}
    
    struct S_Item {
        Item _item;
        string _identifier;
        uint _itemPrice;
        ItemManager.SupplyChainState _state;
        
    }
    
    mapping(uint => S_Item) public items;
    uint itemIndex;
    
    event SupplyChainStep(uint _itemIndex, uint _step, address _itemAddress);
    
    function createItem(string memory _identifier, uint _itemPrice) public {
        Item item = new Item(this, _itemPrice, itemIndex);
        items[itemIndex]._item = item;
        items[itemIndex]._identifier = _identifier;
        items[itemIndex]._itemPrice = _itemPrice;
        items[itemIndex]._state = SupplyChainState.Created;
        
        emit SupplyChainStep(itemIndex, uint (items[itemIndex]._state), address(item));
        itemIndex++;
        
    }
    
    function triggerPayment(uint _itemIndex) public payable {
        require(items[_itemIndex]._itemPrice == msg.value, "Only full payments accepted");
        require(items[_itemIndex]._state == SupplyChainState.Created, "Item is further in the chain");
        items[_itemIndex]._state = SupplyChainState.Paid;
        
        emit SupplyChainStep(_itemIndex, uint (items[_itemIndex]._state), address(items[_itemIndex]._item));
    }
    
    function triggerDelivery(uint _itemIndex) public {
        require(items[_itemIndex]._state == SupplyChainState.Paid, "Item is further in the chain");
        items[_itemIndex]._state = SupplyChainState.Delivered;
        
        emit SupplyChainStep(itemIndex, uint(items[itemIndex]._state), address(items[_itemIndex]._item));

    }
    
}

I am a beginner on solidity and I do not understand where goes wrong. Please help thx

3
  • Have u written this contract?
    – Majd TL
    Sep 24 at 19:17
  • Try to write another function and not use the default receive, and then call it and see if its work :/
    – Majd TL
    Sep 24 at 19:18
  • You're using remix did you try to debug the transaction?
    – Ismael
    Sep 28 at 4:24
0

Your code is an outdated version of an example code in an Udemy course. The course author has provided a newer version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.