tx · EAFn41YHcdH9ZciASRWoGSGcETaejUiRe4v8ArGTEtvb 3MvdzbvEZLHEE3EXHXxaRQjgpNqq28733ao: -0.01000000 Waves 2021.12.24 12:05 [1848144] smart account 3MvdzbvEZLHEE3EXHXxaRQjgpNqq28733ao > SELF 0.00000000 Waves
{ "type": 13, "id": "EAFn41YHcdH9ZciASRWoGSGcETaejUiRe4v8ArGTEtvb", "fee": 1000000, "feeAssetId": null, "timestamp": 1640336773257, "version": 1, "sender": "3MvdzbvEZLHEE3EXHXxaRQjgpNqq28733ao", "senderPublicKey": "HQmnkZjA2Ehwb2iKXAadqRsrXsHGZChU6WfgmHRsMW8t", "proofs": [ "3iaCg3rcmo4vuKfgfbUpx6S7F3owFHWoxKdyAfTaaM3dLuFG2RG9fzihfxors3aA28zqc4KR8LPqdKyJcYqAWwW1" ], "script": "base64:AAIFAAAAAAAAAAoIAhIGCgQICAEBAAAACwEAAAAQZ2V0U3RyaW5nT3JUaHJvdwAAAAIAAAAHYWRkcmVzcwAAAANrZXkJAQAAABN2YWx1ZU9yRXJyb3JNZXNzYWdlAAAAAgkABB0AAAACBQAAAAdhZGRyZXNzBQAAAANrZXkJAAEsAAAAAgkAASwAAAACCQABLAAAAAICAAAADENhbid0IHJlYWQgJwUAAAADa2V5AgAAAA0nIGF0IGFkZHJlc3MgCQAEJQAAAAEFAAAAB2FkZHJlc3MBAAAAE3BhcnNlQWRkcmVzc09yVGhyb3cAAAABAAAADG1heWJlQWRkcmVzcwkBAAAAE3ZhbHVlT3JFcnJvck1lc3NhZ2UAAAACCQAEJgAAAAEFAAAADG1heWJlQWRkcmVzcwkAASwAAAACCQABLAAAAAICAAAAGkNhbid0IHBhcnNlIGFkZHJlc3MgZnJvbSAnBQAAAAxtYXliZUFkZHJlc3MCAAAAAScBAAAAD3BhcnNlSW50T3JUaHJvdwAAAAEAAAAIbWF5YmVJbnQJAQAAABN2YWx1ZU9yRXJyb3JNZXNzYWdlAAAAAgkABLYAAAABBQAAAAhtYX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Ftb3VudFRvUmVjZWl2ZQMDAwkAAAEAAAACCAUAAAAHJG1hdGNoMAAAAAJfMQIAAAADSW50AwkAAAEAAAACCAUAAAAHJG1hdGNoMAAAAAJfMgIAAAADSW50CQAAAQAAAAIIBQAAAAckbWF0Y2gwAAAAAl8zAgAAAANJbnQHBwkAAAEAAAACBQAAAAckbWF0Y2gwAgAAAA8oSW50LCBJbnQsIEludCkHBAAAAAZiZWZvcmUIBQAAAAckbWF0Y2gwAAAAAl8xBAAAAAZpbmNvbWUIBQAAAAckbWF0Y2gwAAAAAl8yBAAAAANmZWUIBQAAAAckbWF0Y2gwAAAAAl8zCQAFFQAAAAMFAAAABmJlZm9yZQUAAAAGaW5jb21lBQAAAANmZWUJAAACAAAAAQIAAAAYQ2FuJ3QgaGFuZGxlIHN3YXAgcmVzdWx0BAAAAA1iYWxhbmNlQmVmb3JlCAUAAAALJHQwMzI2MzM1MjYAAAACXzEEAAAACHJlY2VpdmVkCAUAAAALJHQwMzI2MzM1MjYAAAACXzIEAAAAA2ZlZQgFAAAACyR0MDMyNjMzNTI2AAAAAl8zCQAETAAAAAIJAQAAAA5TY3JpcHRUcmFuc2ZlcgAAAAMIBQAAAANpbnYAAAAGY2FsbGVyCQAAZQAAAAIFAAAACHJlY2VpdmVkBQAAAANmZWUFAAAAD2Fzc2V0UmVjZWl2ZWRJZAkABEwAAAACCQEAAAAOU2NyaXB0VHJhbnNmZXIAAAADBQAAAAljb2xsZWN0b3IFAAAAA2ZlZQUAAAAPYXNzZXRSZWNlaXZlZElkBQAAAANuaWwAAAAA2TkLIg==", "chainId": 84, "height": 1848144, "applicationStatus": "succeeded", "spentComplexity": 0 } View: original | compacted Prev: FoSZnYRKBgsHP6hELgLV7b2NtYV84nbUBa3siPcpkXaS Next: none Diff:
Old | New | Differences | |
---|---|---|---|
1 | 1 | {-# STDLIB_VERSION 5 #-} | |
2 | 2 | {-# SCRIPT_TYPE ACCOUNT #-} | |
3 | 3 | {-# CONTENT_TYPE DAPP #-} | |
4 | - | ||
4 | + | func getStringOrThrow (address,key) = valueOrErrorMessage(getString(address, key), ((("Can't read '" + key) + "' at address ") + toString(address))) | |
5 | 5 | ||
6 | - | let oracle = valueOrErrorMessage(addressFromString(valueOrErrorMessage(getString(this, "oracle"), "")), "") | |
7 | 6 | ||
8 | - | let pools = split(valueOrErrorMessage(getString(oracle, "pools"), ""), ",") | |
7 | + | func parseAddressOrThrow (maybeAddress) = valueOrErrorMessage(addressFromString(maybeAddress), (("Can't parse address from '" + maybeAddress) + "'")) | |
8 | + | ||
9 | + | ||
10 | + | func parseIntOrThrow (maybeInt) = valueOrErrorMessage(parseInt(maybeInt), (("Can't parse integer from '" + maybeInt) + "'")) | |
11 | + | ||
12 | + | ||
13 | + | let collector = parseAddressOrThrow(getStringOrThrow(this, "collector")) | |
14 | + | ||
15 | + | let oracle = parseAddressOrThrow(getStringOrThrow(this, "oracle")) | |
16 | + | ||
17 | + | let pools = split(getStringOrThrow(oracle, "pools"), ",") | |
9 | 18 | ||
10 | 19 | func getAssetId (asset) = if ((asset == "WAVES")) | |
11 | 20 | then unit | |
28 | 37 | func deductFlatFee (amount) = fraction(amount, 1, 10000) | |
29 | 38 | ||
30 | 39 | ||
31 | - | func doSwap (pool,payment,assetReceived, | |
32 | - | let poolAddress = | |
40 | + | func doSwap (pool,payment,assetReceived,minAmountToReceive,estAmountToReceive) = { | |
41 | + | let poolAddress = parseAddressOrThrow(pool) | |
33 | 42 | let assetReceivedId = getAssetId(assetReceived) | |
34 | - | if ((0 >= minReceived)) | |
35 | - | then throw("") | |
36 | - | else if ((0 > estReceived)) | |
37 | - | then throw("") | |
38 | - | else { | |
39 | - | let balanceBefore = getBalance(assetReceivedId) | |
40 | - | if ((balanceBefore == balanceBefore)) | |
41 | - | then if (!(containsElement(pools, pool))) | |
42 | - | then throw("") | |
43 | - | else { | |
44 | - | let poolAssetA = valueOrErrorMessage(getString(poolAddress, "A_asset_id"), "") | |
45 | - | let poolAssetB = valueOrErrorMessage(getString(poolAddress, "B_asset_id"), "") | |
46 | - | let poolAssetIdA = getAssetId(poolAssetA) | |
47 | - | let poolAssetIdB = getAssetId(poolAssetB) | |
48 | - | let versionMajor = valueOrErrorMessage(parseInt(split(valueOrErrorMessage(getString(poolAddress, "version"), ""), ".")[0]), "") | |
49 | - | let args = if ((versionMajor == 1)) | |
50 | - | then [(minReceived + deductFee(minReceived))] | |
51 | - | else if ((versionMajor == 2)) | |
52 | - | then if ((0 >= estReceived)) | |
53 | - | then throw("") | |
54 | - | else [(estReceived + deductFlatFee(estReceived)), (minReceived + deductFee(minReceived))] | |
55 | - | else throw("") | |
43 | + | if ((0 >= minAmountToReceive)) | |
44 | + | then throw("minAmountToReceive must be positive") | |
45 | + | else if ((0 > estAmountToReceive)) | |
46 | + | then throw("estAmountToReceive must be positive") | |
47 | + | else if (!(containsElement(pools, pool))) | |
48 | + | then throw((("There is no pool at address '" + pool) + "'")) | |
49 | + | else { | |
50 | + | let versionMajor = parseIntOrThrow(split(getStringOrThrow(poolAddress, "version"), ".")[0]) | |
51 | + | let args = if ((versionMajor == 1)) | |
52 | + | then [(minAmountToReceive + deductFee(minAmountToReceive))] | |
53 | + | else if ((versionMajor == 2)) | |
54 | + | then if ((0 >= estAmountToReceive)) | |
55 | + | then throw("estAmountToReceive must be positive") | |
56 | + | else [(estAmountToReceive + deductFlatFee(estAmountToReceive)), (minAmountToReceive + deductFee(minAmountToReceive))] | |
57 | + | else throw("") | |
58 | + | let balanceBefore = getBalance(assetReceivedId) | |
59 | + | if ((balanceBefore == balanceBefore)) | |
60 | + | then { | |
56 | 61 | let result = invoke(poolAddress, "exchange", args, [payment]) | |
57 | 62 | if ((result == result)) | |
58 | 63 | then { | |
63 | 68 | then deductFee(received) | |
64 | 69 | else if ((versionMajor == 2)) | |
65 | 70 | then deductFlatFee(received) | |
66 | - | else throw("") | |
71 | + | else throw((("Unknown pool version '" + toString(versionMajor)) + "', but 1 or 2 expected")) | |
67 | 72 | if ((0 >= received)) | |
68 | - | then throw("") | |
73 | + | then throw("Received amount from pool must be positive") | |
69 | 74 | else $Tuple3(balanceBefore, received, fee) | |
70 | 75 | } | |
71 | 76 | else throw("Strict value is not equal to itself.") | |
72 | 77 | } | |
73 | 78 | else throw("Strict value is not equal to itself.") | |
74 | 79 | } | |
75 | - | else throw("Strict value is not equal to itself.") | |
76 | - | } | |
80 | + | else throw("Strict value is not equal to itself.") | |
81 | + | } | |
77 | 82 | } | |
78 | 83 | ||
79 | 84 | ||
80 | 85 | @Callable(inv) | |
81 | - | func swap (pool,assetToReceive, | |
86 | + | func swap (pool,assetToReceive,minAmountToReceive,estAmountToReceive) = { | |
82 | 87 | let pmt = if ((size(inv.payments) > 0)) | |
83 | 88 | then inv.payments[0] | |
84 | - | else throw("") | |
89 | + | else throw("Payment required") | |
85 | 90 | let assetReceivedId = getAssetId(assetToReceive) | |
86 | - | if ((0 >= | |
87 | - | then throw("") | |
91 | + | if ((0 >= minAmountToReceive)) | |
92 | + | then throw("minAmountToReceive must be positive") | |
88 | 93 | else { | |
89 | - | let $ | |
94 | + | let $t032633526 = match doSwap(pool, pmt, assetToReceive, minAmountToReceive, estAmountToReceive) { | |
90 | 95 | case _ => | |
91 | 96 | if (if (if ($isInstanceOf($match0._1, "Int")) | |
92 | 97 | then if ($isInstanceOf($match0._2, "Int")) | |
101 | 106 | let fee = $match0._3 | |
102 | 107 | $Tuple3(before, income, fee) | |
103 | 108 | } | |
104 | - | else throw("") | |
109 | + | else throw("Can't handle swap result") | |
105 | 110 | } | |
106 | - | let balanceBefore = $ | |
107 | - | let received = $ | |
108 | - | let fee = $ | |
111 | + | let balanceBefore = $t032633526._1 | |
112 | + | let received = $t032633526._2 | |
113 | + | let fee = $t032633526._3 | |
109 | 114 | [ScriptTransfer(inv.caller, (received - fee), assetReceivedId), ScriptTransfer(collector, fee, assetReceivedId)] | |
110 | 115 | } | |
111 | 116 | } |
Old | New | Differences | |
---|---|---|---|
1 | 1 | {-# STDLIB_VERSION 5 #-} | |
2 | 2 | {-# SCRIPT_TYPE ACCOUNT #-} | |
3 | 3 | {-# CONTENT_TYPE DAPP #-} | |
4 | - | ||
4 | + | func getStringOrThrow (address,key) = valueOrErrorMessage(getString(address, key), ((("Can't read '" + key) + "' at address ") + toString(address))) | |
5 | 5 | ||
6 | - | let oracle = valueOrErrorMessage(addressFromString(valueOrErrorMessage(getString(this, "oracle"), "")), "") | |
7 | 6 | ||
8 | - | let pools = split(valueOrErrorMessage(getString(oracle, "pools"), ""), ",") | |
7 | + | func parseAddressOrThrow (maybeAddress) = valueOrErrorMessage(addressFromString(maybeAddress), (("Can't parse address from '" + maybeAddress) + "'")) | |
8 | + | ||
9 | + | ||
10 | + | func parseIntOrThrow (maybeInt) = valueOrErrorMessage(parseInt(maybeInt), (("Can't parse integer from '" + maybeInt) + "'")) | |
11 | + | ||
12 | + | ||
13 | + | let collector = parseAddressOrThrow(getStringOrThrow(this, "collector")) | |
14 | + | ||
15 | + | let oracle = parseAddressOrThrow(getStringOrThrow(this, "oracle")) | |
16 | + | ||
17 | + | let pools = split(getStringOrThrow(oracle, "pools"), ",") | |
9 | 18 | ||
10 | 19 | func getAssetId (asset) = if ((asset == "WAVES")) | |
11 | 20 | then unit | |
12 | 21 | else fromBase58String(asset) | |
13 | 22 | ||
14 | 23 | ||
15 | 24 | func getBalance (assetId) = match assetId { | |
16 | 25 | case asset: ByteVector => | |
17 | 26 | assetBalance(this, asset) | |
18 | 27 | case waves: Unit => | |
19 | 28 | wavesBalance(this).available | |
20 | 29 | case _ => | |
21 | 30 | throw("Match error") | |
22 | 31 | } | |
23 | 32 | ||
24 | 33 | ||
25 | 34 | func deductFee (amount) = fraction(amount, 1, 1000) | |
26 | 35 | ||
27 | 36 | ||
28 | 37 | func deductFlatFee (amount) = fraction(amount, 1, 10000) | |
29 | 38 | ||
30 | 39 | ||
31 | - | func doSwap (pool,payment,assetReceived, | |
32 | - | let poolAddress = | |
40 | + | func doSwap (pool,payment,assetReceived,minAmountToReceive,estAmountToReceive) = { | |
41 | + | let poolAddress = parseAddressOrThrow(pool) | |
33 | 42 | let assetReceivedId = getAssetId(assetReceived) | |
34 | - | if ((0 >= minReceived)) | |
35 | - | then throw("") | |
36 | - | else if ((0 > estReceived)) | |
37 | - | then throw("") | |
38 | - | else { | |
39 | - | let balanceBefore = getBalance(assetReceivedId) | |
40 | - | if ((balanceBefore == balanceBefore)) | |
41 | - | then if (!(containsElement(pools, pool))) | |
42 | - | then throw("") | |
43 | - | else { | |
44 | - | let poolAssetA = valueOrErrorMessage(getString(poolAddress, "A_asset_id"), "") | |
45 | - | let poolAssetB = valueOrErrorMessage(getString(poolAddress, "B_asset_id"), "") | |
46 | - | let poolAssetIdA = getAssetId(poolAssetA) | |
47 | - | let poolAssetIdB = getAssetId(poolAssetB) | |
48 | - | let versionMajor = valueOrErrorMessage(parseInt(split(valueOrErrorMessage(getString(poolAddress, "version"), ""), ".")[0]), "") | |
49 | - | let args = if ((versionMajor == 1)) | |
50 | - | then [(minReceived + deductFee(minReceived))] | |
51 | - | else if ((versionMajor == 2)) | |
52 | - | then if ((0 >= estReceived)) | |
53 | - | then throw("") | |
54 | - | else [(estReceived + deductFlatFee(estReceived)), (minReceived + deductFee(minReceived))] | |
55 | - | else throw("") | |
43 | + | if ((0 >= minAmountToReceive)) | |
44 | + | then throw("minAmountToReceive must be positive") | |
45 | + | else if ((0 > estAmountToReceive)) | |
46 | + | then throw("estAmountToReceive must be positive") | |
47 | + | else if (!(containsElement(pools, pool))) | |
48 | + | then throw((("There is no pool at address '" + pool) + "'")) | |
49 | + | else { | |
50 | + | let versionMajor = parseIntOrThrow(split(getStringOrThrow(poolAddress, "version"), ".")[0]) | |
51 | + | let args = if ((versionMajor == 1)) | |
52 | + | then [(minAmountToReceive + deductFee(minAmountToReceive))] | |
53 | + | else if ((versionMajor == 2)) | |
54 | + | then if ((0 >= estAmountToReceive)) | |
55 | + | then throw("estAmountToReceive must be positive") | |
56 | + | else [(estAmountToReceive + deductFlatFee(estAmountToReceive)), (minAmountToReceive + deductFee(minAmountToReceive))] | |
57 | + | else throw("") | |
58 | + | let balanceBefore = getBalance(assetReceivedId) | |
59 | + | if ((balanceBefore == balanceBefore)) | |
60 | + | then { | |
56 | 61 | let result = invoke(poolAddress, "exchange", args, [payment]) | |
57 | 62 | if ((result == result)) | |
58 | 63 | then { | |
59 | 64 | let received = (getBalance(assetReceivedId) - balanceBefore) | |
60 | 65 | if ((received == received)) | |
61 | 66 | then { | |
62 | 67 | let fee = if ((versionMajor == 1)) | |
63 | 68 | then deductFee(received) | |
64 | 69 | else if ((versionMajor == 2)) | |
65 | 70 | then deductFlatFee(received) | |
66 | - | else throw("") | |
71 | + | else throw((("Unknown pool version '" + toString(versionMajor)) + "', but 1 or 2 expected")) | |
67 | 72 | if ((0 >= received)) | |
68 | - | then throw("") | |
73 | + | then throw("Received amount from pool must be positive") | |
69 | 74 | else $Tuple3(balanceBefore, received, fee) | |
70 | 75 | } | |
71 | 76 | else throw("Strict value is not equal to itself.") | |
72 | 77 | } | |
73 | 78 | else throw("Strict value is not equal to itself.") | |
74 | 79 | } | |
75 | - | else throw("Strict value is not equal to itself.") | |
76 | - | } | |
80 | + | else throw("Strict value is not equal to itself.") | |
81 | + | } | |
77 | 82 | } | |
78 | 83 | ||
79 | 84 | ||
80 | 85 | @Callable(inv) | |
81 | - | func swap (pool,assetToReceive, | |
86 | + | func swap (pool,assetToReceive,minAmountToReceive,estAmountToReceive) = { | |
82 | 87 | let pmt = if ((size(inv.payments) > 0)) | |
83 | 88 | then inv.payments[0] | |
84 | - | else throw("") | |
89 | + | else throw("Payment required") | |
85 | 90 | let assetReceivedId = getAssetId(assetToReceive) | |
86 | - | if ((0 >= | |
87 | - | then throw("") | |
91 | + | if ((0 >= minAmountToReceive)) | |
92 | + | then throw("minAmountToReceive must be positive") | |
88 | 93 | else { | |
89 | - | let $ | |
94 | + | let $t032633526 = match doSwap(pool, pmt, assetToReceive, minAmountToReceive, estAmountToReceive) { | |
90 | 95 | case _ => | |
91 | 96 | if (if (if ($isInstanceOf($match0._1, "Int")) | |
92 | 97 | then if ($isInstanceOf($match0._2, "Int")) | |
93 | 98 | then $isInstanceOf($match0._3, "Int") | |
94 | 99 | else false | |
95 | 100 | else false) | |
96 | 101 | then $isInstanceOf($match0, "(Int, Int, Int)") | |
97 | 102 | else false) | |
98 | 103 | then { | |
99 | 104 | let before = $match0._1 | |
100 | 105 | let income = $match0._2 | |
101 | 106 | let fee = $match0._3 | |
102 | 107 | $Tuple3(before, income, fee) | |
103 | 108 | } | |
104 | - | else throw("") | |
109 | + | else throw("Can't handle swap result") | |
105 | 110 | } | |
106 | - | let balanceBefore = $ | |
107 | - | let received = $ | |
108 | - | let fee = $ | |
111 | + | let balanceBefore = $t032633526._1 | |
112 | + | let received = $t032633526._2 | |
113 | + | let fee = $t032633526._3 | |
109 | 114 | [ScriptTransfer(inv.caller, (received - fee), assetReceivedId), ScriptTransfer(collector, fee, assetReceivedId)] | |
110 | 115 | } | |
111 | 116 | } | |
112 | 117 | ||
113 | 118 |
github/deemru/w8io/169f3d6 52.40 ms ◑