Skip to content

Instantly share code, notes, and snippets.

@tomconte
Forked from anonymous/Untitled
Created May 24, 2016 14:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomconte/ca2e2927d72b472c5ee24f3db71a0987 to your computer and use it in GitHub Desktop.
Save tomconte/ca2e2927d72b472c5ee24f3db71a0987 to your computer and use it in GitHub Desktop.
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://chriseth.github.io/browser-solidity/#version=soljson-latest.js&optimize=undefined&gist=
contract ApolloTrade {
uint public kWh_rate = 1000;
mapping (address => uint) energyAccount;
mapping (address => uint) coinAccount;
address public owner;
function ApolloTrade() {
owner = msg.sender;
}
modifier onlyOwner {
if (msg.sender != owner) throw;
_
}
function setRate(uint rate) onlyOwner {
kWh_rate = rate;
}
// I am selling some energy; this will credit my account
function sellEnergy(uint kwh) public {
coinAccount[msg.sender] += (kwh * kWh_rate);
}
// I am buying some energy, thus crediting my energy account
function buyEnergy(uint coin) {
if (coinAccount[msg.sender] > coin) {
coinAccount[msg.sender] -= coin;
energyAccount[msg.sender] += (coin / kWh_rate);
}
}
function getEnergyAccount() returns (uint kwh) {
return energyAccount[msg.sender];
}
function getCoinAccount() returns (uint coin) {
return coinAccount[msg.sender];
}
}
@mynameisvinn
Copy link

throw has been deprecated.

instead, use this code block:

    modifier onlyOwner {
        // if (msg.sender != owner) throw;
        require(msg.sender != owner);
        _;
    }

@hoanbka
Copy link

hoanbka commented Oct 24, 2017

// I am selling some energy; this will credit my account
function sellEnergy(uint kwh) public {
coinAccount[msg.sender] += (kwh * kWh_rate);
}

When the producer sells energy, his coin account will be deposited but I think his energy account has to be deducted also?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment