/ auction / src / main.adl
main.adl
 1  program auction.alpha {
 2      // A bid in an auction.
 3      // - `owner`     : The address of the account that owns the record associated with this bid.
 4      //                 This is separate from the address of the account that placed the bid.
 5      // - `bidder`    : The address of the account that placed the bid.
 6      // - `amount`    : The amount of the bid.
 7      // - `is_winner` : Whether the bid is the winning bid.
 8      record Bid {
 9          owner: address,
10          bidder: address,
11          amount: u64,
12          is_winner: bool,
13      }
14  
15      // Returns a new bid.
16      // - `bidder` : The address of the account that placed the bid.
17      // - `amount` : The amount of the bid.
18      // Requires that `bidder` matches the function caller.
19      // The owner of the record is set to the entity responsible for running the auction (auction runner).
20      // The address of the auction runner is ax1y49n95642ayf5k5fun0h9vmf62c2jmkhls0vl0szu3n429py558sdmftzx.
21      transition place_bid(bidder: address, amount: u64) -> Bid {
22          // Ensure the caller is the auction bidder.
23          assert_eq(self.caller, bidder);
24          // Return a new 'Bid' record for the auction bidder.
25          return Bid {
26              owner: ax1y49n95642ayf5k5fun0h9vmf62c2jmkhls0vl0szu3n429py558sdmftzx,
27              bidder: bidder,
28              amount: amount,
29              is_winner: false,
30          };
31      }
32  
33      // Returns the winning bid.
34      // - `first`  : The first bid.
35      // - `second` : The second bid.
36      // Requires that the function caller is the auction runner.
37      // Assumes that the function is invoked only after the bidding period has ended.
38      // In the event of a tie, the first bid is selected.
39      transition resolve(first: Bid, second: Bid) -> Bid {
40          // Ensure the caller is the auctioneer.
41          assert_eq(self.caller, ax1y49n95642ayf5k5fun0h9vmf62c2jmkhls0vl0szu3n429py558sdmftzx);
42          // Resolve the winner of the auction.
43          if (first.amount >= second.amount) {
44              return first;
45          } else {
46              return second;
47          }
48      }
49  
50      // Returns ownership of the bid to bidder.
51      // - `bid` : The winning bid.
52      // Requires that the function caller is the auction runner.
53      // Assumes that the function is invoked only after all bids have been resolved.
54      transition finish(bid: Bid) -> Bid {
55          // Ensure the caller is the auctioneer.
56          assert_eq(self.caller, ax1y49n95642ayf5k5fun0h9vmf62c2jmkhls0vl0szu3n429py558sdmftzx);
57          // Return 'is_winner' as 'true' in the winning 'Bid'.
58          return Bid {
59              owner: bid.bidder,
60              bidder: bid.bidder,
61              amount: bid.amount,
62              is_winner: true,
63          };
64      }
65  
66      // The constructor is configured to prevent upgrades.
67      @noupgrade
68      async constructor() {}
69  }
70