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