'NEAR Marketplace - Timed Auction Functionality

We're building a marketplace in NEAR. We have two smart contracts for token and marketplace. Same as NEAR example.

In our platform, we have successfully implemented below features:

  • Token mint (used contract method: nft_mint)
  • Token listing for sale at Fixed Price in marketplace (used contract methods: storage_deposit nft_approve)
  • Token purchase (used contract method: offer)
  • Apply creator royalty at mint time.
  • Apply service fee (Transaction fee of Marketplace) on each sale.

Everything is working fine.

Now, we would like to implement the Timed Auction Functionality

Creator can create NFT’s to be sold as an auction so that buyers can bid on them to get a higher price.

Method:- Sell to the highest bidder: where the highest bid wins at the end.

Making a bid on this kind of auction is like making an offer on a fixed-price listing. The seller can choose to accept it at any time, but the buyer will near of the amount of bid to create a bid.

Questions:

  • How we can implement the Timed Auction Functionality with Marketplace Contract?
  • Do we have any documentation or example for the Timed Auction Functionality?

Example of OpenSea's Timed Auction Functionality: enter image description here



Solution 1:[1]

You can't do that directly from within a contract since the contract does not run all the time you will have to call function trough something like https://cron.cat/ or your own.

Contracts perceive time trough current execution so when a function is called env::block_timestamp_ms() within said fn would give us the current time of execution, you can subtract time from it to get some time in the past. We can do manual checks like is the time stamp past this than do that or the less accurate is the blockheight above certain threshold but all this will have to be executed within a function that needs to be called.

Solution 2:[2]

I will preface this post by saying that this is how I would go about solving this in a simple way. There are more complex solutions out there but I'll try and keep it relatively simple. (scroll to the bottom for a TLDR).

Here's a solution to the timed auction functionality. In the marketplace example that you posted, what you'll want to do is store two extra fields in the sale object indicating the desired length of the auction as well as the time when the sale was listed.

Whenever NFTs are put for sale (in the nft_on_approve function), you'll need to add the current block_timestamp (which will give you the number of nanoseconds since the Unix epoch). You'll also need to store the desired length of the auction which is up to you as to how you would like to implement that. It can be time in nanoseconds, milliseconds, seconds, days, either since the Unix epoch or the actual desired duration of the auction.

Once your sale object contains both these new fields, all you would need to do is whenever an offer is made, check if the current block timestamp is within the desired auction length. If it is, proceed, if it's not panic. Since the marketplace can't constantly be polling to see if the auction is finished, a common approach is to use the functionality I described above:

  • You either bid on the NFT before the timestamp or you didn't. If you did, your bid is accepted, if you don't, it panics.

At this point, there is no way for the marketplace to automatically transfer the NFT to the highest bidder the second the auction closes (due to the marketplace's inability to poll). My suggestion here would be to have a claim function that the highest bidder must call if they want the NFT transferred to them. You can get spicy with this and have timeouts whereby if it isn't claimed within 5 days, the original owner can claim it back but for the sake of simplicity, let's not get into that.

As for the bidding mechanic, there's many different ways you can do this. I'll go over one of the more simple approaches. Currently, when a sale is listed with sale conditions, the NFT can only be purchased for more than or equal to the price as shown here. What you'll want to do is store active bids in the sale object.

You'll need to store both the current highest bidder as well as their bid amount. This will be initialized to empty when the NFT is listed and then once an offer is made for less than the desired list price, it's considered a bid. Bids will only be added if they are less than the desired price and greater than the highest current bid. If the offer is more than the list price, the NFT is automatically sold and the purchase is processed.

Combining everything I've outlined so far in a TLDR:

What you would need to add as a minimum PoC to the sales object:

  • Store desired length of auction (in whatever format you want)
  • Store date of listing
  • Store current highest bidder
  • Store current highest bid

Listing mechanics:

  • When a sale is listed, set the date of listing and the desired length of the auction.

Offer mechanics:

  • make sure the attached deposit is greater than the current highest bid (if none, make sure it's >= 0).
  • If the attached deposit is greater than the list price, process the purchase. If it's not, add it as the current highest bid.
  • Make sure that the any offers are made within the desired length of the auction. If they aren't panic, if they are, you're good to go.
  • If somebody is outbid, make sure you refund them for their deposit!

Claim mechanics:

  • Claims can only be made if the auction time has passed. Only the highest bidder can claim the NFT. Once claimed, the purchase is processed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Benjamin Kurrek