Venom In Action. Simple NFT auction
This guide shows you how you can perform on-chain interaction with previously deployed TIP-4 token
Fist of all, as usual, we should setup our development environment with locklift. For this smart-contracts guideline you need to include both TIP-3 and TIP-4 dependencies, because our Auction will be accepted in TIP-3 tokens. Let's explore some scheme of our contracts interaction and describe it
NFT creating is a green arrows flow, auction bids is a yellow. Let's describe a processes
User mints its own NFT via Collection contract
Then user deploys an Auction
Auction deploys its own TIP-3 TokenWallet via given TokenRoot address (familiar mechanic for you from TIP-3 Tokensale guide)
User sends minted NFT to Auction, which one implementing
INftTransferinterface and accept this NFTAnother users sends TIP-3 tokens (bid) to Auction with
notify = trueparameter (see TIP-3 specs or TIP-3 guide)Auction's TokenWallet send a callback to Auction, which one handle TIP-3 transfer - checks if incoming bid amount more than previous bid, and updates a leader bid address
When time is over, finishAuction function will send NFT to auction winner or old owner, if there is no bids was accepted
That's all! As you can see, the main mechanic of our interaction is a callbacks. Let's start implement our contracts. First, implement Collection and NFT contracts same as in TIP-4 quick start guide.
Then, let's deal with Auction contract. We'll get started from state and constructor, as usual. Do not forget to add interfaces we need.
Remember about gas management and token wallet deploying mechanics from previous Venom In Action guide. Implement onTokenWallet callback the same way.
Ok, the next callback we need is onNftTransfer, that will be called when NFT owner send NFT to Auction address
Great! Now we are ready to accept bids. Let's implement another callback onAcceptTokensTransfer, that our TokenWallet will call any time it got an incoming token transaction. Take attention! This is the main logic of our auction!
That's it. How hard is that? The last thing we need - finishAuction function.
You can explore this sample (with tests and some scripts) by going to this <todo: link> repository. But we should talks about scripts we need, because this sample needs not only deploy scripts. Moving on.
We can take collection deploying script and NFT minting scripts from TIP-4 quick start. Script for auction deploying not a really hard too.
The next script, that can be useful for you - sending NFT to Auction. Let's code
Pay attention on callback parameter of NFT's transfer method
This is really important step. You may lose your NFT if don't specify callback for our auction, because callback onNftTransfer won't be called. Same idea should be used by your auction participants. They should send TIP-3 tokens to Auction with notify: true parameter:
All you need now is a write some tests with locklift supports. This all-in-one example with locklift environment, some simple tests and deploy scripts is available in repo.
Last updated