Last active
October 13, 2023 22:34
-
-
Save cpacia/a8c46f79b77d7a55bd367f822b56ae79 to your computer and use it in GitHub Desktop.
Auction contract
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```lisp | |
;; This is an example of an auction contract. Users can place new bids by calling | |
;; the bid method with the appropriate inputs and outputs: | |
;; | |
;; Bid tx-format: | |
;; -------------- | |
;; Input-0: This contract | |
;; Input-1: Bid input | |
;; | |
;; Output-0: This contract | |
;; Output-1: Refund to previous high bidder | |
;; Output-2: Change | |
(lambda (script-params unlocking-params input-index private-params public-params) | |
;; Grab the locktime fields from the public parameters | |
!(def locktime !(param locktime)) | |
!(def locktime-precision !(param locktime-precision)) | |
;; Assert that the locktime precision is less within the required range. | |
!(assert (<= locktime-precision 60)) | |
;; Bid is a method to the contract. It can be called any time prior to | |
;; expiration to increase the current bid amount. | |
;; | |
;; It will also refund the previous high bidder his money. | |
!(defun bid (bid-amount refund-output) ( | |
;; Import the encryption function from the standard library | |
!(import std/crypto) | |
;; Set a bunch of variables based on the input parameters | |
!(def input-script-hash !(param priv-in 0 script-hash)) | |
!(def state !(param priv-in 0 state )) | |
!(def current-bid (car state)) | |
!(def prev-bidder-comm (car (cdr state))) | |
!(def asset-id !(param priv-in 0 asset-id)) | |
!(def refund-comm (hash refund-output)) | |
!(def bid-amount (car (cdr unlocking-params))) | |
!(def new-state !(list bid-amount refund-comm)) | |
!(def in-salt !(param priv-in 0 salt)) | |
!(def new-salt (hash in-salt)) | |
!(def new-output !(list input-script-hash bid-amount asset-id new-state new-salt)) | |
;; Assert that input 1's amount is greater than or equal to the bid amount provided | |
;; in the unlocking params. | |
!(assert (>= !(param priv-in 1 amount) bid-amount)) | |
;; Assert that output 0 is constructed as expected | |
;; - script-hash is same as the input script-hash (recursion) | |
;; - amount is equal to the new bid | |
;; - asset-id is equal to the input asset id | |
;; - the new state is of form (bid-amount bidder-comm) | |
;; - the salt is the hash of the previous salt | |
!(assert-eq !(param priv-out 0) new-output) | |
;; Assert that the input 1 amount is greater than the current bid | |
!(assert (> bid-amount current-bid)) | |
;; Assert that the bidder's commitment contains the correct amount | |
!(assert-eq bid-amount (list-get 1 refund-output)) | |
;; Assert that the bidder's commitment contains the correct asset id | |
!(assert-eq asset-id (list-get 2 refund-output)) | |
;; Assert the public output 1 commitment is equal to the prev-bidder-comm | |
!(assert-eq !(param pub-out 1) prev-bidder-comm) | |
;; Assert that the public output 0 ciphertext is equal to the encryption of | |
;; the new contract output. | |
!(assert-eq !(param pub-out 0 ciphertext) (encrypt new-salt new-output)) | |
;; Assert that the tranasction's locktime is less than the auction expiration | |
!(assert (< locktime (car script-params))) | |
;; Return true | |
t | |
)) | |
;; Withdraw is another method. It can be used the by the auction beneficiary to withdraw | |
;; the funds after expiration | |
!(defun withdraw (signature) ( | |
;; Import the check-sig function from the standard library | |
!(import std/crypto) | |
;; Assert that the tranasction's locktime is greater than or equal to the auction expiration | |
!(assert (>= locktime (car script-params))) | |
;; Assert that the transaction's sig-hash is signed by the key provided in the script-params | |
!(assert (check-sig signature (car (cdr script-params)) !(param sighash))) | |
;; Return true | |
t | |
)) | |
;; Execute one of the two methods based on the selection in the unlocking params | |
(if (= (car unlocking-params) 0) | |
(bid (list-get 1 unlocking-params) (list-get 2 unlocking-params)) | |
(if (= (car unlocking-params) 1) | |
(withdraw (list-get 1 unlocking-params)) | |
nil)) | |
) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment