Coder Social home page Coder Social logo

worldspaceinc / quiz Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 2.0 208 KB

Reward users who pass a set of questions with an ERC-1155 NFT!

Home Page: https://quiz.thirdweb-example.com

License: Apache License 2.0

Shell 0.34% JavaScript 0.63% TypeScript 67.84% CSS 31.19%

quiz's Introduction

Signature Based Minting with Next.JS

Introduction

In this guide, we ask users to answer some questions about thirdweb before they can mint an NFT using signature-based minting!

Tools:

Using This Repo

  • Create a project using this example by running:
npx thirdweb create --template quiz
  • Create your Edition using the thirdweb dashboard
  • Create an environment variable in a .env.local file with your private key, in the form PRIVATE_KEY=xxx, similarly to the .env.example file provided.
  • Ensure to change the contract addresses to your own!

What is Signature Based Minting?

Signature-based minting allows you to grant mint signatures from an admin wallet (that has the minter role) in the contract, and have another wallet mint an NFT that you specify in your contract.

import { ChainId, ThirdwebProvider } from "@thirdweb-dev/react";

// This is the chainId your dApp will work on.
const activeChainId = ChainId.Goerli;

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <ThirdwebProvider desiredChainId={activeChainId}>
      <Component {...pageProps} />
    </ThirdwebProvider>
  );
}

Connecting User's Wallets

Over at the home page at index.tsx, we're using the thirdweb React SDK MetaMask Connector so our user can connect their wallet to our website.

import {
  useAddress,
  useDisconnect,
  useMetamask,
  useNFTCollection,
} from "@thirdweb-dev/react";

  // Helpful thirdweb hooks to connect and manage the wallet from metamask.
  const address = useAddress();
  const connectWithMetamask = useMetamask();
  const isOnWrongNetwork = useNetworkMismatch();
  const [, switchNetwork] = useNetwork();

};

Quiz Questions

We have two stateful variables to store which questions the user is on and if they have failed the quiz:

const [questionNumber, setQuestionNumber] = useState(0);
const [failed, setFailed] = useState(false);

The QuizContainer component stores an array of questions, and their possible answers.

The current question (questionNumber) the user is on is shown in the QuizQuestion component.

The user can click one of the answers to select it.

  • If they get it correct, they see the next question
  • If they get it wrong, they fail the quiz!

When the user finishes the final question and got all questions correct, they see the Mint NFT button which triggers the signature-based minting process outlined below.

Creating NFTs with Signature-based Minting

The way that our signature-based minting process works is in 3 steps:

image.png

  1. The connected wallet calls a Next JS API Route.

  2. The API route generates a signature to mint an NFT based on token ID 0's metadata in the Edition contract.

  3. Once the API function is done processing, it sends the client/user a signature. The user can call mint with this signature to create an NFT with the conditions provided by our server.

API Route

To create an API Route, you'll need to create a file in the /pages/api directory of your project.

On the server-side API route, we can:

  • Run a few checks to see if the requested NFT meets our criteria
  • Generate a signature to mint the NFT if it does.
  • Send the signature back to the client/user if the NFT is eligible.

Initialize the Thirdweb SDK on the server-side

// Initialize the Thirdweb SDK on the serverside
const sdk = ThirdwebSDK.fromPrivateKey(
  // Your wallet private key (read it in from .env.local file)
  process.env.PRIVATE_KEY as string,
  "goerli"
);

Load the NFT Collection via it's contract address using the SDK

const nftCollection = sdk.getEdition(
  // Replace this with your Edition contract address
  "0x000000000000000000000000000000000000000"
);

Generate a signature to mint the NFT

// Generate the signature for the page NFT
const signedPayload = await editionContract.signature.generateFromTokenId({
  quantity: 1,
  tokenId: 0,
  to: address,
});

Return the signature to the client

// Return back the signedPayload to the client.
res.status(200).json(signedPayload);

If at any point this process fails or the request is not valid, we send back an error response instead of the generated signature.

res.status(500).json({ error: `Server error ${e}` });

Making the API Request on the client

With our API route available, we make fetch requests to this API, and securely run that code on the server-side.

Call the API route on the client

// Make a request to /api/server
const signedPayloadReq = await fetch(`/api/server`, {
  method: "POST",
  body: JSON.stringify({
    address: address, // Address of the current user
  }),
});

Mint the NFT with the signature

// Now we can call signature.mint and pass in the signed payload that we received from the server.
// This means we provided a signature for the user to mint an NFT with.
const nft = await nftCollection?.signature.mint(signedPayload);

Join our Discord!

For any questions, suggestions, join our discord at https://discord.gg/thirdweb.

quiz's People

Contributors

jarrodwatts avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.