1 | | - | # no script |
---|
| 1 | + | {-# STDLIB_VERSION 6 #-} |
---|
| 2 | + | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
| 3 | + | {-# CONTENT_TYPE DAPP #-} |
---|
| 4 | + | let SEP = "__" |
---|
| 5 | + | |
---|
| 6 | + | func join (ar) = makeString(ar, SEP) |
---|
| 7 | + | |
---|
| 8 | + | |
---|
| 9 | + | func keyInitialized () = join(["%s", "initialized"]) |
---|
| 10 | + | |
---|
| 11 | + | |
---|
| 12 | + | func keyCoordinatorAddress () = join(["%s", "coordinatorAddress"]) |
---|
| 13 | + | |
---|
| 14 | + | |
---|
| 15 | + | func keyVerifierAddress () = join(["%s", "verifierAddress"]) |
---|
| 16 | + | |
---|
| 17 | + | |
---|
| 18 | + | func keyMinterAddress () = join(["%s", "minterAddress"]) |
---|
| 19 | + | |
---|
| 20 | + | |
---|
| 21 | + | func keyEastAsset () = join(["%s", "eastAsset"]) |
---|
| 22 | + | |
---|
| 23 | + | |
---|
| 24 | + | func keySigned (_address,_txId) = join(["%s%s%s", "signed", _address, _txId]) |
---|
| 25 | + | |
---|
| 26 | + | |
---|
| 27 | + | func keyProtocolActive () = join(["%s", "protocolActive"]) |
---|
| 28 | + | |
---|
| 29 | + | |
---|
| 30 | + | func isInitialized () = valueOrElse(getBoolean(this, keyInitialized()), false) |
---|
| 31 | + | |
---|
| 32 | + | |
---|
| 33 | + | func mustInitialized () = if (!(isInitialized())) |
---|
| 34 | + | then throw("Not initialized") |
---|
| 35 | + | else unit |
---|
| 36 | + | |
---|
| 37 | + | |
---|
| 38 | + | func mustNotInitialized () = if (isInitialized()) |
---|
| 39 | + | then throw("Already initialized") |
---|
| 40 | + | else unit |
---|
| 41 | + | |
---|
| 42 | + | |
---|
| 43 | + | func mustSelf (i) = if ((i.caller != this)) |
---|
| 44 | + | then throw("Only self invocation allowed.") |
---|
| 45 | + | else unit |
---|
| 46 | + | |
---|
| 47 | + | |
---|
| 48 | + | func coordinator () = addressFromStringValue(valueOrErrorMessage(getString(keyCoordinatorAddress()), "Coordinator is not set")) |
---|
| 49 | + | |
---|
| 50 | + | |
---|
| 51 | + | func verifier () = match getString(keyCoordinatorAddress()) { |
---|
| 52 | + | case s: String => |
---|
| 53 | + | getString(addressFromStringValue(s), keyVerifierAddress()) |
---|
| 54 | + | case _: Unit => |
---|
| 55 | + | unit |
---|
| 56 | + | case _ => |
---|
| 57 | + | throw("Match error") |
---|
| 58 | + | } |
---|
| 59 | + | |
---|
| 60 | + | |
---|
| 61 | + | func getAddress (key,err) = addressFromStringValue(valueOrErrorMessage(getString(coordinator(), key), err)) |
---|
| 62 | + | |
---|
| 63 | + | |
---|
| 64 | + | func getMinterAddress () = getAddress(keyMinterAddress(), "Minter is not set") |
---|
| 65 | + | |
---|
| 66 | + | |
---|
| 67 | + | func getEastAssetStr () = valueOrErrorMessage(getString(coordinator(), keyEastAsset()), "East asset is not set") |
---|
| 68 | + | |
---|
| 69 | + | |
---|
| 70 | + | func getEastAsset () = fromBase58String(getEastAssetStr()) |
---|
| 71 | + | |
---|
| 72 | + | |
---|
| 73 | + | func isActive () = valueOrElse(getBoolean(coordinator(), keyProtocolActive()), false) |
---|
| 74 | + | |
---|
| 75 | + | |
---|
| 76 | + | func mustActive () = if (if (!(isActive())) |
---|
| 77 | + | then true |
---|
| 78 | + | else !(isInitialized())) |
---|
| 79 | + | then throw("Protocol is disabled. Please contact support.") |
---|
| 80 | + | else unit |
---|
| 81 | + | |
---|
| 82 | + | |
---|
| 83 | + | func mustHaveOnePayment (i) = if ((size(i.payments) != 1)) |
---|
| 84 | + | then throw("Must have one payment.") |
---|
| 85 | + | else unit |
---|
| 86 | + | |
---|
| 87 | + | |
---|
| 88 | + | func getAssetIdFromString (assetId) = if ((assetId == "WAVES")) |
---|
| 89 | + | then unit |
---|
| 90 | + | else fromBase58String(assetId) |
---|
| 91 | + | |
---|
| 92 | + | |
---|
| 93 | + | func isAsset (p,checkingAsset) = { |
---|
| 94 | + | let assetId = match checkingAsset { |
---|
| 95 | + | case bv: ByteVector => |
---|
| 96 | + | bv |
---|
| 97 | + | case s: String => |
---|
| 98 | + | getAssetIdFromString(s) |
---|
| 99 | + | case _: Unit => |
---|
| 100 | + | unit |
---|
| 101 | + | case _ => |
---|
| 102 | + | throw("Match error") |
---|
| 103 | + | } |
---|
| 104 | + | match assetId { |
---|
| 105 | + | case bv: ByteVector => |
---|
| 106 | + | let name = match assetInfo(bv) { |
---|
| 107 | + | case asset: Asset => |
---|
| 108 | + | asset.name |
---|
| 109 | + | case _: Unit => |
---|
| 110 | + | throw(("Can't find asset " + toBase58String(bv))) |
---|
| 111 | + | case _ => |
---|
| 112 | + | throw("Match error") |
---|
| 113 | + | } |
---|
| 114 | + | let err = throw(("Attached payment asset is not " + name)) |
---|
| 115 | + | match p.assetId { |
---|
| 116 | + | case paymentAsset: ByteVector => |
---|
| 117 | + | if ((paymentAsset != assetId)) |
---|
| 118 | + | then err |
---|
| 119 | + | else unit |
---|
| 120 | + | case _: Unit => |
---|
| 121 | + | err |
---|
| 122 | + | case _ => |
---|
| 123 | + | throw("Match error") |
---|
| 124 | + | } |
---|
| 125 | + | case _: Unit => |
---|
| 126 | + | if ((p.assetId != unit)) |
---|
| 127 | + | then throw("Attached payment asset is not WAVES") |
---|
| 128 | + | else unit |
---|
| 129 | + | case _ => |
---|
| 130 | + | throw("Match error") |
---|
| 131 | + | } |
---|
| 132 | + | } |
---|
| 133 | + | |
---|
| 134 | + | |
---|
| 135 | + | func checkAddress (_address) = match addressFromString(_address) { |
---|
| 136 | + | case address: Address => |
---|
| 137 | + | true |
---|
| 138 | + | case _: Unit => |
---|
| 139 | + | throw("Invalid address") |
---|
| 140 | + | case _ => |
---|
| 141 | + | throw("Match error") |
---|
| 142 | + | } |
---|
| 143 | + | |
---|
| 144 | + | |
---|
| 145 | + | func keyAllowedAddress (_address) = join(["%s%s", "allowedAddress", _address]) |
---|
| 146 | + | |
---|
| 147 | + | |
---|
| 148 | + | func keyAllowedAsset (_asset) = join(["%s%s", "allowedAsset", _asset]) |
---|
| 149 | + | |
---|
| 150 | + | |
---|
| 151 | + | func isAddressEnabled (_address) = valueOrElse(getBoolean(this, keyAllowedAddress(_address)), false) |
---|
| 152 | + | |
---|
| 153 | + | |
---|
| 154 | + | func isAssetEnabled (_asset) = valueOrElse(getBoolean(this, keyAllowedAsset(_asset)), false) |
---|
| 155 | + | |
---|
| 156 | + | |
---|
| 157 | + | func mustAllowedAddress (i) = { |
---|
| 158 | + | let callerAddressStr = toBase58String(i.caller.bytes) |
---|
| 159 | + | let isEnabled = isAddressEnabled(callerAddressStr) |
---|
| 160 | + | if (isEnabled) |
---|
| 161 | + | then unit |
---|
| 162 | + | else throw(((("Unknown caller address. " + toString(i.caller)) + " ") + toBase58String(i.caller.bytes))) |
---|
| 163 | + | } |
---|
| 164 | + | |
---|
| 165 | + | |
---|
| 166 | + | func mustOnlyAllowedAsset (i) = { |
---|
| 167 | + | let assetIdStr = toBase58String(value(i.payments[0].assetId)) |
---|
| 168 | + | let paymentsSize = size(i.payments) |
---|
| 169 | + | let isEnabled = isAssetEnabled(assetIdStr) |
---|
| 170 | + | let isOnly = (paymentsSize == 1) |
---|
| 171 | + | if (if (isEnabled) |
---|
| 172 | + | then isOnly |
---|
| 173 | + | else false) |
---|
| 174 | + | then unit |
---|
| 175 | + | else throw(((("Unknown assetId or payment is not single. " + assetIdStr) + " ") + toString(paymentsSize))) |
---|
| 176 | + | } |
---|
| 177 | + | |
---|
| 178 | + | |
---|
| 179 | + | func checkAsset (_asset) = { |
---|
| 180 | + | let asset = assetInfo(fromBase58String(_asset)) |
---|
| 181 | + | match asset { |
---|
| 182 | + | case info: Asset => |
---|
| 183 | + | if ((info.decimals == 6)) |
---|
| 184 | + | then true |
---|
| 185 | + | else throw("Invalid asset") |
---|
| 186 | + | case _: Unit => |
---|
| 187 | + | throw(("Unknown asset. " + _asset)) |
---|
| 188 | + | case _ => |
---|
| 189 | + | throw("Match error") |
---|
| 190 | + | } |
---|
| 191 | + | } |
---|
| 192 | + | |
---|
| 193 | + | |
---|
| 194 | + | func checkAddressEnabled (_address) = if (isAddressEnabled(_address)) |
---|
| 195 | + | then unit |
---|
| 196 | + | else throw("Address already disabled") |
---|
| 197 | + | |
---|
| 198 | + | |
---|
| 199 | + | func checkAddressDisabled (_address) = if (isAddressEnabled(_address)) |
---|
| 200 | + | then throw("Address already enabled") |
---|
| 201 | + | else unit |
---|
| 202 | + | |
---|
| 203 | + | |
---|
| 204 | + | func checkAssetEnabled (_asset) = if (isAssetEnabled(_asset)) |
---|
| 205 | + | then unit |
---|
| 206 | + | else throw("Asset already disabled") |
---|
| 207 | + | |
---|
| 208 | + | |
---|
| 209 | + | func checkAssetDisabled (_asset) = if (isAssetEnabled(_asset)) |
---|
| 210 | + | then throw("Asset already enabled") |
---|
| 211 | + | else unit |
---|
| 212 | + | |
---|
| 213 | + | |
---|
| 214 | + | @Callable(i) |
---|
| 215 | + | func initialize (_coordinatorAddress) = { |
---|
| 216 | + | let checks = [mustSelf(i), mustNotInitialized(), checkAddress(_coordinatorAddress)] |
---|
| 217 | + | if ((checks == checks)) |
---|
| 218 | + | then [StringEntry(keyCoordinatorAddress(), _coordinatorAddress), BooleanEntry(keyInitialized(), true)] |
---|
| 219 | + | else throw("Strict value is not equal to itself.") |
---|
| 220 | + | } |
---|
| 221 | + | |
---|
| 222 | + | |
---|
| 223 | + | |
---|
| 224 | + | @Callable(i) |
---|
| 225 | + | func issue () = { |
---|
| 226 | + | let checks = [mustActive(), mustAllowedAddress(i), mustOnlyAllowedAsset(i)] |
---|
| 227 | + | if ((checks == checks)) |
---|
| 228 | + | then { |
---|
| 229 | + | let amount = i.payments[0].amount |
---|
| 230 | + | let minter = getMinterAddress() |
---|
| 231 | + | let doIssueEast = invoke(minter, "issue", [amount], nil) |
---|
| 232 | + | if ((doIssueEast == doIssueEast)) |
---|
| 233 | + | then { |
---|
| 234 | + | let eastAsset = getEastAsset() |
---|
| 235 | + | [ScriptTransfer(i.caller, amount, eastAsset)] |
---|
| 236 | + | } |
---|
| 237 | + | else throw("Strict value is not equal to itself.") |
---|
| 238 | + | } |
---|
| 239 | + | else throw("Strict value is not equal to itself.") |
---|
| 240 | + | } |
---|
| 241 | + | |
---|
| 242 | + | |
---|
| 243 | + | |
---|
| 244 | + | @Callable(i) |
---|
| 245 | + | func burnFor (_asset) = { |
---|
| 246 | + | let eastAsset = getEastAsset() |
---|
| 247 | + | let payment = i.payments[0] |
---|
| 248 | + | let checks = [mustActive(), mustAllowedAddress(i), mustHaveOnePayment(i), isAsset(payment, eastAsset), checkAssetEnabled(_asset)] |
---|
| 249 | + | if ((checks == checks)) |
---|
| 250 | + | then { |
---|
| 251 | + | let amount = payment.amount |
---|
| 252 | + | [Burn(eastAsset, amount), ScriptTransfer(i.caller, amount, fromBase58String(_asset))] |
---|
| 253 | + | } |
---|
| 254 | + | else throw("Strict value is not equal to itself.") |
---|
| 255 | + | } |
---|
| 256 | + | |
---|
| 257 | + | |
---|
| 258 | + | |
---|
| 259 | + | @Callable(i) |
---|
| 260 | + | func addAddress (_address) = { |
---|
| 261 | + | let checks = [mustInitialized(), mustSelf(i), checkAddress(_address), checkAddressDisabled(_address)] |
---|
| 262 | + | if ((checks == checks)) |
---|
| 263 | + | then [BooleanEntry(keyAllowedAddress(_address), true)] |
---|
| 264 | + | else throw("Strict value is not equal to itself.") |
---|
| 265 | + | } |
---|
| 266 | + | |
---|
| 267 | + | |
---|
| 268 | + | |
---|
| 269 | + | @Callable(i) |
---|
| 270 | + | func removeAddress (_address) = { |
---|
| 271 | + | let checks = [mustInitialized(), mustSelf(i), checkAddress(_address), checkAddressEnabled(_address)] |
---|
| 272 | + | if ((checks == checks)) |
---|
| 273 | + | then [BooleanEntry(keyAllowedAddress(_address), false)] |
---|
| 274 | + | else throw("Strict value is not equal to itself.") |
---|
| 275 | + | } |
---|
| 276 | + | |
---|
| 277 | + | |
---|
| 278 | + | |
---|
| 279 | + | @Callable(i) |
---|
| 280 | + | func addAsset (_asset) = { |
---|
| 281 | + | let checks = [mustInitialized(), mustSelf(i), checkAsset(_asset), checkAssetDisabled(_asset)] |
---|
| 282 | + | if ((checks == checks)) |
---|
| 283 | + | then [BooleanEntry(keyAllowedAsset(_asset), true)] |
---|
| 284 | + | else throw("Strict value is not equal to itself.") |
---|
| 285 | + | } |
---|
| 286 | + | |
---|
| 287 | + | |
---|
| 288 | + | |
---|
| 289 | + | @Callable(i) |
---|
| 290 | + | func removeAsset (_asset) = { |
---|
| 291 | + | let checks = [mustInitialized(), mustSelf(i), checkAssetEnabled(_asset)] |
---|
| 292 | + | if ((checks == checks)) |
---|
| 293 | + | then [BooleanEntry(keyAllowedAsset(_asset), false)] |
---|
| 294 | + | else throw("Strict value is not equal to itself.") |
---|
| 295 | + | } |
---|
| 296 | + | |
---|
| 297 | + | |
---|
| 298 | + | @Verifier(tx) |
---|
| 299 | + | func verify () = match verifier() { |
---|
| 300 | + | case address: String => |
---|
| 301 | + | valueOrElse(getBoolean(addressFromStringValue(address), keySigned(toString(this), toBase58String(tx.id))), false) |
---|
| 302 | + | case _ => |
---|
| 303 | + | sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey) |
---|
| 304 | + | } |
---|
| 305 | + | |
---|