Programming Languages

Warning: This contract will compile on the /future/ Glow release.

How does the Simple Auction (auction.glow) contract work?

Imagine an auction where each one of the bidders tries to outbid the previous bidder. However, in a blockchain auction, each bidder has to declare the money first to become the top bidder. They get their money back when they are outbid.



Glow code

#lang glow
data Action = Bid(TokenAmount) | BuyItNow | Close;
let simpleAuction = (goods : Assets, expirationTime : Timestamp, buyItNowPrice: TokenAmount) => {
require! Assets.is_positive(goods);
require! expirationTime > currentTime();
deposit! Seller -> goods;
@interaction([Seller, CurrentBidder])
let rec auction = (currentBid) => {
assert! @deposited == goods + currentBid;
choice {
| ForAllParticipant (NewBidder) {
@NewBidder bid = Bid(input(["Enter next bid"], TokenAmout));
publish! NewBidder -> bid ; deposit! NewBidder -> bid;
@NewBidder assume! @value(goods) > @value(bid);
require! currentTime() < expirationTime;
require! bid > currentBid;
require! bid < buyItNowPrice;
withdraw! CurrentBidder <- currentBid;
@interaction([Seller, NewBidder]) auction(bid);
| ForAllParticipant (NewBidder) {
publish! NewBidder -> BuyItNow ; deposit! NewBidder -> buyItNowPrice;
require! currentTime() < expirationTime;
withdraw! NewBidder <- goods;
withdraw! CurrentBidder <- currentBid;
withdraw! Seller <- buyItNowPrice;
| @_ { publish! Close; } =>
require! currentTime() >= expirationTime;
withdraw! Seller <- currentBid;
withdraw! CurrentBidder <- goods;
@interaction([Seller, Seller]) auction(0);
  • Declares the possible commands that could be performed on this contract
  • Only the Seller can create an auction
  • In order to create an auction, a Seller must provide:

The Goods that could be anything that can be represented as an asset, an expirationTime at which point the auction is over and a price at which to buy it immediately buyItNowPrice.

  • The Seller must deposit the goods in the smart contract. This could, for example, a photograph.
  • The real auction begins at 0, and the "first bidder" is the Seller himself.
  • The real auction is an interaction between the Seller and the Current bidder.
  • This function is recursive, so it can call itself.
  • A sanity check, let's make sure that the amount deposited to the contract is the goods plus the current bidder.
  • This is key in this contract; anyone that bids may become a participant.
  • The contract as the new participant for their bid.
  • The new bid should be public and it's deposited into the contract.
  • The new bid must be bigger than the current one in order to replace it.
  • The previous highest bidder gets its money back since it has been replaced by the new bid.
  • Now the auction may continue, recursively, with a new highest bidder.
  • If the new bidder, reaches the buy it now price. Then the auction is settled right now.
  • If the expiration time is reached, then the auction is settled.

Lessons learned

  • Recursive function definitions
  • How to involve participants that are not known beforehand.
  • How to use an expiration date

© IOHK 2015 - 2022

Cardano is an open-source project.

Cardano is a software platform ONLY and does not conduct any independent diligence on, or substantive review of, any blockchain asset, digital currency, cryptocurrency or associated funds. You are fully and solely responsible for evaluating your investments, for determining whether you will exchange blockchain assets based on your own judgement, and for all your decisions as to whether to exchange blockchain assets with Cardano. In many cases, blockchain assets you exchange on the basis of your research may not increase in value, and may decrease in value. Similarly, blockchain assets you exchange on the basis of your research may fall or rise in value after your exchange.

Past performance is not indicative of future results. Any investment in blockchain assets involves the risk of loss of part or all of your investment. The value of the blockchain assets you exchange is subject to market and other investment risks