tx · 6MjK6a8hGxdYsfthntDygKdDQHemsf7ZmNC7B3zz26xs

3N2Z6EjzrzaCpD8ypDYLWDKe2PPVU8y9dJs:  -0.01400000 Waves

2019.09.06 20:03 [664482] smart account 3N2Z6EjzrzaCpD8ypDYLWDKe2PPVU8y9dJs > SELF 0.00000000 Waves

{ "type": 13, "id": "6MjK6a8hGxdYsfthntDygKdDQHemsf7ZmNC7B3zz26xs", "fee": 1400000, "feeAssetId": null, "timestamp": 1567789439748, "version": 1, "sender": "3N2Z6EjzrzaCpD8ypDYLWDKe2PPVU8y9dJs", "senderPublicKey": "HSTmdhPrJF81ozDBaqVn8QVtV76oD7gzDfcHPQRdxdwW", "proofs": [ "RZqvmLj13akfNxF7x8MH518i7BzScxE21hPi6Trf8GBmTb4NjviWTzsUYaRbFY4JzcGD6ewQ54AKqnTNmszwvX6" ], "script": "base64:AAIDAAAAAAAAAAAAAAAKAAAAAA9TZXJ2ZXJQdWJsaWNLZXkJAAJZAAAAAQIAAAAsN2ltOWNhaUNtRTRTTDZqVG00S1RpY0hINGJHUGY1djU5cm5mVUEzY0NSRGoAAAAAElNlcnZlckJldFB1YmxpY0tleQkAAlkAAAABAgAAACw3aW05Y2FpQ21FNFNMNmpUbTRLVGljSEg0YkdQZjV2NTlybmZVQTNjQ1JEagAAAAAHV0FWRUxFVAkAAGgAAAACCQAAaAAAAAIAAAAAAAAAAGQAAAAAAAAAA+gAAAAAAAAAA+gAAAAACkNPTU1JU1NJT04JAABpAAAAAgkAAGgAAAACAAAAAAAAAAAFBQAAAAdXQVZFTEVUAAAAAAAAAAPoAQAAAA9HZW5lcmF0ZVJhbmRJbnQAAAADAAAABmdhbWVJZAAAAAdyc2FTaWduAAAACmxhc3RUaWNrZXQEAAAAC3JzYVNpZ1ZhbGlkCQAB9AAAAAMJAAGbAAAAAQUAAAAGZ2FtZUlkBQAAAAdyc2FTaWduBQAAAA9TZXJ2ZXJQdWJsaWNLZXkDBQAAAAtyc2FTaWdWYWxpZAQAAA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AACXdpbm5lck51bQUAAAAEdW5pdAUAAAADbmlsBAAAAApnYW1lUmVzdWx0CQAETAAAAAIJAQAAAAlEYXRhRW50cnkAAAACCQABLAAAAAIFAAAABmdhbWVJZAIAAAAHX3Jlc3VsdAkAAaQAAAABBQAAAAl3aW5UaWNrZXQJAARMAAAAAgkBAAAACURhdGFFbnRyeQAAAAIJAAEsAAAAAgUAAAAGZ2FtZUlkAgAAAAdfd2lubmVyCQABkQAAAAIFAAAAC3BsYXllcnNMaXN0BQAAAAl3aW5uZXJOdW0JAARMAAAAAgkBAAAACURhdGFFbnRyeQAAAAIJAAEsAAAAAgUAAAAGZ2FtZUlkAgAAAAdfd2luQW10CQABkQAAAAIFAAAADndpbkFtb3VudHNMaXN0BQAAAAl3aW5uZXJOdW0FAAAAA25pbAkBAAAADFNjcmlwdFJlc3VsdAAAAAIJAQAAAAhXcml0ZVNldAAAAAEFAAAACmdhbWVSZXN1bHQJAQAAAAtUcmFuc2ZlclNldAAAAAEFAAAABXdpblRYAAAAAIOrGsA=", "chainId": 84, "height": 664482, "spentComplexity": 0 } View: original | compacted Prev: 4m21MpLuugmVJucmk6mwDGaahTC8cXFgsBiTeFoGu7qD Next: 5tEKP6aVwiDYZSB1sW2Zrrn2cjuUeEa1W1bxU51MuT2W Diff:
OldNewDifferences
88 let WAVELET = ((100 * 1000) * 1000)
99
1010 let COMMISSION = ((5 * WAVELET) / 1000)
11-
12-let TicketsRate = 100
1311
1412 func GenerateRandInt (gameId,rsaSign,lastTicket) = {
1513 let rsaSigValid = sigVerify(toBytes(gameId), rsaSign, ServerPublicKey)
2422 }
2523
2624
27-func getPlayers (gameId) = {
28- let rawDataStr = match getString(this, gameId) {
29- case str: String =>
30- str
31- case _ =>
32- throw(("Couldn't find game by " + gameId))
33- }
34- split(rawDataStr, "_")
25+func getPlayersTickets (gameId,player) = {
26+ let ticketsStr = (((gameId + "_") + player) + "_tickets")
27+ let playerTicketsStr = getStringValue(this, ticketsStr)
28+ playerTicketsStr
3529 }
3630
3731
38-func getPlayersTickets (gameId,Player) = {
39- let playerTicketsStr = match getString(this, ((gameId + "_") + Player)) {
40- case str: String =>
41- str
42- case _ =>
43- throw(("Couldn't find game by " + gameId))
32+func ParseNextAttribute (remaining) = {
33+ let s = size(remaining)
34+ if ((s > 0))
35+ then {
36+ let nn = parseIntValue(take(remaining, 2))
37+ let v = take(drop(remaining, 2), nn)
38+ let tmpRemaining = drop(remaining, (nn + 2))
39+ let remainingState = tmpRemaining
40+[v, remainingState]
41+ }
42+ else throw("Empty string was passed into parseNextAttribute func")
4443 }
45- split(playerTicketsStr, " ")
44+
45+
46+func parseBetStr (ticketsStr) = ["1", "100", "101", "300", "0", "0", "0", "0"]
47+
48+
49+func isWinner (gameId,player,winTicket) = {
50+ let playersTicket = getPlayersTickets(gameId, player)
51+ let bets = parseBetStr(playersTicket)
52+ if (if ((winTicket >= parseIntValue(bets[0])))
53+ then (parseIntValue(bets[1]) >= winTicket)
54+ else false)
55+ then true
56+ else if (if ((winTicket >= parseIntValue(bets[2])))
57+ then (parseIntValue(bets[3]) >= winTicket)
58+ else false)
59+ then true
60+ else if (if ((winTicket >= parseIntValue(bets[4])))
61+ then (parseIntValue(bets[5]) >= winTicket)
62+ else false)
63+ then true
64+ else if (if ((winTicket >= parseIntValue(bets[6])))
65+ then (parseIntValue(bets[7]) >= winTicket)
66+ else false)
67+ then true
68+ else false
69+ }
70+
71+
72+func defineWinner (gameId,players,winAmounts,winTicket) = {
73+ let playersList = split(players, "_")
74+ let winAmountsList = split(winAmounts, "_")
75+ if (isWinner(gameId, playersList[0], winTicket))
76+ then 0
77+ else if (isWinner(gameId, playersList[1], winTicket))
78+ then 1
79+ else if (isWinner(gameId, playersList[2], winTicket))
80+ then 2
81+ else if (isWinner(gameId, playersList[3], winTicket))
82+ then 3
83+ else if (isWinner(gameId, playersList[4], winTicket))
84+ then 4
85+ else if (isWinner(gameId, playersList[5], winTicket))
86+ then 5
87+ else throw("Error with defining winner")
4688 }
4789
4890
4991 @Callable(i)
50-func bet (gameId,gameIdCrypted,tickets) = {
92+func bet (gameId,gameIdCryptedStr,tickets) = {
93+ let gameIdCrypted = fromBase58String(gameIdCryptedStr)
5194 let pmt = extract(i.payment)
5295 let betNotInWaves = isDefined(pmt.assetId)
5396 let feeNotInWaves = isDefined(pmt.assetId)
5497 let playerPubKey58 = toBase58String(i.callerPublicKey)
5598 let playerAddress = addressFromPublicKey(fromBase58String(playerPubKey58))
56- if (betNotInWaves)
57- then throw("Bet amount must be in Waves")
58- else if (feeNotInWaves)
59- then throw("Transaction's fee must be in Waves")
60- else {
61- let rsaSigValid = sigVerify(toBytes(gameId), gameIdCrypted, ServerBetPublicKey)
62- if (rsaSigValid)
63- then {
64- let playerAddressStr = toString(playerAddress)
65- let betData = [DataEntry((playerAddressStr + "_tickets"), tickets)]
66- WriteSet(betData)
67- }
68- else throw("Invalid RSA signature")
69- }
99+ let gameStillOpen = isDefined(getString(this, (gameId + "_result")))
100+ if (!(gameStillOpen))
101+ then if (betNotInWaves)
102+ then throw("Bet amount must be in Waves")
103+ else if (feeNotInWaves)
104+ then throw("Transaction's fee must be in Waves")
105+ else {
106+ let rsaSigValid = sigVerify(toBytes(gameId), gameIdCrypted, ServerBetPublicKey)
107+ if (rsaSigValid)
108+ then {
109+ let playerAddressStr = toString(playerAddress)
110+ let betData = [DataEntry((((gameId + "_") + playerAddressStr) + "_tickets"), tickets)]
111+ WriteSet(betData)
112+ }
113+ else throw("Invalid RSA signature")
114+ }
115+ else throw("This game already end, try make bet again")
70116 }
71117
72118
73119
74120 @Callable(i)
75-func generateResult (gameId,rsaSignCrypted,lastTicket) = {
121+func generateResult (gameId,rsaSignCrypted,lastTicket,players,winAmounts) = {
76122 let rsaSign = fromBase58String(rsaSignCrypted)
77- let randTicket = GenerateRandInt(gameId, rsaSign, lastTicket)
78- let gameResult = [DataEntry((gameId + "_result"), toString(randTicket))]
79- let playersList = getPlayers(gameId)
80- let player1Tickets = getPlayersTickets(gameId, playersList[0])
81- let player1TicketFirst = parseIntValue(player1Tickets[0])
82- let player1TicketLast = parseIntValue(player1Tickets[1])
83- if (if ((randTicket >= player1TicketFirst))
84- then (player1TicketLast >= randTicket)
85- else false)
86- then {
87- let winAmt = 100000000
88- ScriptResult(WriteSet(gameResult), TransferSet([ScriptTransfer(addressFromStringValue(playersList[0]), winAmt, unit)]))
89- }
90- else throw("Player1 not the Winner")
123+ let winTicket = GenerateRandInt(gameId, rsaSign, lastTicket)
124+ let playersList = split(players, "_")
125+ let winAmountsList = split(winAmounts, "_")
126+ let winnerNum = defineWinner(gameId, players, winAmounts, winTicket)
127+ let winTX = [ScriptTransfer(addressFromStringValue(playersList[winnerNum]), parseIntValue(winAmountsList[winnerNum]), unit)]
128+ let gameResult = [DataEntry((gameId + "_result"), toString(winTicket)), DataEntry((gameId + "_winner"), playersList[winnerNum]), DataEntry((gameId + "_winAmt"), winAmountsList[winnerNum])]
129+ ScriptResult(WriteSet(gameResult), TransferSet(winTX))
91130 }
92131
93132
Full:
OldNewDifferences
11 {-# STDLIB_VERSION 3 #-}
22 {-# SCRIPT_TYPE ACCOUNT #-}
33 {-# CONTENT_TYPE DAPP #-}
44 let ServerPublicKey = fromBase58String("7im9caiCmE4SL6jTm4KTicHH4bGPf5v59rnfUA3cCRDj")
55
66 let ServerBetPublicKey = fromBase58String("7im9caiCmE4SL6jTm4KTicHH4bGPf5v59rnfUA3cCRDj")
77
88 let WAVELET = ((100 * 1000) * 1000)
99
1010 let COMMISSION = ((5 * WAVELET) / 1000)
11-
12-let TicketsRate = 100
1311
1412 func GenerateRandInt (gameId,rsaSign,lastTicket) = {
1513 let rsaSigValid = sigVerify(toBytes(gameId), rsaSign, ServerPublicKey)
1614 if (rsaSigValid)
1715 then {
1816 let rand = (toInt(sha256(rsaSign)) % lastTicket)
1917 if ((0 > rand))
2018 then ((-1 * rand) + 1)
2119 else (rand + 1)
2220 }
2321 else throw("Invalid RSA signature")
2422 }
2523
2624
27-func getPlayers (gameId) = {
28- let rawDataStr = match getString(this, gameId) {
29- case str: String =>
30- str
31- case _ =>
32- throw(("Couldn't find game by " + gameId))
33- }
34- split(rawDataStr, "_")
25+func getPlayersTickets (gameId,player) = {
26+ let ticketsStr = (((gameId + "_") + player) + "_tickets")
27+ let playerTicketsStr = getStringValue(this, ticketsStr)
28+ playerTicketsStr
3529 }
3630
3731
38-func getPlayersTickets (gameId,Player) = {
39- let playerTicketsStr = match getString(this, ((gameId + "_") + Player)) {
40- case str: String =>
41- str
42- case _ =>
43- throw(("Couldn't find game by " + gameId))
32+func ParseNextAttribute (remaining) = {
33+ let s = size(remaining)
34+ if ((s > 0))
35+ then {
36+ let nn = parseIntValue(take(remaining, 2))
37+ let v = take(drop(remaining, 2), nn)
38+ let tmpRemaining = drop(remaining, (nn + 2))
39+ let remainingState = tmpRemaining
40+[v, remainingState]
41+ }
42+ else throw("Empty string was passed into parseNextAttribute func")
4443 }
45- split(playerTicketsStr, " ")
44+
45+
46+func parseBetStr (ticketsStr) = ["1", "100", "101", "300", "0", "0", "0", "0"]
47+
48+
49+func isWinner (gameId,player,winTicket) = {
50+ let playersTicket = getPlayersTickets(gameId, player)
51+ let bets = parseBetStr(playersTicket)
52+ if (if ((winTicket >= parseIntValue(bets[0])))
53+ then (parseIntValue(bets[1]) >= winTicket)
54+ else false)
55+ then true
56+ else if (if ((winTicket >= parseIntValue(bets[2])))
57+ then (parseIntValue(bets[3]) >= winTicket)
58+ else false)
59+ then true
60+ else if (if ((winTicket >= parseIntValue(bets[4])))
61+ then (parseIntValue(bets[5]) >= winTicket)
62+ else false)
63+ then true
64+ else if (if ((winTicket >= parseIntValue(bets[6])))
65+ then (parseIntValue(bets[7]) >= winTicket)
66+ else false)
67+ then true
68+ else false
69+ }
70+
71+
72+func defineWinner (gameId,players,winAmounts,winTicket) = {
73+ let playersList = split(players, "_")
74+ let winAmountsList = split(winAmounts, "_")
75+ if (isWinner(gameId, playersList[0], winTicket))
76+ then 0
77+ else if (isWinner(gameId, playersList[1], winTicket))
78+ then 1
79+ else if (isWinner(gameId, playersList[2], winTicket))
80+ then 2
81+ else if (isWinner(gameId, playersList[3], winTicket))
82+ then 3
83+ else if (isWinner(gameId, playersList[4], winTicket))
84+ then 4
85+ else if (isWinner(gameId, playersList[5], winTicket))
86+ then 5
87+ else throw("Error with defining winner")
4688 }
4789
4890
4991 @Callable(i)
50-func bet (gameId,gameIdCrypted,tickets) = {
92+func bet (gameId,gameIdCryptedStr,tickets) = {
93+ let gameIdCrypted = fromBase58String(gameIdCryptedStr)
5194 let pmt = extract(i.payment)
5295 let betNotInWaves = isDefined(pmt.assetId)
5396 let feeNotInWaves = isDefined(pmt.assetId)
5497 let playerPubKey58 = toBase58String(i.callerPublicKey)
5598 let playerAddress = addressFromPublicKey(fromBase58String(playerPubKey58))
56- if (betNotInWaves)
57- then throw("Bet amount must be in Waves")
58- else if (feeNotInWaves)
59- then throw("Transaction's fee must be in Waves")
60- else {
61- let rsaSigValid = sigVerify(toBytes(gameId), gameIdCrypted, ServerBetPublicKey)
62- if (rsaSigValid)
63- then {
64- let playerAddressStr = toString(playerAddress)
65- let betData = [DataEntry((playerAddressStr + "_tickets"), tickets)]
66- WriteSet(betData)
67- }
68- else throw("Invalid RSA signature")
69- }
99+ let gameStillOpen = isDefined(getString(this, (gameId + "_result")))
100+ if (!(gameStillOpen))
101+ then if (betNotInWaves)
102+ then throw("Bet amount must be in Waves")
103+ else if (feeNotInWaves)
104+ then throw("Transaction's fee must be in Waves")
105+ else {
106+ let rsaSigValid = sigVerify(toBytes(gameId), gameIdCrypted, ServerBetPublicKey)
107+ if (rsaSigValid)
108+ then {
109+ let playerAddressStr = toString(playerAddress)
110+ let betData = [DataEntry((((gameId + "_") + playerAddressStr) + "_tickets"), tickets)]
111+ WriteSet(betData)
112+ }
113+ else throw("Invalid RSA signature")
114+ }
115+ else throw("This game already end, try make bet again")
70116 }
71117
72118
73119
74120 @Callable(i)
75-func generateResult (gameId,rsaSignCrypted,lastTicket) = {
121+func generateResult (gameId,rsaSignCrypted,lastTicket,players,winAmounts) = {
76122 let rsaSign = fromBase58String(rsaSignCrypted)
77- let randTicket = GenerateRandInt(gameId, rsaSign, lastTicket)
78- let gameResult = [DataEntry((gameId + "_result"), toString(randTicket))]
79- let playersList = getPlayers(gameId)
80- let player1Tickets = getPlayersTickets(gameId, playersList[0])
81- let player1TicketFirst = parseIntValue(player1Tickets[0])
82- let player1TicketLast = parseIntValue(player1Tickets[1])
83- if (if ((randTicket >= player1TicketFirst))
84- then (player1TicketLast >= randTicket)
85- else false)
86- then {
87- let winAmt = 100000000
88- ScriptResult(WriteSet(gameResult), TransferSet([ScriptTransfer(addressFromStringValue(playersList[0]), winAmt, unit)]))
89- }
90- else throw("Player1 not the Winner")
123+ let winTicket = GenerateRandInt(gameId, rsaSign, lastTicket)
124+ let playersList = split(players, "_")
125+ let winAmountsList = split(winAmounts, "_")
126+ let winnerNum = defineWinner(gameId, players, winAmounts, winTicket)
127+ let winTX = [ScriptTransfer(addressFromStringValue(playersList[winnerNum]), parseIntValue(winAmountsList[winnerNum]), unit)]
128+ let gameResult = [DataEntry((gameId + "_result"), toString(winTicket)), DataEntry((gameId + "_winner"), playersList[winnerNum]), DataEntry((gameId + "_winAmt"), winAmountsList[winnerNum])]
129+ ScriptResult(WriteSet(gameResult), TransferSet(winTX))
91130 }
92131
93132

github/deemru/w8io/026f985 
43.26 ms