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

Commit

Permalink
19 add investment (#20)
Browse files Browse the repository at this point in the history
* Change set result balance to round 1

* Add users winning bets back to winning amount

* Change version to 2

* Fix current block time func

* Fix toDenomination func

* Add construct 223 transfer data func

* Fix bet test
  • Loading branch information
dwalintukan authored May 25, 2019
1 parent 63e7f72 commit 72e12ae
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 51 deletions.
2 changes: 1 addition & 1 deletion 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 = 1;
uint16 private constant VERSION = 2;

address private _configManager;
address private _bodhiTokenAddress;
Expand Down
18 changes: 9 additions & 9 deletions contracts/event/MultipleResultsEvent.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ contract MultipleResultsEvent is NRC223Receiver, Ownable {
ResultBalance[11] balances;
}

uint16 private constant VERSION = 1;
uint16 private constant VERSION = 2;
uint8 private constant INVALID_RESULT_INDEX = 255;

uint8 private _numOfResults;
Expand Down Expand Up @@ -290,10 +290,10 @@ contract MultipleResultsEvent is NRC223Receiver, Ownable {
}

// Calculate users portion of all rounds losers total
uint winningAmt =
allRoundsUserBets.mul(allRoundsLosersTotal).div(allRoundsWinnersTotal);
uint arbitrationRewardAmt =
voteRoundsUserBets.mul(arbitrationReward).div(voteRoundsWinnersTotal);
uint winningAmt = allRoundsUserBets.mul(allRoundsLosersTotal)
.div(allRoundsWinnersTotal).add(allRoundsUserBets);
uint arbitrationRewardAmt = voteRoundsUserBets.mul(arbitrationReward)
.div(voteRoundsWinnersTotal);
winningAmt = winningAmt.add(arbitrationRewardAmt);
return winningAmt;
}
Expand Down Expand Up @@ -443,10 +443,10 @@ contract MultipleResultsEvent is NRC223Receiver, Ownable {
_currentRound = _currentRound + 1;

// Update balances
_eventRounds[0].balances[resultIndex].total =
_eventRounds[0].balances[resultIndex].total.add(value);
_eventRounds[0].balances[resultIndex].bets[from] =
_eventRounds[0].balances[resultIndex].bets[from].add(value);
_eventRounds[1].balances[resultIndex].total =
_eventRounds[1].balances[resultIndex].total.add(value);
_eventRounds[1].balances[resultIndex].bets[from] =
_eventRounds[1].balances[resultIndex].bets[from].add(value);
_resultTotals[resultIndex] = _resultTotals[resultIndex].add(value);
_totalBets = _totalBets.add(value);

Expand Down
47 changes: 19 additions & 28 deletions test/event/multiple-results-event.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
const { assert } = require('chai')
const TimeMachine = require('sol-time-machine')
const sassert = require('sol-assert')

const getConstants = require('../constants')
const {
toDenomination,
bigNumberFloor,
percentIncrease,
currentBlockTime,
paddedHexToAddress,
constructTransfer223Data,
decodeEvent,
} = require('../util')

const NRC223PreMinted = artifacts.require('NRC223PreMinted')
const ConfigManager = artifacts.require('ConfigManager')
const EventFactory = artifacts.require('EventFactory')
Expand All @@ -20,17 +19,9 @@ const MultipleResultsEvent = artifacts.require('MultipleResultsEvent')
const web3 = global.web3

const CREATE_EVENT_FUNC_SIG = '2b2601bf'
const BET_FUNC_SIG = '885ab66d'
const RESULT_INVALID = 'Invalid'
const BET_TOKEN_DECIMALS = 18
const createEventFuncTypes = [
'string',
'bytes32[10]',
'uint256',
'uint256',
'uint256',
'uint256',
'address',
]
const BET_TOKEN_DECIMALS = 8

const getEventParams = async (cOracle) => {
const currTime = await currentBlockTime()
Expand Down Expand Up @@ -65,12 +56,12 @@ const createEvent = async ({
gas,
}) => {
try {
// Construct params
const paramsHex = web3.eth.abi.encodeParameters(
createEventFuncTypes,
// Construct data
const data = constructTransfer223Data(
CREATE_EVENT_FUNC_SIG,
['string', 'bytes32[10]', 'uint256', 'uint256', 'uint256', 'uint256', 'address'],
eventParams,
).substr(2) // Remove hex prefix
const data = `0x${CREATE_EVENT_FUNC_SIG}${paramsHex}`
)

// Send tx
const receipt = await nbotMethods['transfer(address,uint256,bytes)'](
Expand Down Expand Up @@ -168,7 +159,7 @@ contract('MultipleResultsEvent', (accounts) => {
assert.equal(await eventMethods.owner().call(), OWNER)

const eventMeta = await eventMethods.eventMetadata().call()
assert.equal(eventMeta[0], 0)
assert.equal(eventMeta[0], 2)
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 Down Expand Up @@ -420,24 +411,24 @@ contract('MultipleResultsEvent', (accounts) => {
// })
// })

describe('bet()', () => {
describe.only('bet()', () => {
beforeEach(async () => {
const currTime = await currentBlockTime()
console.log('NAKA: currTime', currTime)
console.log('NAKA: eventParams[2]', eventParams[2])
console.log('NAKA: diff', Number(eventParams[2]) - currTime)
await timeMachine.increaseTime(Number(eventParams[2]) - currTime)
assert.isAtLeast(await currentBlockTime(), Number(eventParams[2]))
assert.isBelow(await currentBlockTime(), Number(eventParams[3]))
})

it('allows users to bet', async () => {
const betAmt = toDenomination(1, BET_TOKEN_DECIMALS)
const betResultIndex = 0
await eventMethods.bet(betResultIndex).send({ from: ACCT1, value: betAmt })

const totalAmts = await eventMethods.totalAmounts().call()
assert.equal(totalAmts[0], betAmt)
const amt = toDenomination(1, BET_TOKEN_DECIMALS)
const resultIndex = 0
const data = constructTransfer223Data(BET_FUNC_SIG, ['uint8'], [resultIndex])
await nbotMethods['transfer(address,uint256,bytes)'](
eventAddr,
amt,
web3.utils.hexToBytes(data),
).send({ from: OWNER, gas: 200000 })
assert.equal(await eventMethods.totalBets().call(), amt)
})
})

Expand Down
44 changes: 31 additions & 13 deletions test/util/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
const { find, each, isPlainObject, isArray } = require('lodash')
const { find, each, isPlainObject, isArray, isString } = require('lodash')

const web3 = global.web3

module.exports = class Utils {
/*
* Converts the amount to a lower denomination as a BigNumber.
* eg. (number: 1, decimals: 4) = 10000
* @param number {BigNumber|number|string} The number to convert.
* @param decimals {number} The denomination number of decimals to convert to.
* @retun {BigNumber} The converted BigNumber.
*/
static toDenomination(number, decimals = 0) {
const bn = web3.toBigNumber(number)
const decimalsBn = web3.toBigNumber(10 ** decimals)
return bn.times(decimalsBn)
static addHexPrefix(str) {
if (!isString(str)) return str
if (!str.startsWith('0x')) return `0x${str}`
return str
}

static removeHexPrefix(str) {
if (!isString(str)) return str
if (str.startsWith('0x')) return str.substr(2)
return str
}

/**
* Converts the amount to a lower denomination.
* @param {number|string} amount Amount to convert
* @param {number} decimals Number of decimals
* @return {string} Amount as string number
*/
static toDenomination(amount, decimals = 0) {
const bn = web3.utils.toBN(amount)
const decimalsBn = web3.utils.toBN(10 ** decimals)
return bn.mul(decimalsBn).toString(10)
}

/*
Expand All @@ -37,7 +48,9 @@ module.exports = class Utils {

// Gets the unix time in seconds of the current block
static async currentBlockTime() {
return (await web3.eth.getBlock(web3.eth.blockNumber)).timestamp
const blockNum = await web3.eth.getBlockNumber()
const block = await web3.eth.getBlock(blockNum)
return block.timestamp
}

/*
Expand All @@ -52,6 +65,11 @@ module.exports = class Utils {
return matches && matches[1] + matches[3]
}

static constructTransfer223Data(funcSig, types, params) {
const encoded = Utils.removeHexPrefix(web3.eth.abi.encodeParameters(types, params))
return Utils.addHexPrefix(`${funcSig}${encoded}`)
}

/**
* Gets the object from the ABI given the name and type
* @param {object} abi ABI to search in
Expand Down

0 comments on commit 72e12ae

Please sign in to comment.