1 | | - | # no script |
---|
| 1 | + | {-# STDLIB_VERSION 6 #-} |
---|
| 2 | + | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
| 3 | + | {-# CONTENT_TYPE DAPP #-} |
---|
| 4 | + | let SEP = "__" |
---|
| 5 | + | |
---|
| 6 | + | func keyManagerPublicKey () = "%s__managerPublicKey" |
---|
| 7 | + | |
---|
| 8 | + | |
---|
| 9 | + | func keyManagerVaultAddress () = "%s__managerVaultAddress" |
---|
| 10 | + | |
---|
| 11 | + | |
---|
| 12 | + | func getManagerAddressOrFail () = addressFromStringValue(getStringValue(keyManagerVaultAddress())) |
---|
| 13 | + | |
---|
| 14 | + | |
---|
| 15 | + | func keyWithdrawDelay (assetA,assetB) = makeString(["%s%s%s", "withdrawDelay", assetA, assetB], SEP) |
---|
| 16 | + | |
---|
| 17 | + | |
---|
| 18 | + | func keyDepositFeePermille (assetA,assetB) = makeString(["%s%s%s", "depositFeePermille", assetA, assetB], SEP) |
---|
| 19 | + | |
---|
| 20 | + | |
---|
| 21 | + | func keyWithdrawFeePermille (assetA,assetB) = makeString(["%s%s%s", "withdrawFeePermille", assetA, assetB], SEP) |
---|
| 22 | + | |
---|
| 23 | + | |
---|
| 24 | + | func keyMinAmountDeposit (assetA,assetB) = makeString(["%s%s%s", "minAmountDeposit", assetA, assetB], SEP) |
---|
| 25 | + | |
---|
| 26 | + | |
---|
| 27 | + | func keyAssetsPairStatus (assetA,assetB) = makeString(["%s%s%s", "assetsPairStatus", assetA, assetB], SEP) |
---|
| 28 | + | |
---|
| 29 | + | |
---|
| 30 | + | func keyMinAmountWithdraw (assetA,assetB) = makeString(["%s%s%s", "minAmountWithdraw", assetA, assetB], SEP) |
---|
| 31 | + | |
---|
| 32 | + | |
---|
| 33 | + | func keyBalance (assetA,assetB,userAddress) = makeString(["%s%s%s%s", "balance", assetA, assetB, userAddress], SEP) |
---|
| 34 | + | |
---|
| 35 | + | |
---|
| 36 | + | func keyTotalFeeCollectedDeposit (assetA,assetB) = makeString(["%s%s%s%s", "totalFeeCollected", "deposit", assetA, assetB], SEP) |
---|
| 37 | + | |
---|
| 38 | + | |
---|
| 39 | + | func keyProcessInProgress (userAddress,assetA,assetB,heightInKey) = makeString(["%s%s%s%s%s%d", "withdrawProcess", "inProgress", userAddress, assetA, assetB, toString(heightInKey)], SEP) |
---|
| 40 | + | |
---|
| 41 | + | |
---|
| 42 | + | func keyProcessDone (userAddress,assetA,assetB,heightInKey) = makeString(["%s%s%s%s%s%d", "withdrawProcess", "done", userAddress, assetA, assetB, toString(heightInKey)], SEP) |
---|
| 43 | + | |
---|
| 44 | + | |
---|
| 45 | + | func managerPublicKeyOrUnit () = { |
---|
| 46 | + | let managerVaultAddress = getManagerAddressOrFail() |
---|
| 47 | + | match getString(managerVaultAddress, keyManagerPublicKey()) { |
---|
| 48 | + | case s: String => |
---|
| 49 | + | fromBase58String(s) |
---|
| 50 | + | case _: Unit => |
---|
| 51 | + | unit |
---|
| 52 | + | case _ => |
---|
| 53 | + | throw("Match error") |
---|
| 54 | + | } |
---|
| 55 | + | } |
---|
| 56 | + | |
---|
| 57 | + | |
---|
| 58 | + | func mustManager (i) = { |
---|
| 59 | + | let pd = throw("Permission denied.") |
---|
| 60 | + | match managerPublicKeyOrUnit() { |
---|
| 61 | + | case pk: ByteVector => |
---|
| 62 | + | if ((i.callerPublicKey == pk)) |
---|
| 63 | + | then true |
---|
| 64 | + | else pd |
---|
| 65 | + | case _: Unit => |
---|
| 66 | + | if ((i.caller == this)) |
---|
| 67 | + | then true |
---|
| 68 | + | else pd |
---|
| 69 | + | case _ => |
---|
| 70 | + | throw("Match error") |
---|
| 71 | + | } |
---|
| 72 | + | } |
---|
| 73 | + | |
---|
| 74 | + | |
---|
| 75 | + | func fmtErr (msg) = makeString(["otc_multiasset.ride:", msg], " ") |
---|
| 76 | + | |
---|
| 77 | + | |
---|
| 78 | + | func throwErr (msg) = throw(fmtErr(msg)) |
---|
| 79 | + | |
---|
| 80 | + | |
---|
| 81 | + | @Callable(i) |
---|
| 82 | + | func registerAsset (assetA,assetB,withdrawDelay,depositFee,withdrawFee,minAmountDeposit,minAmountWithdraw,pairStatus) = { |
---|
| 83 | + | let checkCaller = mustManager(i) |
---|
| 84 | + | if ((checkCaller == checkCaller)) |
---|
| 85 | + | then { |
---|
| 86 | + | let withdrawDelayKey = keyWithdrawDelay(assetA, assetB) |
---|
| 87 | + | let depositFeePermilleKey = keyDepositFeePermille(assetA, assetB) |
---|
| 88 | + | let withdrawFeePermilleKey = keyWithdrawFeePermille(assetA, assetB) |
---|
| 89 | + | let minAmountDepositKey = keyMinAmountDeposit(assetA, assetB) |
---|
| 90 | + | let minAmountWithdrawKey = keyMinAmountWithdraw(assetA, assetB) |
---|
| 91 | + | let pairStatusKey = keyAssetsPairStatus(assetA, assetB) |
---|
| 92 | + | [IntegerEntry(withdrawDelayKey, withdrawDelay), IntegerEntry(depositFeePermilleKey, depositFee), IntegerEntry(withdrawFeePermilleKey, withdrawFee), IntegerEntry(minAmountDepositKey, minAmountDeposit), IntegerEntry(minAmountWithdrawKey, minAmountWithdraw), IntegerEntry(pairStatusKey, pairStatus)] |
---|
| 93 | + | } |
---|
| 94 | + | else throw("Strict value is not equal to itself.") |
---|
| 95 | + | } |
---|
| 96 | + | |
---|
| 97 | + | |
---|
| 98 | + | |
---|
| 99 | + | @Callable(i) |
---|
| 100 | + | func swapAssetsAToB (assetB) = { |
---|
| 101 | + | let payment = value(i.payments[0]) |
---|
| 102 | + | let assetA = toBase58String(value(payment.assetId)) |
---|
| 103 | + | let asset = fromBase58String(value(assetB)) |
---|
| 104 | + | let userAddress = toString(i.caller) |
---|
| 105 | + | let minAmountDeposit = valueOrErrorMessage(getInteger(keyMinAmountDeposit(assetA, assetB)), fmtErr("This asset pair does not exist.")) |
---|
| 106 | + | let toDeposit = payment.amount |
---|
| 107 | + | let depositFee = valueOrErrorMessage(getInteger(keyDepositFeePermille(assetA, assetB)), fmtErr("The deposit fee for this pair of assets is not set.")) |
---|
| 108 | + | let fee = ((toDeposit / 1000) * depositFee) |
---|
| 109 | + | let currentUserBalance = valueOrElse(getInteger(keyBalance(assetA, assetB, userAddress)), 0) |
---|
| 110 | + | let totalCommissions = valueOrElse(getInteger(keyTotalFeeCollectedDeposit(assetA, assetB)), 0) |
---|
| 111 | + | let pairStatus = valueOrErrorMessage(getInteger(keyAssetsPairStatus(assetA, assetB)), fmtErr("The asset pair status for this pair of assets is not set.")) |
---|
| 112 | + | let checkPairStatus = if ((pairStatus == 0)) |
---|
| 113 | + | then true |
---|
| 114 | + | else throwErr("The couple's deposit is blocked.") |
---|
| 115 | + | if ((checkPairStatus == checkPairStatus)) |
---|
| 116 | + | then { |
---|
| 117 | + | let checkPayment = if ((toDeposit >= minAmountDeposit)) |
---|
| 118 | + | then true |
---|
| 119 | + | else throwErr("The deposit amount is less than the minimum.") |
---|
| 120 | + | if ((checkPayment == checkPayment)) |
---|
| 121 | + | then { |
---|
| 122 | + | let newBalance = ((currentUserBalance + toDeposit) - fee) |
---|
| 123 | + | let checkBalance = if ((newBalance > 0)) |
---|
| 124 | + | then true |
---|
| 125 | + | else throwErr("The final balance is less than or equal to 0.") |
---|
| 126 | + | if ((checkBalance == checkBalance)) |
---|
| 127 | + | then [IntegerEntry(keyBalance(assetA, assetB, userAddress), newBalance), IntegerEntry(keyTotalFeeCollectedDeposit(assetA, assetB), (totalCommissions + fee)), ScriptTransfer(i.caller, (toDeposit - fee), asset)] |
---|
| 128 | + | else throw("Strict value is not equal to itself.") |
---|
| 129 | + | } |
---|
| 130 | + | else throw("Strict value is not equal to itself.") |
---|
| 131 | + | } |
---|
| 132 | + | else throw("Strict value is not equal to itself.") |
---|
| 133 | + | } |
---|
| 134 | + | |
---|
| 135 | + | |
---|
| 136 | + | |
---|
| 137 | + | @Callable(i) |
---|
| 138 | + | func initializationSwapAssetsBToA (assetA) = { |
---|
| 139 | + | let payment = value(i.payments[0]) |
---|
| 140 | + | let toWithdraw = payment.amount |
---|
| 141 | + | let assetB = toBase58String(value(payment.assetId)) |
---|
| 142 | + | let userAddress = toString(i.caller) |
---|
| 143 | + | let minAmountWithdraw = valueOrErrorMessage(getInteger(keyMinAmountWithdraw(assetA, assetB)), fmtErr("The minimum withdrawal amount for this pair of assets is not set.")) |
---|
| 144 | + | let assetLockHeight = (height + valueOrErrorMessage(getInteger(keyWithdrawDelay(assetA, assetB)), fmtErr("Withdrawal delay is not set for the specified pair."))) |
---|
| 145 | + | let currentUserBalance = valueOrErrorMessage(getInteger(keyBalance(assetA, assetB, userAddress)), fmtErr("This user balance does not exist.")) |
---|
| 146 | + | let totalCommissions = valueOrElse(getInteger(keyTotalFeeCollectedDeposit(assetA, assetB)), 0) |
---|
| 147 | + | let withdrawFeePermilleKey = keyWithdrawFeePermille(assetA, assetB) |
---|
| 148 | + | let withdrawFee = valueOrErrorMessage(getInteger(withdrawFeePermilleKey), fmtErr("The withdrawal fee for this pair of assets is not set.")) |
---|
| 149 | + | let fee = ((toWithdraw / 1000) * withdrawFee) |
---|
| 150 | + | let newBalance = (currentUserBalance - toWithdraw) |
---|
| 151 | + | let checkBalance = if ((newBalance >= 0)) |
---|
| 152 | + | then true |
---|
| 153 | + | else throwErr("Swap amount fail, amount is to small.") |
---|
| 154 | + | if ((checkBalance == checkBalance)) |
---|
| 155 | + | then { |
---|
| 156 | + | let checkPayment = if ((toWithdraw >= minAmountWithdraw)) |
---|
| 157 | + | then true |
---|
| 158 | + | else throwErr("The withdraw amount is less than the minimum.") |
---|
| 159 | + | if ((checkPayment == checkPayment)) |
---|
| 160 | + | then { |
---|
| 161 | + | let checkProcessInProgress = if ((getInteger(keyProcessInProgress(userAddress, assetA, assetB, assetLockHeight)) == unit)) |
---|
| 162 | + | then true |
---|
| 163 | + | else throwErr("At this height, there is already an exchange of this pair.") |
---|
| 164 | + | if ((checkProcessInProgress == checkProcessInProgress)) |
---|
| 165 | + | then [IntegerEntry(keyBalance(assetA, assetB, userAddress), newBalance), IntegerEntry(keyProcessInProgress(userAddress, assetA, assetB, assetLockHeight), (toWithdraw - fee)), IntegerEntry(keyTotalFeeCollectedDeposit(assetA, assetB), (totalCommissions + fee))] |
---|
| 166 | + | else throw("Strict value is not equal to itself.") |
---|
| 167 | + | } |
---|
| 168 | + | else throw("Strict value is not equal to itself.") |
---|
| 169 | + | } |
---|
| 170 | + | else throw("Strict value is not equal to itself.") |
---|
| 171 | + | } |
---|
| 172 | + | |
---|
| 173 | + | |
---|
| 174 | + | |
---|
| 175 | + | @Callable(i) |
---|
| 176 | + | func withdrawAsset (assetA,assetB,heightInKey) = { |
---|
| 177 | + | let userAddress = toString(i.caller) |
---|
| 178 | + | let toWithdraw = valueOrErrorMessage(getInteger(keyProcessInProgress(userAddress, assetA, assetB, heightInKey)), fmtErr("At this height, withdraw was not initialized with this pair of assets.")) |
---|
| 179 | + | let asset = fromBase58String(value(assetA)) |
---|
| 180 | + | let checkHeight = if ((height >= heightInKey)) |
---|
| 181 | + | then true |
---|
| 182 | + | else throwErr((("Withdrawal is possible after " + toString(heightInKey)) + " height or you have already withdrawn.")) |
---|
| 183 | + | if ((checkHeight == checkHeight)) |
---|
| 184 | + | then [DeleteEntry(keyProcessInProgress(userAddress, assetA, assetB, heightInKey)), IntegerEntry(keyProcessDone(userAddress, assetA, assetB, heightInKey), toWithdraw), ScriptTransfer(i.caller, toWithdraw, asset)] |
---|
| 185 | + | else throw("Strict value is not equal to itself.") |
---|
| 186 | + | } |
---|
| 187 | + | |
---|
| 188 | + | |
---|
| 189 | + | |
---|
| 190 | + | @Callable(i) |
---|
| 191 | + | func withdrawFee (assetA,assetB) = { |
---|
| 192 | + | let checkCaller = mustManager(i) |
---|
| 193 | + | if ((checkCaller == checkCaller)) |
---|
| 194 | + | then { |
---|
| 195 | + | let toWithdrawA = valueOrElse(getInteger(keyTotalFeeCollectedDeposit(assetA, assetB)), 0) |
---|
| 196 | + | let withdrawAssetA = fromBase58String(value(assetA)) |
---|
| 197 | + | [IntegerEntry(keyTotalFeeCollectedDeposit(assetA, assetB), 0), ScriptTransfer(i.caller, toWithdrawA, withdrawAssetA)] |
---|
| 198 | + | } |
---|
| 199 | + | else throw("Strict value is not equal to itself.") |
---|
| 200 | + | } |
---|
| 201 | + | |
---|
| 202 | + | |
---|
| 203 | + | @Verifier(tx) |
---|
| 204 | + | func verify () = { |
---|
| 205 | + | let targetPublicKey = match managerPublicKeyOrUnit() { |
---|
| 206 | + | case pk: ByteVector => |
---|
| 207 | + | pk |
---|
| 208 | + | case _: Unit => |
---|
| 209 | + | tx.senderPublicKey |
---|
| 210 | + | case _ => |
---|
| 211 | + | throw("Match error") |
---|
| 212 | + | } |
---|
| 213 | + | sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey) |
---|
| 214 | + | } |
---|
| 215 | + | |
---|