2

I am working on a quiz site but I want the winners and participants to receive NFT at the end of the quiz. But I do not want to manually sign the transactions. I understand I can use ethers.js and use the sendTransaction method to do this. However I am not sure How to write a smart contract for this. I have two NFTs on thirdweb for testing purposes. What I want to achieve is, as soon as the user clicks on the claim button, I want the transaction to be signed on my end automatically in the backend and the user wallet address that I am capturing at the start of quiz with the connect wallet button will be used to send the NFT from xyz marketplace/my-wallet/thirdweb. Also I am not sure if it is possible to send an NFT without a wallet directly to the user wallet address, I do not want to go for lazyminting. My whole project is in simple vanilla JS, css and html and I do not want to use react, so I did not go for thirdweb too.

note: Apologies if this is too silly, I am still new to all this and have not done too much web2/web3 development. This is the biggest project that I am working on my own for now.

2
  • Just connect your front end to the smart contract via ABI. And use any meta transaction sdk like biconomy or gelato for gasless transactions. Notice that both of these are two different steps and independent of each other. Commented Sep 12, 2023 at 1:51
  • Thanks so much for sharing this. I looked online for these two and it is really interesting. One thing which I do not understand is why is someone paying the gas fees on my behalf and that too for free? Commented Sep 12, 2023 at 6:45

1 Answer 1

1

Remix is an excellent tool for writing an deploying smart contracts directly in your browser.

To create a smart contract capable of what you are asking, you will need an ERC-721 contract (which is the NFT contract).

You don't necessarily need anything besides this to make what you want work, all you would need to do is call transfer() on the NFT contract with the address you want to send the NFT to and the tokenId of the NFT you want to send.

To make this call, and any function call for that matter, you will need two things:

  1. The address of the contract you want to call
  2. The abi of the functions you want to call, in this case transfer().

Note that using this method requires you to own the NFTs prior to being able to transfer() them.

Another method you could use if you'd like to create a new NFT collection to award your users with, would be to create a custom function that allows users to mint() an NFT upon completing your quiz.

function claimNft(address to) public {
_mint(to, totalSupply()); 

This function allows anyone to get one of your NFTs by simply inputting their address. (Note this example uses totalSupply() which isn't required by the ERC 721 standard, but you could easily add a counter to track the tokenId as opposed to using the totalSupply() if you didn't want to use it).

Also note, this example is based upon Open Zeppelin's ERC721 contract that comes with the _mint() function.

I recommend you play around with an ERC-721 contract inside of Remix for a little before attempting to integrate it into your project, this way you are a bit more comfortable and familiar with it.

Here is a bare bones example contract that inherits the standard from Open Zeppelin:

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";


contract NFT is ERC721{

    constructor(string memory name, string memory symbol) ERC721(name, symbol){
    }

    
}

Feel free to copy and paste it into your own Remix file to get started. Lastly, you'll need to provide a name and symbol for your NFT collection upon deploying it, good luck and have fun!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.