45 | | - | func depositOwnerCheck (assetName) = { |
---|
46 | | - | let val = getString(this, (assetName + "_DepositOwner")) |
---|
47 | | - | match val { |
---|
48 | | - | case a: String => |
---|
49 | | - | fromBase58String(a) |
---|
50 | | - | case _ => |
---|
51 | | - | throw("Counldn't find deposit owner public key") |
---|
52 | | - | } |
---|
53 | | - | } |
---|
54 | | - | |
---|
55 | | - | |
---|
56 | | - | func depositCreatedCheck (assetName) = { |
---|
57 | | - | let key = (assetName + "_DepositOwner") |
---|
58 | | - | let val = getString(this, key) |
---|
59 | | - | match val { |
---|
60 | | - | case a: String => |
---|
61 | | - | true |
---|
62 | | - | case _ => |
---|
63 | | - | false |
---|
64 | | - | } |
---|
65 | | - | } |
---|
66 | | - | |
---|
67 | | - | |
---|
68 | | - | func amountCheck (assetName,pmtAmount) = { |
---|
69 | | - | let key = (assetName + "_DepositAmount") |
---|
70 | | - | let val = getInteger(this, key) |
---|
71 | | - | let depositAmount = match val { |
---|
72 | | - | case a: Int => |
---|
73 | | - | a |
---|
74 | | - | case _ => |
---|
75 | | - | throw("Couldn't find deposit amount") |
---|
76 | | - | } |
---|
77 | | - | if ((pmtAmount == depositAmount)) |
---|
78 | | - | then pmtAmount |
---|
79 | | - | else throw("Amount is incorrect") |
---|
80 | | - | } |
---|
81 | | - | |
---|
82 | | - | |
---|
83 | | - | func assetBase58ToName (assetId) = if (!(isDefined(assetId))) |
---|
84 | | - | then "WAVES" |
---|
85 | | - | else if ((assetId == btcAssetId)) |
---|
86 | | - | then "BTC" |
---|
87 | | - | else if ((assetId == ethAssetId)) |
---|
88 | | - | then "ETH" |
---|
89 | | - | else if ((assetId == ltcAssetId)) |
---|
90 | | - | then "LTC" |
---|
91 | | - | else if ((assetId == westAssetId)) |
---|
92 | | - | then "WEST" |
---|
93 | | - | else if ((assetId == wctAssetId)) |
---|
94 | | - | then "WCT" |
---|
95 | | - | else if ((assetId == usdtAssetId)) |
---|
96 | | - | then "USDT" |
---|
97 | | - | else if ((assetId == usdnAssetId)) |
---|
98 | | - | then "USDN" |
---|
99 | | - | else throw("AssetId is incorrect") |
---|
100 | | - | |
---|
101 | | - | |
---|
102 | | - | func assetNameToBase58 (assetName) = if ((assetName == "WAVES")) |
---|
103 | | - | then wavesAssetId |
---|
104 | | - | else if ((assetName == "BTC")) |
---|
105 | | - | then btcAssetId |
---|
106 | | - | else if ((assetName == "ETH")) |
---|
107 | | - | then ethAssetId |
---|
108 | | - | else if ((assetName == "LTC")) |
---|
109 | | - | then ltcAssetId |
---|
110 | | - | else if ((assetName == "WEST")) |
---|
111 | | - | then westAssetId |
---|
112 | | - | else if ((assetName == "WCT")) |
---|
113 | | - | then wctAssetId |
---|
114 | | - | else if ((assetName == "USDT")) |
---|
115 | | - | then usdtAssetId |
---|
116 | | - | else if ((assetName == "USDN")) |
---|
117 | | - | then usdnAssetId |
---|
118 | | - | else throw("Asset name is incorrect") |
---|
119 | | - | |
---|
120 | | - | |
---|
121 | | - | func getDividendsPercent (assetName) = { |
---|
122 | | - | let key = (assetName + "_DividendPercent") |
---|
123 | | - | let val = getInteger(this, key) |
---|
124 | | - | match val { |
---|
125 | | - | case a: Int => |
---|
126 | | - | a |
---|
127 | | - | case _ => |
---|
128 | | - | throw("Couldn't Find dividend percent") |
---|
129 | | - | } |
---|
130 | | - | } |
---|
131 | | - | |
---|
132 | | - | |
---|
133 | | - | func getDividendsWithdrawnByUser (assetName) = { |
---|
134 | | - | let val = getInteger(this, (assetName + "_DividendWithdrawnByUser")) |
---|
135 | | - | match val { |
---|
136 | | - | case a: Int => |
---|
137 | | - | a |
---|
138 | | - | case _ => |
---|
139 | | - | 0 |
---|
140 | | - | } |
---|
141 | | - | } |
---|
142 | | - | |
---|
143 | | - | |
---|
144 | | - | func getDividendsWithdrawnByAdmin (assetName) = { |
---|
145 | | - | let val = getInteger(this, (assetName + "_DividendWithdrawnByAdmin")) |
---|
146 | | - | match val { |
---|
147 | | - | case a: Int => |
---|
148 | | - | a |
---|
149 | | - | case _ => |
---|
150 | | - | 0 |
---|
151 | | - | } |
---|
152 | | - | } |
---|
153 | | - | |
---|
154 | | - | |
---|
155 | | - | func getTotalDividendBalance (assetId,getDividendsWithdrawnByUser,getDividendsWithdrawnByAdmin) = { |
---|
156 | | - | let dividendsTotalBalance = ((assetBalance(this, assetId) + getDividendsWithdrawnByUser) + getDividendsWithdrawnByAdmin) |
---|
157 | | - | dividendsTotalBalance |
---|
158 | | - | } |
---|
159 | | - | |
---|
160 | | - | |
---|
161 | | - | func getUserDividendBalance (assetId,totalDividendBalance,dividendsWithdrawnByUser,dividendsPercent) = { |
---|
162 | | - | let stake = ((totalDividendBalance * dividendsPercent) / 100) |
---|
163 | | - | (stake - dividendsWithdrawnByUser) |
---|
164 | | - | } |
---|
165 | | - | |
---|
166 | | - | |
---|
167 | | - | func getAdminDividendBalance (assetId,totalDividendBalance,dividendsWithdrawnByAdmin,dividendsPercent) = { |
---|
168 | | - | let stake = ((totalDividendBalance * (100 - dividendsPercent)) / 100) |
---|
169 | | - | (stake - dividendsWithdrawnByAdmin) |
---|
170 | | - | } |
---|
171 | | - | |
---|
172 | | - | |
---|
173 | | - | @Callable(i) |
---|
174 | | - | func createDeposit (assetName,amount,dividendPercent,startHeight,durationBlocks,startTimestamp) = { |
---|
175 | | - | let callerIsAdmin = (i.callerPublicKey == adminPub) |
---|
176 | | - | let assetCorrect = assetNameCheck(assetName) |
---|
177 | | - | let depositAlreadyCreated = depositCreatedCheck(assetName) |
---|
178 | | - | if (callerIsAdmin) |
---|
179 | | - | then if (depositAlreadyCreated) |
---|
180 | | - | then throw("Deposit is already created") |
---|
181 | | - | else WriteSet([DataEntry((assetName + "_DepositState"), "Created"), DataEntry((assetName + "_DepositAmount"), amount), DataEntry((assetName + "_DividendPercent"), dividendPercent), DataEntry((assetName + "_DepositStartBlock"), startHeight), DataEntry((assetName + "_DepositStartTimestamp"), startTimestamp), DataEntry((assetName + "_DepositDurationBlock"), durationBlocks)]) |
---|
182 | | - | else throw("Only admin can create deposits") |
---|
183 | | - | } |
---|
184 | | - | |
---|
185 | | - | |
---|
186 | | - | |
---|
187 | | - | @Callable(i) |
---|
188 | | - | func deposit () = { |
---|
189 | | - | let callerPub = toBase58String(i.callerPublicKey) |
---|
190 | | - | let pmt = extract(i.payment) |
---|
191 | | - | let assetName = assetBase58ToName(pmt.assetId) |
---|
192 | | - | let amount = amountCheck(assetName, pmt.amount) |
---|
193 | | - | let depositState = getString(this, (assetName + "_DepositState")) |
---|
194 | | - | if ((depositState == "Created")) |
---|
195 | | - | then ScriptResult(WriteSet([DataEntry("_Operation", height), DataEntry((assetName + "_DepositOwner"), callerPub), DataEntry((assetName + "_DepositState"), "Active"), DataEntry((assetName + "_DividendWithdrawn"), 0)]), TransferSet([ScriptTransfer(addressFromPublicKey(dragonFruitPub), amount, pmt.assetId)])) |
---|
196 | | - | else throw("Deposit is unavailable") |
---|
197 | | - | } |
---|
198 | | - | |
---|
199 | | - | |
---|
200 | | - | |
---|
201 | | - | @Callable(i) |
---|
202 | | - | func withdrawDividends (assetName) = { |
---|
203 | | - | let assetId = assetNameToBase58(assetName) |
---|
204 | | - | let depositOwner = depositOwnerCheck(assetName) |
---|
205 | | - | let callerIsDepositOwner = (i.callerPublicKey == depositOwner) |
---|
206 | | - | let callerIsAdmin = (i.callerPublicKey == adminPub) |
---|
207 | | - | let dividendsPercent = getDividendsPercent(assetName) |
---|
208 | | - | let totalDividends = getTotalDividendBalance(assetId, getDividendsWithdrawnByUser(assetName), getDividendsWithdrawnByAdmin(assetName)) |
---|
209 | | - | if (callerIsDepositOwner) |
---|
210 | | - | then { |
---|
211 | | - | let dividendsAvaliable = getUserDividendBalance(assetId, totalDividends, getDividendsWithdrawnByUser(assetName), dividendsPercent) |
---|
212 | | - | if ((dividendsAvaliable > 0)) |
---|
213 | | - | then ScriptResult(WriteSet([DataEntry((assetName + "_DividendWithdrawnByUser"), (getDividendsWithdrawnByUser(assetName) + dividendsAvaliable))]), TransferSet([ScriptTransfer(addressFromPublicKey(depositOwner), dividendsAvaliable, assetId)])) |
---|
214 | | - | else throw("You have no dividends available") |
---|
215 | | - | } |
---|
216 | | - | else if (callerIsAdmin) |
---|
217 | | - | then { |
---|
218 | | - | let dividendsAvaliable = getAdminDividendBalance(assetId, totalDividends, getDividendsWithdrawnByAdmin(assetName), dividendsPercent) |
---|
219 | | - | if ((dividendsAvaliable > 0)) |
---|
220 | | - | then ScriptResult(WriteSet([DataEntry((assetName + "_DividendWithdrawnByAdmin"), (getDividendsWithdrawnByAdmin(assetName) + dividendsAvaliable))]), TransferSet([ScriptTransfer(addressFromPublicKey(adminPub), dividendsAvaliable, assetId)])) |
---|
221 | | - | else throw("You have no dividends available") |
---|
222 | | - | } |
---|
223 | | - | else throw("You have no rights to call this function") |
---|
224 | | - | } |
---|
225 | | - | |
---|
226 | | - | |
---|
227 | | - | @Verifier(tx) |
---|
228 | | - | func verify () = if (sigVerify(tx.bodyBytes, tx.proofs[0], divProgramPub)) |
---|
229 | | - | then match tx { |
---|
230 | | - | case sstx: SetScriptTransaction => |
---|
231 | | - | true |
---|
232 | | - | case _ => |
---|
233 | | - | false |
---|
234 | | - | } |
---|
235 | | - | else false |
---|