tx · 6SXoeWqQf8bx81nzHafoynzZ9VGPorntBDAZ5HHNqUSq 3N2j6yXv5aLx1DTVGpwUmJHKZb5XwMfWZhk: -1.00500000 Waves 2022.07.14 10:47 [2139111] smart account 3N2j6yXv5aLx1DTVGpwUmJHKZb5XwMfWZhk > SELF 0.00000000 Waves
{ "type": 13, "id": "6SXoeWqQf8bx81nzHafoynzZ9VGPorntBDAZ5HHNqUSq", "fee": 100500000, "feeAssetId": null, "timestamp": 1657784930833, "version": 2, "chainId": 84, "sender": "3N2j6yXv5aLx1DTVGpwUmJHKZb5XwMfWZhk", "senderPublicKey": "8ZzMCsQ15BKiHk944146A5WQ8eSFd8vaff4xaktCKSBK", "proofs": [ "Yix9sAH7JJRiTnusxT2KifnW2WDzAac5G6LkJc2mYnCtXQtZVMoAiLRzTVuK2cyruPNR8ofj2AB3tDoB2DvTBm7" ], "script": "base64:BgIRCAISAwoBCBIAEgASBAoCCAgRAAlzZXBhcmF0b3ICAV8BDWdldFBhaXJMZW5ndGgACQELdmFsdWVPckVsc2UCCQCaCAIFBHRoaXMCCnBhaXJMZW5ndGgAAAENc2V0UGFpckxlbmdodAEJbmV3TGVuZ3RoCQEMSW50ZWdlckVudHJ5AgIKcGFpckxlbmd0aAUJbmV3TGVuZ3RoAQpnZXRQYWlyS2V5AgZ0b2tlbjAGdG9rZW4xBAh0b2tlbjBCVgkAsQkBCQDZBAEFBnRva2VuMAQIdG9rZW4xQlYJALEJAQkA2QQBBQZ0b2tlbjEDCQBmAgUIdG9rZW4xQlYFCHRva2VuMEJWCQC5CQIJAMwIAgUGdG9rZW4wCQDMCAIFBnRva2VuMQUDbmlsBQlzZXBhcmF0b3IJALkJAgkAzAgCBQZ0b2tlbjEJAMwIAgUGdG9rZW4wBQNuaWwFCXNlcGFyYXRvcgEOZ2V0UGFpclRva2VuSWQCBnRva2VuMAZ0b2tlbjEJARFAZXh0ck5hdGl2ZSgxMDUzKQIFBHRoaXMJAQpnZXRQYWlyS2V5AgUGdG9r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JSZXNlcnZlMQUHYW1vdW50MQUDbmlsAWkBEGFkZExpcXVpZGl0eVBvb2wCBnRva2VuMAZ0b2tlbjEDCQEJaXNOZXdQYWlyAgUGdG9rZW4wBQZ0b2tlbjEDCQECIT0CBQZ0b2tlbjAFBnRva2VuMQMDCQENaXNUb2tlbkV4aXN0cwEFBnRva2VuMAkBDWlzVG9rZW5FeGlzdHMBBQZ0b2tlbjEHBAhuZXdUb2tlbgkAwggFCQDYBAEJAMkBAggFAWkNdHJhbnNhY3Rpb25JZAAKAgAAAAAABgQHaXNzdWVJZAkA2AQBCQC4CAEFCG5ld1Rva2VuBBBjdXJyZW50UGFpckluZGV4CQENZ2V0UGFpckxlbmd0aAAJAMwIAgUIbmV3VG9rZW4JAMwIAgkBC1N0cmluZ0VudHJ5AgkApAMBBRBjdXJyZW50UGFpckluZGV4BQdpc3N1ZUlkCQDMCAIJAQ1zZXRQYWlyTGVuZ2h0AQkAZAIFEGN1cnJlbnRQYWlySW5kZXgAAQkAzAgCCQEOc2V0UGFpclRva2VuSWQDBQZ0b2tlbjAFBnRva2VuMQUHaXNzdWVJZAkAzAgCCQEOc2V0UGFpckRldGFpbHMFBQdpc3N1ZUlkBQZ0b2tlbjAFBnRva2VuMQAAAAAFA25pbAkAAgECDFdyb25nIHRva2VucwkAAgECDEVxdWFsIHRva2VucwkAAgECE1BhaXIgYWxyZWFkeSBleGlzdHMAv+pZLQ==", "height": 2139111, "applicationStatus": "succeeded", "spentComplexity": 0 } View: original | compacted Prev: GYWtBGQttGYZQtM4hE6jPmscwjcChrrp5SbKeXEytTzY Next: 3T2LbSdMzfMKBjUfxnNod9JeCwsCS5VG2WknH2jnbs7v Diff:
Old | New | Differences | |
---|---|---|---|
45 | 45 | func isNewPair (token0,token1) = !(isDefined(getString(getPairKey(token0, token1)))) | |
46 | 46 | ||
47 | 47 | ||
48 | - | func isTokenExists (tokenId) = true | |
48 | + | func isTokenExists (tokenId) = match assetInfo(fromBase58String(tokenId)) { | |
49 | + | case a: Asset => | |
50 | + | true | |
51 | + | case _ => | |
52 | + | throw("Bad situation") | |
53 | + | } | |
49 | 54 | ||
50 | 55 | ||
51 | - | let maxx = | |
56 | + | let maxx = 100000000 | |
52 | 57 | ||
53 | 58 | let zeroAddr = value(addressFromString("3M4qwDomRabJKLZxuXhwfqLApQkU592nWxF")) | |
54 | 59 | ||
55 | 60 | let MINIMUM_LIQUIDITY = 100000 | |
61 | + | ||
62 | + | func calcAmountOut (amountIn,reserveIn,reserveOut) = { | |
63 | + | let amountInWithFee = (amountIn * 997) | |
64 | + | let numerator = (amountInWithFee * reserveOut) | |
65 | + | let denominator = ((reserveIn * 1000) + amountInWithFee) | |
66 | + | (numerator / denominator) | |
67 | + | } | |
68 | + | ||
69 | + | ||
70 | + | @Callable(i) | |
71 | + | func swap (tokenOut) = { | |
72 | + | let amountIn = value(i.payments[0].amount) | |
73 | + | let tokenIn = toBase58String(value(i.payments[0].assetId)) | |
74 | + | let lpToken = getPairTokenId(tokenIn, tokenOut) | |
75 | + | let $t026392721 = getPairDetails(lpToken) | |
76 | + | let pairToken0 = $t026392721._1 | |
77 | + | let pairToken1 = $t026392721._2 | |
78 | + | let pairReserve0 = $t026392721._3 | |
79 | + | let pairReserve1 = $t026392721._4 | |
80 | + | let amountOut = calcAmountOut(amountIn, pairReserve0, pairReserve1) | |
81 | + | [ScriptTransfer(i.caller, amountOut, fromBase58String(pairToken1)), setPairDetails(lpToken, tokenIn, tokenOut, (pairReserve0 + amountIn), (pairReserve1 - amountOut))] | |
82 | + | } | |
83 | + | ||
84 | + | ||
56 | 85 | ||
57 | 86 | @Callable(i) | |
58 | 87 | func mint () = { | |
61 | 90 | let lpToken = getPairTokenId(token0, token1) | |
62 | 91 | let amount0 = value(i.payments[0].amount) | |
63 | 92 | let amount1 = value(i.payments[1].amount) | |
64 | - | let $ | |
65 | - | let pairToken0 = $ | |
66 | - | let pairToken1 = $ | |
67 | - | let pairReserve0 = $ | |
68 | - | let pairReserve1 = $ | |
93 | + | let $t032693351 = getPairDetails(lpToken) | |
94 | + | let pairToken0 = $t032693351._1 | |
95 | + | let pairToken1 = $t032693351._2 | |
96 | + | let pairReserve0 = $t032693351._3 | |
97 | + | let pairReserve1 = $t032693351._4 | |
69 | 98 | let totalSupply = getPairTotalSupply(lpToken) | |
70 | 99 | if ((totalSupply == 0)) | |
71 | 100 | then { | |
74 | 103 | } | |
75 | 104 | else { | |
76 | 105 | let liquidity = min([((amount0 * totalSupply) / pairReserve0), ((amount1 * totalSupply) / pairReserve1)]) | |
77 | - | [Reissue(fromBase58String(lpToken), liquidity, true), ScriptTransfer(i.caller, liquidity, fromBase58String(lpToken)), setPairTotalSupply(lpToken, liquidity), setPairDetails(lpToken, token0, token1, (pairReserve0 + amount0), (pairReserve1 + amount1))] | |
106 | + | [Reissue(fromBase58String(lpToken), liquidity, true), ScriptTransfer(i.caller, liquidity, fromBase58String(lpToken)), setPairTotalSupply(lpToken, (totalSupply + liquidity)), setPairDetails(lpToken, token0, token1, (pairReserve0 + amount0), (pairReserve1 + amount1))] | |
78 | 107 | } | |
79 | 108 | } | |
80 | 109 | ||
84 | 113 | func burn () = { | |
85 | 114 | let amountlp = value(i.payments[0].amount) | |
86 | 115 | let assetIdLp = value(i.payments[0].assetId) | |
87 | - | let $ | |
88 | - | let pairToken0 = $ | |
89 | - | let pairToken1 = $ | |
90 | - | let pairReserve0 = $ | |
91 | - | let pairReserve1 = $ | |
116 | + | let $t044584558 = getPairDetails(toBase58String(assetIdLp)) | |
117 | + | let pairToken0 = $t044584558._1 | |
118 | + | let pairToken1 = $t044584558._2 | |
119 | + | let pairReserve0 = $t044584558._3 | |
120 | + | let pairReserve1 = $t044584558._4 | |
92 | 121 | let lpToken = getPairTokenId(pairToken0, pairToken1) | |
93 | 122 | let totalSupply = getPairTotalSupply(lpToken) | |
94 | 123 | let share = ((amountlp * maxx) / totalSupply) |
Old | New | Differences | |
---|---|---|---|
1 | 1 | {-# STDLIB_VERSION 6 #-} | |
2 | 2 | {-# SCRIPT_TYPE ACCOUNT #-} | |
3 | 3 | {-# CONTENT_TYPE DAPP #-} | |
4 | 4 | let separator = "_" | |
5 | 5 | ||
6 | 6 | func getPairLength () = valueOrElse(getInteger(this, "pairLength"), 0) | |
7 | 7 | ||
8 | 8 | ||
9 | 9 | func setPairLenght (newLength) = IntegerEntry("pairLength", newLength) | |
10 | 10 | ||
11 | 11 | ||
12 | 12 | func getPairKey (token0,token1) = { | |
13 | 13 | let token0BV = toInt(fromBase58String(token0)) | |
14 | 14 | let token1BV = toInt(fromBase58String(token1)) | |
15 | 15 | if ((token1BV > token0BV)) | |
16 | 16 | then makeString([token0, token1], separator) | |
17 | 17 | else makeString([token1, token0], separator) | |
18 | 18 | } | |
19 | 19 | ||
20 | 20 | ||
21 | 21 | func getPairTokenId (token0,token1) = getStringValue(this, getPairKey(token0, token1)) | |
22 | 22 | ||
23 | 23 | ||
24 | 24 | func setPairTokenId (token0,token1,issueId) = StringEntry(getPairKey(token0, token1), issueId) | |
25 | 25 | ||
26 | 26 | ||
27 | 27 | func joinPairStruct (token0,token1,reserve0,reserve1) = makeString([token0, token1, toString(reserve0), toString(reserve1)], separator) | |
28 | 28 | ||
29 | 29 | ||
30 | 30 | func getPairDetails (lpTokenId) = { | |
31 | 31 | let details = split(valueOrElse(getString(this, lpTokenId), "assetId0_assetId1_0_0"), separator) | |
32 | 32 | $Tuple4(details[0], details[1], parseIntValue(details[2]), parseIntValue(details[3])) | |
33 | 33 | } | |
34 | 34 | ||
35 | 35 | ||
36 | 36 | func setPairDetails (lpTokenId,token0,token1,reserve0,reserve1) = StringEntry(lpTokenId, joinPairStruct(token0, token1, reserve0, reserve1)) | |
37 | 37 | ||
38 | 38 | ||
39 | 39 | func getPairTotalSupply (lpTokenId) = valueOrElse(getInteger(this, (("totalSupply" + separator) + lpTokenId)), 0) | |
40 | 40 | ||
41 | 41 | ||
42 | 42 | func setPairTotalSupply (lpTokenId,newTotalSupply) = IntegerEntry((("totalSupply" + separator) + lpTokenId), newTotalSupply) | |
43 | 43 | ||
44 | 44 | ||
45 | 45 | func isNewPair (token0,token1) = !(isDefined(getString(getPairKey(token0, token1)))) | |
46 | 46 | ||
47 | 47 | ||
48 | - | func isTokenExists (tokenId) = true | |
48 | + | func isTokenExists (tokenId) = match assetInfo(fromBase58String(tokenId)) { | |
49 | + | case a: Asset => | |
50 | + | true | |
51 | + | case _ => | |
52 | + | throw("Bad situation") | |
53 | + | } | |
49 | 54 | ||
50 | 55 | ||
51 | - | let maxx = | |
56 | + | let maxx = 100000000 | |
52 | 57 | ||
53 | 58 | let zeroAddr = value(addressFromString("3M4qwDomRabJKLZxuXhwfqLApQkU592nWxF")) | |
54 | 59 | ||
55 | 60 | let MINIMUM_LIQUIDITY = 100000 | |
61 | + | ||
62 | + | func calcAmountOut (amountIn,reserveIn,reserveOut) = { | |
63 | + | let amountInWithFee = (amountIn * 997) | |
64 | + | let numerator = (amountInWithFee * reserveOut) | |
65 | + | let denominator = ((reserveIn * 1000) + amountInWithFee) | |
66 | + | (numerator / denominator) | |
67 | + | } | |
68 | + | ||
69 | + | ||
70 | + | @Callable(i) | |
71 | + | func swap (tokenOut) = { | |
72 | + | let amountIn = value(i.payments[0].amount) | |
73 | + | let tokenIn = toBase58String(value(i.payments[0].assetId)) | |
74 | + | let lpToken = getPairTokenId(tokenIn, tokenOut) | |
75 | + | let $t026392721 = getPairDetails(lpToken) | |
76 | + | let pairToken0 = $t026392721._1 | |
77 | + | let pairToken1 = $t026392721._2 | |
78 | + | let pairReserve0 = $t026392721._3 | |
79 | + | let pairReserve1 = $t026392721._4 | |
80 | + | let amountOut = calcAmountOut(amountIn, pairReserve0, pairReserve1) | |
81 | + | [ScriptTransfer(i.caller, amountOut, fromBase58String(pairToken1)), setPairDetails(lpToken, tokenIn, tokenOut, (pairReserve0 + amountIn), (pairReserve1 - amountOut))] | |
82 | + | } | |
83 | + | ||
84 | + | ||
56 | 85 | ||
57 | 86 | @Callable(i) | |
58 | 87 | func mint () = { | |
59 | 88 | let token0 = toBase58String(value(i.payments[0].assetId)) | |
60 | 89 | let token1 = toBase58String(value(i.payments[1].assetId)) | |
61 | 90 | let lpToken = getPairTokenId(token0, token1) | |
62 | 91 | let amount0 = value(i.payments[0].amount) | |
63 | 92 | let amount1 = value(i.payments[1].amount) | |
64 | - | let $ | |
65 | - | let pairToken0 = $ | |
66 | - | let pairToken1 = $ | |
67 | - | let pairReserve0 = $ | |
68 | - | let pairReserve1 = $ | |
93 | + | let $t032693351 = getPairDetails(lpToken) | |
94 | + | let pairToken0 = $t032693351._1 | |
95 | + | let pairToken1 = $t032693351._2 | |
96 | + | let pairReserve0 = $t032693351._3 | |
97 | + | let pairReserve1 = $t032693351._4 | |
69 | 98 | let totalSupply = getPairTotalSupply(lpToken) | |
70 | 99 | if ((totalSupply == 0)) | |
71 | 100 | then { | |
72 | 101 | let liquidity = (sqrt((amount0 * amount1), 0, 0, DOWN) - MINIMUM_LIQUIDITY) | |
73 | 102 | [Reissue(fromBase58String(lpToken), (liquidity + MINIMUM_LIQUIDITY), true), ScriptTransfer(zeroAddr, MINIMUM_LIQUIDITY, fromBase58String(lpToken)), ScriptTransfer(i.caller, liquidity, fromBase58String(lpToken)), setPairTotalSupply(lpToken, (liquidity + MINIMUM_LIQUIDITY)), setPairDetails(lpToken, token0, token1, (pairReserve0 + amount0), (pairReserve1 + amount1))] | |
74 | 103 | } | |
75 | 104 | else { | |
76 | 105 | let liquidity = min([((amount0 * totalSupply) / pairReserve0), ((amount1 * totalSupply) / pairReserve1)]) | |
77 | - | [Reissue(fromBase58String(lpToken), liquidity, true), ScriptTransfer(i.caller, liquidity, fromBase58String(lpToken)), setPairTotalSupply(lpToken, liquidity), setPairDetails(lpToken, token0, token1, (pairReserve0 + amount0), (pairReserve1 + amount1))] | |
106 | + | [Reissue(fromBase58String(lpToken), liquidity, true), ScriptTransfer(i.caller, liquidity, fromBase58String(lpToken)), setPairTotalSupply(lpToken, (totalSupply + liquidity)), setPairDetails(lpToken, token0, token1, (pairReserve0 + amount0), (pairReserve1 + amount1))] | |
78 | 107 | } | |
79 | 108 | } | |
80 | 109 | ||
81 | 110 | ||
82 | 111 | ||
83 | 112 | @Callable(i) | |
84 | 113 | func burn () = { | |
85 | 114 | let amountlp = value(i.payments[0].amount) | |
86 | 115 | let assetIdLp = value(i.payments[0].assetId) | |
87 | - | let $ | |
88 | - | let pairToken0 = $ | |
89 | - | let pairToken1 = $ | |
90 | - | let pairReserve0 = $ | |
91 | - | let pairReserve1 = $ | |
116 | + | let $t044584558 = getPairDetails(toBase58String(assetIdLp)) | |
117 | + | let pairToken0 = $t044584558._1 | |
118 | + | let pairToken1 = $t044584558._2 | |
119 | + | let pairReserve0 = $t044584558._3 | |
120 | + | let pairReserve1 = $t044584558._4 | |
92 | 121 | let lpToken = getPairTokenId(pairToken0, pairToken1) | |
93 | 122 | let totalSupply = getPairTotalSupply(lpToken) | |
94 | 123 | let share = ((amountlp * maxx) / totalSupply) | |
95 | 124 | let amount0 = ((share * pairReserve0) / maxx) | |
96 | 125 | let amount1 = ((share * pairReserve1) / maxx) | |
97 | 126 | [Burn(fromBase58String(lpToken), amountlp), ScriptTransfer(i.caller, amount0, fromBase58String(pairToken0)), ScriptTransfer(i.caller, amount1, fromBase58String(pairToken1)), setPairTotalSupply(lpToken, (totalSupply - amountlp)), setPairDetails(lpToken, pairToken0, pairToken1, (pairReserve0 - amount0), (pairReserve1 - amount1))] | |
98 | 127 | } | |
99 | 128 | ||
100 | 129 | ||
101 | 130 | ||
102 | 131 | @Callable(i) | |
103 | 132 | func addLiquidityPool (token0,token1) = if (isNewPair(token0, token1)) | |
104 | 133 | then if ((token0 != token1)) | |
105 | 134 | then if (if (isTokenExists(token0)) | |
106 | 135 | then isTokenExists(token1) | |
107 | 136 | else false) | |
108 | 137 | then { | |
109 | 138 | let newToken = Issue(toBase58String(take(i.transactionId, 10)), "", 0, 0, true) | |
110 | 139 | let issueId = toBase58String(calculateAssetId(newToken)) | |
111 | 140 | let currentPairIndex = getPairLength() | |
112 | 141 | [newToken, StringEntry(toString(currentPairIndex), issueId), setPairLenght((currentPairIndex + 1)), setPairTokenId(token0, token1, issueId), setPairDetails(issueId, token0, token1, 0, 0)] | |
113 | 142 | } | |
114 | 143 | else throw("Wrong tokens") | |
115 | 144 | else throw("Equal tokens") | |
116 | 145 | else throw("Pair already exists") | |
117 | 146 | ||
118 | 147 |
github/deemru/w8io/026f985 36.29 ms ◑