6 | | - | func asUserBalanceData (value) = match value { |
---|
7 | | - | case x: (Int, Int, Int, Int, Int, Boolean) => |
---|
8 | | - | x |
---|
9 | | - | case t => |
---|
10 | | - | throw("expected int5&boolean") |
---|
11 | | - | } |
---|
12 | | - | |
---|
13 | | - | |
---|
14 | | - | let adminStore = "admin" |
---|
15 | | - | |
---|
16 | | - | let configStore = "config" |
---|
17 | | - | |
---|
18 | | - | let reservesStore = "reserves" |
---|
19 | | - | |
---|
20 | | - | let aTokenIdStore = "aTokenId" |
---|
21 | | - | |
---|
22 | | - | let assetIdStore = "assetId" |
---|
23 | | - | |
---|
24 | | - | let admin = addressFromStringValue(getStringValue(this, adminStore)) |
---|
25 | | - | |
---|
26 | | - | let config = addressFromStringValue(getStringValue(this, configStore)) |
---|
27 | | - | |
---|
28 | | - | let reservesStr = valueOrErrorMessage(getString(this, reservesStore), "no reserves registered") |
---|
29 | | - | |
---|
30 | | - | let reserves = split(reservesStr, "|") |
---|
31 | | - | |
---|
32 | | - | func assetIdOfReserve (reserve) = valueOrErrorMessage(getString(reserve, assetIdStore), "no assetId in reserve") |
---|
33 | | - | |
---|
34 | | - | |
---|
35 | | - | func collateralFactor (reserve) = valueOrErrorMessage(getInteger(config, (assetIdOfReserve(reserve) + "_CollateralFactor")), "no CollateralFactor in config") |
---|
36 | | - | |
---|
37 | | - | |
---|
38 | | - | func liquidationThreshold (reserve) = valueOrErrorMessage(getInteger(config, (assetIdOfReserve(reserve) + "_LiquidationThreshold")), "no LiquidationThreshold in config") |
---|
39 | | - | |
---|
40 | | - | |
---|
41 | | - | func liquidationPenalty (assetId) = valueOrErrorMessage(getInteger(config, (assetId + "_LiquidationPenalty")), "no LiquidationPenalty in config") |
---|
42 | | - | |
---|
43 | | - | |
---|
44 | | - | let accountHealthThreshold = valueOrErrorMessage(getInteger(config, "account_health_threshold"), "no account_health_threshold") |
---|
45 | | - | |
---|
46 | | - | let accountHealthOverlap = valueOrErrorMessage(getInteger(config, "account_health_overlap"), "no account_health_overlap") |
---|
47 | | - | |
---|
48 | | - | let collapsePenalty = valueOrErrorMessage(getInteger(config, "collapse_penalty"), "no collapse_penalty") |
---|
49 | | - | |
---|
50 | | - | let liquidators = valueOrElse(getString(config, "liquidators"), "") |
---|
51 | | - | |
---|
52 | | - | func validateReserve (r) = if (contains(reservesStr, r)) |
---|
53 | | - | then true |
---|
54 | | - | else throw(("unknown reserve:" + r)) |
---|
55 | | - | |
---|
56 | | - | |
---|
57 | | - | func userBalance (reserve,user) = asUserBalanceData(invoke(reserve, "userBalance", [user], nil)) |
---|
58 | | - | |
---|
59 | | - | |
---|
60 | | - | func findReserveBy (store,value) = { |
---|
61 | | - | func fold (a,r) = match a { |
---|
62 | | - | case found: Address => |
---|
63 | | - | found |
---|
64 | | - | case _ => |
---|
65 | | - | let reserve = valueOrErrorMessage(addressFromString(r), "reserve bad address") |
---|
66 | | - | if ((valueOrErrorMessage(getString(reserve, store), ("reserve has no " + store)) == value)) |
---|
67 | | - | then reserve |
---|
68 | | - | else unit |
---|
69 | | - | } |
---|
70 | | - | |
---|
71 | | - | match let $l = reserves |
---|
72 | | - | let $s = size($l) |
---|
73 | | - | let $acc0 = unit |
---|
74 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
75 | | - | then $a |
---|
76 | | - | else fold($a, $l[$i]) |
---|
77 | | - | |
---|
78 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
79 | | - | then $a |
---|
80 | | - | else throw("List size exceeds 6") |
---|
81 | | - | |
---|
82 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6) { |
---|
83 | | - | case found: Address => |
---|
84 | | - | found |
---|
85 | | - | case _ => |
---|
86 | | - | throw(("unknown " + store)) |
---|
87 | | - | } |
---|
88 | | - | } |
---|
89 | | - | |
---|
90 | | - | |
---|
91 | | - | func fractionCeil (value,numerator,denominator) = { |
---|
92 | | - | let cand = fraction(value, numerator, denominator) |
---|
93 | | - | let D = 3037000499 |
---|
94 | | - | let exact = ((((cand % D) * (denominator % D)) % D) == (((value % D) * (numerator % D)) % D)) |
---|
95 | | - | if (exact) |
---|
96 | | - | then cand |
---|
97 | | - | else (cand + 1) |
---|
98 | | - | } |
---|
99 | | - | |
---|
100 | | - | |
---|
101 | | - | func userPower (user) = { |
---|
102 | | - | func fold (totals,r) = { |
---|
103 | | - | let $t028302859 = totals |
---|
104 | | - | let totalD = $t028302859._1 |
---|
105 | | - | let totalB = $t028302859._2 |
---|
106 | | - | let reserve = valueOrErrorMessage(addressFromString(r), "reserve bad address") |
---|
107 | | - | let cf = collateralFactor(reserve) |
---|
108 | | - | let lt = liquidationThreshold(reserve) |
---|
109 | | - | let $t030303118 = userBalance(reserve, user) |
---|
110 | | - | let token = $t030303118._1 |
---|
111 | | - | let asset = $t030303118._2 |
---|
112 | | - | let depositUsd = $t030303118._3 |
---|
113 | | - | let debt = $t030303118._4 |
---|
114 | | - | let debtUsd = $t030303118._5 |
---|
115 | | - | let asCollateral = $t030303118._6 |
---|
116 | | - | let effectiveDepositUsd = if (asCollateral) |
---|
117 | | - | then depositUsd |
---|
118 | | - | else 0 |
---|
119 | | - | let overlapUsd = min([debtUsd, effectiveDepositUsd]) |
---|
120 | | - | let overlapCharge = fractionCeil(overlapUsd, accountHealthOverlap, factorsBase) |
---|
121 | | - | if ((debtUsd > effectiveDepositUsd)) |
---|
122 | | - | then $Tuple2(totalD, ((totalB + fraction((debtUsd - effectiveDepositUsd), factorsBase, lt)) + overlapCharge)) |
---|
123 | | - | else $Tuple2((totalD + fraction((effectiveDepositUsd - debtUsd), cf, factorsBase)), (totalB + overlapCharge)) |
---|
124 | | - | } |
---|
125 | | - | |
---|
126 | | - | let $l = reserves |
---|
127 | | - | let $s = size($l) |
---|
128 | | - | let $acc0 = $Tuple2(0, 0) |
---|
129 | | - | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
130 | | - | then $a |
---|
131 | | - | else fold($a, $l[$i]) |
---|
132 | | - | |
---|
133 | | - | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
134 | | - | then $a |
---|
135 | | - | else throw("List size exceeds 6") |
---|
136 | | - | |
---|
137 | | - | $f0_2($f0_1($f0_1($f0_1($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3), 4), 5), 6) |
---|
138 | | - | } |
---|
139 | | - | |
---|
140 | | - | |
---|
141 | | - | func validateAfter (user,op) = { |
---|
142 | | - | let $t036723703 = userPower(user) |
---|
143 | | - | let bp = $t036723703._1 |
---|
144 | | - | let bpu = $t036723703._2 |
---|
145 | | - | let accHealth = (((bp - bpu) * factorsBase) / bp) |
---|
146 | | - | if (if ((bp == 0)) |
---|
147 | | - | then (bpu == 0) |
---|
148 | | - | else false) |
---|
149 | | - | then nil |
---|
150 | | - | else if (if ((bp == 0)) |
---|
151 | | - | then (bpu > 0) |
---|
152 | | - | else false) |
---|
153 | | - | then throw(((op + " too much: breaching liquidation threshold(bp=0, bpu=") + toString(bpu))) |
---|
154 | | - | else if ((accountHealthThreshold > accHealth)) |
---|
155 | | - | then throw((((((((op + " too much: breaching liquidation threshold(bp=") + toString(bp)) + ", bpu=") + toString(bpu)) + ", health=") + toString(accHealth)) + ")")) |
---|
156 | | - | else nil |
---|
157 | | - | } |
---|
158 | | - | |
---|
| 6 | + | let zeftAddress = Address(base58'3N3tDUvYTUkPj99XK6ahYY1seKQssSPSUxa') |
---|
161 | | - | func withdraw (assetId,amount) = { |
---|
162 | | - | let user = toString(i.caller) |
---|
163 | | - | let targetContract = findReserveBy(assetIdStore, assetId) |
---|
164 | | - | let doWithdraw = invoke(targetContract, "withdrawFor", [user, amount], nil) |
---|
165 | | - | if ((doWithdraw == doWithdraw)) |
---|
166 | | - | then validateAfter(user, "withdrawing") |
---|
167 | | - | else throw("Strict value is not equal to itself.") |
---|
168 | | - | } |
---|
169 | | - | |
---|
170 | | - | |
---|
171 | | - | |
---|
172 | | - | @Callable(i) |
---|
173 | | - | func withdraw2 (reserve,amount) = { |
---|
174 | | - | let v = validateReserve(reserve) |
---|
175 | | - | if ((v == v)) |
---|
176 | | - | then { |
---|
177 | | - | let user = toString(i.caller) |
---|
178 | | - | let targetContract = addressFromStringValue(reserve) |
---|
179 | | - | let doWithdraw = invoke(targetContract, "withdrawFor", [user, amount], nil) |
---|
180 | | - | if ((doWithdraw == doWithdraw)) |
---|
181 | | - | then validateAfter(user, "withdrawing2") |
---|
| 9 | + | func call_1 (myaddress) = { |
---|
| 10 | + | let admin = "3N3tDUvYTUkPj99XK6ahYY1seKQssSPSUxa" |
---|
| 11 | + | if ((toString(i.caller) != admin)) |
---|
| 12 | + | then throw("only admin can do") |
---|
| 13 | + | else { |
---|
| 14 | + | let result = reentrantInvoke(addressFromStringValue(myaddress), "usdn_100", nil, nil) |
---|
| 15 | + | if ((result == result)) |
---|
| 16 | + | then throw("success") |
---|