38 | | - | func validateTwoCoords (x1,y1,x2,y2) = if ((x1 > x2)) |
---|
39 | | - | then false |
---|
40 | | - | else if (if ((x1 == x2)) |
---|
41 | | - | then (y1 > y2) |
---|
42 | | - | else false) |
---|
43 | | - | then false |
---|
44 | | - | else true |
---|
45 | | - | |
---|
46 | | - | |
---|
47 | | - | func validateCoords (x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6) = if (!(validateTwoCoords(x1, y1, x2, y2))) |
---|
48 | | - | then false |
---|
49 | | - | else if (!(validateTwoCoords(x2, y2, x3, y3))) |
---|
50 | | - | then false |
---|
51 | | - | else if (!(validateTwoCoords(x3, y3, x4, y4))) |
---|
52 | | - | then false |
---|
53 | | - | else if (!(validateTwoCoords(x4, y4, x5, y5))) |
---|
54 | | - | then false |
---|
55 | | - | else if (!(validateTwoCoords(x5, y5, x6, y6))) |
---|
56 | | - | then false |
---|
57 | | - | else true |
---|
58 | | - | |
---|
59 | | - | |
---|
60 | | - | func genKeyFromCoords (x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6) = ((((((((((((((((((((((("patent:" + toString(x1)) + ",") + toString(y1)) + ";") + toString(x2)) + ",") + toString(y2)) + ";") + toString(x3)) + ",") + toString(y3)) + ";") + toString(x4)) + ",") + toString(y4)) + ";") + toString(x5)) + ",") + toString(y5)) + ";") + toString(x6)) + ",") + toString(y6)) |
---|
61 | | - | |
---|
62 | | - | |
---|
63 | | - | func getKeyBalanceUser (account) = (account + "_balance") |
---|
64 | | - | |
---|
65 | | - | |
---|
66 | | - | func getValueBalanceUser (account) = getNumberByKey(getKeyBalanceUser(account)) |
---|
67 | | - | |
---|
68 | | - | |
---|
69 | | - | func getKeyWinCount (user) = (user + "_wincnt") |
---|
70 | | - | |
---|
71 | | - | |
---|
72 | | - | func getValueWinCount (user) = getNumberByKey(getKeyWinCount(user)) |
---|
73 | | - | |
---|
74 | | - | |
---|
75 | | - | func getKeyLoseCount (user) = (user + "_losecnt") |
---|
76 | | - | |
---|
77 | | - | |
---|
78 | | - | func getValueLoseCount (user) = getNumberByKey(getKeyLoseCount(user)) |
---|
79 | | - | |
---|
80 | | - | |
---|
81 | | - | func getKeyGame (game) = ("game_" + game) |
---|
82 | | - | |
---|
83 | | - | |
---|
84 | | - | func getValueGame (game) = getStrByKey(getKeyGame(game)) |
---|
85 | | - | |
---|
86 | | - | |
---|
87 | | - | func getKeyInsuranceCount (user) = (user + "_insurance_cnt") |
---|
88 | | - | |
---|
89 | | - | |
---|
90 | | - | func getValueInsuranceCount (user) = getNumberByKey(getKeyInsuranceCount(user)) |
---|
91 | | - | |
---|
92 | | - | |
---|
93 | | - | func getKeyUserPatentCount (user) = (user + "_patent_cnt") |
---|
94 | | - | |
---|
95 | | - | |
---|
96 | | - | func getValueUserPatentCount (user) = getNumberByKey(getKeyUserPatentCount(user)) |
---|
| 24 | + | func getEnergyValue (meterAddress,type) = match getInteger(this, (((meterAddress + "_") + type) + "_value")) { |
---|
| 25 | + | case x: Unit => |
---|
| 26 | + | 0 |
---|
| 27 | + | case x: Int => |
---|
| 28 | + | x |
---|
| 29 | + | case _ => |
---|
| 30 | + | throw() |
---|
| 31 | + | } |
---|
100 | | - | func patent (x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6) = { |
---|
101 | | - | let pmt = extract(i.payment) |
---|
102 | | - | if (isDefined(pmt.assetId)) |
---|
103 | | - | then throw("can use WAVES only at the moment") |
---|
104 | | - | else { |
---|
105 | | - | let userAddress = toBase58String(i.caller.bytes) |
---|
106 | | - | if ((pmt.amount != PRICE)) |
---|
107 | | - | then throw("purchase amount cannot be not equal to the price") |
---|
108 | | - | else if (!(validateCoords(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6))) |
---|
109 | | - | then throw("invalid coordinates order") |
---|
110 | | - | else if ((getStrByKey(genKeyFromCoords(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6)) != NONE)) |
---|
111 | | - | then throw("this patent is already exists") |
---|
112 | | - | else if ((getValueUserPatentCount(userAddress) >= 5)) |
---|
113 | | - | then throw("you cannot have more than 2 patents") |
---|
114 | | - | else WriteSet([DataEntry(genKeyFromCoords(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6), userAddress), DataEntry(getKeyUserPatentCount(userAddress), (getValueUserPatentCount(userAddress) + 1))]) |
---|
115 | | - | } |
---|
| 35 | + | func sendData (newValue) = { |
---|
| 36 | + | let meterAddress = toBase58String(i.caller.bytes) |
---|
| 37 | + | let meterStatus = getMeterStatus(meterAddress) |
---|
| 38 | + | if ((meterStatus == "active")) |
---|
| 39 | + | then WriteSet([DataEntry((meterAddress + "_current_value"), newValue)]) |
---|
| 40 | + | else throw("Meter address was not found or meter is not active") |
---|
121 | | - | func buyPatent (x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6) = { |
---|
122 | | - | let pmt = extract(i.payment) |
---|
123 | | - | if (isDefined(pmt.assetId)) |
---|
124 | | - | then throw("can use WAVES only at the moment") |
---|
125 | | - | else { |
---|
126 | | - | let userAddress = toBase58String(i.caller.bytes) |
---|
127 | | - | let patentOwnerAddress = getStrByKey(genKeyFromCoords(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6)) |
---|
128 | | - | if ((pmt.amount != PATENTPRICE)) |
---|
129 | | - | then throw("purchase amount cannot be not equal to the price") |
---|
130 | | - | else if (!(validateCoords(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6))) |
---|
131 | | - | then throw("invalid coordinates order") |
---|
132 | | - | else if ((patentOwnerAddress == NONE)) |
---|
133 | | - | then throw("there is no such patent") |
---|
134 | | - | else if ((patentOwnerAddress == userAddress)) |
---|
135 | | - | then throw("you cannot buy your own patent") |
---|
136 | | - | else WriteSet([DataEntry(genKeyFromCoords(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6), userAddress), DataEntry(getKeyBalanceUser(patentOwnerAddress), (getValueBalanceUser(patentOwnerAddress) + pmt.amount)), DataEntry(getKeyUserPatentCount(userAddress), (getValueUserPatentCount(userAddress) + 1)), DataEntry(getKeyUserPatentCount(patentOwnerAddress), (getValueUserPatentCount(userAddress) - 1))]) |
---|
| 46 | + | func makePayment (meterAddress,tarif,assetId) = { |
---|
| 47 | + | let sender = toBase58String(i.caller.bytes) |
---|
| 48 | + | let meterStatus = getMeterStatus(meterAddress) |
---|
| 49 | + | let bankAddress = getBankAddress() |
---|
| 50 | + | if (if ((meterStatus == "active")) |
---|
| 51 | + | then (sender == bankAddress) |
---|
| 52 | + | else false) |
---|
| 53 | + | then { |
---|
| 54 | + | let currentValue = getEnergyValue(meterAddress, "current") |
---|
| 55 | + | let prevValue = getEnergyValue(meterAddress, "prev") |
---|
| 56 | + | if ((prevValue != currentValue)) |
---|
| 57 | + | then { |
---|
| 58 | + | let balance = assetBalance(this, fromBase58String(assetId)) |
---|
| 59 | + | let amountToPay = (tarif * (currentValue - prevValue)) |
---|
| 60 | + | if ((balance > amountToPay)) |
---|
| 61 | + | then ScriptResult(WriteSet([DataEntry((meterAddress + "_prev_value"), currentValue), DataEntry(bankAddress, ((((("invoice_date_" + toString(lastBlock.timestamp)) + "_client_") + toBase58String(this.bytes)) + "_meter_") + meterAddress))]), TransferSet([ScriptTransfer(i.caller, amountToPay, fromBase58String(assetId))])) |
---|
| 62 | + | else throw("insufficient balance") |
---|
| 63 | + | } |
---|
| 64 | + | else throw("No energy consumed during the period") |
---|
141 | | - | |
---|
142 | | - | @Callable(i) |
---|
143 | | - | func withdraw () = { |
---|
144 | | - | let userAddress = toBase58String(i.caller.bytes) |
---|
145 | | - | let balance = getValueBalanceUser(userAddress) |
---|
146 | | - | if ((0 >= balance)) |
---|
147 | | - | then throw("insufficient balance") |
---|
148 | | - | else ScriptResult(WriteSet([DataEntry(getKeyBalanceUser(userAddress), 0)]), TransferSet([ScriptTransfer(addressFromStringValue(userAddress), balance, unit)])) |
---|
149 | | - | } |
---|
150 | | - | |
---|
151 | | - | |
---|
152 | | - | |
---|
153 | | - | @Callable(i) |
---|
154 | | - | func gameReport (game,winner,loser) = { |
---|
155 | | - | let oracleAddres = toBase58String(i.caller.bytes) |
---|
156 | | - | if ((oracleAddres != ORACLE)) |
---|
157 | | - | then throw("only oracle can inform about games results") |
---|
158 | | - | else if ((winner == loser)) |
---|
159 | | - | then throw("winner and loser must be different") |
---|
160 | | - | else if ((getValueGame(game) != NONE)) |
---|
161 | | - | then throw("this game has already been played") |
---|
162 | | - | else WriteSet([DataEntry(getKeyGame(game), (((winner + "-WIN;") + loser) + "-LOSE")), DataEntry(getKeyWinCount(winner), (getValueWinCount(winner) + 1)), DataEntry(getKeyLoseCount(loser), (getValueLoseCount(loser) + 1))]) |
---|
163 | | - | } |
---|
164 | | - | |
---|
165 | | - | |
---|
166 | | - | |
---|
167 | | - | @Callable(i) |
---|
168 | | - | func insure () = { |
---|
169 | | - | let pmt = extract(i.payment) |
---|
170 | | - | let insurancePrice = fraction(PRICE, 7, 100) |
---|
171 | | - | if (isDefined(pmt.assetId)) |
---|
172 | | - | then throw("can use WAVES only at the moment") |
---|
173 | | - | else { |
---|
174 | | - | let userAddress = toBase58String(i.caller.bytes) |
---|
175 | | - | if ((pmt.amount != insurancePrice)) |
---|
176 | | - | then throw("insurance payment amount cannot be not equal to the insurance price") |
---|
177 | | - | else WriteSet([DataEntry(getKeyInsuranceCount(userAddress), (getValueInsuranceCount(userAddress) + 1))]) |
---|
178 | | - | } |
---|
179 | | - | } |
---|
180 | | - | |
---|
| 70 | + | @Verifier(tx) |
---|
| 71 | + | func verify () = match tx { |
---|
| 72 | + | case t: DataTransaction => |
---|
| 73 | + | false |
---|
| 74 | + | case t: SetScriptTransaction => |
---|
| 75 | + | false |
---|
| 76 | + | case _ => |
---|
| 77 | + | sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey) |
---|
| 78 | + | } |
---|