Automating the end of an auction

I’m trying to implement an NFT Auction and I followed the tutorial on the moralis channel that showed to implement them the following way:

Each time someone outbids another person, the previous bidder’s bid gets put into a pendingReturns array, where that bidder then needs to manually withdraw their money to get it back.

When the auction time is up, people can no longer place bids, but the auction doesn’t officially end (as in the winner gets the NFT and the NFT owner gets the money) until the auctionEnd function is called.

Making a user manually withdraw their money after they get outbid is obviously not the ideal way to do things from a UX perspective, along with making the owner manually end the auction before the NFT can be transferred.

Is it a bad idea to automate this return of funds to the previous bidder right when they get outbid instead of adding it to the pendingReturns mapping for them to take out later? Is there a better way or better place in the contract to do this? If I were to put it at the end of the auction and there were like 10 previous bidders, would the gas costs for calling the end function be enormous?

Also is there a way to automate the ending of the auction so that as soon as the time is up, the transfer of NFT ownership takes place, the auction owner gets paid etc. without needing someone to manually call the end auction method?

Hey @AlekRuzic

If you want to make a functional so that at the end of an auction everyone will automatically recieve their bided money, then you have to decide who will pay gas for the commission (And it will not be expensive if you make all transfers in one transaction.)
I recommend you take a look at ERC20 Crowdsale (you can find there relevant code references e.g. TimedCrowdsale ). Yeah, ERC20 , but I think you easily can rewrite the code to make an ERC721 Auction

1 Like

Interesting, I’ll take a look into that.

I didn’t know that grouping the transfers into one transaction makes much of a difference in terms of gas costs, so I will have to experiment with that a bit too.

In terms of automating the auction end though, do you know of any webhooks or jobs or something like that I can set up so that it automatically ends when the time expires? I took a quick look through the docs and I didn’t really find anything that clearly explains how to solve this.

Hey @AlekRuzic

In terms of automating the auction end though, do you know of any webhooks or jobs or something like that I can set up so that it automatically ends when the time expires? I took a quick look through the docs and I didn’t really find anything that clearly explains how to solve this.

This is a work for smartcontracts. In the article I’ve sent to you there is a TimedCrowdsale. Use this code as a reference

1 Like

I looked into it and TimedCrowdsale only seems to add opening and closing times and the functionality to do things in that time frame which my contract is already capable of, but it doesn’t look like it solves the problem of automatically triggering the endAuction function as soon as the time is up, I’m not sure how opensea does it?

Hey @AlekRuzic

If your smart contract has an event for example the auction is over you can use Sync and Watch Contract Events plugin fo displaying information about the status of the auction on the frontend.

How can it be itriggered?

  • Auction is live:
  1. Manually
  • Auction expired:
  1. Somebody will try to bid -> smart contract will check the status -> trigger event the auction is over
  2. Manually

In your case (auto) you can use Chainlink Alarm Clock (Kovan network), ethereum-alarm-clock and Moralis Jobs

Moralis Cloud Jobs

One of the possible solutions:
You can create a Job in Cloud Functions. You can schedule a job in the Jobs section in the Dashboard by clicking “Schedule a job” in the top right corner.

Cloud functions support Web3 (Web3 in Cloud Code), so you can create for example a Cloud Job which will call a method in your smart contract e.g. contract.methods.endAuction().send(). Before this you can check timestamp and end block of the auction.

In smartcontracts there are mehods: “Read”(their call is free) and “Write”(their call isn’t free).
So, in your case endAuction().send() will be “Write” method and you will pay gas for this. Take a look at web3.eth.Contract (you will need to make a new wallet with small balance to pay for gas and provide a private key)

Hope this will help you :man_mechanic:

1 Like