tx · 6eC7XpinLjD7ZWisd4M5TyHy41mSk9PgvHEUDHHc5UMq 3MsU428UzSefm85q7jvBsRV66Du7E72xEkL: -0.01000000 Waves 2022.06.10 10:18 [2089982] smart account 3MsU428UzSefm85q7jvBsRV66Du7E72xEkL > SELF 0.00000000 Waves
{ "type": 13, "id": "6eC7XpinLjD7ZWisd4M5TyHy41mSk9PgvHEUDHHc5UMq", "fee": 1000000, "feeAssetId": null, "timestamp": 1654845447444, "version": 2, "chainId": 84, "sender": "3MsU428UzSefm85q7jvBsRV66Du7E72xEkL", "senderPublicKey": "58FFoU7LCj1DKboVntQXuE3KQDpf92NouXncN5b4vY8U", "proofs": [ "uUvgCLpCEegbnR9eXKbq5p7kuwpPENVmvFuXvjxFvDZYfVATQchLxZiTZkjcpcNeWzTc7Yg9R9BXMXDLHJs2tur" ], "script": "base64:AAIFAAAAAAAAAAsIAhIAEgMKAQESAAAAAAgAAAAACkRJU0NMQUlNRVICAAAARXRlc3RpbmcgUFVaWkxFIHN0YWtpbmcgc21hcnQgY29udHJhY3Qgd2l0aCByZXdhcmRzIGluIG11bHRpcGxlIGFzc2V0cwAAAAAFc2NhbGUAAAAAAAX14QAAAAAACHB1enpsZUlkAQAAACAtNxOB0yQ3XZ/hb3bT0Q+T7yws9N2JrVxKVuh8yQFyGwAAAAAMZWFybmVkQXNzZXRzCQAETAAAAAIBAAAAIK2dSBaFkXln78NKeoaZ0d1Q6QVJPOT+QERHjOGDxML0BQAAAANuaWwAAAAAEmVhcm5lZEFzc2V0c0Ftb3VudAkAAZAAAAABBQAAAAxlYXJuZWRBc3NldHMBAAAADXRyeUdldEludGVnZXIAAAABAAAAA2tleQQAAAAHJG1hdGNoMAkABBoAAAACBQAAAAR0aGlzBQAAAANrZXkDCQAAAQAAAAIFAAAAByRtYXRjaDACAAAAA0ludAQAAAABYgUAAAAHJG1hdGNoMAUAAAABYgAAAAAAAAAAAAEAAAAdY2FsY3VsYXRlQ3VycmVudEFzc2V0SW50ZXJlc3Q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AABlAAAAAgkBAAAADXRyeUdldEludGVnZXIAAAABAgAAABNnbG9iYWxfcHV6emxlU3Rha2VkBQAAAAxwdXp6bGVBbW91bnQFAAAAA25pbAAAAAFpAQAAAAVjbGFpbQAAAAAJAQAAAAtjbGFpbVJlc3VsdAAAAAEIBQAAAAFpAAAABmNhbGxlcgAAAAEAAAACdHgBAAAABnZlcmlmeQAAAAAJAAH0AAAAAwgFAAAAAnR4AAAACWJvZHlCeXRlcwkAAZEAAAACCAUAAAACdHgAAAAGcHJvb2ZzAAAAAAAAAAAACAUAAAACdHgAAAAPc2VuZGVyUHVibGljS2V54dH/UQ==", "height": 2089982, "applicationStatus": "succeeded", "spentComplexity": 0 } View: original | compacted Prev: 5dtNnjNdnoHmk49iqqVGkQRRGL9J2FenuXy7GKyLfkxN Next: HX2qcsRGvSQgnaxXsFFtksiF2352Mj4QzarSYFrTa4XA Diff:
Old | New | Differences | |
---|---|---|---|
1 | 1 | {-# STDLIB_VERSION 5 #-} | |
2 | 2 | {-# SCRIPT_TYPE ACCOUNT #-} | |
3 | 3 | {-# CONTENT_TYPE DAPP #-} | |
4 | - | let | |
4 | + | let DISCLAIMER = "testing PUZZLE staking smart contract with rewards in multiple assets" | |
5 | 5 | ||
6 | - | let | |
6 | + | let scale = 100000000 | |
7 | 7 | ||
8 | - | let REWARD_AMOUNT = 100000000000 | |
8 | + | let puzzleId = base58'43W4FcqA1rEpSmUGHoGiXvpSLfhadws9LS5j3SJsKxxS' | |
9 | + | ||
10 | + | let earnedAssets = [base58'CgidgEMbCKXXErgJXVyZCFRaK8SBNA73Jg8mNXALtVKh'] | |
11 | + | ||
12 | + | let earnedAssetsAmount = size(earnedAssets) | |
13 | + | ||
14 | + | func tryGetInteger (key) = match getInteger(this, key) { | |
15 | + | case b: Int => | |
16 | + | b | |
17 | + | case _ => | |
18 | + | 0 | |
19 | + | } | |
20 | + | ||
21 | + | ||
22 | + | func calculateCurrentAssetInterest (assetId,assetIdStr) = { | |
23 | + | let totalStaked = tryGetInteger("global_puzzleStaked") | |
24 | + | let tokenBalanceLastCheck = tryGetInteger((("global_lastCheck_" + assetIdStr) + "_earnings")) | |
25 | + | let currentTokenEarnings = max([tokenBalanceLastCheck, assetBalance(this, assetId)]) | |
26 | + | let newEarnings = (currentTokenEarnings - tokenBalanceLastCheck) | |
27 | + | let newInterest = if ((totalStaked == 0)) | |
28 | + | then 0 | |
29 | + | else fraction(newEarnings, scale, totalStaked) | |
30 | + | let lastCheckInterest = tryGetInteger((("global_lastCheck_" + assetIdStr) + "_interest")) | |
31 | + | (lastCheckInterest + newInterest) | |
32 | + | } | |
33 | + | ||
34 | + | ||
35 | + | func claimResult (address) = { | |
36 | + | let addressStr = toString(address) | |
37 | + | let puzzleAmount = tryGetInteger((addressStr + "_puzzleStaked")) | |
38 | + | func handler (accum,assetId) = { | |
39 | + | let assetIdStr = toBase58String(assetId) | |
40 | + | let currentTokenInterest = calculateCurrentAssetInterest(assetId, assetIdStr) | |
41 | + | let currentTokenEarnings = max([tryGetInteger((("global_lastCheck_" + assetIdStr) + "_earnings")), assetBalance(this, assetId)]) | |
42 | + | let rewardAmount = fraction(puzzleAmount, (currentTokenInterest - tryGetInteger((((addressStr + "_lastCheck_") + assetIdStr) + "_interest"))), scale) | |
43 | + | if ((rewardAmount == 0)) | |
44 | + | then accum | |
45 | + | else (accum ++ [IntegerEntry((("global_lastCheck_" + assetIdStr) + "_earnings"), (currentTokenEarnings - rewardAmount)), IntegerEntry((("global_lastCheck_" + assetIdStr) + "_interest"), currentTokenInterest), IntegerEntry((((addressStr + "_lastCheck_") + assetIdStr) + "_interest"), currentTokenInterest), ScriptTransfer(address, rewardAmount, assetId)]) | |
46 | + | } | |
47 | + | ||
48 | + | let $l = earnedAssets | |
49 | + | let $s = size($l) | |
50 | + | let $acc0 = nil | |
51 | + | func $f0_1 ($a,$i) = if (($i >= $s)) | |
52 | + | then $a | |
53 | + | else handler($a, $l[$i]) | |
54 | + | ||
55 | + | func $f0_2 ($a,$i) = if (($i >= $s)) | |
56 | + | then $a | |
57 | + | else throw("List size exceeds 17") | |
58 | + | ||
59 | + | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10), 11), 12), 13), 14), 15), 16), 17) | |
60 | + | } | |
61 | + | ||
9 | 62 | ||
10 | 63 | @Callable(i) | |
11 | 64 | func stake () = { | |
12 | - | let $t0297372 = $Tuple2(i.payments[0].amount, i.payments[0].assetId) | |
13 | - | let pmtAmount = $t0297372._1 | |
14 | - | let pmtAssetId = $t0297372._2 | |
15 | - | if (if (if ((size(i.payments) == 0)) | |
16 | - | then (pmtAssetId != STAKE_ASSET) | |
17 | - | else false) | |
18 | - | then (0 >= pmtAmount) | |
19 | - | else false) | |
20 | - | then throw("send token to stake") | |
65 | + | let addressStr = toString(i.caller) | |
66 | + | let pmt = i.payments[0] | |
67 | + | if ((value(pmt.assetId) != puzzleId)) | |
68 | + | then throw("wrong asset attached") | |
21 | 69 | else { | |
22 | - | let currentKey = toBase58String(i.caller.bytes) | |
23 | - | let getStakedAmount = valueOrElse(getInteger(this, (currentKey + "_staked")), 0) | |
24 | - | let totalAmount = valueOrElse(getInteger(this, "total"), 0) | |
25 | - | let totalStaked = (getStakedAmount + pmtAmount) | |
26 | - | [IntegerEntry((((currentKey + "_") + toString(height)) + "_staked"), totalStaked), IntegerEntry("total", (totalAmount + totalStaked))] | |
70 | + | func handler (accum,assetId) = { | |
71 | + | let assetIdStr = toBase58String(assetId) | |
72 | + | let currentTokenInterest = calculateCurrentAssetInterest(assetId, assetIdStr) | |
73 | + | (accum ++ [IntegerEntry((("global_lastCheck_" + assetIdStr) + "_earnings"), assetBalance(this, assetId)), IntegerEntry((("global_lastCheck_" + assetIdStr) + "_interest"), currentTokenInterest), IntegerEntry((((addressStr + "_lastCheck_") + assetIdStr) + "_interest"), currentTokenInterest)]) | |
74 | + | } | |
75 | + | ||
76 | + | let li = { | |
77 | + | let $l = earnedAssets | |
78 | + | let $s = size($l) | |
79 | + | let $acc0 = nil | |
80 | + | func $f0_1 ($a,$i) = if (($i >= $s)) | |
81 | + | then $a | |
82 | + | else handler($a, $l[$i]) | |
83 | + | ||
84 | + | func $f0_2 ($a,$i) = if (($i >= $s)) | |
85 | + | then $a | |
86 | + | else throw("List size exceeds 17") | |
87 | + | ||
88 | + | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10), 11), 12), 13), 14), 15), 16), 17) | |
89 | + | } | |
90 | + | (li ++ [IntegerEntry((addressStr + "_puzzleStaked"), (tryGetInteger((addressStr + "_puzzleStaked")) + pmt.amount)), IntegerEntry("global_puzzleStaked", (tryGetInteger("global_puzzleStaked") + pmt.amount))]) | |
27 | 91 | } | |
28 | 92 | } | |
29 | 93 | ||
30 | 94 | ||
31 | 95 | ||
32 | 96 | @Callable(i) | |
33 | - | func withdraw () = throw("test") | |
97 | + | func unstake (puzzleAmount) = { | |
98 | + | let addressStr = toString(i.caller) | |
99 | + | let puzzleAvailable = tryGetInteger((addressStr + "_puzzleStaked")) | |
100 | + | if ((puzzleAmount > puzzleAvailable)) | |
101 | + | then throw("you don't have PUZZLE available") | |
102 | + | else (claimResult(i.caller) ++ [IntegerEntry((addressStr + "_puzzleStaked"), (puzzleAvailable - puzzleAmount)), IntegerEntry("global_puzzleStaked", (tryGetInteger("global_puzzleStaked") - puzzleAmount))]) | |
103 | + | } | |
34 | 104 | ||
35 | 105 | ||
36 | 106 | ||
37 | 107 | @Callable(i) | |
38 | - | func claimReward () = throw("test") | |
39 | - | ||
40 | - | ||
41 | - | ||
42 | - | @Callable(i) | |
43 | - | func del (key) = [DeleteEntry(key)] | |
108 | + | func claim () = claimResult(i.caller) | |
44 | 109 | ||
45 | 110 | ||
46 | 111 | @Verifier(tx) |
Old | New | Differences | |
---|---|---|---|
1 | 1 | {-# STDLIB_VERSION 5 #-} | |
2 | 2 | {-# SCRIPT_TYPE ACCOUNT #-} | |
3 | 3 | {-# CONTENT_TYPE DAPP #-} | |
4 | - | let | |
4 | + | let DISCLAIMER = "testing PUZZLE staking smart contract with rewards in multiple assets" | |
5 | 5 | ||
6 | - | let | |
6 | + | let scale = 100000000 | |
7 | 7 | ||
8 | - | let REWARD_AMOUNT = 100000000000 | |
8 | + | let puzzleId = base58'43W4FcqA1rEpSmUGHoGiXvpSLfhadws9LS5j3SJsKxxS' | |
9 | + | ||
10 | + | let earnedAssets = [base58'CgidgEMbCKXXErgJXVyZCFRaK8SBNA73Jg8mNXALtVKh'] | |
11 | + | ||
12 | + | let earnedAssetsAmount = size(earnedAssets) | |
13 | + | ||
14 | + | func tryGetInteger (key) = match getInteger(this, key) { | |
15 | + | case b: Int => | |
16 | + | b | |
17 | + | case _ => | |
18 | + | 0 | |
19 | + | } | |
20 | + | ||
21 | + | ||
22 | + | func calculateCurrentAssetInterest (assetId,assetIdStr) = { | |
23 | + | let totalStaked = tryGetInteger("global_puzzleStaked") | |
24 | + | let tokenBalanceLastCheck = tryGetInteger((("global_lastCheck_" + assetIdStr) + "_earnings")) | |
25 | + | let currentTokenEarnings = max([tokenBalanceLastCheck, assetBalance(this, assetId)]) | |
26 | + | let newEarnings = (currentTokenEarnings - tokenBalanceLastCheck) | |
27 | + | let newInterest = if ((totalStaked == 0)) | |
28 | + | then 0 | |
29 | + | else fraction(newEarnings, scale, totalStaked) | |
30 | + | let lastCheckInterest = tryGetInteger((("global_lastCheck_" + assetIdStr) + "_interest")) | |
31 | + | (lastCheckInterest + newInterest) | |
32 | + | } | |
33 | + | ||
34 | + | ||
35 | + | func claimResult (address) = { | |
36 | + | let addressStr = toString(address) | |
37 | + | let puzzleAmount = tryGetInteger((addressStr + "_puzzleStaked")) | |
38 | + | func handler (accum,assetId) = { | |
39 | + | let assetIdStr = toBase58String(assetId) | |
40 | + | let currentTokenInterest = calculateCurrentAssetInterest(assetId, assetIdStr) | |
41 | + | let currentTokenEarnings = max([tryGetInteger((("global_lastCheck_" + assetIdStr) + "_earnings")), assetBalance(this, assetId)]) | |
42 | + | let rewardAmount = fraction(puzzleAmount, (currentTokenInterest - tryGetInteger((((addressStr + "_lastCheck_") + assetIdStr) + "_interest"))), scale) | |
43 | + | if ((rewardAmount == 0)) | |
44 | + | then accum | |
45 | + | else (accum ++ [IntegerEntry((("global_lastCheck_" + assetIdStr) + "_earnings"), (currentTokenEarnings - rewardAmount)), IntegerEntry((("global_lastCheck_" + assetIdStr) + "_interest"), currentTokenInterest), IntegerEntry((((addressStr + "_lastCheck_") + assetIdStr) + "_interest"), currentTokenInterest), ScriptTransfer(address, rewardAmount, assetId)]) | |
46 | + | } | |
47 | + | ||
48 | + | let $l = earnedAssets | |
49 | + | let $s = size($l) | |
50 | + | let $acc0 = nil | |
51 | + | func $f0_1 ($a,$i) = if (($i >= $s)) | |
52 | + | then $a | |
53 | + | else handler($a, $l[$i]) | |
54 | + | ||
55 | + | func $f0_2 ($a,$i) = if (($i >= $s)) | |
56 | + | then $a | |
57 | + | else throw("List size exceeds 17") | |
58 | + | ||
59 | + | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10), 11), 12), 13), 14), 15), 16), 17) | |
60 | + | } | |
61 | + | ||
9 | 62 | ||
10 | 63 | @Callable(i) | |
11 | 64 | func stake () = { | |
12 | - | let $t0297372 = $Tuple2(i.payments[0].amount, i.payments[0].assetId) | |
13 | - | let pmtAmount = $t0297372._1 | |
14 | - | let pmtAssetId = $t0297372._2 | |
15 | - | if (if (if ((size(i.payments) == 0)) | |
16 | - | then (pmtAssetId != STAKE_ASSET) | |
17 | - | else false) | |
18 | - | then (0 >= pmtAmount) | |
19 | - | else false) | |
20 | - | then throw("send token to stake") | |
65 | + | let addressStr = toString(i.caller) | |
66 | + | let pmt = i.payments[0] | |
67 | + | if ((value(pmt.assetId) != puzzleId)) | |
68 | + | then throw("wrong asset attached") | |
21 | 69 | else { | |
22 | - | let currentKey = toBase58String(i.caller.bytes) | |
23 | - | let getStakedAmount = valueOrElse(getInteger(this, (currentKey + "_staked")), 0) | |
24 | - | let totalAmount = valueOrElse(getInteger(this, "total"), 0) | |
25 | - | let totalStaked = (getStakedAmount + pmtAmount) | |
26 | - | [IntegerEntry((((currentKey + "_") + toString(height)) + "_staked"), totalStaked), IntegerEntry("total", (totalAmount + totalStaked))] | |
70 | + | func handler (accum,assetId) = { | |
71 | + | let assetIdStr = toBase58String(assetId) | |
72 | + | let currentTokenInterest = calculateCurrentAssetInterest(assetId, assetIdStr) | |
73 | + | (accum ++ [IntegerEntry((("global_lastCheck_" + assetIdStr) + "_earnings"), assetBalance(this, assetId)), IntegerEntry((("global_lastCheck_" + assetIdStr) + "_interest"), currentTokenInterest), IntegerEntry((((addressStr + "_lastCheck_") + assetIdStr) + "_interest"), currentTokenInterest)]) | |
74 | + | } | |
75 | + | ||
76 | + | let li = { | |
77 | + | let $l = earnedAssets | |
78 | + | let $s = size($l) | |
79 | + | let $acc0 = nil | |
80 | + | func $f0_1 ($a,$i) = if (($i >= $s)) | |
81 | + | then $a | |
82 | + | else handler($a, $l[$i]) | |
83 | + | ||
84 | + | func $f0_2 ($a,$i) = if (($i >= $s)) | |
85 | + | then $a | |
86 | + | else throw("List size exceeds 17") | |
87 | + | ||
88 | + | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6), 7), 8), 9), 10), 11), 12), 13), 14), 15), 16), 17) | |
89 | + | } | |
90 | + | (li ++ [IntegerEntry((addressStr + "_puzzleStaked"), (tryGetInteger((addressStr + "_puzzleStaked")) + pmt.amount)), IntegerEntry("global_puzzleStaked", (tryGetInteger("global_puzzleStaked") + pmt.amount))]) | |
27 | 91 | } | |
28 | 92 | } | |
29 | 93 | ||
30 | 94 | ||
31 | 95 | ||
32 | 96 | @Callable(i) | |
33 | - | func withdraw () = throw("test") | |
97 | + | func unstake (puzzleAmount) = { | |
98 | + | let addressStr = toString(i.caller) | |
99 | + | let puzzleAvailable = tryGetInteger((addressStr + "_puzzleStaked")) | |
100 | + | if ((puzzleAmount > puzzleAvailable)) | |
101 | + | then throw("you don't have PUZZLE available") | |
102 | + | else (claimResult(i.caller) ++ [IntegerEntry((addressStr + "_puzzleStaked"), (puzzleAvailable - puzzleAmount)), IntegerEntry("global_puzzleStaked", (tryGetInteger("global_puzzleStaked") - puzzleAmount))]) | |
103 | + | } | |
34 | 104 | ||
35 | 105 | ||
36 | 106 | ||
37 | 107 | @Callable(i) | |
38 | - | func claimReward () = throw("test") | |
39 | - | ||
40 | - | ||
41 | - | ||
42 | - | @Callable(i) | |
43 | - | func del (key) = [DeleteEntry(key)] | |
108 | + | func claim () = claimResult(i.caller) | |
44 | 109 | ||
45 | 110 | ||
46 | 111 | @Verifier(tx) | |
47 | 112 | func verify () = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey) | |
48 | 113 |
github/deemru/w8io/169f3d6 34.64 ms ◑