Skip to content

Instantly share code, notes, and snippets.

Created May 5, 2017 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/59c18f69d814a0335d6a9da15f907523 to your computer and use it in GitHub Desktop.
Save anonymous/59c18f69d814a0335d6a9da15f907523 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://ethereum.github.io/browser-solidity/#version=soljson-v0.4.11+commit.68ef5810.js&optimize=undefined&gist=
pragma solidity ^0.4.8;
contract VoteFactory {
uint public VoteCount;
struct pollData {
bytes targetCallData;
uint targetGas;
uint targetValue;
bytes evalCallData;
uint evalGas;
uint evalValue;
}
mapping(uint => pollData) public polls;
function newPoll(
bytes _targetCallData,
uint _targetGas,
uint _targetValue,
bytes _evalCallData,
uint _evalGas,
uint _evalValue
) {
VoteCount++;
polls[VoteCount].targetCallData = _targetCallData;
polls[VoteCount].targetGas = _targetGas;
polls[VoteCount].targetValue = _targetValue;
polls[VoteCount].evalCallData = _evalCallData;
polls[VoteCount].evalGas = _evalGas;
polls[VoteCount].evalValue = _evalValue;
}
function execPollFun(uint _pollId) {
pollData myPoll = polls[_pollId];
if (this.call.gas(myPoll.evalGas).value(myPoll.evalValue)(myPoll.evalCallData)) {
this.call.gas(myPoll.targetGas).value(myPoll.targetValue)(myPoll.targetCallData);
// TODO: add event
// TODO: mark poll as executed so it doesnt happen multiple times
}
}
function ready(uint _pollId) constant returns (bool) {
pollData myPoll = polls[_pollId];
if (this.call.gas(myPoll.evalGas).value(myPoll.evalValue)(myPoll.evalCallData))
return true;
return false;
}
}
contract example is VoteFactory {
uint public myVal;
mapping(uint => uint) public votedFor;
// TODO: should be part of VoteFactory interface
function evalPoll(uint _pollId) constant returns (bool canBeExecuted) {
if (votedFor[_pollId] > 1)
return true;
else
throw;
}
// not generic, should not be part of VoteFactory interface
function voteFor(uint _pollId) {
votedFor[_pollId]++;
}
function newPollSetVal(uint _newVal) {
// todo: maybe this could still be abstracted away a bit more / nicer
bytes memory targetCallData = new bytes(36);
bytes4 b4 = bytes4(sha3("setMyVal(uint256)"));
bytes32 b32 = bytes32(_newVal);
for (uint i = 0; i < 4; i++) {
targetCallData[i] = b4[i];
}
uint offset = 4;
for (i = 0; i < 32; i++) {
targetCallData[i + offset] = b32[i];
}
bytes memory evalCallData = new bytes(36);
b4 = bytes4(sha3("evalPoll(uint256)"));
b32 = bytes32(VoteCount + 1);
for (i = 0; i < 4; i++) {
evalCallData[i] = b4[i];
}
for (i = 0; i < 32; i++) {
evalCallData[i + offset] = b32[i];
}
newPoll(targetCallData, 200000, 0, evalCallData, 200000, 0);
}
function setMyVal(uint _newVal) {
myVal = _newVal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment