Skip to content
This repository has been archived by the owner on Sep 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #76 from bodhiproject/invalid_result
Browse files Browse the repository at this point in the history
Invalid result
  • Loading branch information
dwalintukan authored Dec 30, 2017
2 parents bed5672 + 159930f commit 0063c90
Show file tree
Hide file tree
Showing 10 changed files with 742 additions and 797 deletions.
87 changes: 87 additions & 0 deletions contracts/BaseContract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
pragma solidity ^0.4.18;

contract BaseContract {
struct ResultBalance {
uint256 totalBets;
uint256 totalVotes;
mapping(address => uint256) bets;
mapping(address => uint256) votes;
}

uint8 public constant INVALID_RESULT_INDEX = 255;

uint8 public numOfResults;
uint8 public resultIndex = INVALID_RESULT_INDEX;
uint16 public version;
ResultBalance[11] internal balances;

// Modifiers
modifier validResultIndex(uint8 _resultIndex) {
require (_resultIndex <= numOfResults - 1);
_;
}

/*
* @notice Gets the bet balances of the sender for all the results.
* @return An array of all the bet balances of the sender.
*/
function getBetBalances()
public
view
returns (uint256[11])
{
uint256[11] memory betBalances;
for (uint8 i = 0; i < numOfResults; i++) {
betBalances[i] = balances[i].bets[msg.sender];
}
return betBalances;
}

/*
* @notice Gets total bets for all the results.
* @return An array of total bets for all results.
*/
function getTotalBets()
public
view
returns (uint256[11])
{
uint256[11] memory totalBets;
for (uint8 i = 0; i < numOfResults; i++) {
totalBets[i] = balances[i].totalBets;
}
return totalBets;
}

/*
* @notice Gets the vote balances of the sender for all the results.
* @return An array of all the vote balances of the sender.
*/
function getVoteBalances()
public
view
returns (uint256[11])
{
uint256[11] memory voteBalances;
for (uint8 i = 0; i < numOfResults; i++) {
voteBalances[i] = balances[i].votes[msg.sender];
}
return voteBalances;
}

/*
* @notice Gets total votes for all the results.
* @return An array of total votes for all results.
*/
function getTotalVotes()
public
view
returns (uint256[11])
{
uint256[11] memory totalVotes;
for (uint8 i = 0; i < numOfResults; i++) {
totalVotes[i] = balances[i].totalVotes;
}
return totalVotes;
}
}
121 changes: 19 additions & 102 deletions contracts/events/TopicEvent.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
pragma solidity ^0.4.18;

import "./ITopicEvent.sol";
import "../BaseContract.sol";
import "../storage/IAddressManager.sol";
import "../oracles/IOracleFactory.sol";
import "../tokens/ERC20.sol";
import "../libs/Ownable.sol";
import "../libs/SafeMath.sol";
import "../libs/ByteUtils.sol";

contract TopicEvent is ITopicEvent, Ownable {
contract TopicEvent is ITopicEvent, BaseContract, Ownable {
using ByteUtils for bytes32;
using SafeMath for uint256;

Expand All @@ -24,30 +25,17 @@ contract TopicEvent is ITopicEvent, Ownable {
Collection
}

struct ResultBalance {
uint256 totalBets;
uint256 totalVotes;
mapping(address => uint256) bets;
mapping(address => uint256) votes;
}

struct Oracle {
address oracleAddress;
bool didSetResult;
}

uint8 public constant invalidResultIndex = 255;

bool public resultSet;
uint8 private finalResultIndex = invalidResultIndex;
uint8 public numOfResults;
uint16 public version;
Status public status = Status.Betting;
bytes32[10] public name;
bytes32[10] public resultNames;
bytes32[10] public eventName;
bytes32[11] public eventResults;
uint256 public totalQtumValue;
uint256 public totalBotValue;
ResultBalance[10] private balances;
IAddressManager private addressManager;
ERC20 private token;
Oracle[] public oracles;
Expand All @@ -65,11 +53,6 @@ contract TopicEvent is ITopicEvent, Ownable {
uint256 _botTokenWon);

// Modifiers
modifier validResultIndex(uint8 _resultIndex) {
require (_resultIndex <= numOfResults - 1);
_;
}

modifier fromCentralizedOracle() {
require(msg.sender == oracles[0].oracleAddress);
_;
Expand Down Expand Up @@ -123,15 +106,13 @@ contract TopicEvent is ITopicEvent, Ownable {

version = _version;
owner = _owner;
name = _name;
resultNames = _resultNames;
eventName = _name;

eventResults[0] = "Invalid";
numOfResults++;
for (uint i = 0; i < _resultNames.length; i++) {
if (!_resultNames[i].isEmpty()) {
balances[i] = ResultBalance({
totalBets: 0,
totalVotes: 0
});
eventResults[i + 1] = _resultNames[i];
numOfResults++;
} else {
break;
Expand Down Expand Up @@ -190,7 +171,7 @@ contract TopicEvent is ITopicEvent, Ownable {
oracles[0].didSetResult = true;
resultSet = true;
status = Status.OracleVoting;
finalResultIndex = _resultIndex;
resultIndex = _resultIndex;

balances[_resultIndex].totalVotes = balances[_resultIndex].totalVotes.add(_consensusThreshold);
balances[_resultIndex].votes[_oracle] = balances[_resultIndex].votes[_oracle].add(_consensusThreshold);
Expand Down Expand Up @@ -255,7 +236,7 @@ contract TopicEvent is ITopicEvent, Ownable {
oracles[oracleIndex].didSetResult = true;
resultSet = true;
status = Status.OracleVoting;
finalResultIndex = _resultIndex;
resultIndex = _resultIndex;

return createDecentralizedOracle(_currentConsensusThreshold.add(addressManager.consensusThresholdIncrement()));
}
Expand All @@ -273,7 +254,7 @@ contract TopicEvent is ITopicEvent, Ownable {

status = Status.Collection;

FinalResultSet(version, address(this), finalResultIndex);
FinalResultSet(version, address(this), resultIndex);

return true;
}
Expand Down Expand Up @@ -305,70 +286,6 @@ contract TopicEvent is ITopicEvent, Ownable {
WinningsWithdrawn(version, msg.sender, qtumWon, botWon);
}

/*
* @notice Gets the bet balances of the sender for all the results.
* @return An array of all the bet balances of the sender.
*/
function getBetBalances()
public
view
returns (uint256[10])
{
uint256[10] memory betBalances;
for (uint8 i = 0; i < numOfResults; i++) {
betBalances[i] = balances[i].bets[msg.sender];
}
return betBalances;
}

/*
* @notice Gets the vote balances of the sender for all the results.
* @return An array of all the vote balances of the sender.
*/
function getVoteBalances()
public
view
returns (uint256[10])
{
uint256[10] memory voteBalances;
for (uint8 i = 0; i < numOfResults; i++) {
voteBalances[i] = balances[i].votes[msg.sender];
}
return voteBalances;
}

/*
* @notice Gets total bets for all the results.
* @return An array of total bets for all results.
*/
function getTotalBets()
public
view
returns (uint256[10])
{
uint256[10] memory totalBets;
for (uint8 i = 0; i < numOfResults; i++) {
totalBets[i] = balances[i].totalBets;
}
return totalBets;
}

/*
* @notice Gets total votes for all the results.
* @return An array of total votes for all results.
*/
function getTotalVotes()
public
view
returns (uint256[10])
{
uint256[10] memory totalVotes;
for (uint8 i = 0; i < numOfResults; i++) {
totalVotes[i] = balances[i].totalVotes;
}
return totalVotes;
}

/*
* @notice Gets the final result index and flag indicating if the result is final.
* @return The result index and finalized bool.
Expand All @@ -378,7 +295,7 @@ contract TopicEvent is ITopicEvent, Ownable {
view
returns (uint8, bool)
{
return (finalResultIndex, status == Status.Collection);
return (resultIndex, status == Status.Collection);
}

/*
Expand All @@ -391,11 +308,11 @@ contract TopicEvent is ITopicEvent, Ownable {
inCollectionStatus()
returns (uint256)
{
uint256 senderContribution = balances[finalResultIndex].bets[msg.sender];
uint256 winnersTotal = balances[finalResultIndex].totalBets;
uint256 senderContribution = balances[resultIndex].bets[msg.sender];
uint256 winnersTotal = balances[resultIndex].totalBets;
uint256 losersTotalMinusCut = 0;
for (uint8 i = 0; i < numOfResults; i++) {
if (i != finalResultIndex) {
if (i != resultIndex) {
losersTotalMinusCut = losersTotalMinusCut.add(balances[i].totalBets);
}
}
Expand All @@ -415,11 +332,11 @@ contract TopicEvent is ITopicEvent, Ownable {
returns (uint256, uint256)
{
// Calculate BOT won
uint256 senderContribution = balances[finalResultIndex].votes[msg.sender];
uint256 winnersTotal = balances[finalResultIndex].totalVotes;
uint256 senderContribution = balances[resultIndex].votes[msg.sender];
uint256 winnersTotal = balances[resultIndex].totalVotes;
uint256 losersTotal = 0;
for (uint8 i = 0; i < numOfResults; i++) {
if (i != finalResultIndex) {
if (i != resultIndex) {
losersTotal = losersTotal.add(balances[i].totalVotes);
}
}
Expand Down Expand Up @@ -459,7 +376,7 @@ contract TopicEvent is ITopicEvent, Ownable {
address oracleFactory = addressManager.getOracleFactoryAddress(version);
uint256 arbitrationBlockLength = uint256(addressManager.arbitrationBlockLength());
address newOracle = IOracleFactory(oracleFactory).createDecentralizedOracle(address(this), numOfResults,
finalResultIndex, block.number.add(arbitrationBlockLength), _consensusThreshold);
resultIndex, block.number.add(arbitrationBlockLength), _consensusThreshold);

assert(newOracle != address(0));
oracles.push(Oracle({
Expand Down
8 changes: 4 additions & 4 deletions contracts/oracles/CentralizedOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ contract CentralizedOracle is Oracle {
require(block.number < bettingEndBlock);
require(msg.value > 0);

resultBalances[_resultIndex].totalBets = resultBalances[_resultIndex].totalBets.add(msg.value);
resultBalances[_resultIndex].bets[msg.sender] = resultBalances[_resultIndex].bets[msg.sender].add(msg.value);
balances[_resultIndex].totalBets = balances[_resultIndex].totalBets.add(msg.value);
balances[_resultIndex].bets[msg.sender] = balances[_resultIndex].bets[msg.sender].add(msg.value);

ITopicEvent(eventAddress).betFromOracle.value(msg.value)(msg.sender, _resultIndex);
OracleResultVoted(version, address(this), msg.sender, _resultIndex, msg.value);
Expand All @@ -99,8 +99,8 @@ contract CentralizedOracle is Oracle {
finished = true;
resultIndex = _resultIndex;

resultBalances[_resultIndex].totalVotes = resultBalances[_resultIndex].totalVotes.add(consensusThreshold);
resultBalances[_resultIndex].votes[msg.sender] = resultBalances[_resultIndex].votes[msg.sender]
balances[_resultIndex].totalVotes = balances[_resultIndex].totalVotes.add(consensusThreshold);
balances[_resultIndex].votes[msg.sender] = balances[_resultIndex].votes[msg.sender]
.add(consensusThreshold);

ITopicEvent(eventAddress).centralizedOracleSetResult(msg.sender, _resultIndex, consensusThreshold);
Expand Down
8 changes: 4 additions & 4 deletions contracts/oracles/DecentralizedOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ contract DecentralizedOracle is Oracle {
require(block.number < arbitrationEndBlock);
require(_eventResultIndex != lastResultIndex);

resultBalances[_eventResultIndex].totalVotes = resultBalances[_eventResultIndex].totalVotes.add(_botAmount);
resultBalances[_eventResultIndex].votes[msg.sender] = resultBalances[_eventResultIndex].votes[msg.sender]
balances[_eventResultIndex].totalVotes = balances[_eventResultIndex].totalVotes.add(_botAmount);
balances[_eventResultIndex].votes[msg.sender] = balances[_eventResultIndex].votes[msg.sender]
.add(_botAmount);

ITopicEvent(eventAddress).voteFromOracle(_eventResultIndex, msg.sender, _botAmount);
OracleResultVoted(version, address(this), msg.sender, _eventResultIndex, _botAmount);

if (resultBalances[_eventResultIndex].totalVotes >= consensusThreshold) {
if (balances[_eventResultIndex].totalVotes >= consensusThreshold) {
setResult();
}
}
Expand Down Expand Up @@ -94,7 +94,7 @@ contract DecentralizedOracle is Oracle {

uint256 winningVoteBalance = 0;
for (uint8 i = 0; i < numOfResults; i++) {
uint256 totalVoteBalance = resultBalances[i].totalVotes;
uint256 totalVoteBalance = balances[i].totalVotes;
if (totalVoteBalance > winningVoteBalance) {
winningVoteBalance = totalVoteBalance;
resultIndex = i;
Expand Down
Loading

0 comments on commit 0063c90

Please sign in to comment.