1
+ // ATTENTION: Tests can work incorrectly if you use nodes with cashed requests for balances
2
+ // It could happen that 'testnodes.wavesnodes.com' has that kind of cache
3
+
4
+ const WAVES = 10 ** 8 ;
5
+
6
+ const SETSCRIPT_FEE = 0.01 * WAVES
7
+ const ISSUE_FEE = 1 * WAVES
8
+ const INV_FEE = 0.005 * WAVES
9
+ const ADD_FEE = 0.004 * WAVES
10
+ const MIN_FEE = 0.001 * WAVES
11
+
12
+ const BET_TYPE_NUMBER = 0
13
+ const BET_TYPE_RED_BLACK = 1
14
+ const BET_TYPE_EVEN_ODD = 2
15
+ const BET_TYPE_DESK_HALF = 3
16
+ const BET_TYPE_DESK_THIRD = 4
17
+ const BET_TYPE_ROW = 5
18
+
19
+ const betAmount = 1.5 * WAVES
20
+
21
+ async function rememberBalances ( text , forAddress ) {
22
+ const wavesBal = await balance ( forAddress )
23
+
24
+ console . log ( text + ": " + wavesBal + " WAVES" )
25
+
26
+ return wavesBal
27
+ }
28
+
29
+ function makeBet ( casinoPubKey , roundId , betAmount , betType , guess , playerPubKey ) {
30
+
31
+ const bet = invokeScript ( { fee :INV_FEE , dApp : address ( casinoPubKey ) ,
32
+ call : { function :"bet" , args :[ { type :"string" , value : roundId } , { type :"integer" , value : betType } , { type :"integer" , value : guess } ] } ,
33
+ payment : [ { amount : betAmount , assetId :null } ] } ,
34
+ playerPubKey )
35
+
36
+ console . log ( "Bet. round: '" + roundId + "', amount: " + betAmount / 10 ** 8 + ", betType: " + betType + ", guess: '" + guess + "'" )
37
+ return bet
38
+ }
39
+
40
+ function withdraw ( casinoPubKey , roundId , playerPubKey ) {
41
+
42
+ const withdrawTx = invokeScript ( { fee :INV_FEE , dApp : address ( casinoPubKey ) ,
43
+ call : { function :"withdraw" , args :[ { type :"string" , value : roundId } ] } ,
44
+ payment : [ ] } ,
45
+ playerPubKey )
46
+
47
+ return withdrawTx
48
+ }
49
+
50
+ async function oracleStopRound ( oraclePublicKey , roundId ) {
51
+
52
+ const stopRound = data ( { data :[ { key :roundId + '_stop' , value :true , type : 'boolean' } ] } , oraclePublicKey )
53
+
54
+ await broadcast ( stopRound )
55
+ await waitForTx ( stopRound . id )
56
+ console . log ( "Stop round: " + roundId )
57
+ }
58
+
59
+ func makeOracleResultForNumber ( ) {
60
+ const s = "0" + theAnswer
61
+ // TODO:
62
+ // Currently emulating just bet on number. These "12345" digits must be calculated in other way
63
+ // "1" must be changed to black/red indication
64
+ // "2" must be changed to even/odd indication
65
+ // etc.
66
+ return s . substr ( s . length - 2 ) + "12345"
67
+ }
68
+
69
+ async function oraclePublishCorrectAnswer ( oraclePublicKey , roundId , theAnswer ) {
70
+
71
+ const answerStr = makeOracleResultForNumber ( theAnswer )
72
+
73
+ const oraclePublishAnswer = data ( { data :[ { key :roundId , value :answerStr , type : 'string' } ] } , oraclePublicKey )
74
+
75
+ await broadcast ( oraclePublishAnswer )
76
+ await waitForTx ( oraclePublishAnswer . id )
77
+ console . log ( "Published result for round '" + roundId + "': " + answerStr )
78
+ }
79
+
80
+
81
+ describe ( 'Casino script test suite' , async function ( ) {
82
+
83
+ this . timeout ( 100000 ) ;
84
+
85
+ before ( async function ( ) {
86
+
87
+ await setupAccounts ( { casino : SETSCRIPT_FEE + 35 * betAmount ,
88
+ player1 : 2 * betAmount + 2 * INV_FEE ,
89
+ player2 : betAmount + 2 * INV_FEE ,
90
+ player3 : betAmount + 2 * INV_FEE ,
91
+ oracle : 2 * INV_FEE } ) ;
92
+
93
+ const scriptC = compile ( file ( 'casino.ride' ) . replace ( '$ORACLE_ADDRESS' , address ( accounts . oracle ) ) ) ;
94
+ const ssTx = setScript ( { script :scriptC } , accounts . casino ) ;
95
+ await broadcast ( ssTx ) ;
96
+ await waitForTx ( ssTx . id )
97
+ console . log ( 'Script has been set' )
98
+ } ) ;
99
+
100
+ const roundId = "Round 1"
101
+ const correctAnswer = 12
102
+
103
+ it ( 'Making bets' , async function ( ) {
104
+
105
+ const bet1 = makeBet ( accounts . casino , roundId , betAmount , BET_TYPE_NUMBER , 11 , accounts . player1 )
106
+ await broadcast ( bet1 )
107
+ const bet2 = makeBet ( accounts . casino , roundId , betAmount , BET_TYPE_NUMBER , 10 , accounts . player2 )
108
+ await broadcast ( bet2 )
109
+ const bet3 = makeBet ( accounts . casino , roundId , betAmount , BET_TYPE_NUMBER , correctAnswer , accounts . player3 )
110
+ await broadcast ( bet3 )
111
+
112
+ await waitForTx ( bet1 . id )
113
+ await waitForTx ( bet2 . id )
114
+ await waitForTx ( bet3 . id )
115
+ } )
116
+
117
+ it ( 'Inability to make bet after freeze round' , async function ( ) {
118
+
119
+ await oracleStopRound ( accounts . oracle , roundId )
120
+
121
+ const bet1 = makeBet ( accounts . casino , roundId , betAmount , BET_TYPE_NUMBER , 14 , accounts . player1 )
122
+ await expect ( broadcast ( bet1 ) ) . rejectedWith ( "This round is already played" )
123
+ console . log ( "Bet must failed because round finished" )
124
+ } )
125
+
126
+ it ( 'Withdraw test' , async function ( ) {
127
+
128
+ await oraclePublishCorrectAnswer ( accounts . oracle , roundId , correctAnswer )
129
+
130
+ const withdraw1 = withdraw ( accounts . casino , roundId , accounts . player1 )
131
+ const withdraw2 = withdraw ( accounts . casino , roundId , accounts . player2 )
132
+ const withdraw3 = withdraw ( accounts . casino , roundId , accounts . player3 )
133
+
134
+ const casinoBefore = await rememberBalances ( "casino before: " , address ( accounts . casino ) )
135
+ const winnerBefore = await rememberBalances ( "winner before: " , address ( accounts . player3 ) )
136
+
137
+ expect ( broadcast ( withdraw1 ) ) . rejectedWith ( "You won nothing this round" )
138
+ expect ( broadcast ( withdraw2 ) ) . rejectedWith ( "You won nothing this round" )
139
+ await broadcast ( withdraw3 )
140
+ await waitForTx ( withdraw3 . id )
141
+
142
+ const casinoAfter = await rememberBalances ( "casino after: " , address ( accounts . casino ) )
143
+ const winnerAfter = await rememberBalances ( "winner after: " , address ( accounts . player3 ) )
144
+
145
+ expect ( casinoAfter ) . to . equal ( casinoBefore - 36 * betAmount , "Casino account reduced by win amount" )
146
+ expect ( winnerAfter ) . to . equal ( winnerBefore + 36 * betAmount - INV_FEE , "Winner account got win amount" )
147
+ } )
148
+
149
+
150
+ } )
0 commit comments