-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.rsh
66 lines (63 loc) · 1.82 KB
/
index.rsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'reach 0.1';
export const main = Reach.App(() => {
const Creator = Participant('Creator', {
getSale: Fun([], Object({
nftId: Token,
minBid: UInt,
lenInBlocks: UInt,
})),
auctionReady: Fun([], Null),
seeBid: Fun([Address, UInt], Null),
showOutcome: Fun([Address, UInt], Null),
});
const Bidder = API('Bidder', {
bid: Fun([UInt], Tuple(Address, UInt)),
});
const V = View({
min: UInt,
nft: Token,
currBid: UInt,
});
init();
Creator.only(() => {
const {nftId, minBid, lenInBlocks} = declassify(interact.getSale());
});
Creator.publish(nftId, minBid, lenInBlocks);
const amt = 1;
commit();
Creator.pay([[amt, nftId]]);
Creator.interact.auctionReady();
V.nft.set(nftId);
V.min.set(minBid);
assert(balance(nftId) == amt, "balance of NFT is wrong");
const end = lastConsensusTime() + lenInBlocks;
const [highestBidder ,lastPrice, isFirstBid] =
parallelReduce([Creator, minBid, true])
.define(() => {
V.currBid.set(lastPrice);
})
.invariant(balance(nftId) == amt)
.invariant(balance() == (isFirstBid ? 0 : lastPrice))
.while(lastConsensusTime() <= end)
.api_(Bidder.bid, (bid) => {
check(bid > lastPrice, "bid is too low");
return [ bid, (notify) => {
notify([highestBidder, lastPrice]);
if ( ! isFirstBid ) {
transfer(lastPrice).to(highestBidder);
}
const who = this;
Creator.interact.seeBid(who, bid);
return [who, bid, false];
}];
})
.timeout(absoluteTime(end), () => {
Creator.publish();
return [highestBidder, lastPrice, isFirstBid];
});
transfer(amt, nftId).to(highestBidder);
if ( ! isFirstBid ) { transfer(lastPrice).to(Creator); }
Creator.interact.showOutcome(highestBidder, lastPrice);
commit();
exit();
});