1 | | - | # no script |
---|
| 1 | + | {-# STDLIB_VERSION 6 #-} |
---|
| 2 | + | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
| 3 | + | {-# CONTENT_TYPE DAPP #-} |
---|
| 4 | + | let SEP = "__" |
---|
| 5 | + | |
---|
| 6 | + | let PRECISION = 1000000000000 |
---|
| 7 | + | |
---|
| 8 | + | func join (ar) = makeString(ar, SEP) |
---|
| 9 | + | |
---|
| 10 | + | |
---|
| 11 | + | func keyInitialized () = join(["%s", "initialized"]) |
---|
| 12 | + | |
---|
| 13 | + | |
---|
| 14 | + | func keyCoordinatorAddress () = join(["%s", "coordinatorAddress"]) |
---|
| 15 | + | |
---|
| 16 | + | |
---|
| 17 | + | func keyVerifierAddress () = join(["%s", "verifierAddress"]) |
---|
| 18 | + | |
---|
| 19 | + | |
---|
| 20 | + | func keyRewardExchangerAddress () = join(["%s", "rewardExchangeAddress"]) |
---|
| 21 | + | |
---|
| 22 | + | |
---|
| 23 | + | func keySubvaultAddress () = join(["%s", "subvaultAddress"]) |
---|
| 24 | + | |
---|
| 25 | + | |
---|
| 26 | + | func keySigned (_address,_txId) = join(["%s%s%s", "signed", _address, _txId]) |
---|
| 27 | + | |
---|
| 28 | + | |
---|
| 29 | + | func keyProtocolActive () = join(["%s", "protocolActive"]) |
---|
| 30 | + | |
---|
| 31 | + | |
---|
| 32 | + | func isInitialized () = valueOrElse(getBoolean(this, keyInitialized()), false) |
---|
| 33 | + | |
---|
| 34 | + | |
---|
| 35 | + | func mustNotInitialized () = if (isInitialized()) |
---|
| 36 | + | then throw("Already initialized") |
---|
| 37 | + | else unit |
---|
| 38 | + | |
---|
| 39 | + | |
---|
| 40 | + | func mustSelf (i) = if ((i.caller != this)) |
---|
| 41 | + | then throw("Only self invocation allowed.") |
---|
| 42 | + | else unit |
---|
| 43 | + | |
---|
| 44 | + | |
---|
| 45 | + | func coordinator () = addressFromStringValue(valueOrErrorMessage(getString(keyCoordinatorAddress()), "Coordinator is not set")) |
---|
| 46 | + | |
---|
| 47 | + | |
---|
| 48 | + | func verifier () = match getString(keyCoordinatorAddress()) { |
---|
| 49 | + | case s: String => |
---|
| 50 | + | getString(addressFromStringValue(s), keyVerifierAddress()) |
---|
| 51 | + | case _: Unit => |
---|
| 52 | + | unit |
---|
| 53 | + | case _ => |
---|
| 54 | + | throw("Match error") |
---|
| 55 | + | } |
---|
| 56 | + | |
---|
| 57 | + | |
---|
| 58 | + | func getAddress (key,err) = addressFromStringValue(valueOrErrorMessage(getString(coordinator(), key), err)) |
---|
| 59 | + | |
---|
| 60 | + | |
---|
| 61 | + | func getRewardExchangerAddress () = getAddress(keyRewardExchangerAddress(), "Reward exchanger is not set") |
---|
| 62 | + | |
---|
| 63 | + | |
---|
| 64 | + | func isActive () = valueOrElse(getBoolean(coordinator(), keyProtocolActive()), false) |
---|
| 65 | + | |
---|
| 66 | + | |
---|
| 67 | + | func mustActive () = if (if (!(isActive())) |
---|
| 68 | + | then true |
---|
| 69 | + | else !(isInitialized())) |
---|
| 70 | + | then throw("Protocol is disabled. Please contact support.") |
---|
| 71 | + | else unit |
---|
| 72 | + | |
---|
| 73 | + | |
---|
| 74 | + | func mustHaveOnePayment (i) = if ((size(i.payments) != 1)) |
---|
| 75 | + | then throw("Must have one payment.") |
---|
| 76 | + | else unit |
---|
| 77 | + | |
---|
| 78 | + | |
---|
| 79 | + | func isPositive (number) = if ((0 >= number)) |
---|
| 80 | + | then throw("Attribute should be positive.") |
---|
| 81 | + | else unit |
---|
| 82 | + | |
---|
| 83 | + | |
---|
| 84 | + | func checkAddress (_address) = match addressFromString(_address) { |
---|
| 85 | + | case address: Address => |
---|
| 86 | + | true |
---|
| 87 | + | case _: Unit => |
---|
| 88 | + | throw("Invalid address") |
---|
| 89 | + | case _ => |
---|
| 90 | + | throw("Match error") |
---|
| 91 | + | } |
---|
| 92 | + | |
---|
| 93 | + | |
---|
| 94 | + | func mustWavesPayment (i) = { |
---|
| 95 | + | let check = mustHaveOnePayment(i) |
---|
| 96 | + | if ((check == check)) |
---|
| 97 | + | then { |
---|
| 98 | + | let pmtAssetId = i.payments[0].assetId |
---|
| 99 | + | match pmtAssetId { |
---|
| 100 | + | case a: Unit => |
---|
| 101 | + | unit |
---|
| 102 | + | case _ => |
---|
| 103 | + | throw("Only WAVES accepted") |
---|
| 104 | + | } |
---|
| 105 | + | } |
---|
| 106 | + | else throw("Strict value is not equal to itself.") |
---|
| 107 | + | } |
---|
| 108 | + | |
---|
| 109 | + | |
---|
| 110 | + | func keySWavesAddress () = join(["%s", "sWavesAddress"]) |
---|
| 111 | + | |
---|
| 112 | + | |
---|
| 113 | + | func keySWavesAsset () = join(["%s", "sWavesAsset"]) |
---|
| 114 | + | |
---|
| 115 | + | |
---|
| 116 | + | func keyBalance () = join(["%s", "balance"]) |
---|
| 117 | + | |
---|
| 118 | + | |
---|
| 119 | + | func keyTotalProfit () = join(["%s", "totalProfit"]) |
---|
| 120 | + | |
---|
| 121 | + | |
---|
| 122 | + | func getSWavesAddressStr () = valueOrErrorMessage(getString(this, keySWavesAddress()), "sWaves address is not set") |
---|
| 123 | + | |
---|
| 124 | + | |
---|
| 125 | + | func sWavesAddress () = addressFromStringValue(getSWavesAddressStr()) |
---|
| 126 | + | |
---|
| 127 | + | |
---|
| 128 | + | func getSWavesAssetStr () = valueOrErrorMessage(getString(this, keySWavesAsset()), "sWaves asset is not set") |
---|
| 129 | + | |
---|
| 130 | + | |
---|
| 131 | + | func sWavesAsset () = fromBase58String(getSWavesAssetStr()) |
---|
| 132 | + | |
---|
| 133 | + | |
---|
| 134 | + | func getBalance () = valueOrElse(getInteger(keyBalance()), 0) |
---|
| 135 | + | |
---|
| 136 | + | |
---|
| 137 | + | func getTotalProfit () = valueOrElse(getInteger(keyTotalProfit()), 0) |
---|
| 138 | + | |
---|
| 139 | + | |
---|
| 140 | + | func getSubvaultAddressStr () = valueOrErrorMessage(getString(this, keySubvaultAddress()), "Subvault address is not set") |
---|
| 141 | + | |
---|
| 142 | + | |
---|
| 143 | + | func getSubvaultAddress () = addressFromString(getSubvaultAddressStr()) |
---|
| 144 | + | |
---|
| 145 | + | |
---|
| 146 | + | func mustSubvault (i) = if ((i.caller != getSubvaultAddress())) |
---|
| 147 | + | then throw("Not allowed") |
---|
| 148 | + | else unit |
---|
| 149 | + | |
---|
| 150 | + | |
---|
| 151 | + | func mustExchanger (i) = if ((i.caller != getRewardExchangerAddress())) |
---|
| 152 | + | then throw("Not allowed") |
---|
| 153 | + | else unit |
---|
| 154 | + | |
---|
| 155 | + | |
---|
| 156 | + | func sWavesToWithdraw (_amount) = { |
---|
| 157 | + | let rate = { |
---|
| 158 | + | let @ = invoke(sWavesAddress(), "getRate", nil, nil) |
---|
| 159 | + | if ($isInstanceOf(@, "String")) |
---|
| 160 | + | then @ |
---|
| 161 | + | else throw(($getType(@) + " couldn't be cast to String")) |
---|
| 162 | + | } |
---|
| 163 | + | if ((rate == rate)) |
---|
| 164 | + | then toInt(fraction(toBigInt(_amount), toBigInt(PRECISION), parseBigIntValue(rate), CEILING)) |
---|
| 165 | + | else throw("Strict value is not equal to itself.") |
---|
| 166 | + | } |
---|
| 167 | + | |
---|
| 168 | + | |
---|
| 169 | + | func extractProfit () = { |
---|
| 170 | + | let sWavesAmount = assetBalance(this, sWavesAsset()) |
---|
| 171 | + | if ((sWavesAmount == sWavesAmount)) |
---|
| 172 | + | then { |
---|
| 173 | + | let sWavesPayment = AttachedPayment(sWavesAsset(), sWavesAmount) |
---|
| 174 | + | if ((sWavesPayment == sWavesPayment)) |
---|
| 175 | + | then { |
---|
| 176 | + | let doWithdraw = invoke(sWavesAddress(), "withdraw", nil, [sWavesPayment]) |
---|
| 177 | + | if ((doWithdraw == doWithdraw)) |
---|
| 178 | + | then { |
---|
| 179 | + | let wavesActualBalance = wavesBalance(this).regular |
---|
| 180 | + | if ((wavesActualBalance == wavesActualBalance)) |
---|
| 181 | + | then { |
---|
| 182 | + | let accountingBalance = getBalance() |
---|
| 183 | + | if ((accountingBalance == accountingBalance)) |
---|
| 184 | + | then if ((accountingBalance > wavesActualBalance)) |
---|
| 185 | + | then throw("Unable to proceed. Invalid state: wavesActualBalance < accountingBalance") |
---|
| 186 | + | else { |
---|
| 187 | + | let profit = (wavesActualBalance - accountingBalance) |
---|
| 188 | + | if ((profit == profit)) |
---|
| 189 | + | then { |
---|
| 190 | + | let wavesPayment = AttachedPayment(unit, accountingBalance) |
---|
| 191 | + | if ((wavesPayment == wavesPayment)) |
---|
| 192 | + | then { |
---|
| 193 | + | let doDeposit = invoke(sWavesAddress(), "deposit", nil, [wavesPayment]) |
---|
| 194 | + | if ((doDeposit == doDeposit)) |
---|
| 195 | + | then profit |
---|
| 196 | + | else throw("Strict value is not equal to itself.") |
---|
| 197 | + | } |
---|
| 198 | + | else throw("Strict value is not equal to itself.") |
---|
| 199 | + | } |
---|
| 200 | + | else throw("Strict value is not equal to itself.") |
---|
| 201 | + | } |
---|
| 202 | + | else throw("Strict value is not equal to itself.") |
---|
| 203 | + | } |
---|
| 204 | + | else throw("Strict value is not equal to itself.") |
---|
| 205 | + | } |
---|
| 206 | + | else throw("Strict value is not equal to itself.") |
---|
| 207 | + | } |
---|
| 208 | + | else throw("Strict value is not equal to itself.") |
---|
| 209 | + | } |
---|
| 210 | + | else throw("Strict value is not equal to itself.") |
---|
| 211 | + | } |
---|
| 212 | + | |
---|
| 213 | + | |
---|
| 214 | + | @Callable(i) |
---|
| 215 | + | func initialize (_coordinatorAddress,_subvaultAddress,_sWavesAddress) = { |
---|
| 216 | + | let checks = [mustSelf(i), mustNotInitialized(), checkAddress(_coordinatorAddress), checkAddress(_subvaultAddress), checkAddress(_sWavesAddress)] |
---|
| 217 | + | if ((checks == checks)) |
---|
| 218 | + | then { |
---|
| 219 | + | let sWaves = addressFromStringValue(_sWavesAddress) |
---|
| 220 | + | let sWavesAssetStr = valueOrErrorMessage(getString(sWaves, "ASSET"), "No sWaves Asset") |
---|
| 221 | + | [StringEntry(keyCoordinatorAddress(), _coordinatorAddress), StringEntry(keySubvaultAddress(), _subvaultAddress), StringEntry(keySWavesAddress(), _sWavesAddress), StringEntry(keySWavesAsset(), sWavesAssetStr), BooleanEntry(keyInitialized(), true)] |
---|
| 222 | + | } |
---|
| 223 | + | else throw("Strict value is not equal to itself.") |
---|
| 224 | + | } |
---|
| 225 | + | |
---|
| 226 | + | |
---|
| 227 | + | |
---|
| 228 | + | @Callable(i) |
---|
| 229 | + | func stake () = { |
---|
| 230 | + | let checks = [mustActive(), mustSubvault(i), mustWavesPayment(i)] |
---|
| 231 | + | if ((checks == checks)) |
---|
| 232 | + | then { |
---|
| 233 | + | let payment = i.payments[0] |
---|
| 234 | + | let doDeposit = invoke(sWavesAddress(), "deposit", nil, [payment]) |
---|
| 235 | + | if ((doDeposit == doDeposit)) |
---|
| 236 | + | then { |
---|
| 237 | + | let newAssetBalance = (getBalance() + payment.amount) |
---|
| 238 | + | [IntegerEntry(keyBalance(), newAssetBalance)] |
---|
| 239 | + | } |
---|
| 240 | + | else throw("Strict value is not equal to itself.") |
---|
| 241 | + | } |
---|
| 242 | + | else throw("Strict value is not equal to itself.") |
---|
| 243 | + | } |
---|
| 244 | + | |
---|
| 245 | + | |
---|
| 246 | + | |
---|
| 247 | + | @Callable(i) |
---|
| 248 | + | func unstake (_amount) = { |
---|
| 249 | + | let checks = [mustActive(), mustSubvault(i), isPositive(_amount)] |
---|
| 250 | + | if ((checks == checks)) |
---|
| 251 | + | then if ((_amount > getBalance())) |
---|
| 252 | + | then throw("Unable to proceed. Amount larger than balance.") |
---|
| 253 | + | else { |
---|
| 254 | + | let newAssetBalance = (getBalance() - _amount) |
---|
| 255 | + | let sWavesAmount = sWavesToWithdraw(_amount) |
---|
| 256 | + | if ((sWavesAmount == sWavesAmount)) |
---|
| 257 | + | then { |
---|
| 258 | + | let payment = AttachedPayment(sWavesAsset(), sWavesAmount) |
---|
| 259 | + | let doWithdraw = invoke(sWavesAddress(), "withdraw", nil, [payment]) |
---|
| 260 | + | if ((doWithdraw == doWithdraw)) |
---|
| 261 | + | then [IntegerEntry(keyBalance(), newAssetBalance), ScriptTransfer(i.caller, _amount, unit)] |
---|
| 262 | + | else throw("Strict value is not equal to itself.") |
---|
| 263 | + | } |
---|
| 264 | + | else throw("Strict value is not equal to itself.") |
---|
| 265 | + | } |
---|
| 266 | + | else throw("Strict value is not equal to itself.") |
---|
| 267 | + | } |
---|
| 268 | + | |
---|
| 269 | + | |
---|
| 270 | + | |
---|
| 271 | + | @Callable(i) |
---|
| 272 | + | func claimProfit () = { |
---|
| 273 | + | let checks = [mustActive(), mustExchanger(i)] |
---|
| 274 | + | if ((checks == checks)) |
---|
| 275 | + | then { |
---|
| 276 | + | let profit = extractProfit() |
---|
| 277 | + | if ((profit == profit)) |
---|
| 278 | + | then { |
---|
| 279 | + | let totalProfit = (getTotalProfit() + profit) |
---|
| 280 | + | $Tuple2([IntegerEntry(keyTotalProfit(), totalProfit), ScriptTransfer(i.caller, profit, unit)], profit) |
---|
| 281 | + | } |
---|
| 282 | + | else throw("Strict value is not equal to itself.") |
---|
| 283 | + | } |
---|
| 284 | + | else throw("Strict value is not equal to itself.") |
---|
| 285 | + | } |
---|
| 286 | + | |
---|
| 287 | + | |
---|
| 288 | + | @Verifier(tx) |
---|
| 289 | + | func verify () = match verifier() { |
---|
| 290 | + | case address: String => |
---|
| 291 | + | valueOrElse(getBoolean(addressFromStringValue(address), keySigned(toString(this), toBase58String(tx.id))), false) |
---|
| 292 | + | case _ => |
---|
| 293 | + | sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey) |
---|
| 294 | + | } |
---|
| 295 | + | |
---|