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 #31 from bodhiproject/development
Browse files Browse the repository at this point in the history
ver6 changes
  • Loading branch information
dwalintukan committed Jun 19, 2019
2 parents 77faf0b + 9bb1faa commit 2e57ece
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 211 deletions.
23 changes: 9 additions & 14 deletions contracts/event/EventFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract EventFactory is NRC223Receiver {
uint amount;
}

uint16 private constant VERSION = 5;
uint16 private constant VERSION = 6;

address private _configManager;
address private _bodhiTokenAddress;
Expand Down Expand Up @@ -92,25 +92,22 @@ contract EventFactory is NRC223Receiver {
returns (MultipleResultsEvent)
{
(string memory eventName, bytes32[3] memory eventResults,
uint betStartTime, uint betEndTime, uint resultSetStartTime,
uint resultSetEndTime, address centralizedOracle,
uint betEndTime, uint resultSetStartTime, address centralizedOracle,
uint8 arbitrationOptionIndex, uint arbitrationRewardPercentage) =
abi.decode(params, (string, bytes32[3], uint, uint, uint, uint,
address, uint8, uint));
abi.decode(params, (string, bytes32[3], uint, uint, address, uint8,
uint));
return createMultipleResultsEvent(from, value, eventName, eventResults,
betStartTime, betEndTime, resultSetStartTime, resultSetEndTime,
centralizedOracle, arbitrationOptionIndex, arbitrationRewardPercentage);
betEndTime, resultSetStartTime, centralizedOracle,
arbitrationOptionIndex, arbitrationRewardPercentage);
}

/// @dev Creates a new MultipleResultsEvent. Only tokenFallback can call this.
/// @param creator Address of the creator.
/// @param escrowDeposited Amount of escrow deposited to create the event.
/// @param eventName Question or statement prediction.
/// @param eventResults Possible results.
/// @param betStartTime Unix time when betting will start.
/// @param betEndTime Unix time when betting will end.
/// @param resultSetStartTime Unix time when the CentralizedOracle can set the result.
/// @param resultSetEndTime Unix time when anyone can set the result.
/// @param centralizedOracle Address of the user that will decide the result.
/// @param arbitrationOptionIndex Index of the selected arbitration option.
/// @param arbitrationRewardPercentage Percentage of loser's bets going to winning arbitrators.
Expand All @@ -120,10 +117,8 @@ contract EventFactory is NRC223Receiver {
uint escrowDeposited,
string memory eventName,
bytes32[3] memory eventResults,
uint betStartTime,
uint betEndTime,
uint resultSetStartTime,
uint resultSetEndTime,
address centralizedOracle,
uint8 arbitrationOptionIndex,
uint arbitrationRewardPercentage)
Expand Down Expand Up @@ -152,9 +147,9 @@ contract EventFactory is NRC223Receiver {

// Create event
MultipleResultsEvent mrEvent = new MultipleResultsEvent(
creator, eventName, results, numOfResults, betStartTime,
betEndTime, resultSetStartTime, resultSetEndTime, centralizedOracle,
arbitrationOptionIndex, arbitrationRewardPercentage, _configManager);
creator, eventName, results, numOfResults, betEndTime,
resultSetStartTime, centralizedOracle, arbitrationOptionIndex,
arbitrationRewardPercentage, _configManager);

// Store escrow info
_escrows[address(mrEvent)].depositer = creator;
Expand Down
19 changes: 7 additions & 12 deletions contracts/event/MultipleResultsEvent.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ contract MultipleResultsEvent is NRC223Receiver, Ownable {
uint arbitrationEndTime;
}

uint16 private constant VERSION = 5;
uint16 private constant VERSION = 6;
uint8 private constant INVALID_RESULT_INDEX = 255;
uint256 private constant ORACLE_RESULT_SETTING_LENGTH = 48 * 60 * 60; // 48 hours

uint8 private _numOfResults;
uint8 private _currentRound = 0;
Expand Down Expand Up @@ -101,10 +102,8 @@ contract MultipleResultsEvent is NRC223Receiver, Ownable {
/// @param eventName Question or statement prediction.
/// @param eventResults Possible results.
/// @param numOfResults Number of results.
/// @param betStartTime Unix time when betting will start.
/// @param betEndTime Unix time when betting will end.
/// @param resultSetStartTime Unix time when the CentralizedOracle can set the result.
/// @param resultSetEndTime Unix time when anyone can set the result.
/// @param centralizedOracle Address of the user that will decide the result.
/// @param arbitrationOptionIndex Index of the selected arbitration option.
/// @param arbitrationRewardPercentage Percentage of loser's bets going to winning arbitrators.
Expand All @@ -114,10 +113,8 @@ contract MultipleResultsEvent is NRC223Receiver, Ownable {
string memory eventName,
bytes32[4] memory eventResults,
uint8 numOfResults,
uint betStartTime,
uint betEndTime,
uint resultSetStartTime,
uint resultSetEndTime,
address centralizedOracle,
uint8 arbitrationOptionIndex,
uint arbitrationRewardPercentage,
Expand All @@ -131,13 +128,10 @@ contract MultipleResultsEvent is NRC223Receiver, Ownable {
require(eventNameBytes.length > 0, "Event name cannot be empty");
require(!eventResults[1].isEmpty(), "First event result cannot be empty");
require(!eventResults[2].isEmpty(), "Second event result cannot be empty");
require(betEndTime > betStartTime, "betEndTime should be > betStartTime");
require(betEndTime > block.timestamp, "betEndTime should be > current time");
require(
resultSetStartTime >= betEndTime,
"resultSetStartTime should be >= betEndTime");
require(
resultSetEndTime > resultSetStartTime,
"resultSetEndTime should be > resultSetStartTime");
require(
arbitrationOptionIndex < 4,
"arbitrationOptionIndex should be < 4");
Expand All @@ -148,10 +142,10 @@ contract MultipleResultsEvent is NRC223Receiver, Ownable {
_eventName = eventName;
_eventResults = eventResults;
_numOfResults = numOfResults;
_betStartTime = betStartTime;
_betStartTime = block.timestamp;
_betEndTime = betEndTime;
_resultSetStartTime = resultSetStartTime;
_resultSetEndTime = resultSetEndTime;
_resultSetEndTime = resultSetStartTime.add(ORACLE_RESULT_SETTING_LENGTH);
_centralizedOracle = centralizedOracle;
_arbitrationRewardPercentage = arbitrationRewardPercentage;

Expand Down Expand Up @@ -490,6 +484,7 @@ contract MultipleResultsEvent is NRC223Receiver, Ownable {
private
{
// Calculate next consensus threshold
uint currThreshold = _eventRounds[_currentRound].consensusThreshold;
uint nextThreshold =
getNextThreshold(_eventRounds[_currentRound].consensusThreshold);
uint8 previousRound = _currentRound;
Expand Down Expand Up @@ -517,7 +512,7 @@ contract MultipleResultsEvent is NRC223Receiver, Ownable {
}

// Emit events
emit VoteResultSet(address(this), from, resultIndex, value,
emit VoteResultSet(address(this), from, resultIndex, currThreshold,
previousRound, nextThreshold, arbitrationEndTime);
}

Expand Down
104 changes: 42 additions & 62 deletions test/event/multiple-results-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ const { isNumber } = require('lodash')
const getConstants = require('../constants')
const {
toSatoshi,
bigNumberFloor,
currentBlockTime,
paddedHexToAddress,
constructTransfer223Data,
decodeEvent,
} = require('../util')
Expand All @@ -25,7 +23,7 @@ const SET_RESULT_FUNC_SIG = 'a6b4218b'
const VOTE_FUNC_SIG = '1e00eb7f'
const RESULT_INVALID = 'Invalid'
const RESULT_INDEX_INVALID = 255
const TOKEN_DECIMALS = 8
const ORACLE_RESULT_SETTING_LENGTH = 172800

const fundUsers = async ({ nbotMethods, accounts }) => {
await nbotMethods.transfer(accounts[1], toSatoshi(10000).toString())
Expand All @@ -40,19 +38,16 @@ const fundUsers = async ({ nbotMethods, accounts }) => {
.send({ from: accounts[0] })
}

const getEventParams = async (cOracle) => {
const currTime = await currentBlockTime()
const getEventParams = async (cOracle, currTime) => {
return [
'Test Event 1',
[
web3.utils.fromAscii('A'),
web3.utils.fromAscii('B'),
web3.utils.fromAscii('C'),
],
currTime + 1000,
currTime + 3000,
currTime + 4000,
currTime + 6000,
cOracle,
0,
10,
Expand All @@ -66,8 +61,7 @@ const createEvent = async (
// Construct data
const data = constructTransfer223Data(
CREATE_EVENT_FUNC_SIG,
['string', 'bytes32[3]', 'uint256', 'uint256', 'uint256', 'uint256',
'address', 'uint8', 'uint256'],
['string', 'bytes32[3]', 'uint256', 'uint256', 'address', 'uint8', 'uint256'],
eventParams,
)

Expand Down Expand Up @@ -230,11 +224,11 @@ contract('MultipleResultsEvent', (accounts) => {
configManagerMethods.setEventFactory(eventFactoryAddr).send({ from: OWNER })

// Setup event params
eventParams = await getEventParams(OWNER)
betStartTime = eventParams[2]
betEndTime = eventParams[3]
resultSetStartTime = eventParams[4]
resultSetEndTime = eventParams[5]
betStartTime = await currentBlockTime()
eventParams = await getEventParams(OWNER, betStartTime)
betEndTime = eventParams[2]
resultSetStartTime = eventParams[3]
resultSetEndTime = resultSetStartTime + ORACLE_RESULT_SETTING_LENGTH

// NBOT.transfer() -> create event
eventAddr = await createEvent({
Expand All @@ -254,7 +248,7 @@ contract('MultipleResultsEvent', (accounts) => {
assert.equal(await eventMethods.owner().call(), OWNER)

const eventMeta = await eventMethods.eventMetadata().call()
assert.equal(eventMeta[0], 5)
assert.equal(eventMeta[0], 6)
assert.equal(eventMeta[1], 'Test Event 1')
assert.equal(web3.utils.toUtf8(eventMeta[2][0]), RESULT_INVALID)
assert.equal(web3.utils.toUtf8(eventMeta[2][1]), 'A')
Expand All @@ -263,11 +257,13 @@ contract('MultipleResultsEvent', (accounts) => {
assert.equal(eventMeta[3], 4)

const centralizedMeta = await eventMethods.centralizedMetadata().call()
assert.equal(centralizedMeta[0], eventParams[6])
assert.equal(centralizedMeta[1], eventParams[2])
assert.equal(centralizedMeta[2], eventParams[3])
assert.equal(centralizedMeta[3], eventParams[4])
assert.equal(centralizedMeta[4], eventParams[5])
assert.equal(centralizedMeta[0], eventParams[4])
// Block time during testing isn't completely accurate so test within a range
assert.isTrue(centralizedMeta[1] >= betStartTime - 50
&& centralizedMeta[1] <= betStartTime + 50)
assert.equal(centralizedMeta[2], betEndTime)
assert.equal(centralizedMeta[3], resultSetStartTime)
assert.equal(centralizedMeta[4], resultSetEndTime)

const configMeta = await eventMethods.configMetadata().call()
assert.equal(configMeta[0], escrowAmt)
Expand All @@ -279,12 +275,12 @@ contract('MultipleResultsEvent', (accounts) => {
configMeta[2],
await configManagerMethods.thresholdPercentIncrease().call(),
)
assert.equal(configMeta[3], eventParams[8])
assert.equal(configMeta[3], eventParams[6])
})

it('throws if centralizedOracle address is invalid', async () => {
try {
const params = await getEventParams(INVALID_ADDR)
const params = await getEventParams(INVALID_ADDR, await currentBlockTime())
params[0] = 'Test Event 2'
await createEvent({
nbotMethods,
Expand All @@ -301,7 +297,7 @@ contract('MultipleResultsEvent', (accounts) => {

it('throws if eventName is empty', async () => {
try {
const params = await getEventParams(OWNER)
const params = await getEventParams(OWNER, await currentBlockTime())
params[0] = ''
await createEvent({
nbotMethods,
Expand All @@ -318,7 +314,7 @@ contract('MultipleResultsEvent', (accounts) => {

it('throws if eventResults 0 or 1 are empty', async () => {
try {
const params = await getEventParams(OWNER)
const params = await getEventParams(OWNER, await currentBlockTime())
params[0] = 'Test Event 3'
params[1] = [
web3.utils.fromAscii(''),
Expand All @@ -338,7 +334,7 @@ contract('MultipleResultsEvent', (accounts) => {
}

try {
const params = await getEventParams(OWNER)
const params = await getEventParams(OWNER, await currentBlockTime())
params[0] = 'Test Event 4'
params[1] = [
web3.utils.fromAscii('A'),
Expand All @@ -358,10 +354,10 @@ contract('MultipleResultsEvent', (accounts) => {
}
})

it('throws if betEndTime is <= betStartTime', async () => {
it('throws if resultSetStartTime is < betEndTime', async () => {
try {
const params = await getEventParams(OWNER)
params[0] = 'Test Event 5'
const params = await getEventParams(OWNER, await currentBlockTime())
params[0] = 'Test Event 6'
params[3] = params[2]
await createEvent({
nbotMethods,
Expand All @@ -372,15 +368,15 @@ contract('MultipleResultsEvent', (accounts) => {
gas: MAX_GAS,
})
} catch (e) {
sassert.revert(e, 'betEndTime should be > betStartTime')
sassert.revert(e, 'resultSetStartTime should be >= betEndTime')
}
})

it('throws if resultSetStartTime is < betEndTime', async () => {
it('throws if arbitrationOptionIndex is invalid', async () => {
try {
const params = await getEventParams(OWNER)
params[0] = 'Test Event 6'
params[4] = params[3]
const params = await getEventParams(OWNER, await currentBlockTime())
params[0] = 'Test Event 7'
params[5] = 4
await createEvent({
nbotMethods,
eventParams: params,
Expand All @@ -390,15 +386,15 @@ contract('MultipleResultsEvent', (accounts) => {
gas: MAX_GAS,
})
} catch (e) {
sassert.revert(e, 'resultSetStartTime should be >= betEndTime')
sassert.revert(e, 'arbitrationOptionIndex should be < 4')
}
})

it('throws if resultSetEndTime is <= resultSetStartTime', async () => {
it('throws if arbitrationRewardPercentage is invalid', async () => {
try {
const params = await getEventParams(OWNER)
params[0] = 'Test Event 7'
params[5] = params[4]
const params = await getEventParams(OWNER, await currentBlockTime())
params[0] = 'Test Event 8'
params[6] = 100
await createEvent({
nbotMethods,
eventParams: params,
Expand All @@ -408,7 +404,7 @@ contract('MultipleResultsEvent', (accounts) => {
gas: MAX_GAS,
})
} catch (e) {
sassert.revert(e, 'resultSetEndTime should be > resultSetStartTime')
sassert.revert(e, 'arbitrationRewardPercentage should be < 100')
}
})
})
Expand Down Expand Up @@ -443,9 +439,9 @@ contract('MultipleResultsEvent', (accounts) => {
describe('valid time', () => {
beforeEach(async () => {
const currTime = await currentBlockTime()
await timeMachine.increaseTime(Number(eventParams[2]) - currTime)
assert.isAtLeast(await currentBlockTime(), Number(eventParams[2]))
assert.isBelow(await currentBlockTime(), Number(eventParams[3]))
await timeMachine.increaseTime(betStartTime - currTime)
assert.isAtLeast(await currentBlockTime(), betStartTime)
assert.isBelow(await currentBlockTime(), betEndTime)
})

it('allows users to bet', async () => {
Expand Down Expand Up @@ -474,8 +470,8 @@ contract('MultipleResultsEvent', (accounts) => {

it('throws if the currentRound is not 0', async () => {
const currTime = await currentBlockTime()
await timeMachine.increaseTime(Number(eventParams[4]) - currTime)
assert.isAtLeast(await currentBlockTime(), Number(eventParams[4]))
await timeMachine.increaseTime(resultSetStartTime - currTime)
assert.isAtLeast(await currentBlockTime(), resultSetStartTime)

const amt = await eventMethods.currentConsensusThreshold().call()
await setResult({
Expand Down Expand Up @@ -530,26 +526,10 @@ contract('MultipleResultsEvent', (accounts) => {
})

describe('invalid time', () => {
it('throws if the current time is < betStartTime', async () => {
assert.isBelow(await currentBlockTime(), Number(eventParams[2]))

try {
await placeBet({
nbotMethods,
eventAddr,
amtDecimals: 1,
resultIndex: 0,
from: OWNER,
})
} catch (e) {
sassert.revert(e, 'Current time should be >= betStartTime')
}
})

it('throws if the current time is > betEndTime', async () => {
const currTime = await currentBlockTime()
await timeMachine.increaseTime(Number(eventParams[3]) - currTime)
assert.isAtLeast(await currentBlockTime(), Number(eventParams[3]))
await timeMachine.increaseTime(betEndTime - currTime)
assert.isAtLeast(await currentBlockTime(), betEndTime)

try {
await placeBet({
Expand Down
Loading

0 comments on commit 2e57ece

Please sign in to comment.