whenever I want to run this code I get this error "Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? VM Exception while processing transaction: revert". please, can anyone suggest to me what is the problem. Also, How can I solve it?

pragma solidity ^0.5.0;

contract Energy {

    //Associate to each user the number of kWh available for sale and consumption
    mapping(address => uint) prosumer;

    struct storageVirtuale {
        uint dataDiScadenzaVendita;
        uint prezzoAcquisto; //Price at which it is bought from storage
        uint prezzoVendita; //Price at which it is sold from storage
        uint identificativo;
        uint wallet;    //TODO maybe to remove?
        uint32 capienzaTotale;
        uint32 kWhAttualmenteConservati;
        //After this date it will only be possible to buy the remaining kWh
        bool deactivated;

    mapping(address => storageVirtuale[]) energyManager;
    mapping(address => bool) energyManagersMapping;

    event Buy(address prosumerAddress, address storageOwner, uint storageId, uint32 quantity, uint paid);
    event Sell(address prosumerAddress, address storageOwner, uint storageId, uint32 quantity, uint earned);
    event StorageCreated(address owner, uint id);
    event StorageEdited(address owner, uint id);

    function buyFromStorageVirtuale(address addr, uint id, uint32 quantity) public payable{
        require(energyManager[addr].length > id);
        storageVirtuale memory scelto = energyManager[addr][id];
        require(quantity <= scelto.kWhAttualmenteConservati);
        require(msg.value >= scelto.prezzoVendita * quantity); //The price paid must be enough for the quantity

        prosumer[msg.sender] += quantity;
        scelto.kWhAttualmenteConservati -= quantity;
        energyManager[addr][id] = scelto;

        scelto.wallet += scelto.prezzoVendita * quantity;

        emit Buy(msg.sender, addr, id, quantity, msg.value);

    function sellToStorageVirtuale(address addr, uint id, uint32 quantity) public{
        require(energyManager[addr].length > id);
        storageVirtuale memory scelto = energyManager[addr][id];
        require(quantity <= scelto.capienzaTotale - scelto.kWhAttualmenteConservati); //Lo storage deve avere necessità
        require(prosumer[msg.sender] >= quantity); //The seller must have enough kWh

        prosumer[msg.sender] -= quantity;
        scelto.kWhAttualmenteConservati += quantity;
        scelto.wallet -= scelto.prezzoAcquisto * quantity;
        energyManager[addr][id] = scelto;

        msg.sender.transfer(quantity * scelto.prezzoAcquisto);

        emit Sell(msg.sender, addr, id, quantity, quantity * scelto.prezzoAcquisto);

    function createStorageVirtuale(uint32 capienzaTotale, uint prezzoAcquisto, uint prezzoVendita, uint dataDiScadenzaVendita) public payable{
        //In the creation of storage it is necessary to anticipate the money to pay those who have the price to sell
        require(msg.value >= capienzaTotale * prezzoAcquisto);
        //It is necessary that there is a positive gain in the sale of energy.
        require(prezzoAcquisto < prezzoVendita);

        energyManager[msg.sender].push(storageVirtuale(dataDiScadenzaVendita, prezzoAcquisto, prezzoVendita, energyManager[msg.sender].length, msg.value, capienzaTotale, 0, false));

        emit StorageCreated(msg.sender, energyManager[msg.sender].length - 1);

    function editStorageVirtuale(uint id, uint32 capienzaTotale, uint prezzoAcquisto, uint prezzoVendita, uint dataDiScadenzaVendita) public payable{
        require(energyManager[msg.sender].length > id); //Deve essere un id valido
        //E' necessario che ci sia un guadagno positivo nella vendita dell'energia.
        require(prezzoAcquisto < prezzoVendita);

        storageVirtuale memory temp = energyManager[msg.sender][id];
        //La nuova capienzaTotale non può essere minore dei kWhAttualmenteConservati
        require(capienzaTotale >= temp.kWhAttualmenteConservati);
        //Controlla se il wallet dello storage va bene con la nuova quantità
        require(temp.wallet + msg.value >= capienzaTotale * prezzoAcquisto);

        temp.capienzaTotale = capienzaTotale;
        temp.prezzoAcquisto = prezzoAcquisto;
        temp.prezzoVendita = prezzoVendita;
        temp.dataDiScadenzaVendita = dataDiScadenzaVendita;
        temp.wallet += msg.value;


        emit StorageEdited(msg.sender, id);

    function getPossessedkWh(address he) public view returns(uint){
        return prosumer[he];

    function givekWh(uint quantity) public {
        prosumer[msg.sender] += quantity;

    function disableStorageVirtuale(uint id) public {
        require(energyManager[msg.sender].length > id);
        energyManager[msg.sender][id].deactivated = true;

    function withdrawFromStorageVirtuale(uint id, uint maxWei) public{
        require(energyManager[msg.sender].length > id);
        storageVirtuale memory temp = energyManager[msg.sender][id];
        uint toWithraw = temp.wallet - ((temp.capienzaTotale - temp.kWhAttualmenteConservati) * temp.prezzoAcquisto);
        if(maxWei <= toWithraw){
        } else {

    function getAllStorageVirtuale(address toCheck) public view returns(uint){
        return energyManager[toCheck].length;

    function getStorageVirtuale(address addr, uint id) public view returns(uint, uint, uint, uint, uint, uint32, uint32, bool){
        require(energyManager[addr].length > id);
        storageVirtuale storage temp = energyManager[addr][id];
        return(temp.dataDiScadenzaVendita, temp.prezzoAcquisto, temp.prezzoVendita, temp.identificativo, temp.wallet, temp.capienzaTotale, temp.kWhAttualmenteConservati, temp.deactivated);

    function addEnergyManager(address toAdd) public {
        energyManagersMapping[toAdd] = true;

    function isEnergyManager(address toCheck) public view returns(bool){
        return energyManagersMapping[toCheck];

    //Al deploy il sender deve essere impostato come primo energyManager
    constructor() public{
        energyManagersMapping[msg.sender] = true;

    function () external payable{


    function pay() public payable{

  • 1
    Hi, "whenever I want to run this code" which function in partiular? – Briomkez Dec 15 '18 at 12:04
  • buyfromStorageVirtual and sellToStorageVirtual – A.S Yahaya Dec 18 '18 at 5:57
  • @A.SYahaya What parameters are you using when call the failing functions? The first place to look is at the require() conditions, when they fail it revert the transaction. – Ismael Dec 18 '18 at 21:11
  • Now, I have solved this problem. the next problem I encounter is when I try to communicate with a different account the transaction failed. what is the way forward. but using one account the transaction take place. – A.S Yahaya Dec 20 '18 at 5:33
  • I had the similar issue when I was trying to deploy my contract in the remix. It was because I select "Environment: injected web3", but then I select "Environment: JavaScript VM" my contract deployed correctly – Sergey Chepurnov Jan 26 at 14:23

Your Answer

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

Browse other questions tagged or ask your own question.