4 | | - | let SEP = "__" |
---|
5 | | - | |
---|
6 | | - | let contractFile = "rebalance.ride" |
---|
7 | | - | |
---|
8 | | - | let keyFactoryAddress = "%s__factory" |
---|
9 | | - | |
---|
10 | | - | func keyAdditionalBalance (assetId) = makeString(["%s%s", "additionalBalance", assetId], SEP) |
---|
11 | | - | |
---|
12 | | - | |
---|
13 | | - | func keyInternalAdditionalBalance (assetId) = makeString(["%s%s", "internalAdditionalBalance", assetId], SEP) |
---|
14 | | - | |
---|
15 | | - | |
---|
16 | | - | func keyStakingAssetBalance (assetId) = makeString(["%s%s", "stakingAssetBalance", assetId], SEP) |
---|
17 | | - | |
---|
18 | | - | |
---|
19 | | - | func getAdditionalBalance (assetId) = valueOrElse(getInteger(this, keyAdditionalBalance(assetId)), 0) |
---|
20 | | - | |
---|
21 | | - | |
---|
22 | | - | func getInternalAdditionalBalance (assetId) = valueOrElse(getInteger(this, keyInternalAdditionalBalance(assetId)), 0) |
---|
23 | | - | |
---|
24 | | - | |
---|
25 | | - | func getStakingAssetBalance (assetId) = valueOrElse(getInteger(this, keyStakingAssetBalance(assetId)), 0) |
---|
26 | | - | |
---|
27 | | - | |
---|
28 | | - | func throwError (errorMsg) = throw(((contractFile + ": ") + errorMsg)) |
---|
29 | | - | |
---|
30 | | - | |
---|
31 | | - | func parseAssetId (input) = if ((input == "WAVES")) |
---|
32 | | - | then unit |
---|
33 | | - | else fromBase58String(input) |
---|
34 | | - | |
---|
35 | | - | |
---|
36 | | - | func getRate (proxy) = { |
---|
37 | | - | let inv = invoke(proxy, "getRate", nil, nil) |
---|
38 | | - | if ((inv == inv)) |
---|
39 | | - | then match inv { |
---|
40 | | - | case s: Int => |
---|
41 | | - | s |
---|
42 | | - | case _ => |
---|
43 | | - | throwError("getRate() unexpected type") |
---|
44 | | - | } |
---|
45 | | - | else throw("Strict value is not equal to itself.") |
---|
46 | | - | } |
---|
47 | | - | |
---|
48 | | - | |
---|
49 | | - | func getLeaseProxyConfig (assetId) = { |
---|
50 | | - | let factory = addressFromStringValue(valueOrElse(getString(keyFactoryAddress), "")) |
---|
51 | | - | let myAddressString = toString(this) |
---|
52 | | - | invoke(factory, "getPoolLeaseConfigREADONLY", [myAddressString, assetId], nil) |
---|
53 | | - | } |
---|
54 | | - | |
---|
55 | | - | |
---|
56 | | - | func deposit (assetId,amount,stakingAssetId,proxy) = { |
---|
57 | | - | let currentAdditionalBalance = getAdditionalBalance(assetId) |
---|
58 | | - | if ((currentAdditionalBalance == currentAdditionalBalance)) |
---|
59 | | - | then { |
---|
60 | | - | let currentStakingAssetBalance = getStakingAssetBalance(stakingAssetId) |
---|
61 | | - | if ((currentStakingAssetBalance == currentStakingAssetBalance)) |
---|
62 | | - | then { |
---|
63 | | - | let asset = parseAssetId(assetId) |
---|
64 | | - | if ((amount > 0)) |
---|
65 | | - | then { |
---|
66 | | - | let depositInvoke = invoke(proxy, "deposit", nil, [AttachedPayment(asset, amount)]) |
---|
67 | | - | if ((depositInvoke == depositInvoke)) |
---|
68 | | - | then match depositInvoke { |
---|
69 | | - | case receivedStakingAsset: Int => |
---|
70 | | - | let newAdditionalBalance = (currentAdditionalBalance + amount) |
---|
71 | | - | let newStakingAssetBalance = (currentStakingAssetBalance + receivedStakingAsset) |
---|
72 | | - | [IntegerEntry(keyAdditionalBalance(assetId), newAdditionalBalance), IntegerEntry(keyStakingAssetBalance(stakingAssetId), newStakingAssetBalance)] |
---|
73 | | - | case _ => |
---|
74 | | - | nil |
---|
75 | | - | } |
---|
76 | | - | else throw("Strict value is not equal to itself.") |
---|
77 | | - | } |
---|
78 | | - | else nil |
---|
79 | | - | } |
---|
80 | | - | else throw("Strict value is not equal to itself.") |
---|
81 | | - | } |
---|
82 | | - | else throw("Strict value is not equal to itself.") |
---|
83 | | - | } |
---|
84 | | - | |
---|
85 | | - | |
---|
86 | | - | func withdraw (assetId,amount,stakingAssetId,proxy,proxyRateMul,profitAddress) = { |
---|
87 | | - | let currentAdditionalBalance = getAdditionalBalance(assetId) |
---|
88 | | - | if ((currentAdditionalBalance == currentAdditionalBalance)) |
---|
89 | | - | then { |
---|
90 | | - | let currentStakingAssetBalance = getStakingAssetBalance(stakingAssetId) |
---|
91 | | - | if ((currentStakingAssetBalance == currentStakingAssetBalance)) |
---|
92 | | - | then { |
---|
93 | | - | let currentProxyRate = getRate(proxy) |
---|
94 | | - | if ((currentProxyRate == currentProxyRate)) |
---|
95 | | - | then { |
---|
96 | | - | let oldRate = fraction(proxyRateMul, currentAdditionalBalance, currentStakingAssetBalance) |
---|
97 | | - | let stakingAsset = parseAssetId(stakingAssetId) |
---|
98 | | - | let oldSendStakingAmount = fraction(proxyRateMul, amount, oldRate) |
---|
99 | | - | let sendStakingAssetAmount = fraction(proxyRateMul, amount, currentProxyRate) |
---|
100 | | - | let profitAmount = max([0, (oldSendStakingAmount - sendStakingAssetAmount)]) |
---|
101 | | - | if ((sendStakingAssetAmount > 0)) |
---|
102 | | - | then { |
---|
103 | | - | let withdrawInvoke = invoke(proxy, "withdraw", nil, [AttachedPayment(stakingAsset, sendStakingAssetAmount)]) |
---|
104 | | - | if ((withdrawInvoke == withdrawInvoke)) |
---|
105 | | - | then match withdrawInvoke { |
---|
106 | | - | case receivedAssets: Int => |
---|
107 | | - | let newAdditionalBalance = (currentAdditionalBalance - receivedAssets) |
---|
108 | | - | let newStakingAssetBalance = ((currentStakingAssetBalance - sendStakingAssetAmount) - profitAmount) |
---|
109 | | - | [IntegerEntry(keyAdditionalBalance(assetId), newAdditionalBalance), IntegerEntry(keyStakingAssetBalance(stakingAssetId), newStakingAssetBalance), ScriptTransfer(profitAddress, profitAmount, parseAssetId(stakingAssetId))] |
---|
110 | | - | case _ => |
---|
111 | | - | nil |
---|
112 | | - | } |
---|
113 | | - | else throw("Strict value is not equal to itself.") |
---|
114 | | - | } |
---|
115 | | - | else nil |
---|
116 | | - | } |
---|
117 | | - | else throw("Strict value is not equal to itself.") |
---|
118 | | - | } |
---|
119 | | - | else throw("Strict value is not equal to itself.") |
---|
120 | | - | } |
---|
121 | | - | else throw("Strict value is not equal to itself.") |
---|
122 | | - | } |
---|
123 | | - | |
---|
124 | | - | |
---|
125 | | - | func rebalanceInternal (targetRatio,assetId,stakingAssetId,minBalance,proxy,proxyRateMul,profitAddress) = { |
---|
126 | | - | let currentAdditionalBalance = getAdditionalBalance(assetId) |
---|
127 | | - | if ((currentAdditionalBalance == currentAdditionalBalance)) |
---|
128 | | - | then { |
---|
129 | | - | let currentStakingAssetBalance = getStakingAssetBalance(stakingAssetId) |
---|
130 | | - | if ((currentStakingAssetBalance == currentStakingAssetBalance)) |
---|
131 | | - | then { |
---|
132 | | - | let availableBalance = if ((assetId == "WAVES")) |
---|
133 | | - | then wavesBalance(this).available |
---|
134 | | - | else assetBalance(this, fromBase58String(assetId)) |
---|
135 | | - | if ((availableBalance == availableBalance)) |
---|
136 | | - | then { |
---|
137 | | - | let wholeBalance = max([0, ((availableBalance + currentAdditionalBalance) - minBalance)]) |
---|
138 | | - | let targetAdditionalBalance = fraction(targetRatio, wholeBalance, 100) |
---|
139 | | - | let diff = (currentAdditionalBalance - targetAdditionalBalance) |
---|
140 | | - | if ((diff == 0)) |
---|
141 | | - | then nil |
---|
142 | | - | else if ((0 > diff)) |
---|
143 | | - | then { |
---|
144 | | - | let sendAssetAmount = -(diff) |
---|
145 | | - | deposit(assetId, sendAssetAmount, stakingAssetId, proxy) |
---|
146 | | - | } |
---|
147 | | - | else { |
---|
148 | | - | let getAssetAmount = diff |
---|
149 | | - | withdraw(assetId, getAssetAmount, stakingAssetId, proxy, proxyRateMul, profitAddress) |
---|
150 | | - | } |
---|
151 | | - | } |
---|
152 | | - | else throw("Strict value is not equal to itself.") |
---|
153 | | - | } |
---|
154 | | - | else throw("Strict value is not equal to itself.") |
---|
155 | | - | } |
---|
156 | | - | else throw("Strict value is not equal to itself.") |
---|
157 | | - | } |
---|
158 | | - | |
---|
159 | | - | |
---|
160 | | - | func rebalanceAsset (assetId) = match getLeaseProxyConfig(assetId) { |
---|
161 | | - | case a: (Boolean, Int, Int, String, String, Int, String) => |
---|
162 | | - | let $t048804989 = a |
---|
163 | | - | let isLeasable = $t048804989._1 |
---|
164 | | - | let leasedRatio = $t048804989._2 |
---|
165 | | - | let minBalance = $t048804989._3 |
---|
166 | | - | let proxyAddress = $t048804989._4 |
---|
167 | | - | let proxyAssetId = $t048804989._5 |
---|
168 | | - | let proxyRateMul = $t048804989._6 |
---|
169 | | - | let stakingProfitAddress = $t048804989._7 |
---|
170 | | - | if (isLeasable) |
---|
171 | | - | then rebalanceInternal(leasedRatio, assetId, proxyAssetId, minBalance, addressFromStringValue(proxyAddress), proxyRateMul, addressFromStringValue(stakingProfitAddress)) |
---|
172 | | - | else nil |
---|
173 | | - | case _ => |
---|
174 | | - | throwError((("[" + assetId) + "] Rebalance error")) |
---|
175 | | - | } |
---|
176 | | - | |
---|
177 | | - | |
---|
178 | | - | func rebalanceAsset2 (assetId,ratio) = match getLeaseProxyConfig(assetId) { |
---|
179 | | - | case a: (Boolean, Int, Int, String, String, Int, String) => |
---|
180 | | - | let $t054655574 = a |
---|
181 | | - | let isLeasable = $t054655574._1 |
---|
182 | | - | let leasedRatio = $t054655574._2 |
---|
183 | | - | let minBalance = $t054655574._3 |
---|
184 | | - | let proxyAddress = $t054655574._4 |
---|
185 | | - | let proxyAssetId = $t054655574._5 |
---|
186 | | - | let proxyRateMul = $t054655574._6 |
---|
187 | | - | let stakingProfitAddress = $t054655574._7 |
---|
188 | | - | if (isLeasable) |
---|
189 | | - | then rebalanceInternal(ratio, assetId, proxyAssetId, minBalance, addressFromStringValue(proxyAddress), proxyRateMul, addressFromStringValue(stakingProfitAddress)) |
---|
190 | | - | else nil |
---|
191 | | - | case _ => |
---|
192 | | - | throwError((("[" + assetId) + "] Rebalance error")) |
---|
193 | | - | } |
---|