4 | | - | let incubatorAddress = base58'3PEktVux2RhchSN63DsDo4b4mz4QqzKSeDv' |
---|
5 | | - | |
---|
6 | | - | let breederAddress = base58'3PDVuU45H7Eh5dmtNbnRNRStGwULA7NY6Hb' |
---|
7 | | - | |
---|
8 | | - | let artefactsAddress = base58'3P5E9xamcWoymiqLx8ZdmR7o4fJSRMGp1WR' |
---|
9 | | - | |
---|
10 | | - | let feeAggregator = base58'3P6AobNbcQ3ZjcRY37mJPKN8PP9Giz8kgeY' |
---|
11 | | - | |
---|
12 | | - | let eggAssetId = base58'C1iWsKGqLwjHUndiQ7iXpdmPum9PeCDFfyXBdJJosDRS' |
---|
13 | | - | |
---|
14 | | - | let kGlobalCounter = "GLOBAL_COUNTER" |
---|
15 | | - | |
---|
16 | | - | let feePercent = 5 |
---|
17 | | - | |
---|
18 | | - | let artefactsFeePercent = 20 |
---|
19 | | - | |
---|
20 | | - | func tryGetInteger (key) = { |
---|
21 | | - | let val = match getInteger(this, key) { |
---|
22 | | - | case b: Int => |
---|
23 | | - | b |
---|
24 | | - | case _ => |
---|
25 | | - | 0 |
---|
26 | | - | } |
---|
27 | | - | val |
---|
28 | | - | } |
---|
29 | | - | |
---|
30 | | - | |
---|
31 | | - | func tryGetString (key) = { |
---|
32 | | - | let val = match getString(this, key) { |
---|
33 | | - | case b: String => |
---|
34 | | - | b |
---|
35 | | - | case _ => |
---|
36 | | - | "" |
---|
37 | | - | } |
---|
38 | | - | val |
---|
39 | | - | } |
---|
40 | | - | |
---|
41 | | - | |
---|
42 | | - | func tryGetBoolean (key) = match getBoolean(this, key) { |
---|
43 | | - | case b: Boolean => |
---|
44 | | - | b |
---|
45 | | - | case _ => |
---|
46 | | - | false |
---|
47 | | - | } |
---|
48 | | - | |
---|
49 | | - | |
---|
50 | | - | func getGlobalCounter () = tryGetInteger(kGlobalCounter) |
---|
51 | | - | |
---|
52 | | - | |
---|
53 | | - | func getAssetGenotype (assetId) = if ((value(assetInfo(assetId)).issuer == Address(artefactsAddress))) |
---|
54 | | - | then "ART" |
---|
55 | | - | else { |
---|
56 | | - | let asset1 = match getString(Address(incubatorAddress), toBase58String(assetId)) { |
---|
57 | | - | case b: String => |
---|
58 | | - | b |
---|
59 | | - | case _ => |
---|
60 | | - | "" |
---|
61 | | - | } |
---|
62 | | - | if ((asset1 != "")) |
---|
63 | | - | then asset1 |
---|
64 | | - | else { |
---|
65 | | - | let asset2 = match getString(Address(breederAddress), toBase58String(assetId)) { |
---|
66 | | - | case c: String => |
---|
67 | | - | c |
---|
68 | | - | case _ => |
---|
69 | | - | "" |
---|
70 | | - | } |
---|
71 | | - | if ((asset2 != "")) |
---|
72 | | - | then asset2 |
---|
73 | | - | else "" |
---|
74 | | - | } |
---|
75 | | - | } |
---|
76 | | - | |
---|
77 | | - | |
---|
78 | | - | func getBidCancelResults (auctionId,bidId) = { |
---|
79 | | - | let isEggSell = tryGetBoolean((("auction_" + auctionId) + "_eggSell")) |
---|
80 | | - | let paymentAssetId = if ((isEggSell == true)) |
---|
81 | | - | then eggAssetId |
---|
82 | | - | else unit |
---|
83 | | - | let assetId = getStringValue(this, (("auction_" + auctionId) + "_assetId")) |
---|
84 | | - | let bidAuthor = tryGetString((((("auction_" + auctionId) + "_bid_") + bidId) + "_author")) |
---|
85 | | - | let bidAmount = tryGetInteger((((("auction_" + auctionId) + "_bid_") + bidId) + "_amount")) |
---|
86 | | - | [StringEntry((((("assetId_" + assetId) + "_bid_") + bidId) + "_data"), (((((("{\"auctionId\": \"" + auctionId) + "\", \"author\": \"") + bidAuthor) + "\", \"amount\": \"") + toString(bidAmount)) + "\", \"status\": \"cancelled\"}")), StringEntry((((("auction_" + auctionId) + "_bid_") + bidId) + "_status"), "cancelled"), DeleteEntry(((((("address_" + bidAuthor) + "_auction_") + auctionId) + "_bid_") + bidId)), ScriptTransfer(Address(fromBase58String(bidAuthor)), bidAmount, paymentAssetId)] |
---|
87 | | - | } |
---|
88 | | - | |
---|
| 4 | + | let maxAuctionDuration = (1440 * 30) |
---|
91 | | - | func initAuction (startPrice,instantPrice,description,eggSell) = { |
---|
92 | | - | let globalCounter = getGlobalCounter() |
---|
93 | | - | let auctionId = (toBase58String(i.transactionId) + toString(globalCounter)) |
---|
94 | | - | let pmt = value(i.payments[0]) |
---|
95 | | - | let assetId = value(pmt.assetId) |
---|
96 | | - | let asset = getAssetGenotype(assetId) |
---|
97 | | - | if ((asset == "")) |
---|
98 | | - | then throw("You can only sell an NFT-duck created in incubator or breeder") |
---|
99 | | - | else [StringEntry((("auction_" + auctionId) + "_status"), "open"), BooleanEntry((("auction_" + auctionId) + "_eggSell"), (eggSell == "true")), IntegerEntry(kGlobalCounter, (globalCounter + 1)), StringEntry((("auction_" + toBase58String(assetId)) + "_last"), auctionId), StringEntry((("auction_" + auctionId) + "_assetGen"), asset), StringEntry((("auction_" + auctionId) + "_assetId"), toBase58String(assetId)), IntegerEntry((("auction_" + auctionId) + "_startedAt"), lastBlock.timestamp), StringEntry((("auction_" + auctionId) + "_owner"), toString(i.originCaller)), StringEntry((("auction_" + auctionId) + "_description"), description), IntegerEntry((("auction_" + auctionId) + "_startPrice"), startPrice), BooleanEntry((("auction_" + auctionId) + "_isArtefact"), (value(assetInfo(assetId)).issuer == Address(artefactsAddress))), IntegerEntry((("auction_" + auctionId) + "_instantPrice"), instantPrice), StringEntry((((("address_" + toString(i.originCaller)) + "_auction_") + auctionId) + "_lockedNFT"), toBase58String(assetId))] |
---|
| 7 | + | func startAuction (duration,startPrice,priceAssetId) = { |
---|
| 8 | + | let auctionId = toBase58String(i.transactionId) |
---|
| 9 | + | let endHeight = (lastBlock.height + duration) |
---|
| 10 | + | let pmt = extract(i.payment) |
---|
| 11 | + | if ((duration > maxAuctionDuration)) |
---|
| 12 | + | then throw(("Duration is too long. Must be less than " + toString(maxAuctionDuration))) |
---|
| 13 | + | else WriteSet([DataEntry(auctionId, endHeight), DataEntry((auctionId + "_organizer"), toBase58String(i.caller.bytes)), DataEntry((auctionId + "_lot_assetId"), if (isDefined(pmt.assetId)) |
---|
| 14 | + | then toBase58String(value(pmt.assetId)) |
---|
| 15 | + | else "WAVES"), DataEntry((auctionId + "_lot_amount"), pmt.amount), DataEntry((auctionId + "_startPrice"), startPrice), DataEntry((auctionId + "_priceAssetId"), priceAssetId)]) |
---|
105 | | - | func cancelAuction (auctionId) = { |
---|
106 | | - | let auctionOwner = tryGetString((("auction_" + auctionId) + "_owner")) |
---|
107 | | - | if ((toString(i.originCaller) != auctionOwner)) |
---|
108 | | - | then throw("Only allowed to auction owner") |
---|
109 | | - | else if ((tryGetString((("auction_" + auctionId) + "_status")) != "open")) |
---|
110 | | - | then throw("auction is close") |
---|
111 | | - | else [StringEntry((("auction_" + auctionId) + "_status"), "cancelled"), DeleteEntry((((("address_" + auctionOwner) + "_auction_") + auctionId) + "_lockedNFT")), ScriptTransfer(i.originCaller, 1, fromBase58String(getStringValue(this, (("auction_" + auctionId) + "_assetId"))))] |
---|
| 21 | + | func bid (auctionId) = { |
---|
| 22 | + | let pmt = extract(i.payment) |
---|
| 23 | + | let pmtAssetIdStr = if (isDefined(pmt.assetId)) |
---|
| 24 | + | then toBase58String(value(pmt.assetId)) |
---|
| 25 | + | else "WAVES" |
---|
| 26 | + | let callerAddressStr = toBase58String(i.caller.bytes) |
---|
| 27 | + | let endHeight = getIntegerValue(this, auctionId) |
---|
| 28 | + | let startPrice = getIntegerValue(this, (auctionId + "_startPrice")) |
---|
| 29 | + | let priceAssetId = getStringValue(this, (auctionId + "_priceAssetId")) |
---|
| 30 | + | let winAmount = getInteger(this, (auctionId + "_winAmount")) |
---|
| 31 | + | let winner = getString(this, (auctionId + "_winner")) |
---|
| 32 | + | let bidFromTheSameUser = if (isDefined(winner)) |
---|
| 33 | + | then (value(winner) == callerAddressStr) |
---|
| 34 | + | else false |
---|
| 35 | + | let totalBidAmount = (pmt.amount + (if (bidFromTheSameUser) |
---|
| 36 | + | then value(winAmount) |
---|
| 37 | + | else 0)) |
---|
| 38 | + | if ((lastBlock.height >= endHeight)) |
---|
| 39 | + | then throw("Auction already finished") |
---|
| 40 | + | else if ((priceAssetId != pmtAssetIdStr)) |
---|
| 41 | + | then throw((("Bid must be in asset '" + priceAssetId) + "'")) |
---|
| 42 | + | else if (if (if (isDefined(winAmount)) |
---|
| 43 | + | then (value(winAmount) >= totalBidAmount) |
---|
| 44 | + | else false) |
---|
| 45 | + | then true |
---|
| 46 | + | else if (!(isDefined(winAmount))) |
---|
| 47 | + | then (startPrice >= totalBidAmount) |
---|
| 48 | + | else false) |
---|
| 49 | + | then throw(("Bid must be more then " + toString(if (isDefined(winAmount)) |
---|
| 50 | + | then value(winAmount) |
---|
| 51 | + | else startPrice))) |
---|
| 52 | + | else if (if (bidFromTheSameUser) |
---|
| 53 | + | then true |
---|
| 54 | + | else !(isDefined(winner))) |
---|
| 55 | + | then WriteSet([DataEntry((auctionId + "_winner"), callerAddressStr), DataEntry((auctionId + "_winAmount"), totalBidAmount)]) |
---|
| 56 | + | else { |
---|
| 57 | + | let previousBidderAddr = addressFromStringValue(value(winner)) |
---|
| 58 | + | let priceAsset = if (if ((priceAssetId == "WAVES")) |
---|
| 59 | + | then true |
---|
| 60 | + | else (priceAssetId == "")) |
---|
| 61 | + | then unit |
---|
| 62 | + | else fromBase58String(priceAssetId) |
---|
| 63 | + | ScriptResult(WriteSet([DataEntry((auctionId + "_winner"), callerAddressStr), DataEntry((auctionId + "_winAmount"), totalBidAmount)]), TransferSet([ScriptTransfer(previousBidderAddr, value(winAmount), priceAsset)])) |
---|
| 64 | + | } |
---|
125 | | - | let pmt = value(i.payments[0]) |
---|
126 | | - | let amount = pmt.amount |
---|
127 | | - | let assetId = tryGetString((("auction_" + auctionId) + "_assetId")) |
---|
128 | | - | let bidId = toBase58String(i.transactionId) |
---|
129 | | - | let auctionStatus = tryGetString((("auction_" + auctionId) + "_status")) |
---|
130 | | - | let finalFee = if ((tryGetBoolean((("auction_" + auctionId) + "_isArtefact")) == true)) |
---|
131 | | - | then artefactsFeePercent |
---|
132 | | - | else feePercent |
---|
133 | | - | if ((pmt.assetId != paymentAssetId)) |
---|
134 | | - | then throw((("Auction does only support " + paymentAssetName) + " payments.")) |
---|
135 | | - | else if ((auctionStatus != "open")) |
---|
136 | | - | then throw("Auction is closed. You cannot place a bid.") |
---|
137 | | - | else { |
---|
138 | | - | let instantPrice = tryGetInteger((("auction_" + auctionId) + "_instantPrice")) |
---|
139 | | - | if ((instantPrice > amount)) |
---|
140 | | - | then throw(("For instant purchase you need to pay at least " + toString(instantPrice))) |
---|
141 | | - | else { |
---|
142 | | - | let auctionOwner = tryGetString((("auction_" + auctionId) + "_owner")) |
---|
143 | | - | let recordPrice = tryGetInteger((assetId + "_recordPrice")) |
---|
144 | | - | let newRecordPrice = max([recordPrice, amount]) |
---|
145 | | - | [StringEntry((("auction_" + auctionId) + "_status"), "finished"), IntegerEntry((("auction_" + auctionId) + "_finalPrice"), amount), IntegerEntry((assetId + "_recordPrice"), newRecordPrice), StringEntry((((("assetId_" + assetId) + "_bid_") + bidId) + "_data"), (((((("{\"auctionId\": \"" + auctionId) + "\", \"author\": \"") + toString(i.originCaller)) + "\", \"amount\": \"") + toString(amount)) + "\", \"status\": \"finished\"}")), DeleteEntry((((("address_" + auctionOwner) + "_auction_") + auctionId) + "_lockedNFT")), ScriptTransfer(i.originCaller, 1, fromBase58String(getStringValue(this, (("auction_" + auctionId) + "_assetId")))), ScriptTransfer(addressFromStringValue(auctionOwner), fraction(amount, (100 - finalFee), 100), paymentAssetId), ScriptTransfer(Address(feeAggregator), fraction(amount, finalFee, 100), paymentAssetId)] |
---|
146 | | - | } |
---|
147 | | - | } |
---|
| 75 | + | let callerAddressStr = toBase58String(i.caller.bytes) |
---|
| 76 | + | let endHeight = getIntegerValue(this, auctionId) |
---|
| 77 | + | let organizer = getStringValue(this, (auctionId + "_organizer")) |
---|
| 78 | + | let winner = getString(this, (auctionId + "_winner")) |
---|
| 79 | + | let lotAssetId = getStringValue(this, (auctionId + "_lot_assetId")) |
---|
| 80 | + | let lotAmount = getIntegerValue(this, (auctionId + "_lot_amount")) |
---|
| 81 | + | let priceAssetId = getStringValue(this, (auctionId + "_priceAssetId")) |
---|
| 82 | + | let winAmount = getIntegerValue(this, (auctionId + "_winAmount")) |
---|
| 83 | + | let lotAsset = if ((lotAssetId == "WAVES")) |
---|
| 84 | + | then unit |
---|
| 85 | + | else fromBase58String(lotAssetId) |
---|
| 86 | + | let priceAsset = if (if ((priceAssetId == "WAVES")) |
---|
| 87 | + | then true |
---|
| 88 | + | else (priceAssetId == "")) |
---|
| 89 | + | then unit |
---|
| 90 | + | else fromBase58String(priceAssetId) |
---|
| 91 | + | let winnerAddr = addressFromStringValue(value(winner)) |
---|
| 92 | + | let organizerAddr = addressFromStringValue(value(organizer)) |
---|
| 93 | + | let betAmount = getInteger(this, ((auctionId + "_bidder_") + callerAddressStr)) |
---|
| 94 | + | if ((endHeight > lastBlock.height)) |
---|
| 95 | + | then throw("Auction is not finished yet") |
---|
| 96 | + | else if (!(isDefined(winner))) |
---|
| 97 | + | then if (isDefined(getString(this, (auctionId + "_lot_passed")))) |
---|
| 98 | + | then throw("Organizer has already got his lot back") |
---|
| 99 | + | else ScriptResult(WriteSet([DataEntry((auctionId + "_lot_passed"), organizer)]), TransferSet([ScriptTransfer(organizerAddr, lotAmount, lotAsset)])) |
---|
| 100 | + | else if (isDefined(getString(this, (auctionId + "_lot_passed")))) |
---|
| 101 | + | then throw("Lot is already passed to the winner, and organizer got his reward") |
---|
| 102 | + | else ScriptResult(WriteSet([DataEntry((auctionId + "_lot_passed"), toBase58String(winnerAddr.bytes))]), TransferSet([ScriptTransfer(winnerAddr, lotAmount, lotAsset), ScriptTransfer(organizerAddr, winAmount, priceAsset)])) |
---|
150 | | - | |
---|
151 | | - | |
---|
152 | | - | @Callable(i) |
---|
153 | | - | func placeBid (auctionId) = { |
---|
154 | | - | let isEggSell = tryGetBoolean((("auction_" + auctionId) + "_eggSell")) |
---|
155 | | - | let paymentAssetId = if ((isEggSell == true)) |
---|
156 | | - | then eggAssetId |
---|
157 | | - | else unit |
---|
158 | | - | let paymentAssetName = if ((isEggSell == true)) |
---|
159 | | - | then "EGG" |
---|
160 | | - | else "WAVES" |
---|
161 | | - | let pmt = value(i.payments[0]) |
---|
162 | | - | let amount = pmt.amount |
---|
163 | | - | let bidId = toBase58String(i.transactionId) |
---|
164 | | - | let assetId = getStringValue(this, (("auction_" + auctionId) + "_assetId")) |
---|
165 | | - | if ((pmt.assetId != paymentAssetId)) |
---|
166 | | - | then throw((("Auction does only support " + paymentAssetName) + " payments.")) |
---|
167 | | - | else { |
---|
168 | | - | let minPrice = tryGetInteger((("auction_" + auctionId) + "_startPrice")) |
---|
169 | | - | if ((minPrice > amount)) |
---|
170 | | - | then throw(("The minimal initial price is set as " + toString(minPrice))) |
---|
171 | | - | else [StringEntry((((("auction_" + auctionId) + "_bid_") + bidId) + "_author"), toString(i.originCaller)), StringEntry((((("assetId_" + assetId) + "_bid_") + bidId) + "_data"), (((((("{\"auctionId\": \"" + auctionId) + "\", \"author\": \"") + toString(i.originCaller)) + "\", \"amount\": \"") + toString(amount)) + "\", \"status\": \"open\"}")), IntegerEntry((((("auction_" + auctionId) + "_bid_") + bidId) + "_amount"), amount), StringEntry(((((("address_" + toString(i.originCaller)) + "_auction_") + auctionId) + "_bid_") + bidId), assetId), StringEntry((((("auction_" + auctionId) + "_bid_") + bidId) + "_status"), "open")] |
---|
172 | | - | } |
---|
173 | | - | } |
---|
174 | | - | |
---|
175 | | - | |
---|
176 | | - | |
---|
177 | | - | @Callable(i) |
---|
178 | | - | func cancelBid (auctionId,bidId) = { |
---|
179 | | - | let assetId = getStringValue(this, (("auction_" + auctionId) + "_assetId")) |
---|
180 | | - | let bidAuthor = tryGetString((((("auction_" + auctionId) + "_bid_") + bidId) + "_author")) |
---|
181 | | - | let bidAmount = tryGetInteger((((("auction_" + auctionId) + "_bid_") + bidId) + "_amount")) |
---|
182 | | - | if ((toString(i.originCaller) != bidAuthor)) |
---|
183 | | - | then throw("Only allowed to the bid author") |
---|
184 | | - | else if ((tryGetString((((("auction_" + auctionId) + "_bid_") + bidId) + "_status")) != "open")) |
---|
185 | | - | then throw("bid is not open") |
---|
186 | | - | else getBidCancelResults(auctionId, bidId) |
---|
187 | | - | } |
---|
188 | | - | |
---|
189 | | - | |
---|
190 | | - | |
---|
191 | | - | @Callable(i) |
---|
192 | | - | func cancelOutdatedBid (auctionId,bidId) = throw("to be released") |
---|
193 | | - | |
---|
194 | | - | |
---|
195 | | - | |
---|
196 | | - | @Callable(i) |
---|
197 | | - | func acceptBid (auctionId,bidId) = { |
---|
198 | | - | let isEggSell = tryGetBoolean((("auction_" + auctionId) + "_eggSell")) |
---|
199 | | - | let paymentAssetId = if ((isEggSell == true)) |
---|
200 | | - | then eggAssetId |
---|
201 | | - | else unit |
---|
202 | | - | let auctionOwner = tryGetString((("auction_" + auctionId) + "_owner")) |
---|
203 | | - | let bidAuthor = tryGetString((((("auction_" + auctionId) + "_bid_") + bidId) + "_author")) |
---|
204 | | - | let bidAmount = tryGetInteger((((("auction_" + auctionId) + "_bid_") + bidId) + "_amount")) |
---|
205 | | - | let bidStatus = tryGetString((((("auction_" + auctionId) + "_bid_") + bidId) + "_status")) |
---|
206 | | - | let assetId = tryGetString((("auction_" + auctionId) + "_assetId")) |
---|
207 | | - | let finalFee = if ((tryGetBoolean((("auction_" + auctionId) + "_isArtefact")) == true)) |
---|
208 | | - | then artefactsFeePercent |
---|
209 | | - | else feePercent |
---|
210 | | - | if ((toString(i.originCaller) != auctionOwner)) |
---|
211 | | - | then throw("Only allowed to auction owner") |
---|
212 | | - | else if ((bidStatus != "open")) |
---|
213 | | - | then throw("Bid was cancelled and cannot be accepted") |
---|
214 | | - | else if ((tryGetString((("auction_" + auctionId) + "_status")) != "open")) |
---|
215 | | - | then throw("Auction was closed") |
---|
216 | | - | else { |
---|
217 | | - | let recordPrice = tryGetInteger((assetId + "_recordPrice")) |
---|
218 | | - | let newRecordPrice = max([recordPrice, bidAmount]) |
---|
219 | | - | [StringEntry((((("auction_" + auctionId) + "_bid_") + bidId) + "_status"), "finished"), StringEntry((("auction_" + auctionId) + "_status"), "finished"), StringEntry((((("assetId_" + assetId) + "_bid_") + bidId) + "_data"), (((((("{\"auctionId\": \"" + auctionId) + "\", \"author\": \"") + toString(i.originCaller)) + "\", \"amount\": \"") + toString(bidAmount)) + "\", \"status\": \"finished\"}")), IntegerEntry((("auction_" + auctionId) + "_finalPrice"), bidAmount), IntegerEntry((assetId + "_recordPrice"), newRecordPrice), DeleteEntry(((((("address_" + bidAuthor) + "_auction_") + auctionId) + "_bid_") + bidId)), DeleteEntry((((("address_" + auctionOwner) + "_auction_") + auctionId) + "_lockedNFT")), ScriptTransfer(i.caller, fraction(bidAmount, (100 - finalFee), 100), paymentAssetId), ScriptTransfer(Address(feeAggregator), fraction(bidAmount, finalFee, 100), paymentAssetId), ScriptTransfer(addressFromStringValue(bidAuthor), 1, fromBase58String(assetId))] |
---|
220 | | - | } |
---|
221 | | - | } |
---|
222 | | - | |
---|
223 | | - | |
---|
224 | | - | @Verifier(tx) |
---|
225 | | - | func verify () = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey) |
---|