0

The error message is like this:

//SPDX-License-Identifier:GPL-3.0

pragma solidity 0.8.13;

contract trade{

    constructor() { owner = msg. sender; }

    address owner;

    modifier onlyOwner {
        require(msg.sender == owner);
        _;

    }


//////////////////////
/////required mappings
    // map consumer address to consumer userID

    mapping(address => uint32) public consumers;

    //map producer address to producerID "is a registered producer"
 
    mapping(address => uint) public producers;

    //map address to energy balance of the users"
 
    mapping(address => uint) public userEnergyBal;

///////////////////////////
//////consumer regster part

    modifier onlyRegisteredConsumers {
        require (consumers[msg. sender] > 0);
    _;
    }
    function registerConsumer (address aconsumer, uint32 auserID, uint buserEnergyBal) onlyOwner external {
        consumers [aconsumer] = auserID;
        userEnergyBal [aconsumer] = buserEnergyBal;

    }

////////////////////////////
//////producer register part

    modifier onlyRegisteredProducers {
    require (producers[msg. sender]>0);
    _;
    }
    function registerProducer(address bproducer,uint buserID, uint buserEnergyBal) onlyOwner external {
    require(buserID>0);
    producers [bproducer] = buserID;
    userEnergyBal [bproducer] = buserEnergyBal;
    }

////////////////////
////////trading part

    function buy_energy (address aproducer, uint64 aenergy) onlyRegisteredConsumers external payable {
    sendEthUser(aproducer);
    }

    function sendEthUser(address _user) public payable{
       payable(_user).transfer(2);
    }

}

1 Answer 1

0

Well, you're sending 0 eth with your transaction, and your transaction is trying to send 2 wei to user, does your contract hold those 2 wei initially? If not, that's the reason why it's not working.

12
  • The balance will be transferred from the account that calls the buy_energy() and eventually the sendEthUser(). So why do I need any balance in my contract? @Foxxxey Commented Jun 7, 2023 at 6:32
  • 1
    But you didn't send the 2 wei required. You can see that the value sent is 0 wei.
    – Nal Luksic
    Commented Jun 7, 2023 at 10:52
  • You need to explicitely send those with the transaction, they arent taken automatically from the users wallet.
    – Foxxxey
    Commented Jun 7, 2023 at 11:33
  • Well, I don't get it clear. How do I need to modify the buy_energy() function? Can you help me with the proper code that I need to add or modify? @Foxxxey Commented Jun 7, 2023 at 13:50
  • Well, I don't get it clear. How do I need to modify the buy_energy() function? Can you help me with the proper code that I need to add or modify? @Nal Luksik Commented Jun 7, 2023 at 14:11

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.