tx · 9VSJDAE3AHniSi75bSKhCVmtHkc9WfD5jg2kNLKT1KBf 3MpMuGN8rW9EpPwzETAQz88Pe9TQhSwiriq: -0.01000000 Waves 2023.07.19 17:16 [2672850] smart account 3MpMuGN8rW9EpPwzETAQz88Pe9TQhSwiriq > SELF 0.00000000 Waves
{ "type": 13, "id": "9VSJDAE3AHniSi75bSKhCVmtHkc9WfD5jg2kNLKT1KBf", "fee": 1000000, "feeAssetId": null, "timestamp": 1689776256967, "version": 2, "chainId": 84, "sender": "3MpMuGN8rW9EpPwzETAQz88Pe9TQhSwiriq", "senderPublicKey": "57SkjQUEwC1ayCqLbXLuzBqifK6B6SR6rfxtGsbAo3zW", "proofs": [ "3dKGXXkJyYpt42MLsSvpKZgDuYHWagsiNkpda3M3j6ATJgCKrcyj3CLRmT3tq4zqdiVZN45NbG98T4eMdFurK6sa" ], "script": "base64:BgIaCAISAwoBARIAEgMKAQgSAwoBARIDCgEIEgAWAAxjb250cmFjdEZpbGUCEG1ydF9zdGFraW5nLnJpZGUAA1NFUAICX18ABnNjYWxlOACAwtcvABJBRERSRVNTX0JZVEVTX1NJWkUAGgEIdGhyb3dFcnIBA21zZwkAAgEJAKwCAgkArAICBQxjb250cmFjdEZpbGUCAjogBQNtc2cACmtleUFzc2V0SWQJALkJAgkAzAgCAgIlcwkAzAgCAgdhc3NldElkBQNuaWwFA1NFUAATa2V5RW1pc3Npb25QZXJCbG9jawkAuQkCCQDMCAICAiVzCQDMCAICEGVtaXNzaW9uUGVyQmxvY2sFA25pbAUDU0VQAA1rZXlTdGFydEJsb2NrCQC5CQIJAMwIAgICJXMJAMwIAgIKc3RhcnRCbG9jawUDbmlsBQNTRVAAEGtleVRvdGFsTHBBbW91bnQJALkJAgkAzAgCAgIlcwkAzAgCAg10b3RhbExwQW1vdW50BQNuaWwFA1NFUAATa2V5VG90YWxBc3NldEFtb3VudAkAuQkCCQDMCAICAiVzCQDMCAICEHRvdGFsQXNzZXRBbW91bnQFA25pbAUDU0VQ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ABBd1c2VyVG90YWxBc3NldFdpdGhkcmF3bgkBC3ZhbHVlT3JFbHNlAgkAmggCBQR0aGlzCQEaa2V5VXNlclRvdGFsQXNzZXRXaXRoZHJhd24BBQt1c2VyQWRkcmVzcwAACQCUCgIFA25pbAkAlwoFBQx1c2VyTHBBbW91bnQFHHVzZXJBdmFpbGFibGVBc3NldFRvV2l0aGRyYXcJAQ9nZXRDdXJyZW50UHJpY2UABRV1c2VyVG90YWxTdGFrZWRBbW91bnQFF3VzZXJUb3RhbEFzc2V0V2l0aGRyYXduAWkBFmdldFRvdGFsQXNzZXRzUkVBRE9OTFkACQCUCgIFA25pbAkAlQoDBQ10b3RhbExwQW1vdW50CQErZ2V0VG90YWxBc3NldEFtb3VudFdpdGhQcm9maXRPck1heEF2YWlsYWJsZQAJAQ9nZXRDdXJyZW50UHJpY2UAAHZVUSA=", "height": 2672850, "applicationStatus": "succeeded", "spentComplexity": 0 } View: original | compacted Prev: BrkV6ZnB28fo3AMKbuDLP5GHPgzyrbBBVsRxAjqjnbJw Next: 8Fa8PH8xvgBTVuXa7AWViEpTjZ8rooSMzJf4SRfVELxi Diff:
Old | New | Differences | |
---|---|---|---|
54 | 54 | ||
55 | 55 | func getTotalAssetAmountWithProfitOrMaxAvailable () = { | |
56 | 56 | let totalAssetAmountWithProfit = (totalAssetAmount + calcTotalProfit()) | |
57 | - | match assetIdBytes { | |
57 | + | let totalAmount = match assetIdBytes { | |
58 | 58 | case u: Unit => | |
59 | 59 | min([totalAssetAmountWithProfit, wavesBalance(this).available]) | |
60 | 60 | case b: ByteVector => | |
62 | 62 | case _ => | |
63 | 63 | throw("Match error") | |
64 | 64 | } | |
65 | + | if ((totalLpAmount == 0)) | |
66 | + | then 0 | |
67 | + | else totalAmount | |
65 | 68 | } | |
66 | 69 | ||
67 | 70 | ||
70 | 73 | else (1 * scale8) | |
71 | 74 | ||
72 | 75 | ||
73 | - | func | |
76 | + | func getStakeActions (i,userAddress) = { | |
74 | 77 | let checks = [if ((size(i.payments) == 1)) | |
75 | 78 | then true | |
76 | 79 | else throwErr("should include 1 payment"), if ((i.payments[0].assetId == assetIdBytes)) | |
83 | 86 | let paymentAmount = i.payments[0].amount | |
84 | 87 | let paymentLpAmount = fraction(paymentAmount, scale8, getCurrentPrice()) | |
85 | 88 | let userLpAmount = getUserLpAmount(userAddress) | |
86 | - | let | |
89 | + | let userTotalStakedAmount = valueOrElse(getInteger(this, keyUserTotalAssetStaked(userAddress)), 0) | |
87 | 90 | let newTotalLpAmount = (totalLpAmount + paymentLpAmount) | |
88 | 91 | let newTotalAssetAmount = (totalAssetAmount + paymentAmount) | |
89 | 92 | let newUserLpAmount = (userLpAmount + paymentLpAmount) | |
90 | - | let | |
93 | + | let newUserTotalStakedAmount = (userTotalStakedAmount + paymentAmount) | |
91 | 94 | let updateStartHeightAction = if ((totalLpAmount == 0)) | |
92 | 95 | then [IntegerEntry(keyStartBlock, height)] | |
93 | 96 | else nil | |
94 | - | ([IntegerEntry(keyTotalLpAmount, newTotalLpAmount), IntegerEntry(keyTotalAssetAmount, newTotalAssetAmount), IntegerEntry(keyUserLpAmount(userAddress), newUserLpAmount), IntegerEntry(keyUserTotalAssetStaked(userAddress), | |
97 | + | ([IntegerEntry(keyTotalLpAmount, newTotalLpAmount), IntegerEntry(keyTotalAssetAmount, newTotalAssetAmount), IntegerEntry(keyUserLpAmount(userAddress), newUserLpAmount), IntegerEntry(keyUserTotalAssetStaked(userAddress), newUserTotalStakedAmount)] ++ updateStartHeightAction) | |
95 | 98 | } | |
96 | 99 | else throw("Strict value is not equal to itself.") | |
97 | 100 | } | |
110 | 113 | ||
111 | 114 | ||
112 | 115 | @Callable(i) | |
113 | - | func stake () = | |
116 | + | func stake () = getStakeActions(i, toString(i.caller)) | |
114 | 117 | ||
115 | 118 | ||
116 | 119 | ||
120 | 123 | then true | |
121 | 124 | else throwErr("address is not valid")] | |
122 | 125 | if ((check == check)) | |
123 | - | then | |
126 | + | then getStakeActions(i, userAddress) | |
124 | 127 | else throw("Strict value is not equal to itself.") | |
125 | 128 | } | |
126 | 129 | ||
154 | 157 | func getUserAssetsREADONLY (userAddress) = { | |
155 | 158 | let userLpAmount = getUserLpAmount(userAddress) | |
156 | 159 | let userAvailableAssetToWithdraw = fraction(userLpAmount, getCurrentPrice(), scale8) | |
157 | - | $Tuple2(nil, $Tuple3(userLpAmount, userAvailableAssetToWithdraw, getCurrentPrice())) | |
160 | + | let userTotalStakedAmount = valueOrElse(getInteger(this, keyUserTotalAssetStaked(userAddress)), 0) | |
161 | + | let userTotalAssetWithdrawn = valueOrElse(getInteger(this, keyUserTotalAssetWithdrawn(userAddress)), 0) | |
162 | + | $Tuple2(nil, $Tuple5(userLpAmount, userAvailableAssetToWithdraw, getCurrentPrice(), userTotalStakedAmount, userTotalAssetWithdrawn)) | |
158 | 163 | } | |
159 | 164 | ||
160 | 165 |
Old | New | Differences | |
---|---|---|---|
1 | 1 | {-# STDLIB_VERSION 6 #-} | |
2 | 2 | {-# SCRIPT_TYPE ACCOUNT #-} | |
3 | 3 | {-# CONTENT_TYPE DAPP #-} | |
4 | 4 | let contractFile = "mrt_staking.ride" | |
5 | 5 | ||
6 | 6 | let SEP = "__" | |
7 | 7 | ||
8 | 8 | let scale8 = 100000000 | |
9 | 9 | ||
10 | 10 | let ADDRESS_BYTES_SIZE = 26 | |
11 | 11 | ||
12 | 12 | func throwErr (msg) = throw(((contractFile + ": ") + msg)) | |
13 | 13 | ||
14 | 14 | ||
15 | 15 | let keyAssetId = makeString(["%s", "assetId"], SEP) | |
16 | 16 | ||
17 | 17 | let keyEmissionPerBlock = makeString(["%s", "emissionPerBlock"], SEP) | |
18 | 18 | ||
19 | 19 | let keyStartBlock = makeString(["%s", "startBlock"], SEP) | |
20 | 20 | ||
21 | 21 | let keyTotalLpAmount = makeString(["%s", "totalLpAmount"], SEP) | |
22 | 22 | ||
23 | 23 | let keyTotalAssetAmount = makeString(["%s", "totalAssetAmount"], SEP) | |
24 | 24 | ||
25 | 25 | func keyUserLpAmount (userAddress) = makeString(["%s%s", "userLpAmount", userAddress], SEP) | |
26 | 26 | ||
27 | 27 | ||
28 | 28 | func keyUserTotalAssetWithdrawn (userAddress) = makeString(["%s%s", "totalAssetWithdrawn", userAddress], SEP) | |
29 | 29 | ||
30 | 30 | ||
31 | 31 | func keyUserTotalAssetStaked (userAddress) = makeString(["%s%s", "totalAssetStaked", userAddress], SEP) | |
32 | 32 | ||
33 | 33 | ||
34 | 34 | let totalLpAmount = valueOrElse(getInteger(this, keyTotalLpAmount), 0) | |
35 | 35 | ||
36 | 36 | let totalAssetAmount = valueOrElse(getInteger(this, keyTotalAssetAmount), 0) | |
37 | 37 | ||
38 | 38 | let assetIdString = valueOrElse(getString(this, keyAssetId), "WAVES") | |
39 | 39 | ||
40 | 40 | let assetIdBytes = if ((assetIdString == "WAVES")) | |
41 | 41 | then unit | |
42 | 42 | else fromBase58String(assetIdString) | |
43 | 43 | ||
44 | 44 | func getUserLpAmount (userAddress) = valueOrElse(getInteger(this, keyUserLpAmount(userAddress)), 0) | |
45 | 45 | ||
46 | 46 | ||
47 | 47 | func calcTotalProfit () = { | |
48 | 48 | let emissionPerBlock = valueOrElse(getInteger(this, keyEmissionPerBlock), 0) | |
49 | 49 | let startBlock = valueOrElse(getInteger(this, keyStartBlock), 0) | |
50 | 50 | let elapsedBlocks = (height - startBlock) | |
51 | 51 | max([0, (emissionPerBlock * elapsedBlocks)]) | |
52 | 52 | } | |
53 | 53 | ||
54 | 54 | ||
55 | 55 | func getTotalAssetAmountWithProfitOrMaxAvailable () = { | |
56 | 56 | let totalAssetAmountWithProfit = (totalAssetAmount + calcTotalProfit()) | |
57 | - | match assetIdBytes { | |
57 | + | let totalAmount = match assetIdBytes { | |
58 | 58 | case u: Unit => | |
59 | 59 | min([totalAssetAmountWithProfit, wavesBalance(this).available]) | |
60 | 60 | case b: ByteVector => | |
61 | 61 | min([totalAssetAmountWithProfit, assetBalance(this, b)]) | |
62 | 62 | case _ => | |
63 | 63 | throw("Match error") | |
64 | 64 | } | |
65 | + | if ((totalLpAmount == 0)) | |
66 | + | then 0 | |
67 | + | else totalAmount | |
65 | 68 | } | |
66 | 69 | ||
67 | 70 | ||
68 | 71 | func getCurrentPrice () = if ((totalLpAmount != 0)) | |
69 | 72 | then fraction(getTotalAssetAmountWithProfitOrMaxAvailable(), scale8, totalLpAmount) | |
70 | 73 | else (1 * scale8) | |
71 | 74 | ||
72 | 75 | ||
73 | - | func | |
76 | + | func getStakeActions (i,userAddress) = { | |
74 | 77 | let checks = [if ((size(i.payments) == 1)) | |
75 | 78 | then true | |
76 | 79 | else throwErr("should include 1 payment"), if ((i.payments[0].assetId == assetIdBytes)) | |
77 | 80 | then true | |
78 | 81 | else throwErr(("payment should be in " + assetIdString)), if ((i.payments[0].amount > 0)) | |
79 | 82 | then true | |
80 | 83 | else "payment amount should be greater than 0"] | |
81 | 84 | if ((checks == checks)) | |
82 | 85 | then { | |
83 | 86 | let paymentAmount = i.payments[0].amount | |
84 | 87 | let paymentLpAmount = fraction(paymentAmount, scale8, getCurrentPrice()) | |
85 | 88 | let userLpAmount = getUserLpAmount(userAddress) | |
86 | - | let | |
89 | + | let userTotalStakedAmount = valueOrElse(getInteger(this, keyUserTotalAssetStaked(userAddress)), 0) | |
87 | 90 | let newTotalLpAmount = (totalLpAmount + paymentLpAmount) | |
88 | 91 | let newTotalAssetAmount = (totalAssetAmount + paymentAmount) | |
89 | 92 | let newUserLpAmount = (userLpAmount + paymentLpAmount) | |
90 | - | let | |
93 | + | let newUserTotalStakedAmount = (userTotalStakedAmount + paymentAmount) | |
91 | 94 | let updateStartHeightAction = if ((totalLpAmount == 0)) | |
92 | 95 | then [IntegerEntry(keyStartBlock, height)] | |
93 | 96 | else nil | |
94 | - | ([IntegerEntry(keyTotalLpAmount, newTotalLpAmount), IntegerEntry(keyTotalAssetAmount, newTotalAssetAmount), IntegerEntry(keyUserLpAmount(userAddress), newUserLpAmount), IntegerEntry(keyUserTotalAssetStaked(userAddress), | |
97 | + | ([IntegerEntry(keyTotalLpAmount, newTotalLpAmount), IntegerEntry(keyTotalAssetAmount, newTotalAssetAmount), IntegerEntry(keyUserLpAmount(userAddress), newUserLpAmount), IntegerEntry(keyUserTotalAssetStaked(userAddress), newUserTotalStakedAmount)] ++ updateStartHeightAction) | |
95 | 98 | } | |
96 | 99 | else throw("Strict value is not equal to itself.") | |
97 | 100 | } | |
98 | 101 | ||
99 | 102 | ||
100 | 103 | @Callable(i) | |
101 | 104 | func setEmissionPerBlock (emissionPerBlock) = { | |
102 | 105 | let check = [if ((i.caller == this)) | |
103 | 106 | then true | |
104 | 107 | else throwErr("permission denied")] | |
105 | 108 | if ((check == check)) | |
106 | 109 | then [IntegerEntry(keyTotalAssetAmount, getTotalAssetAmountWithProfitOrMaxAvailable()), IntegerEntry(keyStartBlock, height), IntegerEntry(keyEmissionPerBlock, max([0, emissionPerBlock]))] | |
107 | 110 | else throw("Strict value is not equal to itself.") | |
108 | 111 | } | |
109 | 112 | ||
110 | 113 | ||
111 | 114 | ||
112 | 115 | @Callable(i) | |
113 | - | func stake () = | |
116 | + | func stake () = getStakeActions(i, toString(i.caller)) | |
114 | 117 | ||
115 | 118 | ||
116 | 119 | ||
117 | 120 | @Callable(i) | |
118 | 121 | func stakeFor (userAddress) = { | |
119 | 122 | let check = [if ((size(fromBase58String(userAddress)) == ADDRESS_BYTES_SIZE)) | |
120 | 123 | then true | |
121 | 124 | else throwErr("address is not valid")] | |
122 | 125 | if ((check == check)) | |
123 | - | then | |
126 | + | then getStakeActions(i, userAddress) | |
124 | 127 | else throw("Strict value is not equal to itself.") | |
125 | 128 | } | |
126 | 129 | ||
127 | 130 | ||
128 | 131 | ||
129 | 132 | @Callable(i) | |
130 | 133 | func withdraw (lpAssetWithdrawAmount) = { | |
131 | 134 | let userAddress = toString(i.caller) | |
132 | 135 | let userLpAmount = getUserLpAmount(userAddress) | |
133 | 136 | let check = [if ((lpAssetWithdrawAmount > 0)) | |
134 | 137 | then true | |
135 | 138 | else throwErr("amount should be more than 0"), if ((userLpAmount >= lpAssetWithdrawAmount)) | |
136 | 139 | then true | |
137 | 140 | else throwErr((("cannot withdraw more than available (" + toString(userLpAmount)) + ")"))] | |
138 | 141 | if ((check == check)) | |
139 | 142 | then { | |
140 | 143 | let newUserLpAmount = (userLpAmount - lpAssetWithdrawAmount) | |
141 | 144 | let withdrawAssetAmount = fraction(lpAssetWithdrawAmount, getCurrentPrice(), scale8) | |
142 | 145 | let newTotalLpAmount = (totalLpAmount - lpAssetWithdrawAmount) | |
143 | 146 | let newTotalAssetAmount = fraction(newTotalLpAmount, getCurrentPrice(), scale8) | |
144 | 147 | let userTotalAssetWithdrawn = valueOrElse(getInteger(this, keyUserTotalAssetWithdrawn(userAddress)), 0) | |
145 | 148 | let newUserTotalAssetWithdrawn = (userTotalAssetWithdrawn + withdrawAssetAmount) | |
146 | 149 | [IntegerEntry(keyTotalLpAmount, newTotalLpAmount), IntegerEntry(keyTotalAssetAmount, newTotalAssetAmount), IntegerEntry(keyUserLpAmount(userAddress), newUserLpAmount), IntegerEntry(keyUserTotalAssetWithdrawn(userAddress), newUserTotalAssetWithdrawn), IntegerEntry(keyStartBlock, height), ScriptTransfer(i.caller, withdrawAssetAmount, assetIdBytes)] | |
147 | 150 | } | |
148 | 151 | else throw("Strict value is not equal to itself.") | |
149 | 152 | } | |
150 | 153 | ||
151 | 154 | ||
152 | 155 | ||
153 | 156 | @Callable(i) | |
154 | 157 | func getUserAssetsREADONLY (userAddress) = { | |
155 | 158 | let userLpAmount = getUserLpAmount(userAddress) | |
156 | 159 | let userAvailableAssetToWithdraw = fraction(userLpAmount, getCurrentPrice(), scale8) | |
157 | - | $Tuple2(nil, $Tuple3(userLpAmount, userAvailableAssetToWithdraw, getCurrentPrice())) | |
160 | + | let userTotalStakedAmount = valueOrElse(getInteger(this, keyUserTotalAssetStaked(userAddress)), 0) | |
161 | + | let userTotalAssetWithdrawn = valueOrElse(getInteger(this, keyUserTotalAssetWithdrawn(userAddress)), 0) | |
162 | + | $Tuple2(nil, $Tuple5(userLpAmount, userAvailableAssetToWithdraw, getCurrentPrice(), userTotalStakedAmount, userTotalAssetWithdrawn)) | |
158 | 163 | } | |
159 | 164 | ||
160 | 165 | ||
161 | 166 | ||
162 | 167 | @Callable(i) | |
163 | 168 | func getTotalAssetsREADONLY () = $Tuple2(nil, $Tuple3(totalLpAmount, getTotalAssetAmountWithProfitOrMaxAvailable(), getCurrentPrice())) | |
164 | 169 | ||
165 | 170 |
github/deemru/w8io/026f985 35.22 ms ◑