tx · 6qjiGmkGnHy9XM3BQwo6xgVdGx6go2qGNUPJvCvpBFnS

3NCWFHDzdPHZC6636ZkMLNDup9mjpbTLs7h:  -0.05000000 Waves

2023.03.29 15:46 [2511258] smart account 3NCWFHDzdPHZC6636ZkMLNDup9mjpbTLs7h > SELF 0.00000000 Waves

{ "type": 13, "id": "6qjiGmkGnHy9XM3BQwo6xgVdGx6go2qGNUPJvCvpBFnS", "fee": 5000000, "feeAssetId": null, "timestamp": 1680093977185, "version": 2, "chainId": 84, "sender": "3NCWFHDzdPHZC6636ZkMLNDup9mjpbTLs7h", "senderPublicKey": "3z8Q6Zu3KppVmn6fJJvrLc1Wo3krVHSvfaNcerm82md2", "proofs": [ "32yE3py5Myyinpv8RJMwsg7FrvgRX4n17xvqDAMQoTnv7zdFPMp2njzqiYggwbp6jrk3sKdMfgF1NxQ4NnndRpvF" ], "script": "base64:BgIECAISAAwAA1NFUAICX18ADGNvbnRyYWN0RmlsZQIJcmViYWxhbmNlABFrZXlGYWN0b3J5QWRkcmVzcwILJXNfX2ZhY3RvcnkBFGtleUFkZGl0aW9uYWxCYWxhbmNlAQdhc3NldElkCQC5CQIJAMwIAgIEJXMlcwkAzAgCAhFhZGRpdGlvbmFsQmFsYW5jZQkAzAgCBQdhc3NldElkBQNuaWwFA1NFUAEWa2V5U3Rha2luZ0Fzc2V0QmFsYW5jZQEHYXNzZXRJZAkAuQkCCQDMCAICBCVzJXMJAMwIAgITc3Rha2luZ0Fzc2V0QmFsYW5jZQkAzAgCBQdhc3NldElkBQNuaWwFA1NFUAEUZ2V0QWRkaXRpb25hbEJhbGFuY2UBB2Fzc2V0SWQJAQt2YWx1ZU9yRWxzZQIJAJoIAgUEdGhpcwkBFGtleUFkZGl0aW9uYWxCYWxhbmNlAQUHYXNzZXRJZAAAARZnZXRTdGFraW5nQXNzZXRCYWxhbmNlAQdhc3NldElkCQELdmFsdWVPckVsc2UCCQCaCAIFBHRoaXMJARZrZXlTdGFraW5nQXNzZXRCYWxh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c3MIBQskdDA0MjYwNDM2OQJfNwMFCmlzTGVhc2FibGUJARFyZWJhbGFuY2VJbnRlcm5hbAYFC2xlYXNlZFJhdGlvBQZhc3NldDEFDHByb3h5QXNzZXRJZAUKbWluQmFsYW5jZQkBEUBleHRyTmF0aXZlKDEwNjIpAQUMcHJveHlBZGRyZXNzBQxwcm94eVJhdGVNdWwFA25pbAkAAgECA0JSUgAZBfhq", "height": 2511258, "applicationStatus": "succeeded", "spentComplexity": 0 } View: original | compacted Prev: AMUwG36YQv66tpSkTNkVUyUeaCsDCK5rdczmaWj9tJvc Next: E2AYH9iR5teHM54BQBnsut7vra1dEtDS9zwSorEAccuK Diff:
OldNewDifferences
11 {-# STDLIB_VERSION 6 #-}
22 {-# SCRIPT_TYPE ACCOUNT #-}
33 {-# CONTENT_TYPE DAPP #-}
4+let SEP = "__"
5+
46 let contractFile = "rebalance"
5-
6-let keysTargetRatio = "%s__targetRatio"
7-
8-let keyAdditionalBalance = "%s__additionalWavesBalance"
9-
10-let keySWavesAdditionalBalance = "%s__sWavesBalance"
11-
12-let keySWavesAssetId = "%s__sWavesAssetId"
13-
14-let keyProxyAddress = "%s__proxyAddress"
15-
16-let keyMinBalance = "%s__minWavesBalance"
177
188 let keyFactoryAddress = "%s__factory"
199
20-let MULT12 = 1000000000000
21-
22-func getIntegerOrZero (integerKey) = match getInteger(this, integerKey) {
23- case val: Int =>
24- val
25- case _ =>
26- 0
27-}
10+func keyAdditionalBalance (assetId) = makeString(["%s%s", "additionalBalance", assetId], SEP)
2811
2912
30-func getStringOrThrow (stringKey) = match getString(this, stringKey) {
31- case s: String =>
32- s
33- case _ =>
34- throw((("[" + stringKey) + "] is empty"))
35-}
13+func keyStakingAssetBalance (assetId) = makeString(["%s%s", "stakingAssetBalance", assetId], SEP)
3614
3715
38-func getTargetRatio () = match getInteger(keysTargetRatio) {
39- case ratio: Int =>
40- min([100, max([0, ratio])])
41- case _ =>
42- 0
43-}
16+func getAdditionalBalance (assetId) = valueOrElse(getInteger(this, keyAdditionalBalance(assetId)), 0)
4417
4518
46-func getMinBalance () = match getInteger(keyMinBalance) {
47- case i: Int =>
48- max([0, i])
49- case _ =>
50- 0
51-}
19+func getStakingAssetBalance (assetId) = valueOrElse(getInteger(this, keyStakingAssetBalance(assetId)), 0)
5220
5321
54-func getRate () = {
55- let proxy = addressFromStringValue(getStringOrThrow(keyProxyAddress))
22+func getRate (proxy) = {
5623 let inv = invoke(proxy, "getRate", nil, nil)
5724 if ((inv == inv))
5825 then match inv {
6532 }
6633
6734
68-func getProxyAddress () = addressFromStringValue(getStringOrThrow(keyProxyAddress))
69-
70-
71-func getSWavesId () = fromBase58String(getStringOrThrow(keySWavesAssetId))
72-
73-
74-func getLeaseProxyConfig () = {
35+func getLeaseProxyConfig (assetId) = {
7536 let factory = addressFromStringValue(valueOrElse(getString(keyFactoryAddress), ""))
7637 let myAddressString = toString(this)
77- $Tuple2(invoke(factory, "getPoolLeaseConfigREADONLY", [myAddressString, "WAVES"], nil), myAddressString)
38+ invoke(factory, "getPoolLeaseConfigREADONLY", [myAddressString, assetId], nil)
7839 }
7940
8041
81-func deposit (wavesAmount) = {
82- let proxy = getProxyAddress()
83- let currentAdditionalBalance = getIntegerOrZero(keyAdditionalBalance)
42+func deposit (assetId,amount,stakingAssetId,proxy) = {
43+ let currentAdditionalBalance = getAdditionalBalance(assetId)
8444 if ((currentAdditionalBalance == currentAdditionalBalance))
8545 then {
86- let currentSWavesBalance = getIntegerOrZero(keySWavesAdditionalBalance)
87- if ((currentSWavesBalance == currentSWavesBalance))
88- then if ((wavesAmount > 0))
89- then {
90- let depositInvoke = invoke(proxy, "deposit", nil, [AttachedPayment(unit, wavesAmount)])
91- if ((depositInvoke == depositInvoke))
92- then match depositInvoke {
93- case receivedSWaves: Int =>
94- let newAdditionalBalance = (currentAdditionalBalance + wavesAmount)
95- let newSWavesBalance = (currentSWavesBalance + receivedSWaves)
96-[IntegerEntry(keyAdditionalBalance, newAdditionalBalance), IntegerEntry(keySWavesAdditionalBalance, newSWavesBalance)]
97- case _ =>
98- nil
46+ let currentStakingAssetBalance = getStakingAssetBalance(stakingAssetId)
47+ if ((currentStakingAssetBalance == currentStakingAssetBalance))
48+ then {
49+ let asset = if ((assetId == "WAVES"))
50+ then unit
51+ else fromBase58String(assetId)
52+ if ((amount > 0))
53+ then {
54+ let depositInvoke = invoke(proxy, "deposit", nil, [AttachedPayment(asset, amount)])
55+ if ((depositInvoke == depositInvoke))
56+ then match depositInvoke {
57+ case receivedStakingAsset: Int =>
58+ let newAdditionalBalance = (currentAdditionalBalance + amount)
59+ let newSWavesBalance = (currentStakingAssetBalance + receivedStakingAsset)
60+[IntegerEntry(keyAdditionalBalance(assetId), newAdditionalBalance), IntegerEntry(keyStakingAssetBalance(stakingAssetId), newSWavesBalance)]
61+ case _ =>
62+ nil
63+ }
64+ else throw("Strict value is not equal to itself.")
9965 }
100- else throw("Strict value is not equal to itself.")
101- }
102- else nil
66+ else nil
67+ }
10368 else throw("Strict value is not equal to itself.")
10469 }
10570 else throw("Strict value is not equal to itself.")
10671 }
10772
10873
109-func withdraw (wavesAmount) = {
110- let proxy = getProxyAddress()
111- let currentAdditionalBalance = getIntegerOrZero(keyAdditionalBalance)
74+func withdraw (assetId,amount,stakingAssetId,proxy,proxyRateMul) = {
75+ let currentAdditionalBalance = getAdditionalBalance(assetId)
11276 if ((currentAdditionalBalance == currentAdditionalBalance))
11377 then {
114- let currentSWavesBalance = getIntegerOrZero(keySWavesAdditionalBalance)
115- if ((currentSWavesBalance == currentSWavesBalance))
78+ let currentStakingAssetBalance = getStakingAssetBalance(stakingAssetId)
79+ if ((currentStakingAssetBalance == currentStakingAssetBalance))
11680 then {
117- let currentProxyRate = getRate()
81+ let currentProxyRate = getRate(proxy)
11882 if ((currentProxyRate == currentProxyRate))
11983 then {
120- let sendSWavesAmount = fraction(MULT12, wavesAmount, currentProxyRate)
121- if ((sendSWavesAmount > 0))
84+ let sendStakingAssetAmount = fraction(proxyRateMul, amount, currentProxyRate)
85+ if ((sendStakingAssetAmount > 0))
12286 then {
123- let withdrawInvoke = invoke(proxy, "withdraw", nil, [AttachedPayment(getSWavesId(), sendSWavesAmount)])
87+ let withdrawInvoke = invoke(proxy, "withdraw", nil, [AttachedPayment(fromBase58String(stakingAssetId), sendStakingAssetAmount)])
12488 if ((withdrawInvoke == withdrawInvoke))
12589 then match withdrawInvoke {
126- case receivedWaves: Int =>
127- let newAdditionalBalance = (currentAdditionalBalance - receivedWaves)
128- let newSWavesBalance = (currentSWavesBalance - sendSWavesAmount)
129-[IntegerEntry(keyAdditionalBalance, newAdditionalBalance), IntegerEntry(keySWavesAdditionalBalance, newSWavesBalance)]
90+ case receivedAssets: Int =>
91+ let newAdditionalBalance = (currentAdditionalBalance - receivedAssets)
92+ let newSWavesBalance = (currentStakingAssetBalance - sendStakingAssetAmount)
93+[IntegerEntry(keyAdditionalBalance(assetId), newAdditionalBalance), IntegerEntry(keyStakingAssetBalance(stakingAssetId), newSWavesBalance)]
13094 case _ =>
13195 nil
13296 }
142106 }
143107
144108
145-func rebalanceInternal (targetRatio) = {
146- let availableBalance = wavesBalance(this).available
147- if ((availableBalance == availableBalance))
109+func rebalanceInternal (targetRatio,assetId,stakingAssetId,minBalance,proxy,proxyRateMul) = {
110+ let currentAdditionalBalance = getAdditionalBalance(assetId)
111+ if ((currentAdditionalBalance == currentAdditionalBalance))
148112 then {
149- let additionalBalance = getIntegerOrZero(keyAdditionalBalance)
150- if ((additionalBalance == additionalBalance))
113+ let currentStakingAssetBalance = getStakingAssetBalance(stakingAssetId)
114+ if ((currentStakingAssetBalance == currentStakingAssetBalance))
151115 then {
152- let wholeBalance = max([0, ((availableBalance + additionalBalance) - getMinBalance())])
153- let targetAdditionalBalance = fraction(targetRatio, wholeBalance, 100)
154- let diff = (additionalBalance - targetAdditionalBalance)
155- if ((diff == 0))
156- then nil
157- else if ((0 > diff))
158- then {
159- let sendWavesAmount = -(diff)
160- deposit(sendWavesAmount)
161- }
162- else {
163- let getWavesAmount = diff
164- withdraw(getWavesAmount)
165- }
116+ let availableBalance = if ((assetId == "WAVES"))
117+ then wavesBalance(this).available
118+ else assetBalance(this, fromBase58String(assetId))
119+ if ((availableBalance == availableBalance))
120+ then {
121+ let wholeBalance = max([0, ((availableBalance + currentAdditionalBalance) - minBalance)])
122+ let targetAdditionalBalance = fraction(targetRatio, wholeBalance, 100)
123+ let diff = (currentAdditionalBalance - targetAdditionalBalance)
124+ if ((diff == 0))
125+ then nil
126+ else if ((0 > diff))
127+ then {
128+ let sendAssetAmount = -(diff)
129+ deposit(assetId, sendAssetAmount, stakingAssetId, proxy)
130+ }
131+ else {
132+ let getAssetAmount = diff
133+ withdraw(assetId, getAssetAmount, stakingAssetId, proxy, proxyRateMul)
134+ }
135+ }
136+ else throw("Strict value is not equal to itself.")
166137 }
167138 else throw("Strict value is not equal to itself.")
168139 }
171142
172143
173144 @Callable(i)
174-func rebalanceRatio (targetRatio) = rebalanceInternal(targetRatio)
175-
176-
177-
178-@Callable(i)
179-func rebalance () = rebalanceInternal(getTargetRatio())
145+func rebalance () = {
146+ let asset1 = "WAVES"
147+ match getLeaseProxyConfig(asset1) {
148+ case a: (Boolean, Int, Int, String, String, Int, String) =>
149+ let $t042604369 = a
150+ let isLeasable = $t042604369._1
151+ let leasedRatio = $t042604369._2
152+ let minBalance = $t042604369._3
153+ let proxyAddress = $t042604369._4
154+ let proxyAssetId = $t042604369._5
155+ let proxyRateMul = $t042604369._6
156+ let stakingProfitAddress = $t042604369._7
157+ if (isLeasable)
158+ then rebalanceInternal(leasedRatio, asset1, proxyAssetId, minBalance, addressFromStringValue(proxyAddress), proxyRateMul)
159+ else nil
160+ case _ =>
161+ throw("BRR")
162+ }
163+ }
180164
181165
Full:
OldNewDifferences
11 {-# STDLIB_VERSION 6 #-}
22 {-# SCRIPT_TYPE ACCOUNT #-}
33 {-# CONTENT_TYPE DAPP #-}
4+let SEP = "__"
5+
46 let contractFile = "rebalance"
5-
6-let keysTargetRatio = "%s__targetRatio"
7-
8-let keyAdditionalBalance = "%s__additionalWavesBalance"
9-
10-let keySWavesAdditionalBalance = "%s__sWavesBalance"
11-
12-let keySWavesAssetId = "%s__sWavesAssetId"
13-
14-let keyProxyAddress = "%s__proxyAddress"
15-
16-let keyMinBalance = "%s__minWavesBalance"
177
188 let keyFactoryAddress = "%s__factory"
199
20-let MULT12 = 1000000000000
21-
22-func getIntegerOrZero (integerKey) = match getInteger(this, integerKey) {
23- case val: Int =>
24- val
25- case _ =>
26- 0
27-}
10+func keyAdditionalBalance (assetId) = makeString(["%s%s", "additionalBalance", assetId], SEP)
2811
2912
30-func getStringOrThrow (stringKey) = match getString(this, stringKey) {
31- case s: String =>
32- s
33- case _ =>
34- throw((("[" + stringKey) + "] is empty"))
35-}
13+func keyStakingAssetBalance (assetId) = makeString(["%s%s", "stakingAssetBalance", assetId], SEP)
3614
3715
38-func getTargetRatio () = match getInteger(keysTargetRatio) {
39- case ratio: Int =>
40- min([100, max([0, ratio])])
41- case _ =>
42- 0
43-}
16+func getAdditionalBalance (assetId) = valueOrElse(getInteger(this, keyAdditionalBalance(assetId)), 0)
4417
4518
46-func getMinBalance () = match getInteger(keyMinBalance) {
47- case i: Int =>
48- max([0, i])
49- case _ =>
50- 0
51-}
19+func getStakingAssetBalance (assetId) = valueOrElse(getInteger(this, keyStakingAssetBalance(assetId)), 0)
5220
5321
54-func getRate () = {
55- let proxy = addressFromStringValue(getStringOrThrow(keyProxyAddress))
22+func getRate (proxy) = {
5623 let inv = invoke(proxy, "getRate", nil, nil)
5724 if ((inv == inv))
5825 then match inv {
5926 case s: String =>
6027 parseIntValue(s)
6128 case _ =>
6229 throw("Rate is zero (0)")
6330 }
6431 else throw("Strict value is not equal to itself.")
6532 }
6633
6734
68-func getProxyAddress () = addressFromStringValue(getStringOrThrow(keyProxyAddress))
69-
70-
71-func getSWavesId () = fromBase58String(getStringOrThrow(keySWavesAssetId))
72-
73-
74-func getLeaseProxyConfig () = {
35+func getLeaseProxyConfig (assetId) = {
7536 let factory = addressFromStringValue(valueOrElse(getString(keyFactoryAddress), ""))
7637 let myAddressString = toString(this)
77- $Tuple2(invoke(factory, "getPoolLeaseConfigREADONLY", [myAddressString, "WAVES"], nil), myAddressString)
38+ invoke(factory, "getPoolLeaseConfigREADONLY", [myAddressString, assetId], nil)
7839 }
7940
8041
81-func deposit (wavesAmount) = {
82- let proxy = getProxyAddress()
83- let currentAdditionalBalance = getIntegerOrZero(keyAdditionalBalance)
42+func deposit (assetId,amount,stakingAssetId,proxy) = {
43+ let currentAdditionalBalance = getAdditionalBalance(assetId)
8444 if ((currentAdditionalBalance == currentAdditionalBalance))
8545 then {
86- let currentSWavesBalance = getIntegerOrZero(keySWavesAdditionalBalance)
87- if ((currentSWavesBalance == currentSWavesBalance))
88- then if ((wavesAmount > 0))
89- then {
90- let depositInvoke = invoke(proxy, "deposit", nil, [AttachedPayment(unit, wavesAmount)])
91- if ((depositInvoke == depositInvoke))
92- then match depositInvoke {
93- case receivedSWaves: Int =>
94- let newAdditionalBalance = (currentAdditionalBalance + wavesAmount)
95- let newSWavesBalance = (currentSWavesBalance + receivedSWaves)
96-[IntegerEntry(keyAdditionalBalance, newAdditionalBalance), IntegerEntry(keySWavesAdditionalBalance, newSWavesBalance)]
97- case _ =>
98- nil
46+ let currentStakingAssetBalance = getStakingAssetBalance(stakingAssetId)
47+ if ((currentStakingAssetBalance == currentStakingAssetBalance))
48+ then {
49+ let asset = if ((assetId == "WAVES"))
50+ then unit
51+ else fromBase58String(assetId)
52+ if ((amount > 0))
53+ then {
54+ let depositInvoke = invoke(proxy, "deposit", nil, [AttachedPayment(asset, amount)])
55+ if ((depositInvoke == depositInvoke))
56+ then match depositInvoke {
57+ case receivedStakingAsset: Int =>
58+ let newAdditionalBalance = (currentAdditionalBalance + amount)
59+ let newSWavesBalance = (currentStakingAssetBalance + receivedStakingAsset)
60+[IntegerEntry(keyAdditionalBalance(assetId), newAdditionalBalance), IntegerEntry(keyStakingAssetBalance(stakingAssetId), newSWavesBalance)]
61+ case _ =>
62+ nil
63+ }
64+ else throw("Strict value is not equal to itself.")
9965 }
100- else throw("Strict value is not equal to itself.")
101- }
102- else nil
66+ else nil
67+ }
10368 else throw("Strict value is not equal to itself.")
10469 }
10570 else throw("Strict value is not equal to itself.")
10671 }
10772
10873
109-func withdraw (wavesAmount) = {
110- let proxy = getProxyAddress()
111- let currentAdditionalBalance = getIntegerOrZero(keyAdditionalBalance)
74+func withdraw (assetId,amount,stakingAssetId,proxy,proxyRateMul) = {
75+ let currentAdditionalBalance = getAdditionalBalance(assetId)
11276 if ((currentAdditionalBalance == currentAdditionalBalance))
11377 then {
114- let currentSWavesBalance = getIntegerOrZero(keySWavesAdditionalBalance)
115- if ((currentSWavesBalance == currentSWavesBalance))
78+ let currentStakingAssetBalance = getStakingAssetBalance(stakingAssetId)
79+ if ((currentStakingAssetBalance == currentStakingAssetBalance))
11680 then {
117- let currentProxyRate = getRate()
81+ let currentProxyRate = getRate(proxy)
11882 if ((currentProxyRate == currentProxyRate))
11983 then {
120- let sendSWavesAmount = fraction(MULT12, wavesAmount, currentProxyRate)
121- if ((sendSWavesAmount > 0))
84+ let sendStakingAssetAmount = fraction(proxyRateMul, amount, currentProxyRate)
85+ if ((sendStakingAssetAmount > 0))
12286 then {
123- let withdrawInvoke = invoke(proxy, "withdraw", nil, [AttachedPayment(getSWavesId(), sendSWavesAmount)])
87+ let withdrawInvoke = invoke(proxy, "withdraw", nil, [AttachedPayment(fromBase58String(stakingAssetId), sendStakingAssetAmount)])
12488 if ((withdrawInvoke == withdrawInvoke))
12589 then match withdrawInvoke {
126- case receivedWaves: Int =>
127- let newAdditionalBalance = (currentAdditionalBalance - receivedWaves)
128- let newSWavesBalance = (currentSWavesBalance - sendSWavesAmount)
129-[IntegerEntry(keyAdditionalBalance, newAdditionalBalance), IntegerEntry(keySWavesAdditionalBalance, newSWavesBalance)]
90+ case receivedAssets: Int =>
91+ let newAdditionalBalance = (currentAdditionalBalance - receivedAssets)
92+ let newSWavesBalance = (currentStakingAssetBalance - sendStakingAssetAmount)
93+[IntegerEntry(keyAdditionalBalance(assetId), newAdditionalBalance), IntegerEntry(keyStakingAssetBalance(stakingAssetId), newSWavesBalance)]
13094 case _ =>
13195 nil
13296 }
13397 else throw("Strict value is not equal to itself.")
13498 }
13599 else nil
136100 }
137101 else throw("Strict value is not equal to itself.")
138102 }
139103 else throw("Strict value is not equal to itself.")
140104 }
141105 else throw("Strict value is not equal to itself.")
142106 }
143107
144108
145-func rebalanceInternal (targetRatio) = {
146- let availableBalance = wavesBalance(this).available
147- if ((availableBalance == availableBalance))
109+func rebalanceInternal (targetRatio,assetId,stakingAssetId,minBalance,proxy,proxyRateMul) = {
110+ let currentAdditionalBalance = getAdditionalBalance(assetId)
111+ if ((currentAdditionalBalance == currentAdditionalBalance))
148112 then {
149- let additionalBalance = getIntegerOrZero(keyAdditionalBalance)
150- if ((additionalBalance == additionalBalance))
113+ let currentStakingAssetBalance = getStakingAssetBalance(stakingAssetId)
114+ if ((currentStakingAssetBalance == currentStakingAssetBalance))
151115 then {
152- let wholeBalance = max([0, ((availableBalance + additionalBalance) - getMinBalance())])
153- let targetAdditionalBalance = fraction(targetRatio, wholeBalance, 100)
154- let diff = (additionalBalance - targetAdditionalBalance)
155- if ((diff == 0))
156- then nil
157- else if ((0 > diff))
158- then {
159- let sendWavesAmount = -(diff)
160- deposit(sendWavesAmount)
161- }
162- else {
163- let getWavesAmount = diff
164- withdraw(getWavesAmount)
165- }
116+ let availableBalance = if ((assetId == "WAVES"))
117+ then wavesBalance(this).available
118+ else assetBalance(this, fromBase58String(assetId))
119+ if ((availableBalance == availableBalance))
120+ then {
121+ let wholeBalance = max([0, ((availableBalance + currentAdditionalBalance) - minBalance)])
122+ let targetAdditionalBalance = fraction(targetRatio, wholeBalance, 100)
123+ let diff = (currentAdditionalBalance - targetAdditionalBalance)
124+ if ((diff == 0))
125+ then nil
126+ else if ((0 > diff))
127+ then {
128+ let sendAssetAmount = -(diff)
129+ deposit(assetId, sendAssetAmount, stakingAssetId, proxy)
130+ }
131+ else {
132+ let getAssetAmount = diff
133+ withdraw(assetId, getAssetAmount, stakingAssetId, proxy, proxyRateMul)
134+ }
135+ }
136+ else throw("Strict value is not equal to itself.")
166137 }
167138 else throw("Strict value is not equal to itself.")
168139 }
169140 else throw("Strict value is not equal to itself.")
170141 }
171142
172143
173144 @Callable(i)
174-func rebalanceRatio (targetRatio) = rebalanceInternal(targetRatio)
175-
176-
177-
178-@Callable(i)
179-func rebalance () = rebalanceInternal(getTargetRatio())
145+func rebalance () = {
146+ let asset1 = "WAVES"
147+ match getLeaseProxyConfig(asset1) {
148+ case a: (Boolean, Int, Int, String, String, Int, String) =>
149+ let $t042604369 = a
150+ let isLeasable = $t042604369._1
151+ let leasedRatio = $t042604369._2
152+ let minBalance = $t042604369._3
153+ let proxyAddress = $t042604369._4
154+ let proxyAssetId = $t042604369._5
155+ let proxyRateMul = $t042604369._6
156+ let stakingProfitAddress = $t042604369._7
157+ if (isLeasable)
158+ then rebalanceInternal(leasedRatio, asset1, proxyAssetId, minBalance, addressFromStringValue(proxyAddress), proxyRateMul)
159+ else nil
160+ case _ =>
161+ throw("BRR")
162+ }
163+ }
180164
181165

github/deemru/w8io/026f985 
38.06 ms