25 | | - | func getVoteAmountKey (id,type) = if ((type == "upvote")) |
---|
26 | | - | then (((id + "_") + type) + "_amount") |
---|
27 | | - | else throw("Invalid vote type") |
---|
28 | | - | |
---|
29 | | - | |
---|
30 | | - | func getStakerKey (staker) = (staker + "_stake") |
---|
31 | | - | |
---|
32 | | - | |
---|
33 | | - | func getCurrentStake (staker) = match getInteger(this, getStakerKey(staker)) { |
---|
34 | | - | case a: Int => |
---|
35 | | - | a |
---|
36 | | - | case _ => |
---|
37 | | - | 0 |
---|
38 | | - | } |
---|
39 | | - | |
---|
40 | | - | |
---|
41 | | - | func getProfileKey (id) = (id + "_entrepreneurProfile") |
---|
42 | | - | |
---|
43 | | - | |
---|
44 | | - | func getStatusKey (id) = (id + "_status") |
---|
45 | | - | |
---|
46 | | - | |
---|
47 | | - | func getVoteKey (id,type) = if ((type == "upvote")) |
---|
48 | | - | then (id + "_upvote") |
---|
49 | | - | else throw("Invalid vote type") |
---|
50 | | - | |
---|
51 | | - | |
---|
52 | | - | func profileExist (id) = match getString(this, getProfileKey(id)) { |
---|
53 | | - | case a: String => |
---|
54 | | - | true |
---|
55 | | - | case _ => |
---|
56 | | - | false |
---|
57 | | - | } |
---|
58 | | - | |
---|
59 | | - | |
---|
60 | | - | func getVoteStatus (voteId) = match getString(this, getStatusKey(voteId)) { |
---|
61 | | - | case a: String => |
---|
62 | | - | a |
---|
63 | | - | case _ => |
---|
64 | | - | false |
---|
65 | | - | } |
---|
66 | | - | |
---|
67 | | - | |
---|
68 | | - | func getVoterAmountKey (id,voteId) = (((id + "_") + voteId) + "_amount") |
---|
69 | | - | |
---|
70 | | - | |
---|
71 | | - | func getVoteAmountCount (voteId,vote) = match getInteger(this, getVoteAmountKey(voteId, vote)) { |
---|
72 | | - | case a: Int => |
---|
73 | | - | a |
---|
74 | | - | case _ => |
---|
75 | | - | 0 |
---|
76 | | - | } |
---|
77 | | - | |
---|
78 | | - | |
---|
79 | | - | func getLastClaimHeight (pubKey) = match getInteger(this, ("last_claim_" + pubKey)) { |
---|
80 | | - | case a: Int => |
---|
81 | | - | a |
---|
82 | | - | case _ => |
---|
83 | | - | 0 |
---|
84 | | - | } |
---|
85 | | - | |
---|
86 | | - | |
---|
87 | | - | func getVoteCount (voteId,vote) = match getInteger(this, getVoteKey(voteId, vote)) { |
---|
88 | | - | case a: Int => |
---|
89 | | - | a |
---|
90 | | - | case _ => |
---|
91 | | - | throw("Not a valid vote ID") |
---|
92 | | - | } |
---|
93 | | - | |
---|
94 | | - | |
---|
95 | | - | func getVoterKey (address,voteId) = ((((address + "_") + voteId) + "_") + "vote") |
---|
96 | | - | |
---|
97 | | - | |
---|
98 | | - | func getVoter (address,voteId) = match getString(this, getVoterKey(address, voteId)) { |
---|
99 | | - | case a: String => |
---|
100 | | - | a |
---|
101 | | - | case _ => |
---|
102 | | - | "" |
---|
103 | | - | } |
---|
104 | | - | |
---|
105 | | - | |
---|
106 | | - | func canVote (address,voteId) = { |
---|
107 | | - | let voter = getVoter(address, voteId) |
---|
108 | | - | if ((voter == "")) |
---|
109 | | - | then true |
---|
110 | | - | else false |
---|
| 22 | + | func getFeeAmount (amount) = { |
---|
| 23 | + | let fee = fraction(amount, 3, 100) |
---|
| 24 | + | fee |
---|
122 | | - | func getClaimedByStaker (address) = match getInteger(this, ("claimed_total_" + address)) { |
---|
123 | | - | case a: Int => |
---|
124 | | - | a |
---|
125 | | - | case _ => |
---|
126 | | - | 0 |
---|
127 | | - | } |
---|
| 34 | + | func getTotalFeeEth () = getIntKeyData(totalFeeEthKey) |
---|
| 35 | + | |
---|
| 36 | + | |
---|
| 37 | + | func getTotalFeeUsd () = getIntKeyData(totalFeeUsdKey) |
---|
| 38 | + | |
---|
| 39 | + | |
---|
| 40 | + | func getTotalFeeDyzrapt () = getIntKeyData(totalFeeDyzraptKey) |
---|
| 41 | + | |
---|
| 42 | + | |
---|
| 43 | + | func usdnToDyzrapt (amount) = { |
---|
| 44 | + | let dyzraptPrice = valueOrErrorMessage(getInteger(oracleAddress, priceOfDyzraptKey), "Unable to load price") |
---|
| 45 | + | fraction(amount, 10000, dyzraptPrice) |
---|
| 46 | + | } |
---|
| 47 | + | |
---|
| 48 | + | |
---|
| 49 | + | func dyzraptToUsdn (amount) = { |
---|
| 50 | + | let dyzraptPrice = valueOrErrorMessage(getInteger(oracleAddress, priceOfDyzraptKey), "Unable to load price") |
---|
| 51 | + | fraction(amount, dyzraptPrice, 10000) |
---|
| 52 | + | } |
---|
131 | | - | func addEntrepreneurProfile (name,avatar,bio,gender,location) = if (if (if ((i.caller.bytes == adminAddress1)) |
---|
132 | | - | then true |
---|
133 | | - | else (i.caller.bytes == adminAddress2)) |
---|
134 | | - | then true |
---|
135 | | - | else (i.caller.bytes == adminAddress3)) |
---|
136 | | - | then { |
---|
137 | | - | let transactionId = toBase58String(i.transactionId) |
---|
138 | | - | WriteSet([DataEntry(getProfileKey(transactionId), ((((((((name + "_") + avatar) + "_") + bio) + "_") + gender) + "_") + location)), DataEntry(getVoteKey(transactionId, "upvote"), 0), DataEntry(getVoteAmountKey(transactionId, "upvote"), 0), DataEntry(getStatusKey(transactionId), "open")]) |
---|
139 | | - | } |
---|
140 | | - | else throw("Only Dyzrupt Admin can call this dApp function") |
---|
141 | | - | |
---|
142 | | - | |
---|
143 | | - | |
---|
144 | | - | @Callable(i) |
---|
145 | | - | func vote (voteId,vote) = { |
---|
146 | | - | let pmt = extract(i.payment) |
---|
147 | | - | let voter = toBase58String(i.caller.bytes) |
---|
148 | | - | if ((pmt.amount == 0)) |
---|
149 | | - | then throw("You cannot vote without paying voting fee") |
---|
150 | | - | else if ((pmt.assetId != dyzId)) |
---|
151 | | - | then throw("You can only vote with DYZRUPT token") |
---|
152 | | - | else if (canVote(toBase58String(i.caller.bytes), voteId)) |
---|
153 | | - | then if (profileExist(voteId)) |
---|
154 | | - | then if ((getVoteStatus(voteId) == "open")) |
---|
155 | | - | then if ((vote == upVote)) |
---|
156 | | - | then { |
---|
157 | | - | let voteCount = getVoteCount(voteId, vote) |
---|
158 | | - | let voteAmountCount = getVoteAmountCount(voteId, vote) |
---|
159 | | - | let newVoteCount = (voteCount + 1) |
---|
160 | | - | let newVoteAmountCount = (voteAmountCount + pmt.amount) |
---|
161 | | - | ScriptResult(WriteSet([DataEntry(getVoteKey(voteId, vote), newVoteCount), DataEntry(getVoterKey(voter, voteId), voteId), DataEntry(getVoterAmountKey(voter, voteId), pmt.amount), DataEntry(getVoteAmountKey(voteId, vote), newVoteAmountCount)]), TransferSet([ScriptTransfer(this, pmt.amount, dyzId)])) |
---|
162 | | - | } |
---|
163 | | - | else throw("Not a valid vote value") |
---|
164 | | - | else throw("This profile is not open for voting") |
---|
165 | | - | else throw("Invalid Profile") |
---|
166 | | - | else throw("You can't vote twice") |
---|
| 56 | + | func test () = { |
---|
| 57 | + | let pmt = value(i.payments[0]) |
---|
| 58 | + | let assetId = pmt.assetId |
---|
| 59 | + | let amount = pmt.amount |
---|
| 60 | + | if ((assetId == dyzraptAssetId)) |
---|
| 61 | + | then throw(("its a dyzrapt. Amount=" + toString(amount))) |
---|
| 62 | + | else throw("its not a dyzrapt") |
---|
172 | | - | func openVote (voteId) = if (if (if ((i.caller.bytes == adminAddress1)) |
---|
173 | | - | then true |
---|
174 | | - | else (i.caller.bytes == adminAddress2)) |
---|
175 | | - | then true |
---|
176 | | - | else (i.caller.bytes == adminAddress3)) |
---|
177 | | - | then if (profileExist(voteId)) |
---|
178 | | - | then WriteSet([DataEntry(getStatusKey(voteId), "open")]) |
---|
179 | | - | else throw("Invalid ID") |
---|
180 | | - | else throw("Only Dyzrupt Admin can call this dApp function") |
---|
181 | | - | |
---|
182 | | - | |
---|
183 | | - | |
---|
184 | | - | @Callable(i) |
---|
185 | | - | func closeVote (voteId) = if (if (if ((i.caller.bytes == adminAddress1)) |
---|
186 | | - | then true |
---|
187 | | - | else (i.caller.bytes == adminAddress2)) |
---|
188 | | - | then true |
---|
189 | | - | else (i.caller.bytes == adminAddress3)) |
---|
190 | | - | then if (profileExist(voteId)) |
---|
191 | | - | then WriteSet([DataEntry(getStatusKey(voteId), "closed")]) |
---|
192 | | - | else throw("Invalid ID") |
---|
193 | | - | else throw("Only Dyzrupt Admin can call this dApp function") |
---|
194 | | - | |
---|
195 | | - | |
---|
196 | | - | |
---|
197 | | - | @Callable(i) |
---|
198 | | - | func withdraw (voteId) = { |
---|
199 | | - | let voter = toBase58String(i.caller.bytes) |
---|
200 | | - | if ((getVoter(voter, voteId) != "")) |
---|
201 | | - | then if ((getVoteStatus(voteId) == "closed")) |
---|
202 | | - | then { |
---|
203 | | - | let currentKey = getVoterAmountKey(voter, voteId) |
---|
204 | | - | let currentAmount = match getInteger(this, currentKey) { |
---|
205 | | - | case a: Int => |
---|
206 | | - | a |
---|
207 | | - | case _ => |
---|
208 | | - | throw("You didn't vote for this entrepreneur") |
---|
209 | | - | } |
---|
210 | | - | if ((currentAmount == 0)) |
---|
211 | | - | then throw("Seems you have previously withdrawn.") |
---|
212 | | - | else ScriptResult(WriteSet([DataEntry(currentKey, 0)]), TransferSet([ScriptTransfer(i.caller, currentAmount, dyzId)])) |
---|
213 | | - | } |
---|
214 | | - | else throw("Can't withdraw vote amount while voting is still open.") |
---|
215 | | - | else throw("You didn't vote for this entrepreneur") |
---|
| 68 | + | func swapUsdnToDyzrapt () = { |
---|
| 69 | + | let pmt = value(i.payments[0]) |
---|
| 70 | + | let assetId = pmt.assetId |
---|
| 71 | + | let totalAmount = pmt.amount |
---|
| 72 | + | if ((assetId != usdnAssetId)) |
---|
| 73 | + | then throw("Only USDN tokens are allowed for swapping") |
---|
| 74 | + | else { |
---|
| 75 | + | let fee = getFeeAmount(totalAmount) |
---|
| 76 | + | let amount = (totalAmount - fee) |
---|
| 77 | + | let dyzraptAmount = usdnToDyzrapt(amount) |
---|
| 78 | + | let savedFee = getTotalFeeUsd() |
---|
| 79 | + | let totalFee = (savedFee + fee) |
---|
| 80 | + | [IntegerEntry(totalFeeUsdKey, totalFee), ScriptTransfer(i.caller, dyzraptAmount, dyzraptAssetId)] |
---|
| 81 | + | } |
---|
221 | | - | func stake () = { |
---|
222 | | - | let staker = toBase58String(i.caller.bytes) |
---|
223 | | - | let pmt = extract(i.payment) |
---|
224 | | - | if ((pmt.amount == 0)) |
---|
225 | | - | then throw("Stake amount too low") |
---|
226 | | - | else if ((pmt.assetId != dyzId)) |
---|
227 | | - | then throw("You can only stake with DYZRUPT token") |
---|
228 | | - | else { |
---|
229 | | - | let currentStake = getCurrentStake(staker) |
---|
230 | | - | let stakeableAmount = fraction(pmt.amount, 5, 100) |
---|
231 | | - | let amountToStake = (pmt.amount - stakeableAmount) |
---|
232 | | - | let newStakedAmount = (currentStake + amountToStake) |
---|
233 | | - | let stakePool = getStakePool() |
---|
234 | | - | let newStakePool = (stakePool + stakeableAmount) |
---|
235 | | - | ScriptResult(WriteSet([DataEntry(getStakerKey(staker), newStakedAmount), DataEntry(getStakePoolKey(), newStakePool)]), TransferSet(nil)) |
---|
236 | | - | } |
---|
| 87 | + | func swapEthToDyzrapt () = { |
---|
| 88 | + | let pmt = value(i.payments[0]) |
---|
| 89 | + | let assetId = pmt.assetId |
---|
| 90 | + | let totalAmount = pmt.amount |
---|
| 91 | + | if ((assetId != ethAssetId)) |
---|
| 92 | + | then throw("Only WETH tokens are allowed for swapping") |
---|
| 93 | + | else { |
---|
| 94 | + | let fee = getFeeAmount(totalAmount) |
---|
| 95 | + | let amount = (totalAmount - fee) |
---|
| 96 | + | let ethPrice = valueOrErrorMessage(getInteger(oracleAddress, priceOfEthKey), "Unable to load price") |
---|
| 97 | + | let usdnAmount = fraction(amount, ethPrice, 10000) |
---|
| 98 | + | let dyzraptAmount = usdnToDyzrapt(usdnAmount) |
---|
| 99 | + | let savedFee = getTotalFeeEth() |
---|
| 100 | + | let totalFee = (savedFee + fee) |
---|
| 101 | + | [IntegerEntry(totalFeeEthKey, totalFee), ScriptTransfer(i.caller, dyzraptAmount, dyzraptAssetId)] |
---|
| 102 | + | } |
---|
242 | | - | func unstake (amount) = { |
---|
243 | | - | let staker = toBase58String(i.caller.bytes) |
---|
244 | | - | let currentStake = getCurrentStake(staker) |
---|
245 | | - | if ((currentStake == 0)) |
---|
246 | | - | then throw("You don't have any stakes") |
---|
247 | | - | else if ((amount > currentStake)) |
---|
248 | | - | then throw("You can't withdraw an amount higher than you staked") |
---|
249 | | - | else { |
---|
250 | | - | let unstakeableAmount = fraction(amount, 10, 100) |
---|
251 | | - | let amountToUnstake = (amount - unstakeableAmount) |
---|
252 | | - | let newUnstakedAmount = (currentStake - amount) |
---|
253 | | - | let stakePool = getStakePool() |
---|
254 | | - | let newStakePool = (stakePool + unstakeableAmount) |
---|
255 | | - | ScriptResult(WriteSet([DataEntry(getStakerKey(staker), newUnstakedAmount), DataEntry(getStakePoolKey(), newStakePool)]), TransferSet([ScriptTransfer(i.caller, amountToUnstake, dyzId)])) |
---|
256 | | - | } |
---|
| 108 | + | func swapDyzraptToUsdn () = { |
---|
| 109 | + | let pmt = value(i.payments[0]) |
---|
| 110 | + | let assetId = pmt.assetId |
---|
| 111 | + | let totalAmount = pmt.amount |
---|
| 112 | + | if ((assetId != dyzraptAssetId)) |
---|
| 113 | + | then throw("Only DYZRAPT tokens are allowed for swapping") |
---|
| 114 | + | else { |
---|
| 115 | + | let fee = getFeeAmount(totalAmount) |
---|
| 116 | + | let amount = (totalAmount - fee) |
---|
| 117 | + | let usdnAmount = dyzraptToUsdn(amount) |
---|
| 118 | + | let savedFee = getTotalFeeDyzrapt() |
---|
| 119 | + | let totalFee = (savedFee + fee) |
---|
| 120 | + | [IntegerEntry(totalFeeDyzraptKey, totalFee), ScriptTransfer(i.caller, usdnAmount, usdnAssetId)] |
---|
| 121 | + | } |
---|
262 | | - | func claimDailyStakeReward (reward) = { |
---|
263 | | - | let staker = toBase58String(i.caller.bytes) |
---|
264 | | - | let currentStake = getCurrentStake(staker) |
---|
265 | | - | let stakePool = getStakePool() |
---|
266 | | - | let currentHeight = height |
---|
267 | | - | let lastClaimHeight = getLastClaimHeight(staker) |
---|
268 | | - | let claimedTotalSoFar = getClaimedTotal() |
---|
269 | | - | let claimedByStaker = getClaimedByStaker(staker) |
---|
270 | | - | if ((currentStake == 0)) |
---|
271 | | - | then throw("You don't have any stakes") |
---|
272 | | - | else if ((stakePool == 0)) |
---|
273 | | - | then throw("Nothing in Stake pool at this time. Try again later") |
---|
274 | | - | else if ((reward > stakePool)) |
---|
275 | | - | then throw("No rewards at this time") |
---|
276 | | - | else if (((lastClaimHeight + 1440) > currentHeight)) |
---|
277 | | - | then throw("You can only claim once a day") |
---|
278 | | - | else { |
---|
279 | | - | let newStakePool = (stakePool - reward) |
---|
280 | | - | let newStakeAmount = currentStake |
---|
281 | | - | ScriptResult(WriteSet([DataEntry(("claimed_total_" + staker), (claimedByStaker + reward)), DataEntry("claimed_total", (claimedTotalSoFar + reward)), DataEntry(("last_claim_" + staker), currentHeight), DataEntry(getStakerKey(staker), newStakeAmount), DataEntry(getStakePoolKey(), newStakePool)]), TransferSet([ScriptTransfer(i.caller, reward, dyzId)])) |
---|
282 | | - | } |
---|
| 127 | + | func swapDyzraptToEth () = { |
---|
| 128 | + | let pmt = value(i.payments[0]) |
---|
| 129 | + | let assetId = pmt.assetId |
---|
| 130 | + | let totalAmount = pmt.amount |
---|
| 131 | + | if ((assetId != dyzraptAssetId)) |
---|
| 132 | + | then throw("Only DYZRAPT tokens are allowed for swapping") |
---|
| 133 | + | else { |
---|
| 134 | + | let fee = getFeeAmount(totalAmount) |
---|
| 135 | + | let amount = (totalAmount - fee) |
---|
| 136 | + | let usdnAmount = dyzraptToUsdn(amount) |
---|
| 137 | + | let ethPrice = valueOrErrorMessage(getInteger(oracleAddress, priceOfEthKey), "Unable to load price") |
---|
| 138 | + | let ethAmount = fraction(usdnAmount, 10000, ethPrice) |
---|
| 139 | + | let savedFee = getTotalFeeDyzrapt() |
---|
| 140 | + | let totalFee = (savedFee + fee) |
---|
| 141 | + | [IntegerEntry(totalFeeDyzraptKey, totalFee), ScriptTransfer(i.caller, ethAmount, ethAssetId)] |
---|
| 142 | + | } |
---|