Auction contracts on the Bitcoin network

We designed and implemented a secure auction contract on the Bitcoin network. It is open and transparent, everyone can participate, and the bidder with the highest bid after the bidding ends will win the bid. Bidders are bound by their bids, while auctioneers are bound by auction results.

achieve

// Auction: highest bid before deadline wins
contract Auction {
    
    
    PubKey auctioner;
    int auctionDeadline;

    // bid with a higher offer
    public function bid(Ripemd160 bidder, int bid, SigHashPreimage txPreimage) {
    
    
        require(Tx.checkPreimage(txPreimage));

        int highestBid = Util.value(txPreimage);
        require(bid > highestBid);

        // read previous highest bidder
        bytes lockingScript = Util.scriptCode(txPreimage);
        int scriptLen = len(lockingScript);
        Ripemd160 highestBidder = Ripemd160(lockingScript[scriptLen - Util.PubKeyHashLen : ]);

        // auction continues with a higher bidder
        bytes auctionScript = lockingScript[: scriptLen - Util.PubKeyHashLen] + bidder;
        bytes auctionOutput = Util.buildOutput(auctionScript, bid);

        // refund previous highest bidder
        bytes refundScript = Util.buildPublicKeyHashScript(highestBidder);
        bytes refundOutput = Util.buildOutput(refundScript, highestBid);
        
        require(hash256(auctionScript + refundOutput) == Util.hashOutputs(txPreimage));
    }

    // withdraw after bidding is over
    public function close(Sig sig, SigHashPreimage txPreimage) {
    
    
    		require(Tx.checkPreimage(txPreimage));
    		require(Util.nLocktime(txPreimage) >= this.auctionDeadline);
        require(checkSig(sig, this.auctioner));
	  }
}
  • bid (Bidding) Functional logic: If a higher bid is found, the current winning bidder is updated, and the previous highest bidder is refunded.
  • close (Deal) Functional logic: The auctioneer can close the auction and accept the offer after expiration.

Possible extension

There are many ways to extend this basic contract. For example, if the auctioned item is marked and stored in a UTXO (such as NFT), one input of the transaction Tx can be required to be a UTXO of a token, and it is transferred to the buyer through an output, so that the transaction operation is atomic. May cheat.

Guess you like

Origin blog.csdn.net/freedomhero/article/details/114638176