1 | | - | {-# STDLIB_VERSION 3 #-} |
---|
2 | | - | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
3 | | - | {-# CONTENT_TYPE DAPP #-} |
---|
4 | | - | let creditInterest = 15 |
---|
5 | | - | |
---|
6 | | - | @Callable(i) |
---|
7 | | - | func addHolder () = { |
---|
8 | | - | let pmt = extract(i.payment) |
---|
9 | | - | if (if (isDefined(pmt.assetId)) |
---|
10 | | - | then true |
---|
11 | | - | else (100000000 > pmt.amount)) |
---|
12 | | - | then throw("can hodl waves only at the moment") |
---|
13 | | - | else { |
---|
14 | | - | let callerAddress = toBase58String(i.caller.bytes) |
---|
15 | | - | let currentHoldersCount = match getInteger(this, "holder_count") { |
---|
16 | | - | case i1: Int => |
---|
17 | | - | i1 |
---|
18 | | - | case _ => |
---|
19 | | - | 0 |
---|
20 | | - | } |
---|
21 | | - | let dataList = match getInteger(this, (callerAddress + "_holder_index")) { |
---|
22 | | - | case hi: Int => |
---|
23 | | - | let holderAvailable = extract(getInteger(this, ("holder_available_" + toString(hi)))) |
---|
24 | | - | [DataEntry(("holder_available_" + toString(hi)), (holderAvailable + pmt.amount))] |
---|
25 | | - | case _ => |
---|
26 | | - | [DataEntry(("holder_available_" + toString(currentHoldersCount)), pmt.amount), DataEntry(("holder_credited_" + toString(currentHoldersCount)), 0), DataEntry((callerAddress + "_holder_index"), currentHoldersCount), DataEntry("holder_count", (currentHoldersCount + 1))] |
---|
27 | | - | } |
---|
28 | | - | let availableFunds = match getInteger(this, "available_funds") { |
---|
29 | | - | case i2: Int => |
---|
30 | | - | i2 |
---|
31 | | - | case _ => |
---|
32 | | - | 0 |
---|
33 | | - | } |
---|
34 | | - | WriteSet(DataEntry("available_funds", (availableFunds + pmt.amount)) :: dataList) |
---|
35 | | - | } |
---|
36 | | - | } |
---|
37 | | - | |
---|
38 | | - | |
---|
39 | | - | |
---|
40 | | - | @Callable(i) |
---|
41 | | - | func takeInvestedFunds (amount) = { |
---|
42 | | - | let callerAddress = toBase58String(i.caller.bytes) |
---|
43 | | - | let holderIndex = extract(getInteger(this, (callerAddress + "_holder_index"))) |
---|
44 | | - | let currentHolderAvailable = extract(getInteger(this, ("holder_available_" + toString(holderIndex)))) |
---|
45 | | - | let availableFunds = match getInteger(this, "available_funds") { |
---|
46 | | - | case i3: Int => |
---|
47 | | - | i3 |
---|
48 | | - | case _ => |
---|
49 | | - | 0 |
---|
50 | | - | } |
---|
51 | | - | if ((amount > currentHolderAvailable)) |
---|
52 | | - | then throw("") |
---|
53 | | - | else ScriptResult(WriteSet([DataEntry(("holder_available_" + toString(holderIndex)), (currentHolderAvailable - amount)), DataEntry("available_funds", (availableFunds - amount))]), TransferSet([ScriptTransfer(i.caller, amount, unit)])) |
---|
54 | | - | } |
---|
55 | | - | |
---|
56 | | - | |
---|
57 | | - | |
---|
58 | | - | @Callable(i) |
---|
59 | | - | func addDebitor (amount,href0,href1,href2) = { |
---|
60 | | - | let currentHoldersCount = match getInteger(this, "holder_count") { |
---|
61 | | - | case i4: Int => |
---|
62 | | - | i4 |
---|
63 | | - | case _ => |
---|
64 | | - | 0 |
---|
65 | | - | } |
---|
66 | | - | let creditId = toBase58String(i.transactionId) |
---|
67 | | - | let dataList = if (if ((href0 >= 0)) |
---|
68 | | - | then (currentHoldersCount > href0) |
---|
69 | | - | else false) |
---|
70 | | - | then { |
---|
71 | | - | let avValue0 = extract(getInteger(this, ("holder_available_" + toString(href0)))) |
---|
72 | | - | let remaining0 = amount |
---|
73 | | - | let taken0 = if ((0 >= remaining0)) |
---|
74 | | - | then 0 |
---|
75 | | - | else if ((remaining0 > avValue0)) |
---|
76 | | - | then avValue0 |
---|
77 | | - | else remaining0 |
---|
78 | | - | let counter0 = if ((taken0 > 0)) |
---|
79 | | - | then (0 + 1) |
---|
80 | | - | else 0 |
---|
81 | | - | let subList0 = if (((remaining0 - taken0) > 0)) |
---|
82 | | - | then if ((counter0 >= 3)) |
---|
83 | | - | then throw("Funds unavailable") |
---|
84 | | - | else if (if ((href1 >= 0)) |
---|
85 | | - | then (currentHoldersCount > href1) |
---|
86 | | - | else false) |
---|
87 | | - | then { |
---|
88 | | - | let avValue1 = extract(getInteger(this, ("holder_available_" + toString(href1)))) |
---|
89 | | - | let remaining1 = (remaining0 - taken0) |
---|
90 | | - | let taken1 = if ((0 >= remaining1)) |
---|
91 | | - | then 0 |
---|
92 | | - | else if ((remaining1 > avValue1)) |
---|
93 | | - | then avValue1 |
---|
94 | | - | else remaining1 |
---|
95 | | - | let counter1 = if ((taken1 > 0)) |
---|
96 | | - | then (counter0 + 1) |
---|
97 | | - | else counter0 |
---|
98 | | - | let subList1 = if (((remaining1 - taken1) > 0)) |
---|
99 | | - | then if ((counter1 >= 3)) |
---|
100 | | - | then throw("Funds unavailable") |
---|
101 | | - | else if (if ((href2 >= 0)) |
---|
102 | | - | then (currentHoldersCount > href2) |
---|
103 | | - | else false) |
---|
104 | | - | then { |
---|
105 | | - | let avValue2 = extract(getInteger(this, ("holder_available_" + toString(href2)))) |
---|
106 | | - | let remaining2 = (remaining1 - taken1) |
---|
107 | | - | let taken2 = if ((0 >= remaining2)) |
---|
108 | | - | then 0 |
---|
109 | | - | else if ((remaining2 > avValue2)) |
---|
110 | | - | then avValue2 |
---|
111 | | - | else remaining2 |
---|
112 | | - | let counter2 = if ((taken2 > 0)) |
---|
113 | | - | then (counter1 + 1) |
---|
114 | | - | else counter1 |
---|
115 | | - | let subList2 = if (((remaining2 - taken2) > 0)) |
---|
116 | | - | then if ((counter2 >= 3)) |
---|
117 | | - | then throw("Funds unavailable") |
---|
118 | | - | else nil |
---|
119 | | - | else nil |
---|
120 | | - | if ((taken2 >= (remaining2 / (3 - counter1)))) |
---|
121 | | - | then [DataEntry(("holder_available_" + toString(href2)), (avValue2 - taken2)), DataEntry(("holder_credited_" + toString(href2)), (extract(getInteger(this, ("holder_credited_" + toString(href2)))) + taken2)), DataEntry(((creditId + "_") + toString(counter1)), href2), DataEntry(((creditId + "_taken_") + toString(counter1)), taken2)] :: subList2 |
---|
122 | | - | else if (if ((2 == (currentHoldersCount - 1))) |
---|
123 | | - | then ((remaining2 - taken2) > 0) |
---|
124 | | - | else false) |
---|
125 | | - | then throw("") |
---|
126 | | - | else subList2 |
---|
127 | | - | } |
---|
128 | | - | else nil |
---|
129 | | - | else nil |
---|
130 | | - | if ((taken1 >= (remaining1 / (3 - counter0)))) |
---|
131 | | - | then [DataEntry(("holder_available_" + toString(href1)), (avValue1 - taken1)), DataEntry(("holder_credited_" + toString(href1)), (extract(getInteger(this, ("holder_credited_" + toString(href1)))) + taken1)), DataEntry(((creditId + "_") + toString(counter0)), href1), DataEntry(((creditId + "_taken_") + toString(counter0)), taken1)] :: subList1 |
---|
132 | | - | else if (if ((1 == (currentHoldersCount - 1))) |
---|
133 | | - | then ((remaining1 - taken1) > 0) |
---|
134 | | - | else false) |
---|
135 | | - | then throw("") |
---|
136 | | - | else subList1 |
---|
137 | | - | } |
---|
138 | | - | else nil |
---|
139 | | - | else nil |
---|
140 | | - | if ((taken0 >= (remaining0 / (3 - 0)))) |
---|
141 | | - | then [DataEntry(("holder_available_" + toString(href0)), (avValue0 - taken0)), DataEntry(("holder_credited_" + toString(href0)), (extract(getInteger(this, ("holder_credited_" + toString(href0)))) + taken0)), DataEntry(((creditId + "_") + toString(0)), href0), DataEntry(((creditId + "_taken_") + toString(0)), taken0)] :: subList0 |
---|
142 | | - | else if (if ((0 == (currentHoldersCount - 1))) |
---|
143 | | - | then ((remaining0 - taken0) > 0) |
---|
144 | | - | else false) |
---|
145 | | - | then throw("") |
---|
146 | | - | else subList0 |
---|
147 | | - | } |
---|
148 | | - | else nil |
---|
149 | | - | let availableFunds = match getInteger(this, "available_funds") { |
---|
150 | | - | case i5: Int => |
---|
151 | | - | i5 |
---|
152 | | - | case _ => |
---|
153 | | - | 0 |
---|
154 | | - | } |
---|
155 | | - | let creditedFunds = match getInteger(this, "credited_funds") { |
---|
156 | | - | case i6: Int => |
---|
157 | | - | i6 |
---|
158 | | - | case _ => |
---|
159 | | - | 0 |
---|
160 | | - | } |
---|
161 | | - | WriteSet([DataEntry("available_funds", (availableFunds - amount)), DataEntry("credited_funds", (creditedFunds + amount)), DataEntry((creditId + "_taker"), i.caller.bytes), DataEntry((creditId + "_amount"), amount), DataEntry((creditId + "_active"), true)] :: dataList) |
---|
162 | | - | } |
---|
163 | | - | |
---|
164 | | - | |
---|
165 | | - | |
---|
166 | | - | @Callable(i) |
---|
167 | | - | func returnCredit (creditId) = { |
---|
168 | | - | let availableFunds = match getInteger(this, "available_funds") { |
---|
169 | | - | case i7: Int => |
---|
170 | | - | i7 |
---|
171 | | - | case _ => |
---|
172 | | - | 0 |
---|
173 | | - | } |
---|
174 | | - | let creditedFunds = match getInteger(this, "credited_funds") { |
---|
175 | | - | case i8: Int => |
---|
176 | | - | i8 |
---|
177 | | - | case _ => |
---|
178 | | - | 0 |
---|
179 | | - | } |
---|
180 | | - | let b58CreditId = toBase58String(creditId) |
---|
181 | | - | let active = extract(getBoolean(this, (b58CreditId + "_active"))) |
---|
182 | | - | if (active) |
---|
183 | | - | then { |
---|
184 | | - | let currentCallerCredited = extract(getInteger(this, (b58CreditId + "_amount"))) |
---|
185 | | - | let withInterest = ((currentCallerCredited * (100 + creditInterest)) / 100) |
---|
186 | | - | let currentHoldersCount = match getInteger(this, "holder_count") { |
---|
187 | | - | case i9: Int => |
---|
188 | | - | i9 |
---|
189 | | - | case _ => |
---|
190 | | - | 0 |
---|
191 | | - | } |
---|
192 | | - | let pmt = extract(i.payment) |
---|
193 | | - | if (if (isDefined(pmt.assetId)) |
---|
194 | | - | then true |
---|
195 | | - | else (withInterest > pmt.amount)) |
---|
196 | | - | then throw("") |
---|
197 | | - | else { |
---|
198 | | - | let dataList = { |
---|
199 | | - | let returnIndex0 = match getInteger(this, (b58CreditId + "_0")) { |
---|
200 | | - | case i10: Int => |
---|
201 | | - | i10 |
---|
202 | | - | case _ => |
---|
203 | | - | -1 |
---|
204 | | - | } |
---|
205 | | - | if ((returnIndex0 >= 0)) |
---|
206 | | - | then { |
---|
207 | | - | let taken0 = extract(getInteger(this, (b58CreditId + "_taken_0"))) |
---|
208 | | - | let profit0 = ((taken0 * creditInterest) / 100) |
---|
209 | | - | let subList0 = { |
---|
210 | | - | let returnIndex1 = match getInteger(this, (b58CreditId + "_1")) { |
---|
211 | | - | case i11: Int => |
---|
212 | | - | i11 |
---|
213 | | - | case _ => |
---|
214 | | - | -1 |
---|
215 | | - | } |
---|
216 | | - | if ((returnIndex1 >= 0)) |
---|
217 | | - | then { |
---|
218 | | - | let taken1 = extract(getInteger(this, (b58CreditId + "_taken_1"))) |
---|
219 | | - | let profit1 = ((taken1 * creditInterest) / 100) |
---|
220 | | - | let subList1 = { |
---|
221 | | - | let returnIndex2 = match getInteger(this, (b58CreditId + "_2")) { |
---|
222 | | - | case i12: Int => |
---|
223 | | - | i12 |
---|
224 | | - | case _ => |
---|
225 | | - | -1 |
---|
226 | | - | } |
---|
227 | | - | if ((returnIndex2 >= 0)) |
---|
228 | | - | then { |
---|
229 | | - | let taken2 = extract(getInteger(this, (b58CreditId + "_taken_2"))) |
---|
230 | | - | let profit2 = ((taken2 * creditInterest) / 100) |
---|
231 | | - | let subList2 = nil |
---|
232 | | - | let returnAv2 = extract(getInteger(this, ("holder_available_" + toString(returnIndex2)))) |
---|
233 | | - | let returnCr2 = extract(getInteger(this, ("holder_credited_" + toString(returnIndex2)))) |
---|
234 | | - | [DataEntry(("holder_available_" + toString(returnIndex2)), ((returnAv2 + taken2) + profit2)), DataEntry(("holder_credited_" + toString(returnIndex2)), (returnCr2 - taken2))] :: subList2 |
---|
235 | | - | } |
---|
236 | | - | else nil |
---|
237 | | - | } |
---|
238 | | - | let returnAv1 = extract(getInteger(this, ("holder_available_" + toString(returnIndex1)))) |
---|
239 | | - | let returnCr1 = extract(getInteger(this, ("holder_credited_" + toString(returnIndex1)))) |
---|
240 | | - | [DataEntry(("holder_available_" + toString(returnIndex1)), ((returnAv1 + taken1) + profit1)), DataEntry(("holder_credited_" + toString(returnIndex1)), (returnCr1 - taken1))] :: subList1 |
---|
241 | | - | } |
---|
242 | | - | else nil |
---|
243 | | - | } |
---|
244 | | - | let returnAv0 = extract(getInteger(this, ("holder_available_" + toString(returnIndex0)))) |
---|
245 | | - | let returnCr0 = extract(getInteger(this, ("holder_credited_" + toString(returnIndex0)))) |
---|
246 | | - | [DataEntry(("holder_available_" + toString(returnIndex0)), ((returnAv0 + taken0) + profit0)), DataEntry(("holder_credited_" + toString(returnIndex0)), (returnCr0 - taken0))] :: subList0 |
---|
247 | | - | } |
---|
248 | | - | else nil |
---|
249 | | - | } |
---|
250 | | - | WriteSet([DataEntry((b58CreditId + "_active"), false), DataEntry("available_funds", (availableFunds + withInterest)), DataEntry("credited_funds", (creditedFunds - currentCallerCredited))] :: dataList) |
---|
251 | | - | } |
---|
252 | | - | } |
---|
253 | | - | else throw("") |
---|
254 | | - | } |
---|
255 | | - | |
---|
256 | | - | |
---|
| 1 | + | {-# STDLIB_VERSION 2 #-} |
---|
| 2 | + | {-# CONTENT_TYPE EXPRESSION #-} |
---|
| 3 | + | true |
---|