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:BgIECAISAAwAA1NFUAICX18ADGNvbnRyYWN0RmlsZQIJcmViYWxhbmNlABFrZXlGYWN0b3J5QWRkcmVzcwILJXNfX2ZhY3RvcnkBFGtleUFkZGl0aW9uYWxCYWxhbmNlAQdhc3NldElkCQC5CQIJAMwIAgIEJXMlcwkAzAgCAhFhZGRpdGlvbmFsQmFsYW5jZQkAzAgCBQdhc3NldElkBQNuaWwFA1NFUAEWa2V5U3Rha2luZ0Fzc2V0QmFsYW5jZQEHYXNzZXRJZAkAuQkCCQDMCAICBCVzJXMJAMwIAgITc3Rha2luZ0Fzc2V0QmFsYW5jZQkAzAgCBQdhc3NldElkBQNuaWwFA1NFUAEUZ2V0QWRkaXRpb25hbEJhbGFuY2UBB2Fzc2V0SWQJAQt2YWx1ZU9yRWxzZQIJAJoIAgUEdGhpcwkBFGtleUFkZGl0aW9uYWxCYWxhbmNlAQUHYXNzZXRJZAAAARZnZXRTdGFraW5nQXNzZXRCYWxhbmNlAQdhc3NldElkCQELdmFsdWVPckVsc2UCCQCaCAIFBHRoaXMJARZrZXlTdGFraW5nQXNzZXRCYWxhb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3MIBQskdDA0MjYwNDM2OQJfNwMFCmlzTGVhc2FibGUJARFyZWJhbGFuY2VJbnRlcm5hbAYFC2xlYXNlZFJhdGlvBQZhc3NldDEFDHByb3h5QXNzZXRJZAUKbWluQmFsYW5jZQkBEUBleHRyTmF0aXZlKDEwNjIpAQUMcHJveHlBZGRyZXNzBQxwcm94eVJhdGVNdWwFA25pbAkAAgECA0JSUgAZBfhq", "height": 2511258, "applicationStatus": "succeeded", "spentComplexity": 0 } View: original | compacted Prev: AMUwG36YQv66tpSkTNkVUyUeaCsDCK5rdczmaWj9tJvc Next: E2AYH9iR5teHM54BQBnsut7vra1dEtDS9zwSorEAccuK Diff:
Old | New | Differences | |
---|---|---|---|
1 | 1 | {-# STDLIB_VERSION 6 #-} | |
2 | 2 | {-# SCRIPT_TYPE ACCOUNT #-} | |
3 | 3 | {-# CONTENT_TYPE DAPP #-} | |
4 | + | let SEP = "__" | |
5 | + | ||
4 | 6 | 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" | |
17 | 7 | ||
18 | 8 | let keyFactoryAddress = "%s__factory" | |
19 | 9 | ||
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) | |
28 | 11 | ||
29 | 12 | ||
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) | |
36 | 14 | ||
37 | 15 | ||
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) | |
44 | 17 | ||
45 | 18 | ||
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) | |
52 | 20 | ||
53 | 21 | ||
54 | - | func getRate () = { | |
55 | - | let proxy = addressFromStringValue(getStringOrThrow(keyProxyAddress)) | |
22 | + | func getRate (proxy) = { | |
56 | 23 | let inv = invoke(proxy, "getRate", nil, nil) | |
57 | 24 | if ((inv == inv)) | |
58 | 25 | then match inv { | |
65 | 32 | } | |
66 | 33 | ||
67 | 34 | ||
68 | - | func getProxyAddress () = addressFromStringValue(getStringOrThrow(keyProxyAddress)) | |
69 | - | ||
70 | - | ||
71 | - | func getSWavesId () = fromBase58String(getStringOrThrow(keySWavesAssetId)) | |
72 | - | ||
73 | - | ||
74 | - | func getLeaseProxyConfig () = { | |
35 | + | func getLeaseProxyConfig (assetId) = { | |
75 | 36 | let factory = addressFromStringValue(valueOrElse(getString(keyFactoryAddress), "")) | |
76 | 37 | let myAddressString = toString(this) | |
77 | - | | |
38 | + | invoke(factory, "getPoolLeaseConfigREADONLY", [myAddressString, assetId], nil) | |
78 | 39 | } | |
79 | 40 | ||
80 | 41 | ||
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) | |
84 | 44 | if ((currentAdditionalBalance == currentAdditionalBalance)) | |
85 | 45 | 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.") | |
99 | 65 | } | |
100 | - | else throw("Strict value is not equal to itself.") | |
101 | - | } | |
102 | - | else nil | |
66 | + | else nil | |
67 | + | } | |
103 | 68 | else throw("Strict value is not equal to itself.") | |
104 | 69 | } | |
105 | 70 | else throw("Strict value is not equal to itself.") | |
106 | 71 | } | |
107 | 72 | ||
108 | 73 | ||
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) | |
112 | 76 | if ((currentAdditionalBalance == currentAdditionalBalance)) | |
113 | 77 | then { | |
114 | - | let | |
115 | - | if (( | |
78 | + | let currentStakingAssetBalance = getStakingAssetBalance(stakingAssetId) | |
79 | + | if ((currentStakingAssetBalance == currentStakingAssetBalance)) | |
116 | 80 | then { | |
117 | - | let currentProxyRate = getRate() | |
81 | + | let currentProxyRate = getRate(proxy) | |
118 | 82 | if ((currentProxyRate == currentProxyRate)) | |
119 | 83 | then { | |
120 | - | let | |
121 | - | if (( | |
84 | + | let sendStakingAssetAmount = fraction(proxyRateMul, amount, currentProxyRate) | |
85 | + | if ((sendStakingAssetAmount > 0)) | |
122 | 86 | then { | |
123 | - | let withdrawInvoke = invoke(proxy, "withdraw", nil, [AttachedPayment( | |
87 | + | let withdrawInvoke = invoke(proxy, "withdraw", nil, [AttachedPayment(fromBase58String(stakingAssetId), sendStakingAssetAmount)]) | |
124 | 88 | if ((withdrawInvoke == withdrawInvoke)) | |
125 | 89 | then match withdrawInvoke { | |
126 | - | case | |
127 | - | let newAdditionalBalance = (currentAdditionalBalance - | |
128 | - | let newSWavesBalance = ( | |
129 | - | [IntegerEntry(keyAdditionalBalance, newAdditionalBalance), IntegerEntry( | |
90 | + | case receivedAssets: Int => | |
91 | + | let newAdditionalBalance = (currentAdditionalBalance - receivedAssets) | |
92 | + | let newSWavesBalance = (currentStakingAssetBalance - sendStakingAssetAmount) | |
93 | + | [IntegerEntry(keyAdditionalBalance(assetId), newAdditionalBalance), IntegerEntry(keyStakingAssetBalance(stakingAssetId), newSWavesBalance)] | |
130 | 94 | case _ => | |
131 | 95 | nil | |
132 | 96 | } | |
142 | 106 | } | |
143 | 107 | ||
144 | 108 | ||
145 | - | func rebalanceInternal (targetRatio) = { | |
146 | - | let | |
147 | - | if (( | |
109 | + | func rebalanceInternal (targetRatio,assetId,stakingAssetId,minBalance,proxy,proxyRateMul) = { | |
110 | + | let currentAdditionalBalance = getAdditionalBalance(assetId) | |
111 | + | if ((currentAdditionalBalance == currentAdditionalBalance)) | |
148 | 112 | then { | |
149 | - | let | |
150 | - | if (( | |
113 | + | let currentStakingAssetBalance = getStakingAssetBalance(stakingAssetId) | |
114 | + | if ((currentStakingAssetBalance == currentStakingAssetBalance)) | |
151 | 115 | 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.") | |
166 | 137 | } | |
167 | 138 | else throw("Strict value is not equal to itself.") | |
168 | 139 | } | |
171 | 142 | ||
172 | 143 | ||
173 | 144 | @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 | + | } | |
180 | 164 | ||
181 | 165 |
Old | New | Differences | |
---|---|---|---|
1 | 1 | {-# STDLIB_VERSION 6 #-} | |
2 | 2 | {-# SCRIPT_TYPE ACCOUNT #-} | |
3 | 3 | {-# CONTENT_TYPE DAPP #-} | |
4 | + | let SEP = "__" | |
5 | + | ||
4 | 6 | 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" | |
17 | 7 | ||
18 | 8 | let keyFactoryAddress = "%s__factory" | |
19 | 9 | ||
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) | |
28 | 11 | ||
29 | 12 | ||
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) | |
36 | 14 | ||
37 | 15 | ||
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) | |
44 | 17 | ||
45 | 18 | ||
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) | |
52 | 20 | ||
53 | 21 | ||
54 | - | func getRate () = { | |
55 | - | let proxy = addressFromStringValue(getStringOrThrow(keyProxyAddress)) | |
22 | + | func getRate (proxy) = { | |
56 | 23 | let inv = invoke(proxy, "getRate", nil, nil) | |
57 | 24 | if ((inv == inv)) | |
58 | 25 | then match inv { | |
59 | 26 | case s: String => | |
60 | 27 | parseIntValue(s) | |
61 | 28 | case _ => | |
62 | 29 | throw("Rate is zero (0)") | |
63 | 30 | } | |
64 | 31 | else throw("Strict value is not equal to itself.") | |
65 | 32 | } | |
66 | 33 | ||
67 | 34 | ||
68 | - | func getProxyAddress () = addressFromStringValue(getStringOrThrow(keyProxyAddress)) | |
69 | - | ||
70 | - | ||
71 | - | func getSWavesId () = fromBase58String(getStringOrThrow(keySWavesAssetId)) | |
72 | - | ||
73 | - | ||
74 | - | func getLeaseProxyConfig () = { | |
35 | + | func getLeaseProxyConfig (assetId) = { | |
75 | 36 | let factory = addressFromStringValue(valueOrElse(getString(keyFactoryAddress), "")) | |
76 | 37 | let myAddressString = toString(this) | |
77 | - | | |
38 | + | invoke(factory, "getPoolLeaseConfigREADONLY", [myAddressString, assetId], nil) | |
78 | 39 | } | |
79 | 40 | ||
80 | 41 | ||
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) | |
84 | 44 | if ((currentAdditionalBalance == currentAdditionalBalance)) | |
85 | 45 | 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.") | |
99 | 65 | } | |
100 | - | else throw("Strict value is not equal to itself.") | |
101 | - | } | |
102 | - | else nil | |
66 | + | else nil | |
67 | + | } | |
103 | 68 | else throw("Strict value is not equal to itself.") | |
104 | 69 | } | |
105 | 70 | else throw("Strict value is not equal to itself.") | |
106 | 71 | } | |
107 | 72 | ||
108 | 73 | ||
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) | |
112 | 76 | if ((currentAdditionalBalance == currentAdditionalBalance)) | |
113 | 77 | then { | |
114 | - | let | |
115 | - | if (( | |
78 | + | let currentStakingAssetBalance = getStakingAssetBalance(stakingAssetId) | |
79 | + | if ((currentStakingAssetBalance == currentStakingAssetBalance)) | |
116 | 80 | then { | |
117 | - | let currentProxyRate = getRate() | |
81 | + | let currentProxyRate = getRate(proxy) | |
118 | 82 | if ((currentProxyRate == currentProxyRate)) | |
119 | 83 | then { | |
120 | - | let | |
121 | - | if (( | |
84 | + | let sendStakingAssetAmount = fraction(proxyRateMul, amount, currentProxyRate) | |
85 | + | if ((sendStakingAssetAmount > 0)) | |
122 | 86 | then { | |
123 | - | let withdrawInvoke = invoke(proxy, "withdraw", nil, [AttachedPayment( | |
87 | + | let withdrawInvoke = invoke(proxy, "withdraw", nil, [AttachedPayment(fromBase58String(stakingAssetId), sendStakingAssetAmount)]) | |
124 | 88 | if ((withdrawInvoke == withdrawInvoke)) | |
125 | 89 | then match withdrawInvoke { | |
126 | - | case | |
127 | - | let newAdditionalBalance = (currentAdditionalBalance - | |
128 | - | let newSWavesBalance = ( | |
129 | - | [IntegerEntry(keyAdditionalBalance, newAdditionalBalance), IntegerEntry( | |
90 | + | case receivedAssets: Int => | |
91 | + | let newAdditionalBalance = (currentAdditionalBalance - receivedAssets) | |
92 | + | let newSWavesBalance = (currentStakingAssetBalance - sendStakingAssetAmount) | |
93 | + | [IntegerEntry(keyAdditionalBalance(assetId), newAdditionalBalance), IntegerEntry(keyStakingAssetBalance(stakingAssetId), newSWavesBalance)] | |
130 | 94 | case _ => | |
131 | 95 | nil | |
132 | 96 | } | |
133 | 97 | else throw("Strict value is not equal to itself.") | |
134 | 98 | } | |
135 | 99 | else nil | |
136 | 100 | } | |
137 | 101 | else throw("Strict value is not equal to itself.") | |
138 | 102 | } | |
139 | 103 | else throw("Strict value is not equal to itself.") | |
140 | 104 | } | |
141 | 105 | else throw("Strict value is not equal to itself.") | |
142 | 106 | } | |
143 | 107 | ||
144 | 108 | ||
145 | - | func rebalanceInternal (targetRatio) = { | |
146 | - | let | |
147 | - | if (( | |
109 | + | func rebalanceInternal (targetRatio,assetId,stakingAssetId,minBalance,proxy,proxyRateMul) = { | |
110 | + | let currentAdditionalBalance = getAdditionalBalance(assetId) | |
111 | + | if ((currentAdditionalBalance == currentAdditionalBalance)) | |
148 | 112 | then { | |
149 | - | let | |
150 | - | if (( | |
113 | + | let currentStakingAssetBalance = getStakingAssetBalance(stakingAssetId) | |
114 | + | if ((currentStakingAssetBalance == currentStakingAssetBalance)) | |
151 | 115 | 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.") | |
166 | 137 | } | |
167 | 138 | else throw("Strict value is not equal to itself.") | |
168 | 139 | } | |
169 | 140 | else throw("Strict value is not equal to itself.") | |
170 | 141 | } | |
171 | 142 | ||
172 | 143 | ||
173 | 144 | @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 | + | } | |
180 | 164 | ||
181 | 165 |
github/deemru/w8io/026f985 38.06 ms ◑