-
Notifications
You must be signed in to change notification settings - Fork 3
Improvements #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Improvements #2
Changes from all commits
f709cf1
5daede4
f2d5563
6cff52a
1149ff3
0a543e8
bec233e
a6b8b0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,8 @@ async function deploy(deployer) { | |
| Random.address, | ||
| LOTTO_FEE_PERCENT, | ||
| MAX_NUMBER, | ||
| LOTTERY_DURATION | ||
| LOTTERY_DURATION, | ||
| web3.toWei(0.1, 'ether') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move 0.1 ether to a named constant. |
||
| ); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,43 +7,65 @@ const { | |
| assertThrowsInvalidOpcode, | ||
| assertNumberEqual, | ||
| assertValueEqual, | ||
| assertValueAlmostEqual | ||
| assertValueAlmostEqual, | ||
| } = require('./Helpers.js'); | ||
|
|
||
| const MAX_NUMBER = 10; | ||
| const LOTTERY_DURATION = 10; | ||
|
|
||
| const TICKET_FEE_WEI = web3.toWei(0.1, 'ether'); | ||
| const EXPECTED_TICKET_FEE_WEI = web3.toWei(0.1, 'ether'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for a separate const. |
||
|
|
||
| const LOTTO_FEE_PERCENT = 0.01; | ||
| const LOTTO_FEE_PERCENT_WEI = web3.toWei(LOTTO_FEE_PERCENT, 'ether'); | ||
|
|
||
| const EXPECTED_PRIZE = TICKET_FEE_WEI - (LOTTO_FEE_PERCENT * TICKET_FEE_WEI); | ||
|
|
||
|
|
||
| contract(`MicroLotto with max number of ${MAX_NUMBER} and fee percent ${LOTTO_FEE_PERCENT}`, accounts => { | ||
| contract( | ||
| `MicroLotto with max number of ${MAX_NUMBER} and fee percent ${LOTTO_FEE_PERCENT}`, | ||
| accounts => { | ||
| const OWNER = accounts[0]; | ||
| const PLAYER = accounts[1]; | ||
| const EXPECTED_NUMBER = 1; | ||
| const UNLUCKY_NUMBER = 2; | ||
| const EXPECTED_BALANCE = web3.toWei(5, 'ether'); | ||
| const EXPECTED_MAX_FUNCTION_FEE = web3.toWei(0.01, 'ether'); | ||
|
|
||
| let lotto; | ||
| let initialBalance; | ||
|
|
||
| beforeEach(async () => { | ||
| const random = await RandomMock.new(EXPECTED_NUMBER, { | ||
| from: OWNER | ||
| from: OWNER, | ||
| }); | ||
|
|
||
| lotto = await MicroLotto.new( | ||
| random.address, | ||
| LOTTO_FEE_PERCENT_WEI, | ||
| MAX_NUMBER, | ||
| LOTTERY_DURATION, | ||
| TICKET_FEE_WEI, | ||
| { from: OWNER } | ||
| ); | ||
| }); | ||
|
|
||
| it('Should create lotto object with ticketFee', async () => { | ||
| const ticketFee = await lotto.ticketFee(); | ||
| assert.equal(TICKET_FEE_WEI, EXPECTED_TICKET_FEE_WEI); | ||
| }); | ||
|
|
||
| it('Should transfer ether from contract address to owner address', async () => { | ||
| const balanceBefore = await getBalance(OWNER); | ||
| await lotto.ownerCollect(); | ||
| const balanceAfter = await getBalance(OWNER); | ||
| assertValueAlmostEqual(balanceBefore, balanceAfter, EXPECTED_MAX_FUNCTION_FEE); | ||
| }); | ||
|
|
||
| it('Should exit when ownerCollect is called by not owner user', async () => { | ||
| const action = lotto.ownerCollect.bind(lotto, { from: PLAYER }); | ||
| assertThrowsInvalidOpcode(action); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Convert to lambda. |
||
| }); | ||
|
|
||
| context(`Given filled ticket on expected number ${EXPECTED_NUMBER}`, () => { | ||
| let fillTicketResult; | ||
| let event; | ||
|
|
@@ -79,7 +101,7 @@ contract(`MicroLotto with max number of ${MAX_NUMBER} and fee percent ${LOTTO_FE | |
| beforeEach(async () => { | ||
| await waitUntilClosed(); | ||
| drawResult = await lotto.draw({ | ||
| from: PLAYER | ||
| from: PLAYER, | ||
| }); | ||
| }); | ||
|
|
||
|
|
@@ -93,6 +115,14 @@ contract(`MicroLotto with max number of ${MAX_NUMBER} and fee percent ${LOTTO_FE | |
|
|
||
| }); | ||
|
|
||
| it('Should be able to redeem prize', async () => { | ||
| const balanceBefore = await getBalance(PLAYER); | ||
| await lotto.redeemPrize({ from: PLAYER }); | ||
| const wonPrize = await lotto.wonPrizes(PLAYER); | ||
| const balanceAfter = await getBalance(PLAYER); | ||
| assertValueAlmostEqual(balanceBefore, balanceAfter - wonPrize, EXPECTED_MAX_FUNCTION_FEE); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| context(`Given filled ticket for unlucky number ${UNLUCKY_NUMBER}`, () => { | ||
|
|
@@ -110,7 +140,7 @@ contract(`MicroLotto with max number of ${MAX_NUMBER} and fee percent ${LOTTO_FE | |
| beforeEach(async () => { | ||
| await waitUntilClosed(); | ||
| drawResult = await lotto.draw({ | ||
| from: PLAYER | ||
| from: PLAYER, | ||
| }); | ||
| }); | ||
|
|
||
|
|
@@ -120,14 +150,10 @@ contract(`MicroLotto with max number of ${MAX_NUMBER} and fee percent ${LOTTO_FE | |
| assertNumberEqual(event.args.drawnNumber, EXPECTED_NUMBER); | ||
| assertNumberEqual(event.args.value, TICKET_FEE_WEI); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| }); | ||
|
|
||
| }); | ||
|
|
||
|
|
||
| async function waitUntilClosed () { | ||
| async function waitUntilClosed() { | ||
| await mineBlocks(LOTTERY_DURATION + 1); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
accumulatedValueis resetted indraw()and blocks the funds forever. SplitaccumulatedValueto separate fieldsprizeandfees.