I am working on a project for p2p energy trading using blockchain. I am building an experiment on my own to demonstrate the concept for my master thesis.
For this project I have two python scripts, one for a prosumer (user that provide energy to the system) and one for a consumer (user that consume the system energy).
Each script is reading a value from a monitoring electricity system, that are updated at the same time.
Every time a new value is read a new "cycle" begin.
They are both calling the same smart contract using web3.py library, and when a new value is read from the monitoring system, they are updating two variables in the smart contract: Available Energy and Demand Energy. I have no problem doing that.
Considering that for a prosumer the value parsing into the smart contract call is positive and negative for a consumer, the smart contract know if we have a prosumer or consumer and can run the right function corresponding to it.
function checkProsumerOrConsumer(address payable _user, int _newEnergy, int _pastEnergy) public payable {
if(_newEnergy < 0){ //Consumer case
if(_pastEnergy < 0){
updateDemandEnergy(-_newEnergy, -_pastEnergy);
}
else if(_pastEnergy>0){
updateAvailableEnergy(0, _pastEnergy);
updateDemandEnergy(-_newEnergy,0);
}
else{
updateDemandEnergy(-_newEnergy,0);
}
//Wait 3 seconds function
consumer(_user, -_newEnergy);
}
else{ //Prosumer case
if(_pastEnergy > 0){
updateAvailableEnergy(_newEnergy, _pastEnergy);
}
else if(_pastEnergy < 0){
updateDemandEnergy(0, -_pastEnergy);
updateAvailableEnergy(_newEnergy,0);
}
else{
updateAvailableEnergy(_newEnergy,0);
}
//Wait 3 seconds function
prosumer(_user, _newEnergy);
}
}
The additional conditions are made in case an user switch from a prosumer to a consumer or the inverse.
It's working like it should be but I need to implement a delay (or other solution?) just before the call of the prosumer and consumer functions in my smart contract to let the variables AvailableEnergy and DemandEnergy be updated for the same "cycle" and proceed to transaction. Like 2 or 3 seconds should be enough.
The solution would be to implement a wait function (or something else? I'm up for any ideas) but from what I read this is not really something that usualy done in ethereum. I read that maybe I could use "Ethereum Alarm Clock", is suitable to do what I want to do? How can I use it in my smart contract?
Many thanks, Alban
I'm using Renix and solidity ^0.5.10. I'm doploying the contract on Ganache blockchain.