This repository serves as a repository to demonstrate how to write a dutch auction smart contract in the Aiken Smart Contract Language for Cardano.
A dutch auction is essentially an auction where the price starts at a certain price, but the price goes lower and lower as the auction goes on.
For example, an auction can begin at a price of $100,000, then after an hour, the price can go down to $90,000, then perhaps after another hour the price goes down to $81,000, and so on.
The Smart Contract supports the capability of auctioning one asset, or many of the same asset for a set price.
A decay rate: Defining the rate of price decay. For example, if the decay rate is 50, then the price will decay by 5%. The decay rate must be a value between 1,999. Similarly, if the decay rate is 100, then the price will decay by 10% at each time step.
A time step: This defines the time step that an advance in the auction must be incremented by to move forward. Let's say the auction starts 12:00pm, and ends at 5:00pm, and the time step is 1 hour, then at each time step the price will decay by the increment defined by the decay rate, and eventually the auction will end at 5:00pm.
An output reference: This allows for each instance of the auction to be defined by a unique NFT. This guarantees that ALL auctions are unique and can't be mixed up. It also allows for easy tracking of the auction state.
Price: The price, in ADA, of the asset at the current state of the auction. Asset: The asset being sold. end_bound: The end bound of the current state of the auction. beneficiary: Defines where proceeds of the sales must be paid to in the case of a purchase, or where leftovers of the assets must be paid to in case of an auction end burn. final_time: defines the ending time of the auction, which would allow for a burn of the NFT and for the assets left over to be returned to the beneficiary.
The contract is initiated with a mint of an NFT with a script comprised of the above mentioned parameters. At the same moment of the mint, a UTxO is created initiating the auction state with the datum described above living at that UTxO.
To make a purchase a transaction must be constructed spending the UTxO with the auction NFT, depositing the asset to the purchaser, depositing ada to the beneficiary, and spending the NFT back to the script. If the time for the current state of the script has elapsed, then a purchase can't be made, and the script is in a locked state where the price must be updated.
To update the script, the NFT containing UTxO must be spent back to the script with an updated datum incrementing the time state, and decreasing the price.
Eventually, one of two things will happen: The assets will be purchased, in which case the NFT can be burned freeing the minAda attached to the NFT, and the auction instance is gone forever. The assets are not purchased, and the time state exceeds the final time, and the NFT can be burned returning leftoever assets to the beneficiary, and the auction instance is gone forever.
Below is a transition state diagram describing the dutch auction contract.
Install aiken version 1.1.7.
Run:
aiken build
To run the aiken tests run:
aiken check
After building, tests can be run with the off chain code to demonstrate deployment and transitions of the contract.
To do this, in the off-chain directory run:
npm i
tsc
This will build the runnable .mjs files in the dist/ directory.
The pertinent files are: mint_and_purchase.mjs advance.mjs burn_auction_ended.mjs burn_sold_out.mjs
To run any of them run node off-chain/dist/file.mjs, for example:
node off-chain/dist/mint_and_purchase.mjs
Mint: https://preview.cardanoscan.io/transaction/a2a73b7440e9f831832756553b467aa11da5c352669f2992613c007afb61ed67 Purchase: https://preview.cardanoscan.io/transaction/44e828195f1f011f3823803c597bc5ed4591c591e36dfd1c2e40362bd8b7c0c3 Advance: https://preview.cardanoscan.io/transaction/b7829d28f45d76aa59c3a08fcfb8ffaaaac72767e6a85fddbbfe1306e854b840 Burn Sold Out: https://preview.cardanoscan.io/transaction/22763aeab25ad6e690ed00b48b6f0f93450bccd786c8c377928cb46f9c6c982e Burn Auction Ended: https://preview.cardanoscan.io/transaction/2cba1b024d7bdf539b131f413dc454cea4b3e58a48551947b8e8b7f9558ee0f7
