1 | | - | {-# STDLIB_VERSION 6 #-} |
---|
2 | | - | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
3 | | - | {-# CONTENT_TYPE DAPP #-} |
---|
4 | | - | let SEP = "__" |
---|
5 | | - | |
---|
6 | | - | let scale8 = 100000000 |
---|
7 | | - | |
---|
8 | | - | let poolStatsPriceKey = 4 |
---|
9 | | - | |
---|
10 | | - | let idxPoolAddress = 1 |
---|
11 | | - | |
---|
12 | | - | let idxPoolStatus = 2 |
---|
13 | | - | |
---|
14 | | - | let idxPoolLPAssetId = 3 |
---|
15 | | - | |
---|
16 | | - | let idxAmtAssetId = 4 |
---|
17 | | - | |
---|
18 | | - | let idxPriceAssetId = 5 |
---|
19 | | - | |
---|
20 | | - | let idxAmtAssetDcm = 6 |
---|
21 | | - | |
---|
22 | | - | let idxPriceAssetDcm = 7 |
---|
23 | | - | |
---|
24 | | - | let idxIAmtAssetId = 8 |
---|
25 | | - | |
---|
26 | | - | let idxIPriceAssetId = 9 |
---|
27 | | - | |
---|
28 | | - | let idxLPAssetDcm = 10 |
---|
29 | | - | |
---|
30 | | - | let idxMatcherPublicKey = 11 |
---|
31 | | - | |
---|
32 | | - | func keyRegisterPut (poolAddress,txId) = ((("%s%s%s__P__" + poolAddress) + "__") + txId) |
---|
33 | | - | |
---|
34 | | - | |
---|
35 | | - | func keyRegisterInvest (poolAddress,txId) = ((("%s%s%s__invest__" + poolAddress) + "__") + txId) |
---|
36 | | - | |
---|
37 | | - | |
---|
38 | | - | func keyPoolTotalAmtByAsset (poolAddress,assetId) = ((("%s%s%s__total__" + poolAddress) + "__") + assetId) |
---|
39 | | - | |
---|
40 | | - | |
---|
41 | | - | func keyFactoryContract () = "%s__factoryContract" |
---|
42 | | - | |
---|
43 | | - | |
---|
44 | | - | func keySlippageAmtPerAssetCumulative (poolAddress,assetId) = (((("%s%s%s__" + poolAddress) + "__") + assetId) + "__slippageCumulative") |
---|
45 | | - | |
---|
46 | | - | |
---|
47 | | - | func keyFactoryConfig () = "%s__factoryConfig" |
---|
48 | | - | |
---|
49 | | - | |
---|
50 | | - | func keyMappingPoolContractAddressToPoolAssets (poolContractAddress) = (("%s%s%s__" + poolContractAddress) + "__mappings__poolContract2PoolAssets") |
---|
51 | | - | |
---|
52 | | - | |
---|
53 | | - | func keyMappingsInternal2baseAssetId (internalBaseAsset) = ("%s%s%d__mappings__internal2baseAssetId__" + toString(internalBaseAsset)) |
---|
54 | | - | |
---|
55 | | - | |
---|
56 | | - | func keyPoolConfig (iAmtAsset,iPriceAsset) = (((("%d%d%s__" + iAmtAsset) + "__") + iPriceAsset) + "__config") |
---|
57 | | - | |
---|
58 | | - | |
---|
59 | | - | func keyAllPoolsShutdown () = "%s__shutdown" |
---|
60 | | - | |
---|
61 | | - | |
---|
62 | | - | func keyPoolWeight (contractAddress) = ("%s%s__poolWeight__" + contractAddress) |
---|
63 | | - | |
---|
64 | | - | |
---|
65 | | - | func keyManagerPublicKey () = "%s__managerPublicKey" |
---|
66 | | - | |
---|
67 | | - | |
---|
68 | | - | func keyPendingManagerPublicKey () = "%s__pendingManagerPublicKey" |
---|
69 | | - | |
---|
70 | | - | |
---|
71 | | - | let factoryContract = addressFromStringValue(valueOrErrorMessage(getString(this, keyFactoryContract()), "No Factory Acc found.")) |
---|
72 | | - | |
---|
73 | | - | func convertAssetId (assetId) = if (!(isDefined(assetId))) |
---|
74 | | - | then "WAVES" |
---|
75 | | - | else assetId |
---|
76 | | - | |
---|
77 | | - | |
---|
78 | | - | func dataPutActionInfo (inAssetId,inAssetAmt,txHeight,txTimestamp) = makeString(["%s%d%d%d", inAssetId, toString(inAssetAmt), toString(txHeight), toString(txTimestamp)], SEP) |
---|
79 | | - | |
---|
80 | | - | |
---|
81 | | - | func dataInvestActionInfo (outAmountAssetAmt,outPriceAssetAmt,txHeight,txTimestamp) = makeString(["%d%d%d%d", toString(outAmountAssetAmt), toString(outPriceAssetAmt), toString(txHeight), toString(txTimestamp)], SEP) |
---|
82 | | - | |
---|
83 | | - | |
---|
84 | | - | func asString (val) = match val { |
---|
85 | | - | case valStr: String => |
---|
86 | | - | valStr |
---|
87 | | - | case _ => |
---|
88 | | - | throw("fail to cast into String") |
---|
89 | | - | } |
---|
90 | | - | |
---|
91 | | - | |
---|
92 | | - | func toScale (amt,resScale,curScale) = fraction(amt, resScale, curScale) |
---|
93 | | - | |
---|
94 | | - | |
---|
95 | | - | func managerPublicKeyOrUnit () = match getString(keyManagerPublicKey()) { |
---|
96 | | - | case s: String => |
---|
97 | | - | fromBase58String(s) |
---|
98 | | - | case _: Unit => |
---|
99 | | - | unit |
---|
100 | | - | case _ => |
---|
101 | | - | throw("Match error") |
---|
102 | | - | } |
---|
103 | | - | |
---|
104 | | - | |
---|
105 | | - | func pendingManagerPublicKeyOrUnit () = match getString(keyPendingManagerPublicKey()) { |
---|
106 | | - | case s: String => |
---|
107 | | - | fromBase58String(s) |
---|
108 | | - | case _: Unit => |
---|
109 | | - | unit |
---|
110 | | - | case _ => |
---|
111 | | - | throw("Match error") |
---|
112 | | - | } |
---|
113 | | - | |
---|
114 | | - | |
---|
115 | | - | func mustManager (i) = { |
---|
116 | | - | let pd = throw("Permission denied") |
---|
117 | | - | match managerPublicKeyOrUnit() { |
---|
118 | | - | case pk: ByteVector => |
---|
119 | | - | if ((i.callerPublicKey == pk)) |
---|
120 | | - | then true |
---|
121 | | - | else pd |
---|
122 | | - | case _: Unit => |
---|
123 | | - | if ((i.caller == this)) |
---|
124 | | - | then true |
---|
125 | | - | else pd |
---|
126 | | - | case _ => |
---|
127 | | - | throw("Match error") |
---|
128 | | - | } |
---|
129 | | - | } |
---|
130 | | - | |
---|
131 | | - | |
---|
132 | | - | @Callable(i) |
---|
133 | | - | func constructor (factoryContract) = { |
---|
134 | | - | let checkCaller = mustManager(i) |
---|
135 | | - | if ((checkCaller == checkCaller)) |
---|
136 | | - | then [StringEntry(keyFactoryContract(), factoryContract)] |
---|
137 | | - | else throw("Strict value is not equal to itself.") |
---|
138 | | - | } |
---|
139 | | - | |
---|
140 | | - | |
---|
141 | | - | |
---|
142 | | - | @Callable(i) |
---|
143 | | - | func put () = { |
---|
144 | | - | let pool = toString(i.caller) |
---|
145 | | - | let pmtAmtAsset = value(i.payments[0]) |
---|
146 | | - | let pmtAssetId = if (!(isDefined(pmtAmtAsset.assetId))) |
---|
147 | | - | then "WAVES" |
---|
148 | | - | else toBase58String(value(pmtAmtAsset.assetId)) |
---|
149 | | - | let pmtAssetAmt = pmtAmtAsset.amount |
---|
150 | | - | let poolAssets = split(valueOrErrorMessage(getString(factoryContract, keyMappingPoolContractAddressToPoolAssets(pool)), "Invalid caller"), SEP) |
---|
151 | | - | let amountAssetId = parseIntValue(poolAssets[1]) |
---|
152 | | - | let priceAssetId = parseIntValue(poolAssets[2]) |
---|
153 | | - | let amountAsset = valueOrErrorMessage(getString(factoryContract, keyMappingsInternal2baseAssetId(amountAssetId)), "Cannot find asset") |
---|
154 | | - | let priceAsset = valueOrErrorMessage(getString(factoryContract, keyMappingsInternal2baseAssetId(priceAssetId)), "Cannot find asset") |
---|
155 | | - | let totalAmt = valueOrElse(getInteger(this, keyPoolTotalAmtByAsset(pool, pmtAssetId)), 0) |
---|
156 | | - | let cumulativeSlippageAmt = valueOrElse(getInteger(this, keySlippageAmtPerAssetCumulative(pool, pmtAssetId)), 0) |
---|
157 | | - | [StringEntry(keyRegisterPut(pool, toBase58String(i.transactionId)), dataPutActionInfo(pmtAssetId, pmtAssetAmt, height, lastBlock.timestamp)), IntegerEntry(keyPoolTotalAmtByAsset(pool, pmtAssetId), (totalAmt + pmtAssetAmt)), IntegerEntry(keySlippageAmtPerAssetCumulative(pool, pmtAssetId), (cumulativeSlippageAmt + pmtAssetAmt))] |
---|
158 | | - | } |
---|
159 | | - | |
---|
160 | | - | |
---|
161 | | - | |
---|
162 | | - | @Callable(i) |
---|
163 | | - | func invest (poolAddressStr) = { |
---|
164 | | - | let poolAddress = valueOrErrorMessage(addressFromString(poolAddressStr), "invalid pool address") |
---|
165 | | - | let poolAssets = split(valueOrErrorMessage(getString(factoryContract, keyMappingPoolContractAddressToPoolAssets(poolAddressStr)), "Invalid pool passed."), SEP) |
---|
166 | | - | let amId = valueOrErrorMessage(getString(factoryContract, keyMappingsInternal2baseAssetId(parseIntValue(poolAssets[1]))), "No asset mapping found") |
---|
167 | | - | let prId = valueOrErrorMessage(getString(factoryContract, keyMappingsInternal2baseAssetId(parseIntValue(poolAssets[2]))), "No asset mapping found") |
---|
168 | | - | let amBalance = valueOrElse(getInteger(this, keyPoolTotalAmtByAsset(poolAddressStr, amId)), 0) |
---|
169 | | - | let prBalance = valueOrElse(getInteger(this, keyPoolTotalAmtByAsset(poolAddressStr, prId)), 0) |
---|
170 | | - | let estByPrResult = invoke(poolAddress, "evaluatePutByPriceAssetREADONLY", [prBalance], nil) |
---|
171 | | - | let estByAmResult = invoke(poolAddress, "evaluatePutByAmountAssetREADONLY", [amBalance], nil) |
---|
172 | | - | let IdxEstAmAmount = 7 |
---|
173 | | - | let IdxEstPrAmount = 8 |
---|
174 | | - | let estAmAmt = valueOrErrorMessage(parseIntValue(split(asString(estByPrResult), SEP)[IdxEstAmAmount]), "fail to parse estimated amAsset amount") |
---|
175 | | - | let estPrAmt = valueOrErrorMessage(parseIntValue(split(asString(estByAmResult), SEP)[IdxEstPrAmount]), "fail to parse estimated prAsset amount") |
---|
176 | | - | let $t067836884 = if ((amBalance >= estAmAmt)) |
---|
177 | | - | then $Tuple2(estAmAmt, prBalance) |
---|
178 | | - | else $Tuple2(amBalance, estPrAmt) |
---|
179 | | - | let amAmt = $t067836884._1 |
---|
180 | | - | let prAmt = $t067836884._2 |
---|
181 | | - | let amAssetId = if ((amId == "WAVES")) |
---|
182 | | - | then unit |
---|
183 | | - | else fromBase58String(amId) |
---|
184 | | - | let prAssetId = if ((prId == "WAVES")) |
---|
185 | | - | then unit |
---|
186 | | - | else fromBase58String(prId) |
---|
187 | | - | let lpPut = invoke(poolAddress, "putForFree", [1000], [AttachedPayment(amAssetId, amAmt), AttachedPayment(prAssetId, prAmt)]) |
---|
188 | | - | if ((lpPut == lpPut)) |
---|
189 | | - | then [StringEntry(keyRegisterInvest(poolAddressStr, toBase58String(i.transactionId)), dataInvestActionInfo(amAmt, prAmt, height, lastBlock.timestamp)), IntegerEntry(keyPoolTotalAmtByAsset(poolAddressStr, amId), (amBalance - amAmt)), IntegerEntry(keyPoolTotalAmtByAsset(poolAddressStr, prId), (prBalance - prAmt))] |
---|
190 | | - | else throw("Strict value is not equal to itself.") |
---|
191 | | - | } |
---|
192 | | - | |
---|
193 | | - | |
---|
194 | | - | |
---|
195 | | - | @Callable(i) |
---|
196 | | - | func setManager (pendingManagerPublicKey) = { |
---|
197 | | - | let checkCaller = mustManager(i) |
---|
198 | | - | if ((checkCaller == checkCaller)) |
---|
199 | | - | then { |
---|
200 | | - | let checkManagerPublicKey = fromBase58String(pendingManagerPublicKey) |
---|
201 | | - | if ((checkManagerPublicKey == checkManagerPublicKey)) |
---|
202 | | - | then [StringEntry(keyPendingManagerPublicKey(), pendingManagerPublicKey)] |
---|
203 | | - | else throw("Strict value is not equal to itself.") |
---|
204 | | - | } |
---|
205 | | - | else throw("Strict value is not equal to itself.") |
---|
206 | | - | } |
---|
207 | | - | |
---|
208 | | - | |
---|
209 | | - | |
---|
210 | | - | @Callable(i) |
---|
211 | | - | func confirmManager () = { |
---|
212 | | - | let pm = pendingManagerPublicKeyOrUnit() |
---|
213 | | - | let hasPM = if (isDefined(pm)) |
---|
214 | | - | then true |
---|
215 | | - | else throw("No pending manager") |
---|
216 | | - | if ((hasPM == hasPM)) |
---|
217 | | - | then { |
---|
218 | | - | let checkPM = if ((i.callerPublicKey == value(pm))) |
---|
219 | | - | then true |
---|
220 | | - | else throw("You are not pending manager") |
---|
221 | | - | if ((checkPM == checkPM)) |
---|
222 | | - | then [StringEntry(keyManagerPublicKey(), toBase58String(value(pm))), DeleteEntry(keyPendingManagerPublicKey())] |
---|
223 | | - | else throw("Strict value is not equal to itself.") |
---|
224 | | - | } |
---|
225 | | - | else throw("Strict value is not equal to itself.") |
---|
226 | | - | } |
---|
227 | | - | |
---|
228 | | - | |
---|
229 | | - | @Verifier(tx) |
---|
230 | | - | func verify () = { |
---|
231 | | - | let targetPublicKey = match managerPublicKeyOrUnit() { |
---|
232 | | - | case pk: ByteVector => |
---|
233 | | - | pk |
---|
234 | | - | case _: Unit => |
---|
235 | | - | tx.senderPublicKey |
---|
236 | | - | case _ => |
---|
237 | | - | throw("Match error") |
---|
238 | | - | } |
---|
239 | | - | sigVerify(tx.bodyBytes, tx.proofs[0], targetPublicKey) |
---|
240 | | - | } |
---|
241 | | - | |
---|
| 1 | + | # no script |
---|