32 | | - | func bet (choice) = { |
---|
33 | | - | let pmnt = extract(i.payment) |
---|
34 | | - | let gameId = toBase58String(i.transactionId) |
---|
35 | | - | let gameStart = height |
---|
36 | | - | let gameCounter = { |
---|
37 | | - | let gameNumber = { |
---|
38 | | - | let counterValue = getInteger(this, "GamesCounter") |
---|
39 | | - | match counterValue { |
---|
40 | | - | case p: Int => |
---|
41 | | - | p |
---|
42 | | - | case _ => |
---|
43 | | - | 0 |
---|
44 | | - | } |
---|
45 | | - | } |
---|
46 | | - | (gameNumber + 1) |
---|
47 | | - | } |
---|
48 | | - | let betAssetIdCheck = isDefined(pmnt.assetId) |
---|
49 | | - | let playerPublicKey = toBase58String(i.callerPublicKey) |
---|
50 | | - | let playerChoice = if (if ((choice == "1")) |
---|
51 | | - | then true |
---|
52 | | - | else (choice == "2")) |
---|
53 | | - | then choice |
---|
54 | | - | else throw("Player's choice is incorrect") |
---|
55 | | - | let playerPrize = { |
---|
56 | | - | let bet = (pmnt.amount - comission) |
---|
57 | | - | if (if (if (if (if ((bet == bet1)) |
---|
58 | | - | then true |
---|
59 | | - | else (bet == bet5)) |
---|
60 | | - | then true |
---|
61 | | - | else (bet == bet10)) |
---|
62 | | - | then true |
---|
63 | | - | else (bet == bet25)) |
---|
64 | | - | then true |
---|
65 | | - | else (bet == bet100)) |
---|
66 | | - | then (bet * 2) |
---|
67 | | - | else throw("Payment amount is incorrect") |
---|
68 | | - | } |
---|
69 | | - | let reservedBalance = { |
---|
70 | | - | let currentReservedBalance = { |
---|
71 | | - | let valueReservedBalance = getInteger(this, "Reserved") |
---|
72 | | - | match valueReservedBalance { |
---|
73 | | - | case a: Int => |
---|
74 | | - | a |
---|
75 | | - | case _ => |
---|
76 | | - | 0 |
---|
77 | | - | } |
---|
78 | | - | } |
---|
79 | | - | let newReservedBalance = (currentReservedBalance + playerPrize) |
---|
80 | | - | if ((wavesBalance(this) >= newReservedBalance)) |
---|
81 | | - | then { |
---|
82 | | - | let increaseReservedBalance = newReservedBalance |
---|
83 | | - | increaseReservedBalance |
---|
84 | | - | } |
---|
85 | | - | else throw("Bet was rejected. Insufficient funds on game account.") |
---|
86 | | - | } |
---|
87 | | - | if (betAssetIdCheck) |
---|
88 | | - | then throw("You can make bet in Waves only") |
---|
89 | | - | else { |
---|
90 | | - | let dataString = ((((((((stateAcc + "_") + playerPublicKey) + "_") + playerChoice) + "_") + toString(playerPrize)) + "_") + toString(gameStart)) |
---|
91 | | - | WriteSet([DataEntry("Reserved", reservedBalance), DataEntry(gameId, dataString)]) |
---|
92 | | - | } |
---|
93 | | - | } |
---|
94 | | - | |
---|
95 | | - | |
---|
96 | | - | |
---|
97 | | - | @Callable(i) |
---|
98 | | - | func withdraw (gameId,rsaSign) = { |
---|
99 | | - | let dataList = { |
---|
100 | | - | let getDataString = getString(this, gameId) |
---|
101 | | - | let a = match getDataString { |
---|
102 | | - | case a: String => |
---|
103 | | - | a |
---|
104 | | - | case _ => |
---|
105 | | - | throw("GameId was not found") |
---|
106 | | - | } |
---|
107 | | - | split(a, "_") |
---|
108 | | - | } |
---|
109 | | - | let gameState = dataList[0] |
---|
110 | | - | let playerAddress = { |
---|
111 | | - | let a = dataList[1] |
---|
112 | | - | addressFromPublicKey(fromBase58String(a)) |
---|
113 | | - | } |
---|
114 | | - | let playerChoice = dataList[2] |
---|
115 | | - | let rsaToInt = { |
---|
116 | | - | let rsaCheck = rsaVerify(SHA256, toBytes(gameId), rsaSign, toBytes(oraclePublicKey)) |
---|
117 | | - | if (rsaCheck) |
---|
118 | | - | then { |
---|
119 | | - | let rsaInt = ((toInt(sha256(rsaSign)) % 99) + 1) |
---|
120 | | - | if (if ((rsaInt > 0)) |
---|
121 | | - | then (100 >= rsaInt) |
---|
122 | | - | else false) |
---|
123 | | - | then rsaInt |
---|
124 | | - | else throw("Rsa integer is not in range") |
---|
125 | | - | } |
---|
126 | | - | else throw("Rsa Signature is invalid") |
---|
127 | | - | } |
---|
128 | | - | let gameStart = { |
---|
129 | | - | let a = dataList[4] |
---|
130 | | - | parseIntValue(a) |
---|
131 | | - | } |
---|
132 | | - | let playerPrize = { |
---|
133 | | - | let a = dataList[3] |
---|
134 | | - | parseIntValue(a) |
---|
135 | | - | } |
---|
136 | | - | let newReservedBalance = { |
---|
137 | | - | let valueReservedBalance = getInteger(this, "Reserved") |
---|
138 | | - | let extractReservedBalance = match valueReservedBalance { |
---|
139 | | - | case a: Int => |
---|
140 | | - | a |
---|
141 | | - | case _ => |
---|
142 | | - | 0 |
---|
143 | | - | } |
---|
144 | | - | let decreaseReservedBalance = (extractReservedBalance - playerPrize) |
---|
145 | | - | if ((decreaseReservedBalance > 0)) |
---|
146 | | - | then decreaseReservedBalance |
---|
147 | | - | else throw("Reserved balance couldn't be less than 0") |
---|
148 | | - | } |
---|
149 | | - | let stringForNewData = ((((((dataList[1] + "_") + dataList[2]) + "_") + dataList[3]) + "_") + dataList[4]) |
---|
150 | | - | let gameTime = (gameTimeFrame > (height - gameStart)) |
---|
151 | | - | let stateCheck = (gameState == stateAcc) |
---|
152 | | - | if (stateCheck) |
---|
153 | | - | then if (gameTime) |
---|
154 | | - | then { |
---|
155 | | - | let playerWin = { |
---|
156 | | - | let rsaToState = if ((48 > rsaToInt)) |
---|
157 | | - | then "1" |
---|
158 | | - | else if ((rsaToInt > 52)) |
---|
159 | | - | then "2" |
---|
160 | | - | else "3" |
---|
161 | | - | (rsaToState == playerChoice) |
---|
162 | | - | } |
---|
163 | | - | if (playerWin) |
---|
164 | | - | then ScriptResult(WriteSet([DataEntry(gameId, ((((stateWin + "_") + stringForNewData) + "_") + toString(rsaToInt))), DataEntry("Reserved", newReservedBalance)]), TransferSet([ScriptTransfer(playerAddress, playerPrize, unit)])) |
---|
165 | | - | else WriteSet([DataEntry(gameId, ((((stateLose + "_") + stringForNewData) + "_") + toString(rsaToInt))), DataEntry("Reserved", newReservedBalance)]) |
---|
166 | | - | } |
---|
167 | | - | else throw("Game state is invalid") |
---|
168 | | - | else ScriptResult(WriteSet([DataEntry(gameId, ((((stateTimeout + "_") + stringForNewData) + "_") + "00")), DataEntry("Reserved", newReservedBalance)]), TransferSet([ScriptTransfer(playerAddress, playerPrize, unit)])) |
---|
| 7 | + | func checkSign (number,TxId,Signature,PublicKey) = { |
---|
| 8 | + | let a = rsaVerify(SHA256, toBytes(TxId), Signature, PublicKey) |
---|
| 9 | + | WriteSet([DataEntry((number + "_Pub"), PublicKey), DataEntry((number + "_TXID"), TxId), DataEntry((number + "_Signature"), Signature), DataEntry((number + "_Verify"), a)]) |
---|