2022.04.13 16:37 [3072186] smart account 3P9ZegsKUtsEpdRPNVrMH7nHEEqY5MrmjDp > SELF 0.00000000 Waves

{ "type": 13, "id": "A7q88Vt6Dh2jRBUfksFbz8JepYK3Ut8t8f45jtXVdMuh", "fee": 1400000, "feeAssetId": null, "timestamp": 1649856914525, "version": 1, "sender": "3P9ZegsKUtsEpdRPNVrMH7nHEEqY5MrmjDp", "senderPublicKey": "7ziFsWp9eo6kUB6ovqQzJfjgQarifNfpCGNW9DZi8YhN", "proofs": [ "kwwbhRNaHknWXZHqH8jrgeMCt8aEwzsDhnC7gi2yRoShzhfAY8sqPWz9Ry36HEup9Vh1GN7tF9nMXs2YEu8JoYu", "2dDiw8vg5gMDJwoZBMpqGiZob7i8ssZRj5BSBAAsgbiZpsPrPpcd8EEjNZbYYHUs1NRhezvmCCokUW9aZNWeLYsk" ], "script": "base64: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", "chainId": 87, "height": 3072186, "applicationStatus": "succeeded", "spentComplexity": 0 } View: original | compacted Prev: A5eQYkLtH4rRupN23Gb5as2MZiH5agjhKP1ZNdwSCQtR Next: none Diff:
OldNewDifferences
1-{-# STDLIB_VERSION 5 #-}
1+{-# STDLIB_VERSION 4 #-}
22 {-# SCRIPT_TYPE ACCOUNT #-}
33 {-# CONTENT_TYPE DAPP #-}
4-func SE (k,v) = StringEntry(k, v)
5-
6-
7-func IE (k,v) = IntegerEntry(k, v)
8-
9-
10-func ST (a,amt,t) = ScriptTransfer(a, amt, t)
11-
12-
13-let ten6 = 1000000
14-
15-let ten8 = 100000000
16-
17-let MAX = 9223372036854775807
18-
19-let configProviderKey = "configProvider"
20-
21-let configProvider = match getString(this, configProviderKey) {
22- case s: String =>
23- addressFromStringValue(s)
24- case _ =>
25- this
26-}
27-
284 func localI (k,e) = valueOrErrorMessage(getInteger(this, k), e)
295
306
317 func localS (k,e) = valueOrErrorMessage(getString(this, k), e)
32-
33-
34-func confI (k,e) = valueOrErrorMessage(getInteger(configProvider, k), e)
35-
36-
37-func confS (k,e) = valueOrErrorMessage(getString(configProvider, k), e)
388
399
4010 let BULLK = "BULLId"
5121
5222 let BEARCIRCK = "bearCirculation"
5323
54-let ISSPERCK = "issuePercentile"
55-
56-let REDPERCK = "redeemPercentile"
57-
58-let MINISSK = "minIssue"
59-
60-let MINREDK = "minRedeem"
61-
62-let MINPOOLK = "minPool"
63-
6424 let FEEACCK = "feesAccumulated"
65-
66-let WLISTK = "issueWhiteList"
67-
68-let REBPERCK = "rebalancePercentile"
69-
70-let REBIDXK = "lastSettlementPriceId"
71-
72-let HEADK = "headPointer"
73-
74-let TAILK = "tailPointer"
75-
76-let QSIZEK = "queueSize"
77-
78-let POOLUSDNK = "poolMainTokenValue"
79-
80-let POOLUPK = "poolUp"
81-
82-let POOLDWNK = "poolDwn"
83-
84-let POOLCIRCK = "poolTokenCirculation"
85-
86-let POOLK = "poolToken"
87-
88-let LEVK = "leverage"
89-
90-let WAVESFEEK = "wavesPacemakerFee"
91-
92-let USDNFEEK = "usdnPacemakerFee"
93-
94-let oraclev2Key = "oraclev2"
95-
96-let assetidv2Key = "assetidv2"
97-
98-let oraclev2 = valueOrErrorMessage(getString(this, oraclev2Key), "no oraclev2 key")
99-
100-let assetidv2 = valueOrErrorMessage(getString(this, assetidv2Key), "no assetidv2")
101-
102-let maxDeviation = valueOrElse(getInteger(configProvider, (toString(this) + "_deviationPercentile")), 0)
103-
104-let lastPriceIndexKey = ("%s%s__idxCurrent__" + assetidv2)
105-
106-let priceIndexPrefix = (("%s%s%d__idx2Height__" + assetidv2) + "__")
107-
108-let priceHeightPrefix = (("%s%s%d__priceByHeight__" + assetidv2) + "__")
109-
110-let minUsdnFee = valueOrElse(getInteger(configProvider, USDNFEEK), 0)
111-
112-let minWavesFee = valueOrElse(getInteger(configProvider, WAVESFEEK), 0)
11325
11426 let bullCol = localI(BULLCOLK, "no 0")
11527
12537
12638 let mainToken = localS(USDNK, "no 16")
12739
128-let issuePercentile = confI(ISSPERCK, "no 4")
40+let feeAddrKey = "feeAddress"
12941
130-let redeemPercentile = confI(REDPERCK, "no 5")
42+let feeAddress = valueOrErrorMessage(addressFromString(valueOrErrorMessage(getString(this, feeAddrKey), "no fee address")), "bad feeAddress")
13143
132-let minIssue = confI(MINISSK, "no 6")
133-
134-let minRedeem = confI(MINREDK, "no 7")
135-
136-let minPool = confI(MINPOOLK, "no 8")
137-
138-let rebalancePercentile = valueOrElse(getInteger(configProvider, ((toString(this) + "_") + REBPERCK)), 0)
139-
140-let whitelist = confS(WLISTK, "no 9")
141-
142-func allowed (a) = if ((whitelist == ""))
143- then true
144- else isDefined(indexOf(whitelist, toString(a)))
44+func requestIssueInternal (inv,tokenId,minPayout) = throw("issuing of EURUP and EURDOWN is disabled")
14545
14646
147-let poolMain = localI(POOLUSDNK, "no")
148-
149-let poolUp = localI(POOLUPK, "no 10")
150-
151-let poolDwn = localI(POOLDWNK, "no 11")
152-
153-let poolToken = localS(POOLK, "no 12")
154-
155-let poolTokenCirculation = localI(POOLCIRCK, "no 13")
156-
157-let poolBullExposure = fraction(bullCol, poolUp, bullCirc)
158-
159-let poolBearExposure = fraction(bearCol, poolDwn, bearCirc)
160-
161-let poolValue = ((poolMain + poolBullExposure) + poolBearExposure)
162-
163-let oracle = valueOrErrorMessage(addressFromString(oraclev2), "bad oracle address")
164-
165-let closeInterval = confI("closeInterval", "no CI")
166-
167-let settledPriceIndex = valueOrErrorMessage(getInteger(this, REBIDXK), "no last rebalance price")
168-
169-let oraclePriceIndex = valueOrErrorMessage(getInteger(oracle, lastPriceIndexKey), ((("bad oracle data at " + toString(oracle)) + ": no integer at ") + lastPriceIndexKey))
170-
171-let leverage = valueOrElse(getInteger(this, LEVK), 3)
172-
173-func heightByIndex (priceIndex) = valueOrErrorMessage(getInteger(oracle, (priceIndexPrefix + toString(priceIndex))), ("no data at index " + toString(priceIndex)))
174-
175-
176-func priceByHeight (priceHeight) = valueOrErrorMessage(getInteger(oracle, (priceHeightPrefix + toString(priceHeight))), ("no data for height " + toString(priceHeight)))
177-
178-
179-func priceByIndex (priceIndex) = priceByHeight(heightByIndex(priceIndex))
180-
181-
182-let queueSize = valueOrElse(getInteger(this, QSIZEK), 0)
183-
184-let headPointer = valueOrElse(getString(this, HEADK), "")
185-
186-let tailPointer = valueOrElse(getString(this, TAILK), "")
187-
188-let feesAccumulated = valueOrElse(getInteger(this, FEEACCK), 0)
189-
190-let queueEmpty = (headPointer == "")
191-
192-let queuePriceIndex = if (queueEmpty)
193- then MAX
194- else {
195- let data = split(localS(headPointer, "settle2: bad head pointer"), "|")
196- parseIntValue(data[3])
197- }
198-
199-let settledPrice = priceByIndex(settledPriceIndex)
200-
201-let ISSUE = "ISSUE"
202-
203-let REDEEM = "REDEEM"
204-
205-let POOL = "POOL"
206-
207-let UNPOOL = "UNPOOL"
208-
209-let feeAddrKey = "feeAddress"
210-
211-let stakingAddrKey = "stakingAddress"
212-
213-let daemonPubKeyKey = "daemonPublicKey"
214-
215-let feeAddress = valueOrErrorMessage(addressFromString(confS(feeAddrKey, "no feeAddress")), "bad feeAddress")
216-
217-let stakingAddress = confS(stakingAddrKey, "no stakingAddress")
218-
219-let daemonPublicKey = fromBase58String(confS(daemonPubKeyKey, "no daemonPublicKey"))
220-
221-let rpdAddress = valueOrErrorMessage(addressFromString("3PNikM6yp4NqcSU8guxQtmR5onr2D4e8yTJ"), "bad rpd address")
222-
223-let admins = ["2HHqV8W9DJayV5R6tBD2Sb8srphpoboDi7r1t1aPiumC", "5ZXe82RRASU7qshXM2J9JNYhqJ9GWYjjVq2gwUV5Naz9", "5WRXFSjwcTbNfKcJs8ZqXmSSWYsSVJUtMvMqZj5hH4Nc"]
224-
225-func buildNewItem (action,amt,token,priceIndex,invoker,minPayout,maxPayout) = (((((((((((((action + "|") + toString(amt)) + "|") + token) + "|") + toString(priceIndex)) + "|") + invoker) + "|") + toString(minPayout)) + "|") + toString(maxPayout)) + "|")
226-
227-
228-func diffAbs (a1,a2) = {
229- let diff = (a1 - a2)
230- if ((diff > 0))
231- then diff
232- else (0 - diff)
47+func requestRedeemInternal (inv) = {
48+ let removedTokens = inv.payments[0].amount
49+ let tokenId = toBase58String(value(inv.payments[0].assetId))
50+ let invoker = inv.caller
51+ if ((tokenId == BULL))
52+ then {
53+ let payout = fraction(bullCol, removedTokens, bullCirc)
54+[IntegerEntry(BULLCOLK, (bullCol - payout)), IntegerEntry(BULLCIRCK, (bullCirc - removedTokens)), ScriptTransfer(invoker, payout, fromBase58String(mainToken))]
55+ }
56+ else if ((tokenId == BEAR))
57+ then {
58+ let payout = fraction(bearCol, removedTokens, bearCirc)
59+[IntegerEntry(BEARCOLK, (bearCol - payout)), IntegerEntry(BEARCIRCK, (bearCirc - removedTokens)), ScriptTransfer(invoker, payout, fromBase58String(mainToken))]
60+ }
61+ else throw("bad token attached")
23362 }
23463
23564
236-func userDiffAbs () = diffAbs((bullCol - poolBullExposure), (bearCol - poolBearExposure))
237-
238-
239-func maxIssue (tokenId) = {
240- let poolInvestment = if ((poolUp > 0))
241- then BULL
242- else BEAR
243- if ((tokenId != poolInvestment))
244- then poolMain
245- else (userDiffAbs() + poolValue)
246- }
247-
248-
249-func validatePMFee (i,minPayout) = if ((0 > minPayout))
250- then throw("negative min payout")
251- else {
252- let p = i.payments[1]
253- let ok = match p.assetId {
254- case bv: ByteVector =>
255- if ((toBase58String(bv) == mainToken))
256- then (p.amount >= minUsdnFee)
257- else false
258- case waves: Unit =>
259- (p.amount >= minWavesFee)
260- case _ =>
261- throw("Match error")
262- }
263- if (!(ok))
264- then throw("incorrect pacemaker fee")
265- else true
266- }
267-
268-
269-func enqueue (id,action,amt,token,priceIndex,invoker,minPayout,maxPayout) = if (!(valueOrElse(getBoolean(this, "enableEnqueue"), true)))
270- then throw("enqueue paused")
271- else {
272- let increaseQueueSize = IE(QSIZEK, (queueSize + 1))
273- let itm = buildNewItem(action, amt, token, priceIndex, invoker, minPayout, maxPayout)
274- if ((queueSize == 0))
275- then [SE(HEADK, id), SE(TAILK, id), SE(id, itm), increaseQueueSize]
276- else {
277- let prevId = localS(TAILK, "can't get tail pointer")
278- let prevItm = localS(prevId, "can't resolve pointer")
279- let updatedPrevItm = (prevItm + id)
280-[SE(prevId, updatedPrevItm), SE(id, itm), SE(TAILK, id), increaseQueueSize]
281- }
282- }
283-
284-
285-func poolSupport (curBullCol0,curBearCol0,curBullCirc0,curBearCirc0,curPoolMain0,curPoolUp0,curPoolDwn0,steps) = {
286- func closeUp (c1,c2,a0,a1,c0,pu,pd) = {
287- let diff = (c1 - c2)
288- let exp = fraction(c1, pu, a0)
289- let liquidatedTokens = if ((diff > exp))
290- then pu
291- else fraction(diff, a0, c1)
292- let liquidatedValue = if ((diff > exp))
293- then exp
294- else fraction(liquidatedTokens, c1, a0)
295- $Tuple7((c1 - liquidatedValue), c2, (a0 - liquidatedTokens), a1, (c0 + liquidatedValue), (pu - liquidatedTokens), pd)
296- }
297-
298- func closeDwn (c1,c2,a0,a1,c0,pu,pd) = {
299- let diff = (c2 - c1)
300- let exp = fraction(c2, pd, a1)
301- let liquidatedTokens = if ((diff > exp))
302- then pd
303- else fraction(diff, a1, c2)
304- let liquidatedValue = if ((diff > exp))
305- then exp
306- else fraction(liquidatedTokens, c2, a1)
307- $Tuple7(c1, (c2 - liquidatedValue), a0, (a1 - liquidatedTokens), (c0 + liquidatedValue), pu, (pd - liquidatedTokens))
308- }
309-
310- func openDwn (c1,c2,a0,a1,c0,pu,pd) = {
311- let diff = (c1 - c2)
312- let spentPoolValue = if ((c0 > diff))
313- then diff
314- else c0
315- let acquiredTokens = fraction(spentPoolValue, a1, c2)
316- $Tuple7(c1, (c2 + spentPoolValue), a0, (a1 + acquiredTokens), (c0 - spentPoolValue), pu, (pd + acquiredTokens))
317- }
318-
319- func openUp (c1,c2,a0,a1,c0,pu,pd) = {
320- let diff = (c2 - c1)
321- let spentPoolValue = if ((c0 > diff))
322- then diff
323- else c0
324- let acquiredTokens = fraction(spentPoolValue, a0, c1)
325- $Tuple7((c1 + spentPoolValue), c2, (a0 + acquiredTokens), a1, (c0 - spentPoolValue), (pu + acquiredTokens), pd)
326- }
327-
328- let $t0925510290 = if ((curBullCol0 > curBearCol0))
329- then {
330- let afterCloseUp = closeUp(curBullCol0, curBearCol0, curBullCirc0, curBearCirc0, curPoolMain0, curPoolUp0, curPoolDwn0)
331- let $t094819618 = afterCloseUp
332- let a = $t094819618._1
333- let b = $t094819618._2
334- let c = $t094819618._3
335- let d = $t094819618._4
336- let e = $t094819618._5
337- let f = $t094819618._6
338- let g = $t094819618._7
339- if ((f > 0))
340- then afterCloseUp
341- else if ((f == 0))
342- then openDwn(a, b, c, d, e, f, g)
343- else throw("poolUp < 0")
344- }
345- else {
346- let afterCloseDwn = closeDwn(curBullCol0, curBearCol0, curBullCirc0, curBearCirc0, curPoolMain0, curPoolUp0, curPoolDwn0)
347- let $t0996910109 = afterCloseDwn
348- let a = $t0996910109._1
349- let b = $t0996910109._2
350- let c = $t0996910109._3
351- let d = $t0996910109._4
352- let e = $t0996910109._5
353- let f = $t0996910109._6
354- let g = $t0996910109._7
355- if ((g > 0))
356- then afterCloseDwn
357- else if ((g == 0))
358- then openUp(a, b, c, d, e, f, g)
359- else throw("poolDwn < 0")
360- }
361- let c1 = $t0925510290._1
362- let c2 = $t0925510290._2
363- let a0 = $t0925510290._3
364- let a1 = $t0925510290._4
365- let c0 = $t0925510290._5
366- let pu = $t0925510290._6
367- let pd = $t0925510290._7
368- let charge = fraction(userDiffAbs(), (rebalancePercentile * steps), ((1440 * 100) * 100))
369- let c1SplitP = if ((pd > 0))
370- then charge
371- else 0
372- let c2SplitP = if ((pu > 0))
373- then charge
374- else 0
375- $Tuple7((c1 - c1SplitP), (c2 - c2SplitP), a0, a1, ((c0 + c1SplitP) + c2SplitP), pu, pd)
376- }
377-
378-
379-func poolSup (curBullCol0,curBearCol0,curBullCirc0,curBearCirc0) = {
380- let $t01071510900 = poolSupport(curBullCol0, curBearCol0, curBullCirc0, curBearCirc0, poolMain, poolUp, poolDwn, 0)
381- let bullCol1 = $t01071510900._1
382- let bearCol1 = $t01071510900._2
383- let bullCic1 = $t01071510900._3
384- let bearCirc1 = $t01071510900._4
385- let poolMain1 = $t01071510900._5
386- let poolUp1 = $t01071510900._6
387- let poolDwn1 = $t01071510900._7
388-[IE(BULLCOLK, bullCol1), IE(BULLCIRCK, bullCic1), IE(BEARCOLK, bearCol1), IE(BEARCIRCK, bearCirc1), IE(POOLUSDNK, poolMain1), IE(POOLUPK, poolUp1), IE(POOLDWNK, poolDwn1)]
389- }
390-
391-
392-func dequeue () = {
393- func sp (a,mx) = if ((mx >= a))
394- then $Tuple2(a, 0)
395- else $Tuple2(mx, (a - mx))
396-
397- if ((queueSize == 0))
398- then throw("nothing to settle")
399- else {
400- func collectFee (fees) = IE(FEEACCK, (feesAccumulated + fees))
401-
402- let decreaseQueueSize = IE(QSIZEK, (queueSize - 1))
403- let isLastElement = (headPointer == tailPointer)
404- let overwriteTail = SE(TAILK, "")
405- let dataStr = localS(headPointer, "bad head pointer(dequeue)")
406- let data = split(dataStr, "|")
407- let action = data[0]
408- let amt = parseIntValue(data[1])
409- let token = data[2]
410- let priceIndex = parseIntValue(data[3])
411- let invoker = addressFromStringValue(data[4])
412- let minPayout = if ((8 > size(data)))
413- then 0
414- else parseIntValue(data[5])
415- let maxPayout = if ((8 > size(data)))
416- then MAX
417- else parseIntValue(data[6])
418- let next = data[(size(data) - 1)]
419- func payback (tkn) = [SE(HEADK, next), decreaseQueueSize, ST(invoker, amt, fromBase58String(tkn))]
420-
421- let items = if ((settledPriceIndex != priceIndex))
422- then throw(((("corrupt state, settledPriceIndex=" + toString(settledPriceIndex)) + ", request price id=") + toString(priceIndex)))
423- else if ((action == ISSUE))
424- then {
425- let feeSize = fraction(amt, issuePercentile, 10000)
426- let addedCollateral = (amt - feeSize)
427- let defaultAddedTokens = if ((token == BULL))
428- then fraction(bullCirc, addedCollateral, bullCol)
429- else if ((token == BEAR))
430- then fraction(bearCirc, addedCollateral, bearCol)
431- else throw("bad token id")
432- let $t01291212985 = sp(defaultAddedTokens, maxPayout)
433- let addedToCirculation = $t01291212985._1
434- let extraTokens = $t01291212985._2
435- let $t01300213173 = if ((token == BULL))
436- then $Tuple4(defaultAddedTokens, addedCollateral, 0, 0)
437- else $Tuple4(0, 0, defaultAddedTokens, addedCollateral)
438- let plusBulls = $t01300213173._1
439- let plusBullCol = $t01300213173._2
440- let plusBears = $t01300213173._3
441- let plusBearCol = $t01300213173._4
442- if ((minPayout > addedToCirculation))
443- then payback(mainToken)
444- else (poolSup((bullCol + plusBullCol), (bearCol + plusBearCol), (bullCirc + plusBulls), (bearCirc + plusBears)) ++ [SE(HEADK, next), collectFee(feeSize), decreaseQueueSize, ST(invoker, addedToCirculation, fromBase58String(token)), ST(feeAddress, extraTokens, fromBase58String(token))])
445- }
446- else if ((action == REDEEM))
447- then {
448- let removedTokens = amt
449- let estimatedPayout = if ((token == BULL))
450- then fraction(bullCol, removedTokens, bullCirc)
451- else if ((token == BEAR))
452- then fraction(bearCol, removedTokens, bearCirc)
453- else throw("bad token id")
454- let feeSize = fraction(estimatedPayout, redeemPercentile, 10000)
455- let defaultPayout = (estimatedPayout - feeSize)
456- let $t01424014294 = sp(defaultPayout, maxPayout)
457- let userPayout = $t01424014294._1
458- let extra = $t01424014294._2
459- let $t01431114476 = if ((token == BULL))
460- then $Tuple4(removedTokens, estimatedPayout, 0, 0)
461- else $Tuple4(0, 0, removedTokens, estimatedPayout)
462- let minusBulls = $t01431114476._1
463- let minusBullCol = $t01431114476._2
464- let minusBears = $t01431114476._3
465- let minusBearCol = $t01431114476._4
466- if ((minPayout > userPayout))
467- then payback(token)
468- else (poolSup((bullCol - minusBullCol), (bearCol - minusBearCol), (bullCirc - minusBulls), (bearCirc - minusBears)) ++ [SE(HEADK, next), collectFee(feeSize), decreaseQueueSize, ST(invoker, userPayout, fromBase58String(mainToken)), ST(feeAddress, extra, fromBase58String(mainToken))])
469- }
470- else if ((action == POOL))
471- then {
472- let issueTokens = fraction(poolTokenCirculation, amt, poolValue)
473- if ((minPayout > issueTokens))
474- then payback(mainToken)
475- else [IE(POOLUSDNK, (poolMain + amt)), IE(POOLCIRCK, (poolTokenCirculation + issueTokens)), SE(HEADK, next), decreaseQueueSize, ST(invoker, issueTokens, fromBase58String(poolToken))]
476- }
477- else if ((action == UNPOOL))
478- then {
479- func share (a) = fraction(a, amt, poolTokenCirculation)
480-
481- let unpooledMain = share(poolMain)
482- let unpooledUp = share(poolUp)
483- let unpooledDwn = share(poolDwn)
484- let unpooledUpValue = fraction(unpooledUp, bullCol, bullCirc)
485- let unpooledDwnValue = fraction(unpooledDwn, bearCol, bearCirc)
486- let totalUnpooledValue = ((unpooledMain + unpooledUpValue) + unpooledDwnValue)
487- if ((minPayout > totalUnpooledValue))
488- then payback(poolToken)
489- else [IE(POOLUSDNK, (poolMain - unpooledMain)), IE(POOLCIRCK, (poolTokenCirculation - amt)), IE(POOLUPK, (poolUp - unpooledUp)), IE(POOLDWNK, (poolDwn - unpooledDwn)), IE(BULLCIRCK, (bullCirc - unpooledUp)), IE(BEARCIRCK, (bearCirc - unpooledDwn)), IE(BULLCOLK, (bullCol - unpooledUpValue)), IE(BEARCOLK, (bearCol - unpooledDwnValue)), SE(HEADK, next), decreaseQueueSize, ST(invoker, totalUnpooledValue, fromBase58String(mainToken))]
490- }
491- else throw(("bad action: " + action))
492- if (isLastElement)
493- then overwriteTail :: items
494- else items
495- }
496- }
497-
498-
499-func rebalance (offset) = if (!(valueOrElse(getBoolean(this, "enableRebalance"), true)))
500- then throw("rebalance paused")
501- else if ((offset == 0))
502- then throw("[OK] offset=0")
503- else {
504- func LV (v,p0,p1,m) = {
505- let denom = {
506- let md = (((2 * (if ((p1 > p0))
507- then p1
508- else p0)) * m) / 3037000499)
509- if ((10 > md))
510- then 10
511- else if ((100 > md))
512- then 100
513- else 1000
514- }
515- let pmax = ((if ((p1 > p0))
516- then p1
517- else p0) / denom)
518- let pmin = ((if ((p0 > p1))
519- then p1
520- else p0) / denom)
521- let a = (pmin * pmin)
522- let b = ((((m * m) * pmax) * pmax) - (((((2 * m) * m) - m) * pmax) * pmin))
523- let ma = ((m * m) - m)
524- fraction(v, ((ma * a) + b), (((ma + 1) * a) + b))
525- }
526-
527- let unsettledPriceIndex = (settledPriceIndex + offset)
528- let nextPrice = priceByIndex(unsettledPriceIndex)
529- let minVol = if ((bearCol > bullCol))
530- then bullCol
531- else bearCol
532- let redist = LV(minVol, settledPrice, nextPrice, leverage)
533- let bullsEarn = (nextPrice > settledPrice)
534- let newBullCol = if (bullsEarn)
535- then (bullCol + redist)
536- else (bullCol - redist)
537- let newBearCol = if (bullsEarn)
538- then (bearCol - redist)
539- else (bearCol + redist)
540- let $t01827018468 = poolSupport(newBullCol, newBearCol, bullCirc, bearCirc, poolMain, poolUp, poolDwn, offset)
541- let updBullCol = $t01827018468._1
542- let updBearCol = $t01827018468._2
543- let updBullCirc = $t01827018468._3
544- let updBearCirc = $t01827018468._4
545- let updPoolMain = $t01827018468._5
546- let updPoolUp = $t01827018468._6
547- let updPoolDwn = $t01827018468._7
548-[IE(BULLCOLK, updBullCol), IE(BEARCOLK, updBearCol), IE(BULLCIRCK, updBullCirc), IE(BEARCIRCK, updBearCirc), IE(POOLUSDNK, updPoolMain), IE(POOLUPK, updPoolUp), IE(POOLDWNK, updPoolDwn), IE(REBIDXK, unsettledPriceIndex)]
549- }
550-
551-
552-func calcMax (min,avg) = if ((min > avg))
553- then throw(((("price too old: minPayout " + toString(min)) + " > avg = ") + toString(avg)))
554- else ((avg + avg) - min)
555-
556-
55765 @Callable(i)
558-func enableEnqueue (arg) = if ((i.caller != feeAddress))
559- then throw("only admin can")
560- else [BooleanEntry("enableEnqueue", arg)]
561-
562-
563-
564-@Callable(i)
565-func enableRebalance (arg) = if ((i.caller != feeAddress))
566- then throw("only admin can")
567- else [BooleanEntry("enableRebalance", arg)]
568-
569-
570-
571-@Callable(i)
572-func withdrawFee (amount) = if ((amount > feesAccumulated))
573- then throw(("too much. available: " + toString(feesAccumulated)))
574- else [IE(FEEACCK, (feesAccumulated - amount)), ST(feeAddress, amount, fromBase58String(mainToken))]
575-
576-
577-
578-@Callable(inv)
579-func requestRedeemSl (minPayout) = if (validatePMFee(inv, minPayout))
580- then {
581- let amt = inv.payments[0].amount
582- let tokenId = toBase58String(valueOrErrorMessage(inv.payments[0].assetId, "bad token att"))
583- if (if ((tokenId != BULL))
584- then (tokenId != BEAR)
585- else false)
586- then throw("bad token req")
587- else {
588- let $t01994220081 = if ((tokenId == BULL))
589- then $Tuple2(bullCol, bullCirc)
590- else if ((tokenId == BEAR))
591- then $Tuple2(bearCol, bearCirc)
592- else throw("bad token req")
593- let col = $t01994220081._1
594- let circ = $t01994220081._2
595- let est = fraction(amt, col, circ)
596- let $t02012920223 = if ((minPayout == 0))
597- then $Tuple2(0, MAX)
598- else $Tuple2(minPayout, calcMax(minPayout, est))
599- let minP = $t02012920223._1
600- let maxP = $t02012920223._2
601- if ((inv.caller == this))
602- then throw("can't do")
603- else {
604- let estimated = fraction(col, amt, circ)
605- if ((minRedeem > estimated))
606- then throw((("Attached payment too small. Min redeem amount is " + toString((minRedeem / 1000000))) + " USDN"))
607- else enqueue(toBase58String(inv.transactionId), REDEEM, amt, tokenId, (oraclePriceIndex + 1), toString(inv.caller), minP, maxP)
608- }
609- }
610- }
611- else throw()
612-
613-
614-
615-@Callable(inv)
616-func requestIssueSl (tokenId,minPayout) = if (validatePMFee(inv, minPayout))
617- then if (if ((tokenId != BULL))
618- then (tokenId != BEAR)
619- else false)
620- then throw("bad token req")
621- else if (!(allowed(inv.caller)))
622- then throw("only whitelisted can do")
623- else if ((inv.payments[0].assetId != fromBase58String(mainToken)))
624- then throw("bad token att")
625- else {
626- let amt = inv.payments[0].amount
627- let $t02114621327 = if ((tokenId == BULL))
628- then $Tuple2(bullCol, bullCirc)
629- else if ((tokenId == BEAR))
630- then $Tuple2(bearCol, bearCirc)
631- else throw("bad token req")
632- let col = $t02114621327._1
633- let circ = $t02114621327._2
634- let est = fraction(amt, circ, col)
635- let $t02136921463 = if ((minPayout == 0))
636- then $Tuple2(0, MAX)
637- else $Tuple2(minPayout, calcMax(minPayout, est))
638- let minP = $t02136921463._1
639- let maxP = $t02136921463._2
640- if ((minIssue > amt))
641- then throw((("Attached payment too small. Min required: " + toString((minIssue / 1000000))) + " USDN"))
642- else {
643- let maxAllowed = maxIssue(tokenId)
644- if (if ((whitelist == ""))
645- then (inv.payments[0].amount > maxAllowed)
646- else false)
647- then throw((("trying to issue more than pool can handle. Max attachment allowed = " + toString((maxAllowed / 1000000))) + " USDN"))
648- else enqueue(toBase58String(inv.transactionId), ISSUE, amt, tokenId, (oraclePriceIndex + 1), toString(inv.caller), minP, maxP)
649- }
650- }
651- else throw()
652-
653-
654-
655-@Callable(inv)
656-func requestPool () = if (!(allowed(inv.caller)))
657- then throw("only whitelisted can do")
658- else {
659- let pmt = inv.payments[0]
660- if ((pmt.assetId != fromBase58String(mainToken)))
661- then throw("USDN required")
662- else if ((minPool > pmt.amount))
663- then throw(("pool min: " + toString(minPool)))
664- else {
665- let estimate = fraction(poolTokenCirculation, pmt.amount, poolValue)
666- enqueue(toBase58String(inv.transactionId), POOL, inv.payments[0].amount, "", (oraclePriceIndex + 1), toString(inv.caller), 0, MAX)
667- }
668- }
669-
670-
671-
672-@Callable(inv)
673-func requestUnpool () = {
674- let pmt = inv.payments[0]
675- if ((pmt.assetId != fromBase58String(poolToken)))
676- then throw("bad token att")
677- else {
678- let estimate = fraction(poolValue, pmt.amount, poolTokenCirculation)
679- if ((minPool > estimate))
680- then throw(((("unpool at least for" + toString(minPool)) + " ") + mainToken))
681- else enqueue(toBase58String(inv.transactionId), UNPOOL, inv.payments[0].amount, "", (oraclePriceIndex + 1), toString(inv.caller), 0, MAX)
682- }
66+func withdrawFee (amount) = {
67+ let acc = valueOrErrorMessage(getInteger(this, FEEACCK), "no fees acc")
68+ if ((amount > acc))
69+ then throw(("too much. available: " + toString(acc)))
70+ else [IntegerEntry(FEEACCK, (acc - amount)), ScriptTransfer(feeAddress, amount, fromBase58String(mainToken))]
68371 }
68472
68573
68674
68775 @Callable(inv)
688-func settle () = {
689- let canRebalance = (oraclePriceIndex > settledPriceIndex)
690- if (queueEmpty)
691- then if (canRebalance)
692- then rebalance(1)
693- else throw("[OK] all done, carry on")
694- else if ((queuePriceIndex > settledPriceIndex))
695- then if (canRebalance)
696- then rebalance(1)
697- else throw("[OK] need to wait")
698- else if ((queuePriceIndex == settledPriceIndex))
699- then dequeue()
700- else throw("future price already rebalanced")
701- }
76+func requestRedeem () = requestRedeemInternal(inv)
70277
70378
70479
70580 @Callable(inv)
706-func settle2 () = {
707- func hasDeviation (priceIndex) = {
708- let p = priceByIndex(priceIndex)
709- (fraction(diffAbs(settledPrice, p), 1000000, settledPrice) >= maxDeviation)
710- }
711-
712- let maxPossibleShift = (min([queuePriceIndex, oraclePriceIndex]) - settledPriceIndex)
713- func f (acc,shift) = if (if (acc._2)
714- then true
715- else (shift > maxPossibleShift))
716- then acc
717- else $Tuple2(shift, if ((shift == 7))
718- then true
719- else hasDeviation((settledPriceIndex + shift)))
720-
721- let $t02447524574 = {
722- let $list2451924574 = [1, 2, 3, 4, 5, 6, 7]
723- let $size2451924574 = size($list2451924574)
724- let $acc02451924574 = $Tuple2(settledPriceIndex, false)
725- if (($size2451924574 == 0))
726- then $acc02451924574
727- else {
728- let $acc12451924574 = f($acc02451924574, $list2451924574[0])
729- if (($size2451924574 == 1))
730- then $acc12451924574
731- else {
732- let $acc22451924574 = f($acc12451924574, $list2451924574[1])
733- if (($size2451924574 == 2))
734- then $acc22451924574
735- else {
736- let $acc32451924574 = f($acc22451924574, $list2451924574[2])
737- if (($size2451924574 == 3))
738- then $acc32451924574
739- else {
740- let $acc42451924574 = f($acc32451924574, $list2451924574[3])
741- if (($size2451924574 == 4))
742- then $acc42451924574
743- else {
744- let $acc52451924574 = f($acc42451924574, $list2451924574[4])
745- if (($size2451924574 == 5))
746- then $acc52451924574
747- else {
748- let $acc62451924574 = f($acc52451924574, $list2451924574[5])
749- if (($size2451924574 == 6))
750- then $acc62451924574
751- else {
752- let $acc72451924574 = f($acc62451924574, $list2451924574[6])
753- if (($size2451924574 == 7))
754- then $acc72451924574
755- else {
756- let $acc82451924574 = f($acc72451924574, $list2451924574[7])
757- throw("List size exceed 7")
758- }
759- }
760- }
761- }
762- }
763- }
764- }
765- }
766- }
767- let okShift = $t02447524574._1
768- let forcedByDeviationOrTimeout = $t02447524574._2
769- let queueWaits = (oraclePriceIndex >= queuePriceIndex)
770- if ((settledPriceIndex > queuePriceIndex))
771- then throw("settle2: future price already settled/rebalanced")
772- else if ((settledPriceIndex == queuePriceIndex))
773- then dequeue()
774- else if (if ((okShift > 0))
775- then if (forcedByDeviationOrTimeout)
776- then true
777- else queueWaits
778- else false)
779- then rebalance(okShift)
780- else throw(("settle2: [OK] all done, carry on. available shift: " + toString(okShift)))
781- }
81+func requestRedeemSl (sl) = requestRedeemInternal(inv)
78282
78383
78484
78585 @Callable(inv)
786-func settle3 () = {
787- let maxPossibleShift = (min([queuePriceIndex, oraclePriceIndex]) - settledPriceIndex)
788- let okShift = min([maxPossibleShift, 7])
789- if ((settledPriceIndex > queuePriceIndex))
790- then throw("settle3: future price already settled/rebalanced")
791- else if ((settledPriceIndex == queuePriceIndex))
792- then dequeue()
793- else if ((okShift > 0))
794- then rebalance(okShift)
795- else nil
796- }
86+func requestIssue (tokenId) = throw("issuing of EURUP and EURDOWN is disabled")
79787
79888
79989
800-@Callable(i)
801-func stakeAll () = if (if ((i.caller != this))
802- then (i.caller != addressFromStringValue(getStringValue(this, "pacewrapper")))
803- else false)
804- then throw("forbidden")
805- else {
806- let tkn = fromBase58String(mainToken)
807- let currentBalance = assetBalance(this, tkn)
808- let stake = if ((currentBalance > 0))
809- then invoke(rpdAddress, "lockNeutrinoSP", [toString(feeAddress), 100], [AttachedPayment(tkn, currentBalance)])
810- else unit
811- if ((stake == stake))
812- then $Tuple2(nil, currentBalance)
813- else throw("Strict value is not equal to itself.")
814- }
815-
816-
817-
818-@Callable(i)
819-func unstakeAll () = if (if ((i.caller != this))
820- then (i.caller != addressFromStringValue(getStringValue(this, "pacewrapper")))
821- else false)
822- then throw("forbidden")
823- else {
824- let currentStaked = valueOrElse(getInteger(rpdAddress, ((("rpd_balance_" + mainToken) + "_") + toString(this))), 0)
825- let unstake = if ((currentStaked > 0))
826- then invoke(rpdAddress, "unlockNeutrino", [currentStaked, mainToken], nil)
827- else unit
828- if ((unstake == unstake))
829- then $Tuple2(nil, currentStaked)
830- else throw("Strict value is not equal to itself.")
831- }
832-
833-
834-
835-@Callable(i)
836-func state () = $Tuple2(nil, ((("maxIssueBull = " + toString(maxIssue(BULL))) + ", maxIssueBear = ") + toString(maxIssue(BEAR))))
90+@Callable(inv)
91+func requestIssueSl (tokenId,sl) = throw("issuing of EURUP and EURDOWN is disabled")
83792
83893
83994 @Verifier(tx)
840-func verify () = ((((if (sigVerify(tx.bodyBytes, tx.proofs[0], fromBase58String(admins[0])))
841- then 1
842- else 0) + (if (sigVerify(tx.bodyBytes, tx.proofs[1], fromBase58String(admins[1])))
843- then 1
844- else 0)) + (if (sigVerify(tx.bodyBytes, tx.proofs[2], fromBase58String(admins[2])))
845- then 1
846- else 0)) > 1)
95+func verify () = {
96+ let adm = ["2HHqV8W9DJayV5R6tBD2Sb8srphpoboDi7r1t1aPiumC", "5ZXe82RRASU7qshXM2J9JNYhqJ9GWYjjVq2gwUV5Naz9", "5WRXFSjwcTbNfKcJs8ZqXmSSWYsSVJUtMvMqZj5hH4Nc"]
97+ ((((if (sigVerify(tx.bodyBytes, tx.proofs[0], fromBase58String(adm[0])))
98+ then 1
99+ else 0) + (if (sigVerify(tx.bodyBytes, tx.proofs[1], fromBase58String(adm[1])))
100+ then 1
101+ else 0)) + (if (sigVerify(tx.bodyBytes, tx.proofs[2], fromBase58String(adm[2])))
102+ then 1
103+ else 0)) > 1)
104+ }
847105
Full:
OldNewDifferences
1-{-# STDLIB_VERSION 5 #-}
1+{-# STDLIB_VERSION 4 #-}
22 {-# SCRIPT_TYPE ACCOUNT #-}
33 {-# CONTENT_TYPE DAPP #-}
4-func SE (k,v) = StringEntry(k, v)
5-
6-
7-func IE (k,v) = IntegerEntry(k, v)
8-
9-
10-func ST (a,amt,t) = ScriptTransfer(a, amt, t)
11-
12-
13-let ten6 = 1000000
14-
15-let ten8 = 100000000
16-
17-let MAX = 9223372036854775807
18-
19-let configProviderKey = "configProvider"
20-
21-let configProvider = match getString(this, configProviderKey) {
22- case s: String =>
23- addressFromStringValue(s)
24- case _ =>
25- this
26-}
27-
284 func localI (k,e) = valueOrErrorMessage(getInteger(this, k), e)
295
306
317 func localS (k,e) = valueOrErrorMessage(getString(this, k), e)
32-
33-
34-func confI (k,e) = valueOrErrorMessage(getInteger(configProvider, k), e)
35-
36-
37-func confS (k,e) = valueOrErrorMessage(getString(configProvider, k), e)
388
399
4010 let BULLK = "BULLId"
4111
4212 let BEARK = "BEARId"
4313
4414 let USDNK = "mainTokenId"
4515
4616 let BULLCOLK = "bullCollateral"
4717
4818 let BEARCOLK = "bearCollateral"
4919
5020 let BULLCIRCK = "bullCirculation"
5121
5222 let BEARCIRCK = "bearCirculation"
5323
54-let ISSPERCK = "issuePercentile"
55-
56-let REDPERCK = "redeemPercentile"
57-
58-let MINISSK = "minIssue"
59-
60-let MINREDK = "minRedeem"
61-
62-let MINPOOLK = "minPool"
63-
6424 let FEEACCK = "feesAccumulated"
65-
66-let WLISTK = "issueWhiteList"
67-
68-let REBPERCK = "rebalancePercentile"
69-
70-let REBIDXK = "lastSettlementPriceId"
71-
72-let HEADK = "headPointer"
73-
74-let TAILK = "tailPointer"
75-
76-let QSIZEK = "queueSize"
77-
78-let POOLUSDNK = "poolMainTokenValue"
79-
80-let POOLUPK = "poolUp"
81-
82-let POOLDWNK = "poolDwn"
83-
84-let POOLCIRCK = "poolTokenCirculation"
85-
86-let POOLK = "poolToken"
87-
88-let LEVK = "leverage"
89-
90-let WAVESFEEK = "wavesPacemakerFee"
91-
92-let USDNFEEK = "usdnPacemakerFee"
93-
94-let oraclev2Key = "oraclev2"
95-
96-let assetidv2Key = "assetidv2"
97-
98-let oraclev2 = valueOrErrorMessage(getString(this, oraclev2Key), "no oraclev2 key")
99-
100-let assetidv2 = valueOrErrorMessage(getString(this, assetidv2Key), "no assetidv2")
101-
102-let maxDeviation = valueOrElse(getInteger(configProvider, (toString(this) + "_deviationPercentile")), 0)
103-
104-let lastPriceIndexKey = ("%s%s__idxCurrent__" + assetidv2)
105-
106-let priceIndexPrefix = (("%s%s%d__idx2Height__" + assetidv2) + "__")
107-
108-let priceHeightPrefix = (("%s%s%d__priceByHeight__" + assetidv2) + "__")
109-
110-let minUsdnFee = valueOrElse(getInteger(configProvider, USDNFEEK), 0)
111-
112-let minWavesFee = valueOrElse(getInteger(configProvider, WAVESFEEK), 0)
11325
11426 let bullCol = localI(BULLCOLK, "no 0")
11527
11628 let bearCol = localI(BEARCOLK, "no 1")
11729
11830 let bullCirc = localI(BULLCIRCK, "no 2")
11931
12032 let bearCirc = localI(BEARCIRCK, "no 3")
12133
12234 let BULL = localS(BULLK, "no 14")
12335
12436 let BEAR = localS(BEARK, "no 15")
12537
12638 let mainToken = localS(USDNK, "no 16")
12739
128-let issuePercentile = confI(ISSPERCK, "no 4")
40+let feeAddrKey = "feeAddress"
12941
130-let redeemPercentile = confI(REDPERCK, "no 5")
42+let feeAddress = valueOrErrorMessage(addressFromString(valueOrErrorMessage(getString(this, feeAddrKey), "no fee address")), "bad feeAddress")
13143
132-let minIssue = confI(MINISSK, "no 6")
133-
134-let minRedeem = confI(MINREDK, "no 7")
135-
136-let minPool = confI(MINPOOLK, "no 8")
137-
138-let rebalancePercentile = valueOrElse(getInteger(configProvider, ((toString(this) + "_") + REBPERCK)), 0)
139-
140-let whitelist = confS(WLISTK, "no 9")
141-
142-func allowed (a) = if ((whitelist == ""))
143- then true
144- else isDefined(indexOf(whitelist, toString(a)))
44+func requestIssueInternal (inv,tokenId,minPayout) = throw("issuing of EURUP and EURDOWN is disabled")
14545
14646
147-let poolMain = localI(POOLUSDNK, "no")
148-
149-let poolUp = localI(POOLUPK, "no 10")
150-
151-let poolDwn = localI(POOLDWNK, "no 11")
152-
153-let poolToken = localS(POOLK, "no 12")
154-
155-let poolTokenCirculation = localI(POOLCIRCK, "no 13")
156-
157-let poolBullExposure = fraction(bullCol, poolUp, bullCirc)
158-
159-let poolBearExposure = fraction(bearCol, poolDwn, bearCirc)
160-
161-let poolValue = ((poolMain + poolBullExposure) + poolBearExposure)
162-
163-let oracle = valueOrErrorMessage(addressFromString(oraclev2), "bad oracle address")
164-
165-let closeInterval = confI("closeInterval", "no CI")
166-
167-let settledPriceIndex = valueOrErrorMessage(getInteger(this, REBIDXK), "no last rebalance price")
168-
169-let oraclePriceIndex = valueOrErrorMessage(getInteger(oracle, lastPriceIndexKey), ((("bad oracle data at " + toString(oracle)) + ": no integer at ") + lastPriceIndexKey))
170-
171-let leverage = valueOrElse(getInteger(this, LEVK), 3)
172-
173-func heightByIndex (priceIndex) = valueOrErrorMessage(getInteger(oracle, (priceIndexPrefix + toString(priceIndex))), ("no data at index " + toString(priceIndex)))
174-
175-
176-func priceByHeight (priceHeight) = valueOrErrorMessage(getInteger(oracle, (priceHeightPrefix + toString(priceHeight))), ("no data for height " + toString(priceHeight)))
177-
178-
179-func priceByIndex (priceIndex) = priceByHeight(heightByIndex(priceIndex))
180-
181-
182-let queueSize = valueOrElse(getInteger(this, QSIZEK), 0)
183-
184-let headPointer = valueOrElse(getString(this, HEADK), "")
185-
186-let tailPointer = valueOrElse(getString(this, TAILK), "")
187-
188-let feesAccumulated = valueOrElse(getInteger(this, FEEACCK), 0)
189-
190-let queueEmpty = (headPointer == "")
191-
192-let queuePriceIndex = if (queueEmpty)
193- then MAX
194- else {
195- let data = split(localS(headPointer, "settle2: bad head pointer"), "|")
196- parseIntValue(data[3])
197- }
198-
199-let settledPrice = priceByIndex(settledPriceIndex)
200-
201-let ISSUE = "ISSUE"
202-
203-let REDEEM = "REDEEM"
204-
205-let POOL = "POOL"
206-
207-let UNPOOL = "UNPOOL"
208-
209-let feeAddrKey = "feeAddress"
210-
211-let stakingAddrKey = "stakingAddress"
212-
213-let daemonPubKeyKey = "daemonPublicKey"
214-
215-let feeAddress = valueOrErrorMessage(addressFromString(confS(feeAddrKey, "no feeAddress")), "bad feeAddress")
216-
217-let stakingAddress = confS(stakingAddrKey, "no stakingAddress")
218-
219-let daemonPublicKey = fromBase58String(confS(daemonPubKeyKey, "no daemonPublicKey"))
220-
221-let rpdAddress = valueOrErrorMessage(addressFromString("3PNikM6yp4NqcSU8guxQtmR5onr2D4e8yTJ"), "bad rpd address")
222-
223-let admins = ["2HHqV8W9DJayV5R6tBD2Sb8srphpoboDi7r1t1aPiumC", "5ZXe82RRASU7qshXM2J9JNYhqJ9GWYjjVq2gwUV5Naz9", "5WRXFSjwcTbNfKcJs8ZqXmSSWYsSVJUtMvMqZj5hH4Nc"]
224-
225-func buildNewItem (action,amt,token,priceIndex,invoker,minPayout,maxPayout) = (((((((((((((action + "|") + toString(amt)) + "|") + token) + "|") + toString(priceIndex)) + "|") + invoker) + "|") + toString(minPayout)) + "|") + toString(maxPayout)) + "|")
226-
227-
228-func diffAbs (a1,a2) = {
229- let diff = (a1 - a2)
230- if ((diff > 0))
231- then diff
232- else (0 - diff)
47+func requestRedeemInternal (inv) = {
48+ let removedTokens = inv.payments[0].amount
49+ let tokenId = toBase58String(value(inv.payments[0].assetId))
50+ let invoker = inv.caller
51+ if ((tokenId == BULL))
52+ then {
53+ let payout = fraction(bullCol, removedTokens, bullCirc)
54+[IntegerEntry(BULLCOLK, (bullCol - payout)), IntegerEntry(BULLCIRCK, (bullCirc - removedTokens)), ScriptTransfer(invoker, payout, fromBase58String(mainToken))]
55+ }
56+ else if ((tokenId == BEAR))
57+ then {
58+ let payout = fraction(bearCol, removedTokens, bearCirc)
59+[IntegerEntry(BEARCOLK, (bearCol - payout)), IntegerEntry(BEARCIRCK, (bearCirc - removedTokens)), ScriptTransfer(invoker, payout, fromBase58String(mainToken))]
60+ }
61+ else throw("bad token attached")
23362 }
23463
23564
236-func userDiffAbs () = diffAbs((bullCol - poolBullExposure), (bearCol - poolBearExposure))
237-
238-
239-func maxIssue (tokenId) = {
240- let poolInvestment = if ((poolUp > 0))
241- then BULL
242- else BEAR
243- if ((tokenId != poolInvestment))
244- then poolMain
245- else (userDiffAbs() + poolValue)
246- }
247-
248-
249-func validatePMFee (i,minPayout) = if ((0 > minPayout))
250- then throw("negative min payout")
251- else {
252- let p = i.payments[1]
253- let ok = match p.assetId {
254- case bv: ByteVector =>
255- if ((toBase58String(bv) == mainToken))
256- then (p.amount >= minUsdnFee)
257- else false
258- case waves: Unit =>
259- (p.amount >= minWavesFee)
260- case _ =>
261- throw("Match error")
262- }
263- if (!(ok))
264- then throw("incorrect pacemaker fee")
265- else true
266- }
267-
268-
269-func enqueue (id,action,amt,token,priceIndex,invoker,minPayout,maxPayout) = if (!(valueOrElse(getBoolean(this, "enableEnqueue"), true)))
270- then throw("enqueue paused")
271- else {
272- let increaseQueueSize = IE(QSIZEK, (queueSize + 1))
273- let itm = buildNewItem(action, amt, token, priceIndex, invoker, minPayout, maxPayout)
274- if ((queueSize == 0))
275- then [SE(HEADK, id), SE(TAILK, id), SE(id, itm), increaseQueueSize]
276- else {
277- let prevId = localS(TAILK, "can't get tail pointer")
278- let prevItm = localS(prevId, "can't resolve pointer")
279- let updatedPrevItm = (prevItm + id)
280-[SE(prevId, updatedPrevItm), SE(id, itm), SE(TAILK, id), increaseQueueSize]
281- }
282- }
283-
284-
285-func poolSupport (curBullCol0,curBearCol0,curBullCirc0,curBearCirc0,curPoolMain0,curPoolUp0,curPoolDwn0,steps) = {
286- func closeUp (c1,c2,a0,a1,c0,pu,pd) = {
287- let diff = (c1 - c2)
288- let exp = fraction(c1, pu, a0)
289- let liquidatedTokens = if ((diff > exp))
290- then pu
291- else fraction(diff, a0, c1)
292- let liquidatedValue = if ((diff > exp))
293- then exp
294- else fraction(liquidatedTokens, c1, a0)
295- $Tuple7((c1 - liquidatedValue), c2, (a0 - liquidatedTokens), a1, (c0 + liquidatedValue), (pu - liquidatedTokens), pd)
296- }
297-
298- func closeDwn (c1,c2,a0,a1,c0,pu,pd) = {
299- let diff = (c2 - c1)
300- let exp = fraction(c2, pd, a1)
301- let liquidatedTokens = if ((diff > exp))
302- then pd
303- else fraction(diff, a1, c2)
304- let liquidatedValue = if ((diff > exp))
305- then exp
306- else fraction(liquidatedTokens, c2, a1)
307- $Tuple7(c1, (c2 - liquidatedValue), a0, (a1 - liquidatedTokens), (c0 + liquidatedValue), pu, (pd - liquidatedTokens))
308- }
309-
310- func openDwn (c1,c2,a0,a1,c0,pu,pd) = {
311- let diff = (c1 - c2)
312- let spentPoolValue = if ((c0 > diff))
313- then diff
314- else c0
315- let acquiredTokens = fraction(spentPoolValue, a1, c2)
316- $Tuple7(c1, (c2 + spentPoolValue), a0, (a1 + acquiredTokens), (c0 - spentPoolValue), pu, (pd + acquiredTokens))
317- }
318-
319- func openUp (c1,c2,a0,a1,c0,pu,pd) = {
320- let diff = (c2 - c1)
321- let spentPoolValue = if ((c0 > diff))
322- then diff
323- else c0
324- let acquiredTokens = fraction(spentPoolValue, a0, c1)
325- $Tuple7((c1 + spentPoolValue), c2, (a0 + acquiredTokens), a1, (c0 - spentPoolValue), (pu + acquiredTokens), pd)
326- }
327-
328- let $t0925510290 = if ((curBullCol0 > curBearCol0))
329- then {
330- let afterCloseUp = closeUp(curBullCol0, curBearCol0, curBullCirc0, curBearCirc0, curPoolMain0, curPoolUp0, curPoolDwn0)
331- let $t094819618 = afterCloseUp
332- let a = $t094819618._1
333- let b = $t094819618._2
334- let c = $t094819618._3
335- let d = $t094819618._4
336- let e = $t094819618._5
337- let f = $t094819618._6
338- let g = $t094819618._7
339- if ((f > 0))
340- then afterCloseUp
341- else if ((f == 0))
342- then openDwn(a, b, c, d, e, f, g)
343- else throw("poolUp < 0")
344- }
345- else {
346- let afterCloseDwn = closeDwn(curBullCol0, curBearCol0, curBullCirc0, curBearCirc0, curPoolMain0, curPoolUp0, curPoolDwn0)
347- let $t0996910109 = afterCloseDwn
348- let a = $t0996910109._1
349- let b = $t0996910109._2
350- let c = $t0996910109._3
351- let d = $t0996910109._4
352- let e = $t0996910109._5
353- let f = $t0996910109._6
354- let g = $t0996910109._7
355- if ((g > 0))
356- then afterCloseDwn
357- else if ((g == 0))
358- then openUp(a, b, c, d, e, f, g)
359- else throw("poolDwn < 0")
360- }
361- let c1 = $t0925510290._1
362- let c2 = $t0925510290._2
363- let a0 = $t0925510290._3
364- let a1 = $t0925510290._4
365- let c0 = $t0925510290._5
366- let pu = $t0925510290._6
367- let pd = $t0925510290._7
368- let charge = fraction(userDiffAbs(), (rebalancePercentile * steps), ((1440 * 100) * 100))
369- let c1SplitP = if ((pd > 0))
370- then charge
371- else 0
372- let c2SplitP = if ((pu > 0))
373- then charge
374- else 0
375- $Tuple7((c1 - c1SplitP), (c2 - c2SplitP), a0, a1, ((c0 + c1SplitP) + c2SplitP), pu, pd)
376- }
377-
378-
379-func poolSup (curBullCol0,curBearCol0,curBullCirc0,curBearCirc0) = {
380- let $t01071510900 = poolSupport(curBullCol0, curBearCol0, curBullCirc0, curBearCirc0, poolMain, poolUp, poolDwn, 0)
381- let bullCol1 = $t01071510900._1
382- let bearCol1 = $t01071510900._2
383- let bullCic1 = $t01071510900._3
384- let bearCirc1 = $t01071510900._4
385- let poolMain1 = $t01071510900._5
386- let poolUp1 = $t01071510900._6
387- let poolDwn1 = $t01071510900._7
388-[IE(BULLCOLK, bullCol1), IE(BULLCIRCK, bullCic1), IE(BEARCOLK, bearCol1), IE(BEARCIRCK, bearCirc1), IE(POOLUSDNK, poolMain1), IE(POOLUPK, poolUp1), IE(POOLDWNK, poolDwn1)]
389- }
390-
391-
392-func dequeue () = {
393- func sp (a,mx) = if ((mx >= a))
394- then $Tuple2(a, 0)
395- else $Tuple2(mx, (a - mx))
396-
397- if ((queueSize == 0))
398- then throw("nothing to settle")
399- else {
400- func collectFee (fees) = IE(FEEACCK, (feesAccumulated + fees))
401-
402- let decreaseQueueSize = IE(QSIZEK, (queueSize - 1))
403- let isLastElement = (headPointer == tailPointer)
404- let overwriteTail = SE(TAILK, "")
405- let dataStr = localS(headPointer, "bad head pointer(dequeue)")
406- let data = split(dataStr, "|")
407- let action = data[0]
408- let amt = parseIntValue(data[1])
409- let token = data[2]
410- let priceIndex = parseIntValue(data[3])
411- let invoker = addressFromStringValue(data[4])
412- let minPayout = if ((8 > size(data)))
413- then 0
414- else parseIntValue(data[5])
415- let maxPayout = if ((8 > size(data)))
416- then MAX
417- else parseIntValue(data[6])
418- let next = data[(size(data) - 1)]
419- func payback (tkn) = [SE(HEADK, next), decreaseQueueSize, ST(invoker, amt, fromBase58String(tkn))]
420-
421- let items = if ((settledPriceIndex != priceIndex))
422- then throw(((("corrupt state, settledPriceIndex=" + toString(settledPriceIndex)) + ", request price id=") + toString(priceIndex)))
423- else if ((action == ISSUE))
424- then {
425- let feeSize = fraction(amt, issuePercentile, 10000)
426- let addedCollateral = (amt - feeSize)
427- let defaultAddedTokens = if ((token == BULL))
428- then fraction(bullCirc, addedCollateral, bullCol)
429- else if ((token == BEAR))
430- then fraction(bearCirc, addedCollateral, bearCol)
431- else throw("bad token id")
432- let $t01291212985 = sp(defaultAddedTokens, maxPayout)
433- let addedToCirculation = $t01291212985._1
434- let extraTokens = $t01291212985._2
435- let $t01300213173 = if ((token == BULL))
436- then $Tuple4(defaultAddedTokens, addedCollateral, 0, 0)
437- else $Tuple4(0, 0, defaultAddedTokens, addedCollateral)
438- let plusBulls = $t01300213173._1
439- let plusBullCol = $t01300213173._2
440- let plusBears = $t01300213173._3
441- let plusBearCol = $t01300213173._4
442- if ((minPayout > addedToCirculation))
443- then payback(mainToken)
444- else (poolSup((bullCol + plusBullCol), (bearCol + plusBearCol), (bullCirc + plusBulls), (bearCirc + plusBears)) ++ [SE(HEADK, next), collectFee(feeSize), decreaseQueueSize, ST(invoker, addedToCirculation, fromBase58String(token)), ST(feeAddress, extraTokens, fromBase58String(token))])
445- }
446- else if ((action == REDEEM))
447- then {
448- let removedTokens = amt
449- let estimatedPayout = if ((token == BULL))
450- then fraction(bullCol, removedTokens, bullCirc)
451- else if ((token == BEAR))
452- then fraction(bearCol, removedTokens, bearCirc)
453- else throw("bad token id")
454- let feeSize = fraction(estimatedPayout, redeemPercentile, 10000)
455- let defaultPayout = (estimatedPayout - feeSize)
456- let $t01424014294 = sp(defaultPayout, maxPayout)
457- let userPayout = $t01424014294._1
458- let extra = $t01424014294._2
459- let $t01431114476 = if ((token == BULL))
460- then $Tuple4(removedTokens, estimatedPayout, 0, 0)
461- else $Tuple4(0, 0, removedTokens, estimatedPayout)
462- let minusBulls = $t01431114476._1
463- let minusBullCol = $t01431114476._2
464- let minusBears = $t01431114476._3
465- let minusBearCol = $t01431114476._4
466- if ((minPayout > userPayout))
467- then payback(token)
468- else (poolSup((bullCol - minusBullCol), (bearCol - minusBearCol), (bullCirc - minusBulls), (bearCirc - minusBears)) ++ [SE(HEADK, next), collectFee(feeSize), decreaseQueueSize, ST(invoker, userPayout, fromBase58String(mainToken)), ST(feeAddress, extra, fromBase58String(mainToken))])
469- }
470- else if ((action == POOL))
471- then {
472- let issueTokens = fraction(poolTokenCirculation, amt, poolValue)
473- if ((minPayout > issueTokens))
474- then payback(mainToken)
475- else [IE(POOLUSDNK, (poolMain + amt)), IE(POOLCIRCK, (poolTokenCirculation + issueTokens)), SE(HEADK, next), decreaseQueueSize, ST(invoker, issueTokens, fromBase58String(poolToken))]
476- }
477- else if ((action == UNPOOL))
478- then {
479- func share (a) = fraction(a, amt, poolTokenCirculation)
480-
481- let unpooledMain = share(poolMain)
482- let unpooledUp = share(poolUp)
483- let unpooledDwn = share(poolDwn)
484- let unpooledUpValue = fraction(unpooledUp, bullCol, bullCirc)
485- let unpooledDwnValue = fraction(unpooledDwn, bearCol, bearCirc)
486- let totalUnpooledValue = ((unpooledMain + unpooledUpValue) + unpooledDwnValue)
487- if ((minPayout > totalUnpooledValue))
488- then payback(poolToken)
489- else [IE(POOLUSDNK, (poolMain - unpooledMain)), IE(POOLCIRCK, (poolTokenCirculation - amt)), IE(POOLUPK, (poolUp - unpooledUp)), IE(POOLDWNK, (poolDwn - unpooledDwn)), IE(BULLCIRCK, (bullCirc - unpooledUp)), IE(BEARCIRCK, (bearCirc - unpooledDwn)), IE(BULLCOLK, (bullCol - unpooledUpValue)), IE(BEARCOLK, (bearCol - unpooledDwnValue)), SE(HEADK, next), decreaseQueueSize, ST(invoker, totalUnpooledValue, fromBase58String(mainToken))]
490- }
491- else throw(("bad action: " + action))
492- if (isLastElement)
493- then overwriteTail :: items
494- else items
495- }
496- }
497-
498-
499-func rebalance (offset) = if (!(valueOrElse(getBoolean(this, "enableRebalance"), true)))
500- then throw("rebalance paused")
501- else if ((offset == 0))
502- then throw("[OK] offset=0")
503- else {
504- func LV (v,p0,p1,m) = {
505- let denom = {
506- let md = (((2 * (if ((p1 > p0))
507- then p1
508- else p0)) * m) / 3037000499)
509- if ((10 > md))
510- then 10
511- else if ((100 > md))
512- then 100
513- else 1000
514- }
515- let pmax = ((if ((p1 > p0))
516- then p1
517- else p0) / denom)
518- let pmin = ((if ((p0 > p1))
519- then p1
520- else p0) / denom)
521- let a = (pmin * pmin)
522- let b = ((((m * m) * pmax) * pmax) - (((((2 * m) * m) - m) * pmax) * pmin))
523- let ma = ((m * m) - m)
524- fraction(v, ((ma * a) + b), (((ma + 1) * a) + b))
525- }
526-
527- let unsettledPriceIndex = (settledPriceIndex + offset)
528- let nextPrice = priceByIndex(unsettledPriceIndex)
529- let minVol = if ((bearCol > bullCol))
530- then bullCol
531- else bearCol
532- let redist = LV(minVol, settledPrice, nextPrice, leverage)
533- let bullsEarn = (nextPrice > settledPrice)
534- let newBullCol = if (bullsEarn)
535- then (bullCol + redist)
536- else (bullCol - redist)
537- let newBearCol = if (bullsEarn)
538- then (bearCol - redist)
539- else (bearCol + redist)
540- let $t01827018468 = poolSupport(newBullCol, newBearCol, bullCirc, bearCirc, poolMain, poolUp, poolDwn, offset)
541- let updBullCol = $t01827018468._1
542- let updBearCol = $t01827018468._2
543- let updBullCirc = $t01827018468._3
544- let updBearCirc = $t01827018468._4
545- let updPoolMain = $t01827018468._5
546- let updPoolUp = $t01827018468._6
547- let updPoolDwn = $t01827018468._7
548-[IE(BULLCOLK, updBullCol), IE(BEARCOLK, updBearCol), IE(BULLCIRCK, updBullCirc), IE(BEARCIRCK, updBearCirc), IE(POOLUSDNK, updPoolMain), IE(POOLUPK, updPoolUp), IE(POOLDWNK, updPoolDwn), IE(REBIDXK, unsettledPriceIndex)]
549- }
550-
551-
552-func calcMax (min,avg) = if ((min > avg))
553- then throw(((("price too old: minPayout " + toString(min)) + " > avg = ") + toString(avg)))
554- else ((avg + avg) - min)
555-
556-
55765 @Callable(i)
558-func enableEnqueue (arg) = if ((i.caller != feeAddress))
559- then throw("only admin can")
560- else [BooleanEntry("enableEnqueue", arg)]
561-
562-
563-
564-@Callable(i)
565-func enableRebalance (arg) = if ((i.caller != feeAddress))
566- then throw("only admin can")
567- else [BooleanEntry("enableRebalance", arg)]
568-
569-
570-
571-@Callable(i)
572-func withdrawFee (amount) = if ((amount > feesAccumulated))
573- then throw(("too much. available: " + toString(feesAccumulated)))
574- else [IE(FEEACCK, (feesAccumulated - amount)), ST(feeAddress, amount, fromBase58String(mainToken))]
575-
576-
577-
578-@Callable(inv)
579-func requestRedeemSl (minPayout) = if (validatePMFee(inv, minPayout))
580- then {
581- let amt = inv.payments[0].amount
582- let tokenId = toBase58String(valueOrErrorMessage(inv.payments[0].assetId, "bad token att"))
583- if (if ((tokenId != BULL))
584- then (tokenId != BEAR)
585- else false)
586- then throw("bad token req")
587- else {
588- let $t01994220081 = if ((tokenId == BULL))
589- then $Tuple2(bullCol, bullCirc)
590- else if ((tokenId == BEAR))
591- then $Tuple2(bearCol, bearCirc)
592- else throw("bad token req")
593- let col = $t01994220081._1
594- let circ = $t01994220081._2
595- let est = fraction(amt, col, circ)
596- let $t02012920223 = if ((minPayout == 0))
597- then $Tuple2(0, MAX)
598- else $Tuple2(minPayout, calcMax(minPayout, est))
599- let minP = $t02012920223._1
600- let maxP = $t02012920223._2
601- if ((inv.caller == this))
602- then throw("can't do")
603- else {
604- let estimated = fraction(col, amt, circ)
605- if ((minRedeem > estimated))
606- then throw((("Attached payment too small. Min redeem amount is " + toString((minRedeem / 1000000))) + " USDN"))
607- else enqueue(toBase58String(inv.transactionId), REDEEM, amt, tokenId, (oraclePriceIndex + 1), toString(inv.caller), minP, maxP)
608- }
609- }
610- }
611- else throw()
612-
613-
614-
615-@Callable(inv)
616-func requestIssueSl (tokenId,minPayout) = if (validatePMFee(inv, minPayout))
617- then if (if ((tokenId != BULL))
618- then (tokenId != BEAR)
619- else false)
620- then throw("bad token req")
621- else if (!(allowed(inv.caller)))
622- then throw("only whitelisted can do")
623- else if ((inv.payments[0].assetId != fromBase58String(mainToken)))
624- then throw("bad token att")
625- else {
626- let amt = inv.payments[0].amount
627- let $t02114621327 = if ((tokenId == BULL))
628- then $Tuple2(bullCol, bullCirc)
629- else if ((tokenId == BEAR))
630- then $Tuple2(bearCol, bearCirc)
631- else throw("bad token req")
632- let col = $t02114621327._1
633- let circ = $t02114621327._2
634- let est = fraction(amt, circ, col)
635- let $t02136921463 = if ((minPayout == 0))
636- then $Tuple2(0, MAX)
637- else $Tuple2(minPayout, calcMax(minPayout, est))
638- let minP = $t02136921463._1
639- let maxP = $t02136921463._2
640- if ((minIssue > amt))
641- then throw((("Attached payment too small. Min required: " + toString((minIssue / 1000000))) + " USDN"))
642- else {
643- let maxAllowed = maxIssue(tokenId)
644- if (if ((whitelist == ""))
645- then (inv.payments[0].amount > maxAllowed)
646- else false)
647- then throw((("trying to issue more than pool can handle. Max attachment allowed = " + toString((maxAllowed / 1000000))) + " USDN"))
648- else enqueue(toBase58String(inv.transactionId), ISSUE, amt, tokenId, (oraclePriceIndex + 1), toString(inv.caller), minP, maxP)
649- }
650- }
651- else throw()
652-
653-
654-
655-@Callable(inv)
656-func requestPool () = if (!(allowed(inv.caller)))
657- then throw("only whitelisted can do")
658- else {
659- let pmt = inv.payments[0]
660- if ((pmt.assetId != fromBase58String(mainToken)))
661- then throw("USDN required")
662- else if ((minPool > pmt.amount))
663- then throw(("pool min: " + toString(minPool)))
664- else {
665- let estimate = fraction(poolTokenCirculation, pmt.amount, poolValue)
666- enqueue(toBase58String(inv.transactionId), POOL, inv.payments[0].amount, "", (oraclePriceIndex + 1), toString(inv.caller), 0, MAX)
667- }
668- }
669-
670-
671-
672-@Callable(inv)
673-func requestUnpool () = {
674- let pmt = inv.payments[0]
675- if ((pmt.assetId != fromBase58String(poolToken)))
676- then throw("bad token att")
677- else {
678- let estimate = fraction(poolValue, pmt.amount, poolTokenCirculation)
679- if ((minPool > estimate))
680- then throw(((("unpool at least for" + toString(minPool)) + " ") + mainToken))
681- else enqueue(toBase58String(inv.transactionId), UNPOOL, inv.payments[0].amount, "", (oraclePriceIndex + 1), toString(inv.caller), 0, MAX)
682- }
66+func withdrawFee (amount) = {
67+ let acc = valueOrErrorMessage(getInteger(this, FEEACCK), "no fees acc")
68+ if ((amount > acc))
69+ then throw(("too much. available: " + toString(acc)))
70+ else [IntegerEntry(FEEACCK, (acc - amount)), ScriptTransfer(feeAddress, amount, fromBase58String(mainToken))]
68371 }
68472
68573
68674
68775 @Callable(inv)
688-func settle () = {
689- let canRebalance = (oraclePriceIndex > settledPriceIndex)
690- if (queueEmpty)
691- then if (canRebalance)
692- then rebalance(1)
693- else throw("[OK] all done, carry on")
694- else if ((queuePriceIndex > settledPriceIndex))
695- then if (canRebalance)
696- then rebalance(1)
697- else throw("[OK] need to wait")
698- else if ((queuePriceIndex == settledPriceIndex))
699- then dequeue()
700- else throw("future price already rebalanced")
701- }
76+func requestRedeem () = requestRedeemInternal(inv)
70277
70378
70479
70580 @Callable(inv)
706-func settle2 () = {
707- func hasDeviation (priceIndex) = {
708- let p = priceByIndex(priceIndex)
709- (fraction(diffAbs(settledPrice, p), 1000000, settledPrice) >= maxDeviation)
710- }
711-
712- let maxPossibleShift = (min([queuePriceIndex, oraclePriceIndex]) - settledPriceIndex)
713- func f (acc,shift) = if (if (acc._2)
714- then true
715- else (shift > maxPossibleShift))
716- then acc
717- else $Tuple2(shift, if ((shift == 7))
718- then true
719- else hasDeviation((settledPriceIndex + shift)))
720-
721- let $t02447524574 = {
722- let $list2451924574 = [1, 2, 3, 4, 5, 6, 7]
723- let $size2451924574 = size($list2451924574)
724- let $acc02451924574 = $Tuple2(settledPriceIndex, false)
725- if (($size2451924574 == 0))
726- then $acc02451924574
727- else {
728- let $acc12451924574 = f($acc02451924574, $list2451924574[0])
729- if (($size2451924574 == 1))
730- then $acc12451924574
731- else {
732- let $acc22451924574 = f($acc12451924574, $list2451924574[1])
733- if (($size2451924574 == 2))
734- then $acc22451924574
735- else {
736- let $acc32451924574 = f($acc22451924574, $list2451924574[2])
737- if (($size2451924574 == 3))
738- then $acc32451924574
739- else {
740- let $acc42451924574 = f($acc32451924574, $list2451924574[3])
741- if (($size2451924574 == 4))
742- then $acc42451924574
743- else {
744- let $acc52451924574 = f($acc42451924574, $list2451924574[4])
745- if (($size2451924574 == 5))
746- then $acc52451924574
747- else {
748- let $acc62451924574 = f($acc52451924574, $list2451924574[5])
749- if (($size2451924574 == 6))
750- then $acc62451924574
751- else {
752- let $acc72451924574 = f($acc62451924574, $list2451924574[6])
753- if (($size2451924574 == 7))
754- then $acc72451924574
755- else {
756- let $acc82451924574 = f($acc72451924574, $list2451924574[7])
757- throw("List size exceed 7")
758- }
759- }
760- }
761- }
762- }
763- }
764- }
765- }
766- }
767- let okShift = $t02447524574._1
768- let forcedByDeviationOrTimeout = $t02447524574._2
769- let queueWaits = (oraclePriceIndex >= queuePriceIndex)
770- if ((settledPriceIndex > queuePriceIndex))
771- then throw("settle2: future price already settled/rebalanced")
772- else if ((settledPriceIndex == queuePriceIndex))
773- then dequeue()
774- else if (if ((okShift > 0))
775- then if (forcedByDeviationOrTimeout)
776- then true
777- else queueWaits
778- else false)
779- then rebalance(okShift)
780- else throw(("settle2: [OK] all done, carry on. available shift: " + toString(okShift)))
781- }
81+func requestRedeemSl (sl) = requestRedeemInternal(inv)
78282
78383
78484
78585 @Callable(inv)
786-func settle3 () = {
787- let maxPossibleShift = (min([queuePriceIndex, oraclePriceIndex]) - settledPriceIndex)
788- let okShift = min([maxPossibleShift, 7])
789- if ((settledPriceIndex > queuePriceIndex))
790- then throw("settle3: future price already settled/rebalanced")
791- else if ((settledPriceIndex == queuePriceIndex))
792- then dequeue()
793- else if ((okShift > 0))
794- then rebalance(okShift)
795- else nil
796- }
86+func requestIssue (tokenId) = throw("issuing of EURUP and EURDOWN is disabled")
79787
79888
79989
800-@Callable(i)
801-func stakeAll () = if (if ((i.caller != this))
802- then (i.caller != addressFromStringValue(getStringValue(this, "pacewrapper")))
803- else false)
804- then throw("forbidden")
805- else {
806- let tkn = fromBase58String(mainToken)
807- let currentBalance = assetBalance(this, tkn)
808- let stake = if ((currentBalance > 0))
809- then invoke(rpdAddress, "lockNeutrinoSP", [toString(feeAddress), 100], [AttachedPayment(tkn, currentBalance)])
810- else unit
811- if ((stake == stake))
812- then $Tuple2(nil, currentBalance)
813- else throw("Strict value is not equal to itself.")
814- }
815-
816-
817-
818-@Callable(i)
819-func unstakeAll () = if (if ((i.caller != this))
820- then (i.caller != addressFromStringValue(getStringValue(this, "pacewrapper")))
821- else false)
822- then throw("forbidden")
823- else {
824- let currentStaked = valueOrElse(getInteger(rpdAddress, ((("rpd_balance_" + mainToken) + "_") + toString(this))), 0)
825- let unstake = if ((currentStaked > 0))
826- then invoke(rpdAddress, "unlockNeutrino", [currentStaked, mainToken], nil)
827- else unit
828- if ((unstake == unstake))
829- then $Tuple2(nil, currentStaked)
830- else throw("Strict value is not equal to itself.")
831- }
832-
833-
834-
835-@Callable(i)
836-func state () = $Tuple2(nil, ((("maxIssueBull = " + toString(maxIssue(BULL))) + ", maxIssueBear = ") + toString(maxIssue(BEAR))))
90+@Callable(inv)
91+func requestIssueSl (tokenId,sl) = throw("issuing of EURUP and EURDOWN is disabled")
83792
83893
83994 @Verifier(tx)
840-func verify () = ((((if (sigVerify(tx.bodyBytes, tx.proofs[0], fromBase58String(admins[0])))
841- then 1
842- else 0) + (if (sigVerify(tx.bodyBytes, tx.proofs[1], fromBase58String(admins[1])))
843- then 1
844- else 0)) + (if (sigVerify(tx.bodyBytes, tx.proofs[2], fromBase58String(admins[2])))
845- then 1
846- else 0)) > 1)
95+func verify () = {
96+ let adm = ["2HHqV8W9DJayV5R6tBD2Sb8srphpoboDi7r1t1aPiumC", "5ZXe82RRASU7qshXM2J9JNYhqJ9GWYjjVq2gwUV5Naz9", "5WRXFSjwcTbNfKcJs8ZqXmSSWYsSVJUtMvMqZj5hH4Nc"]
97+ ((((if (sigVerify(tx.bodyBytes, tx.proofs[0], fromBase58String(adm[0])))
98+ then 1
99+ else 0) + (if (sigVerify(tx.bodyBytes, tx.proofs[1], fromBase58String(adm[1])))
100+ then 1
101+ else 0)) + (if (sigVerify(tx.bodyBytes, tx.proofs[2], fromBase58String(adm[2])))
102+ then 1
103+ else 0)) > 1)
104+ }
847105

github/deemru/w8io/786bc32 
62.72 ms