Created
September 15, 2020 14:44
-
-
Save ryestew/63c0420a124514c5df2ff16cc00383b9 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.6.6+commit.6c089d02.js&optimize=false&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.7.0; | |
/** | |
* @title Storage | |
* @dev Store & retreive value in a variable | |
*/ | |
contract Storage { | |
uint256 number; | |
/** | |
* @dev Store value in variable | |
* @param num value to store | |
*/ | |
function store(uint256 num) public { | |
number = num; | |
} | |
/** | |
* @dev Return value | |
* @return value of 'number' | |
*/ | |
function retreive() public view returns (uint256){ | |
return number; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.7.0; | |
/** | |
* @title Owner | |
* @dev Set & change owner | |
*/ | |
contract Owner { | |
address private owner; | |
// event for EVM logging | |
event OwnerSet(address indexed oldOwner, address indexed newOwner); | |
// modifier to check if caller is owner | |
modifier isOwner() { | |
// If the first argument of 'require' evaluates to 'false', execution terminates and all | |
// changes to the state and to Ether balances are reverted. | |
// This used to consume all gas in old EVM versions, but not anymore. | |
// It is often a good idea to use 'require' to check if functions are called correctly. | |
// As a second argument, you can also provide an explanation about what went wrong. | |
require(msg.sender == owner, "Caller is not owner"); | |
_; | |
} | |
/** | |
* @dev Set contract deployer as owner | |
*/ | |
constructor() public { | |
owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor | |
emit OwnerSet(address(0), owner); | |
} | |
/** | |
* @dev Change owner | |
* @param newOwner address of new owner | |
*/ | |
function changeOwner(address newOwner) public isOwner { | |
emit OwnerSet(owner, newOwner); | |
owner = newOwner; | |
} | |
/** | |
* @dev Return owner address | |
* @return address of owner | |
*/ | |
function getOwner() external view returns (address) { | |
return owner; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.7.0; | |
/** | |
* @title Ballot | |
* @dev Implements voting process along with vote delegation | |
*/ | |
contract Ballot { | |
struct Voter { | |
uint weight; // weight is accumulated by delegation | |
bool voted; // if true, that person already voted | |
address delegate; // person delegated to | |
uint vote; // index of the voted proposal | |
} | |
struct Proposal { | |
// If you can limit the length to a certain number of bytes, | |
// always use one of bytes1 to bytes32 because they are much cheaper | |
bytes32 name; // short name (up to 32 bytes) | |
uint voteCount; // number of accumulated votes | |
} | |
address public chairperson; | |
mapping(address => Voter) public voters; | |
Proposal[] public proposals; | |
/** | |
* @dev Create a new ballot to choose one of 'proposalNames'. | |
* @param proposalNames names of proposals | |
*/ | |
constructor(bytes32[] memory proposalNames) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 1; | |
for (uint i = 0; i < proposalNames.length; i++) { | |
// 'Proposal({...})' creates a temporary | |
// Proposal object and 'proposals.push(...)' | |
// appends it to the end of 'proposals'. | |
proposals.push(Proposal({ | |
name: proposalNames[i], | |
voteCount: 0 | |
})); | |
} | |
} | |
/** | |
* @dev Give 'voter' the right to vote on this ballot. May only be called by 'chairperson'. | |
* @param voter address of voter | |
*/ | |
function giveRightToVote(address voter) public { | |
require( | |
msg.sender == chairperson, | |
"Only chairperson can give right to vote." | |
); | |
require( | |
!voters[voter].voted, | |
"The voter already voted." | |
); | |
require(voters[voter].weight == 0); | |
voters[voter].weight = 1; | |
} | |
/** | |
* @dev Delegate your vote to the voter 'to'. | |
* @param to address to which vote is delegated | |
*/ | |
function delegate(address to) public { | |
Voter storage sender = voters[msg.sender]; | |
require(!sender.voted, "You already voted."); | |
require(to != msg.sender, "Self-delegation is disallowed."); | |
while (voters[to].delegate != address(0)) { | |
to = voters[to].delegate; | |
// We found a loop in the delegation, not allowed. | |
require(to != msg.sender, "Found loop in delegation."); | |
} | |
sender.voted = true; | |
sender.delegate = to; | |
Voter storage delegate_ = voters[to]; | |
if (delegate_.voted) { | |
// If the delegate already voted, | |
// directly add to the number of votes | |
proposals[delegate_.vote].voteCount += sender.weight; | |
} else { | |
// If the delegate did not vote yet, | |
// add to her weight. | |
delegate_.weight += sender.weight; | |
} | |
} | |
/** | |
* @dev Give your vote (including votes delegated to you) to proposal 'proposals[proposal].name'. | |
* @param proposal index of proposal in the proposals array | |
*/ | |
function vote(uint proposal) public { | |
Voter storage sender = voters[msg.sender]; | |
require(sender.weight != 0, "Has no right to vote"); | |
require(!sender.voted, "Already voted."); | |
sender.voted = true; | |
sender.vote = proposal; | |
// If 'proposal' is out of the range of the array, | |
// this will throw automatically and revert all | |
// changes. | |
proposals[proposal].voteCount += sender.weight; | |
} | |
/** | |
* @dev Computes the winning proposal taking all previous votes into account. | |
* @return winningProposal_ index of winning proposal in the proposals array | |
*/ | |
function winningProposal() public view | |
returns (uint winningProposal_) | |
{ | |
uint winningVoteCount = 0; | |
for (uint p = 0; p < proposals.length; p++) { | |
if (proposals[p].voteCount > winningVoteCount) { | |
winningVoteCount = proposals[p].voteCount; | |
winningProposal_ = p; | |
} | |
} | |
} | |
/** | |
* @dev Calls winningProposal() function to get the index of the winner contained in the proposals array and then | |
* @return winnerName_ the name of the winner | |
*/ | |
function winnerName() public view | |
returns (bytes32 winnerName_) | |
{ | |
winnerName_ = proposals[winningProposal()].name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.7.0; | |
import "remix_tests.sol"; // this import is automatically injected by Remix. | |
import "./3_Ballot.sol"; | |
contract BallotTest { | |
bytes32[] proposalNames; | |
Ballot ballotToTest; | |
function beforeAll () public { | |
proposalNames.push(bytes32("candidate1")); | |
ballotToTest = new Ballot(proposalNames); | |
} | |
function checkWinningProposal () public { | |
ballotToTest.vote(0); | |
Assert.equal(ballotToTest.winningProposal(), uint(0), "proposal at index 0 should be the winning proposal"); | |
Assert.equal(ballotToTest.winnerName(), bytes32("candidate1"), "candidate1 should be the winner name"); | |
} | |
function checkWinninProposalWithReturnValue () public view returns (bool) { | |
return ballotToTest.winningProposal() == 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LEX Paid Escrow. | |
LEXON: 0.2.12 | |
COMMENT: 3.f - an escrow that is controlled by a third party for a fee. | |
“Payer” is a person. | |
“Payee” is a person. | |
“Arbiter” is a person. | |
“Fee” is an amount. | |
The Payer pays an Amount into escrow, | |
appoints the Payee, | |
appoints the Arbiter, | |
and also fixes the Fee. | |
CLAUSE: Pay Out. | |
The Arbiter may pay from escrow the Fee to themselves, | |
and afterwards pay the remainder of the escrow to the Payee. | |
CLAUSE: Pay Back. | |
The Arbiter may pay from escrow the Fee to themselves, | |
and afterwards return the remainder of the escrow to the Payer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LEX Paid Escrow. | |
LEXON: 0.2.12 | |
COMMENT: 3.f - an escrow that is controlled by a third party for a fee. | |
“Payer” is a person. | |
“Payee” is a person. | |
“Arbiter” is a person. | |
“Fee” is an amount. | |
The Payer pays an Amount into escrow, | |
appoints the Payee, | |
appoints the Arbiter, | |
and also fixes the Fee. | |
CLAUSE: Pay Out. | |
The Arbiter may pay from escrow the Fee to themselves, | |
and afterwards pay the remainder of the escrow to the Payee. | |
CLAUSE: Pay Back. | |
The Arbiter may pay from escrow the Fee to themselves, | |
and afterwards return the remainder of the escrow to the Payer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LEX Paid Escrow. | |
LEXON: 0.2.12 | |
COMMENT: 3.f - an escrow that is controlled by a third party for a fee. | |
“Payer” is a person. | |
“Payee” is a person. | |
“Arbiter” is a person. | |
“Fee” is an amount. | |
The Payer pays an Amount into escrow, | |
appoints the Payee, | |
appoints the Arbiter, | |
and also fixes the Fee. | |
CLAUSE: Pay Out. | |
The Arbiter may pay from escrow the Fee to themselves, | |
and afterwards pay the remainder of the escrow to the Payee. | |
CLAUSE: Pay Back. | |
The Arbiter may pay from escrow the Fee to themselves, | |
and afterwards return the remainder of the escrow to the Payer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.33 <0.7.00; | |
/** | |
* Ethereum Foundation/REMIX, BEC Blockchain Engineering Council | |
* GriMix: Smart Legal Energy Contract (SLEC) | |
// there are four types of contracts: master, p2p.buyer, p2p.seller and gridDSO | |
//defining the master SLEC contract | |
contract masterSLEC { | |
address private owner; | |
constructor( | |
_unint biddingTime, | |
_unint deliveryTime, | |
_unint reservePrice, //your desired price | |
_unint minIncrement | |
) public { | |
} | |
//define energy units | |
uint64 constant mWh = 1; | |
uint64 constant Wh = 1000 * mWh; | |
uint64 constant kWh = 1000 * Wh; | |
uint64 constant MWh = 1000 * kWh; | |
//seller.producer function | |
struct sellEnergy { | |
// sellerProducer's public key | |
address sellerProducer; | |
// day for which the offer is valid | |
uint32 day; | |
// sellerProducer price (bid) vs market price | |
uint32 seller.price; | |
// sellerProducer energy to sell | |
uint64 sellenergy; | |
// seller user ID | |
uint64 sellerUserID | |
// timestamp for when the energy offer was submitted | |
uint64 timestamp; | |
} | |
//buyer.consumer function | |
struct buyEnergy { | |
address buyer.consumer; | |
uint32 day; | |
uint32 buyer.price; | |
uint64 buyenergy; | |
uint32 buyerUserID; | |
uint64 timestamp; | |
——- this part yet to define the parameters and improve —- | |
//energy auction | |
energy contract Auction { | |
function placeBid() returns (bool success) {} | |
function withdraw() returns (bool success) {} | |
function cancelAuction() returns (bool success) {} | |
event LogBid(address bidder, uint bid, address highestBidder, uint highestBid, uint highestBindingBid); | |
event LogWithdrawal(address withdrawer, address withdrawalAccount, uint amount); | |
event LogCanceled(); | |
} | |
} | |
——- defining the other contracts ——- | |
//defining the P2P SLEC contract | |
contract P2PSLEC.buyer { | |
address private owner; | |
} | |
contract P2PSLEC.seller { | |
address private owner; | |
} | |
//defining the gridDSO contract | |
contract gridDSO { | |
address private owner; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.33 <0.7.00; | |
/** | |
* Ethereum Foundation/REMIX, BEC Blockchain Engineering Council | |
* GriMix: Smart Legal Energy Contract (SLEC) | |
*/ | |
// there are four types of contracts: master, p2p.buyer, p2p.seller and gridDSO | |
//defining the master SLEC contract | |
/* | |
// Rob's notes about the process | |
// register a participant | |
// authorize a participant to be a prosumer (seller of energy) | |
// authorize a participant to be a consumer (buyer of energy) | |
Auction | |
// normal auction features: | |
// offer energy for sale | |
// request of energy - (a bid for energy - but I'm not calling it bid because we are not bidding for a specific seller's energy) | |
// funds are sent to the contract with the bid and are held in escrow. | |
// cancel energy offer | |
// cancel request of energy | |
// register another bid | |
// find preliminary matches of buyers and sellers based on distance & largest overlap of hours desired | |
// distance to | |
// from the narrowed pool of buyers - a normal auction proceeds where the buyer is chosen by the hightest price | |
or is it a more normal auction where (much simpler) | |
bidders offer to buy energy from a specific buyer? | |
So a consumer needs to make the distance calculation before bothering to bid on a "lot" of energy. | |
So a consumer needs to find a seller with roughly the same hours. | |
// energy for sale - lotId (is this the txn addr of saving this txn?) prosumerId, location, hoursAvailable[], quantity, max price per kw/hr, isOpen | |
// energy bid - bidId (is this the txn addr of saving this txn?), consumerId, distance to seller, hoursWanted[], quantity, bidPrice per kw/hr - but this amount is sent with the txn and held by the contract, isOpen | |
*/ | |
contract masterSLEC { | |
address private owner; | |
//define energy units | |
uint64 constant mWh = 1; | |
uint64 constant Wh = 1000 * mWh; | |
uint64 constant kWh = 1000 * Wh; | |
uint64 constant MWh = 1000 * kWh; | |
uint8 bidIncrement; | |
struct Participant { | |
address prosumerID; // producer or consumer or both | |
bool canBuy; // has permission to buy | |
bool canSell; // has permission to sell | |
string buyerSpecs; // specs of the equipment or should this be a struct? | |
string sellerSpecs; // specs of the equipment or should this be a struct? | |
string location; //longlat | |
} | |
mapping(address => Participant) public participants; | |
struct sellEnergy { | |
address sellerProducer; // sellerProducer's public key | |
uint startTimeStamp; | |
uint endTimeStamp; | |
uint32 sellerPrice; // sellerProducer price (bid) vs market price | |
uint64 sellenergy; // sellerProducer energy to sell - amount of kw/h? | |
uint64 timestamp; // timestamp for when the energy offer was submitted | |
} | |
struct buyEnergy { | |
address buyerAddress; | |
uint64 timestamp; | |
uint price; | |
uint startTime; | |
uint64[] hourTimestampsDesired; // array of hours | |
} | |
function register() | |
function checkSeller() // participant has the right to sell | |
function checkBuyer() // participant has the right to buy | |
function offerEnergyLot(address prosumerAddress, longlat location, array hoursAvailable, quantity, max price per kw/hr, isOpen) | |
function placeBid(address buyerAddress, uint64 timestamp, uint pricePerKWH, array hoursDesired, uint distToSeller) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.33 <0.7.00; | |
/** | |
* Ethereum Foundation/REMIX, BEC Blockchain Engineering Council | |
* GriMix: Smart Legal Energy Contract (SLEC) | |
*/ | |
// there are four types of contracts: master, p2p.buyer, p2p.seller and gridDSO | |
//defining the master SLEC contract | |
contract masterSLEC { | |
address private owner; | |
//define energy units | |
uint64 constant mWh = 1; | |
uint64 constant Wh = 1000 * mWh; | |
uint64 constant kWh = 1000 * Wh; | |
uint64 constant MWh = 1000 * kWh; | |
uint8 bidIncrement; | |
struct participant { | |
address prosumerID; // producer or consumer or both | |
bool canBuy; // has permission to buy | |
bool canSell; // has permission to sell | |
mapping buyer; // specs of the equipment or should this be a struct? | |
mapping seller; // specs of the equipment or should this be a struct? | |
string location; //longlat | |
} | |
mapping participants; | |
mapping(address => Voter) public voters; | |
struct sellEnergy { | |
address sellerProducer; // sellerProducer's public key | |
uint32 day; // day for which the offer is valid | |
uint32 sellerPrice; // sellerProducer price (bid) vs market price | |
uint64 sellenergy; // sellerProducer energy to sell | |
uint64 sellerUserID // seller user ID // why is this different from their address? | |
uint64 timestamp; // timestamp for when the energy offer was submitted | |
} | |
struct bid { | |
} | |
//bad name but this is the energy for sale | |
stuct offerForSale { | |
address seller; | |
uint64 availWhen | |
uint64 timestamp; | |
uint8 maxAmount; // is this the reserve price not to be exceeded - based on the current market price | |
} | |
struct offerToBuy { | |
address buyer; | |
uint8 bidAmount; | |
uint8 distanceMeters to seller; | |
struct | |
} | |
struct buyEnergy { | |
address buyerConsumer; | |
uint32 day; | |
uint32 buyerPrice; | |
uint64 buyenergy; | |
uint32 buyerUserID; | |
uint64 timestamp; | |
} | |
mapping dayHours (day, hour1=>amount, hr2=>amount, hr3=>amount); | |
function register() | |
function checkSeller() | |
function checkBuyer() | |
function(addr, canSell, timeStart, hours ){ | |
goes to auction | |
} | |
} | |
energy contract Auction { | |
function placeBid() returns (bool success) {} | |
function withdraw() returns (bool success) {} | |
function cancelAuction() returns (bool success) {} | |
event LogBid(address bidder, uint bid, address highestBidder, uint highestBid, uint highestBindingBid); | |
event LogWithdrawal(address withdrawer, address withdrawalAccount, uint amount); | |
event LogCanceled(); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >0.6.99 <0.8.0; | |
contract SimpleAuction { | |
// Parameters of the auction. Times are either | |
// absolute unix timestamps (seconds since 1970-01-01) | |
// or time periods in seconds. | |
address payable public beneficiary; | |
uint public auctionEndTime; | |
// Current state of the auction. | |
address public highestBidder; | |
uint public highestBid; | |
// Allowed withdrawals of previous bids | |
mapping(address => uint) pendingReturns; | |
// Set to true at the end, disallows any change. | |
// By default initialized to `false`. | |
bool ended; | |
// Events that will be emitted on changes. | |
event HighestBidIncreased(address bidder, uint amount); | |
event AuctionEnded(address winner, uint amount); | |
// The following is a so-called natspec comment, | |
// recognizable by the three slashes. | |
// It will be shown when the user is asked to | |
// confirm a transaction. | |
/// Create a simple auction with `_biddingTime` | |
/// seconds bidding time on behalf of the | |
/// beneficiary address `_beneficiary`. | |
constructor( | |
uint _biddingTime, | |
address payable _beneficiary | |
) { | |
beneficiary = _beneficiary; | |
auctionEndTime = block.timestamp + _biddingTime; | |
} | |
/// Bid on the auction with the value sent | |
/// together with this transaction. | |
/// The value will only be refunded if the | |
/// auction is not won. | |
function bid() public payable { | |
// No arguments are necessary, all | |
// information is already part of | |
// the transaction. The keyword payable | |
// is required for the function to | |
// be able to receive Ether. | |
// Revert the call if the bidding | |
// period is over. | |
require( | |
block.timestamp <= auctionEndTime, | |
"Auction already ended." | |
); | |
// If the bid is not higher, send the | |
// money back (the failing require | |
// will revert all changes in this | |
// function execution including | |
// it having received the money). | |
require( | |
msg.value > highestBid, | |
"There already is a higher bid." | |
); | |
if (highestBid != 0) { | |
// Sending back the money by simply using | |
// highestBidder.send(highestBid) is a security risk | |
// because it could execute an untrusted contract. | |
// It is always safer to let the recipients | |
// withdraw their money themselves. | |
pendingReturns[highestBidder] += highestBid; | |
} | |
highestBidder = msg.sender; | |
highestBid = msg.value; | |
emit HighestBidIncreased(msg.sender, msg.value); | |
} | |
/// Withdraw a bid that was overbid. | |
function withdraw() public returns (bool) { | |
uint amount = pendingReturns[msg.sender]; | |
if (amount > 0) { | |
// It is important to set this to zero because the recipient | |
// can call this function again as part of the receiving call | |
// before `send` returns. | |
pendingReturns[msg.sender] = 0; | |
if (!msg.sender.send(amount)) { | |
// No need to call throw here, just reset the amount owing | |
pendingReturns[msg.sender] = amount; | |
return false; | |
} | |
} | |
return true; | |
} | |
/// End the auction and send the highest bid | |
/// to the beneficiary. | |
function auctionEnd() public { | |
// It is a good guideline to structure functions that interact | |
// with other contracts (i.e. they call functions or send Ether) | |
// into three phases: | |
// 1. checking conditions | |
// 2. performing actions (potentially changing conditions) | |
// 3. interacting with other contracts | |
// If these phases are mixed up, the other contract could call | |
// back into the current contract and modify the state or cause | |
// effects (ether payout) to be performed multiple times. | |
// If functions called internally include interaction with external | |
// contracts, they also have to be considered interaction with | |
// external contracts. | |
// 1. Conditions | |
require(block.timestamp >= auctionEndTime, "Auction not yet ended."); | |
require(!ended, "auctionEnd has already been called."); | |
// 2. Effects | |
ended = true; | |
emit AuctionEnded(highestBidder, highestBid); | |
// 3. Interaction | |
beneficiary.transfer(highestBid); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
try { | |
// remix.call({name: 'contentImport', key:'resolve', payload: []}).then((result) => { console.log(result) }).catch((error) => { console.log(error) }) | |
remix.call('contentImport' , 'resolve', 'https://raw.githubusercontent.com/ethereum/remix-project/master/LICENSE').then((result) => { console.log(result) }).catch((error) => { console.log(error) }) | |
} catch (e) { | |
console.log(e.message) | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment