LYNC
DashboardSupport
  • 👋Introducing LYNC
  • Products
    • LYNC Account Abstraction SDK
    • LYNC Account Abstraction WebGL SDK
    • Metamask Wallet
    • LYNC Metamask PC SDK
    • OKX Wallet
    • LYNC In-Game Marketplace SDK
    • NFT Fetcher
    • No-Code Smart Contract Deployer
    • EVM Lootbox SDK
      • Getting Started
      • Using the SDK
        • Creating a new lootbox
        • Opening a lootbox
        • Claiming rewards
        • Whitelisting users using lootbox
        • Lootbox admin functions
        • Useful information functions from LyncLootBox class
        • Multicall : opening and claiming multiple lootboxes in a single transaction
      • Example Codes in React & TypeScript
        • Example code for creating a new lootbox
        • Example code for opening a lootbox
        • Example code for claiming a lootbox
    • Launch your products on Telegram
      • Configure Your Telegram Bot
      • Deploying Your Product on TG
  • APTOS
    • LYNC Paymaster - Supporting Aptos
      • How to Register on LYNC Dashboard
      • How to Create Paymaster
      • How to Fund the Paymaster
      • Module & Function Whitelist
      • How to Integrate Paymaster
        • Unity
        • API
        • JavaScript/ TypeScript
    • LYNC Unity Aptos SDK
    • Keyless Accounts
      • Configure Your OIDC Provider
      • Integrate Keyless in Unity
    • Wallet Creation and Transaction APIs
      • Pre-requisites
      • Integration
        • API Overview
        • Create a new wallet
        • Get already created wallet
        • Mint NFT transactions
        • Send generic transactions on Aptos
    • LYNC Social Login SDK on Aptos
      • Getting Started
      • Using the SDK
      • Example Codes in React & TypeScript
      • Types Used in the Methods Provided by the SDK
      • Other Specification
    • LYNC Aptos Lootbox
      • Getting Started
      • Creating a Lootbox
      • Opening a Lootbox
      • Claiming the Rewards
      • Conclusion
  • Movement Labs
    • 📇Indexer
      • How to run index custom data?
      • Example Queries
        • Get Token Info
        • Get Token Balances
        • Get NFTs Owned by an Account
    • NFT Deployer
    • LYNC Social Login SDK on Movement
      • Getting Started
      • Using the SDK
      • Example Codes in React & TypeScript
      • Types and Enums Used in the Methods Provided by the SDK
      • Other Specification
    • LYNC Unity Movement SDK
    • Wallet Creation and Transaction APIs
      • Pre-requisites
      • Integration
        • API Overview
        • Create a new wallet
        • Get already created wallet
        • Mint NFT transactions
    • LYNC Paymaster - Supporting Movement
      • How to Register on LYNC Dashboard
      • How to Create Paymaster
      • How to Fund the Paymaster
      • Module & Function Whitelist
      • How to Integrate Paymaster
        • JavaScript/ TypeScript
  • Supra
    • LYNC Paymaster - Supporting Supra L1
      • How to Register on LYNC Dashboard
      • How to Create Paymaster
      • How to Fund the Paymaster
      • Module & Function Whitelist
      • How to Integrate Paymaster
        • JavaScript/ TypeScript
    • LYNC Unity SUPRA SDK
    • Wallet Creation and Transaction APIs
      • Pre-requisites
      • Integration
        • API Overview
        • Create a new wallet
        • Get already created wallet
        • Mint NFT transactions
  • Fuel
    • LYNC Unity Fuel SDK
    • LYNC NFT Deployer
      • Introduction
      • Getting Started
      • Deploying Your NFTs
        • Launch Your Entire Collection
      • Troubleshooting and FAQs
    • LYNC Fuel Lootbox
      • Getting Started
      • Creating a Lootbox
      • Opening a Lootbox
      • Claiming the Rewards
      • Conclusion
    • Wallet Creation and Transaction APIs
      • Pre-requisites
      • Integration
        • API Overview
        • Create a new wallet
        • Get already created wallet
    • Fuel Marketplace NPM SDK
      • Getting Started
      • Using the SDK
        • Using hooks to get the marketplace data
        • Using services provided by the SDK to list, buy, and manage tokens
        • Some useful functions provided by the SDK
        • Error codes for the SDK
        • Interfaces and Enums provided by the SDK
      • Support
  • Metis
    • Wallet Creation and Transaction APIs
      • Pre-requisites
      • Integration
        • API Overview
        • Create a new wallet
        • Get already created wallet
        • Mint NFT transactions
  • NPM Packages
    • Marketplace
      • Hook: useAllCollectionNFT
      • Hook: useAllBuyNFT
      • Hook: useAllOwnerNFT
      • Hook: useNFTDetails
      • Hook: useAllNFTForRent
Powered by GitBook
On this page
  • Example:
  • Types used in the above example:
  1. Products
  2. EVM Lootbox SDK
  3. Example Codes in React & TypeScript

Example code for creating a new lootbox

Example:

import { LyncLootBoxManager, ERC1155Item, ERC20Item, ERC721Item, LootBoxError } from "@lyncworld/lootbox-evm-sdk";
import { ethers } from "ethers";

const lootboxManager = new LyncLootBoxManager();

const erc20Items: ERC20Item = [...]; // array of ERC20 items
const erc721Items: ERC721Item = [...]; // array of ERC721 items
const erc1155Items: ERC1155Item = [...]; // array of ERC1155 items
const whitelist = [...]; // array of whitelisted addresses (Keep empty for no whitelist.)
const trustedForwarder = '0x...'; // trusted forwarder address
const startLootbox = true;

async function createNewLootbox() {
  try {
    const provider = new ethers.providers.Web3Provider(window.ethereum);
    await provider.send("eth_requestAccounts", []);

    const signer = provider.getSigner();

    const responseData = await lootboxManager.createLootbox(
      signer,
      erc20Items,
      erc721Items,
      erc1155Items,
      whitelist,
      trustedForwarder,
      startLootbox
    );
    const lootboxData = responseData[0].data;

    if (!lootboxData) {
      console.error("Something went wrong while creating lootbox. Please try again later.");
      return;
    }

    return lootboxData;
  } catch (err: unknown) {
    console.error("Error creating lootbox: ", err);

    if (err instanceof LootBoxError || err instanceof Error) {
      toast.error(err.message);
    } else {
      toast.error("Something went wrong while creating lootbox. Please try again later.");
    }
  } finally {
    setCreatingLootbox(false);
  }
}

Types used in the above example:

type RangeInclusive = {
  // start of the range
  start: BigNumberish;

  // end of the range
  end: BigNumberish;
};

type ERC20Item = {
  contractAddress?: string;
  name?: string;
  symbol?: string;

  // When a user opens the loot box, they will get token amounts between this range
  pickRange: RangeInclusive;

  // Total amount of tokens to be deposited to the loot box for rewards 
  tokenAmount: BigNumberish;
};

type ERC721Item = {
  contractAddress?: string;
  name?: string;
  symbol?: string;
  uriBase?: string;

  // When a user opens the loot box, they will get randomly NFTs with token IDs between
  // this range.
  pickRange: RangeInclusive;

  // Range of token IDs to be deposited to the loot box for rewards
  inputTokenIdRange: RangeInclusive;

  // Array of unique token IDs to be deposited to the loot box for rewards
  inputTokenIds: BigNumberish[];
};

Note - In the above type for ERC721 item, either `inputTokenIdRange` or `inputTokenIds` has to be passed when creating a lootbox using the SDK. You cannot pass both `inputTokenIdRange` and `inputTokenIds`.

type ERC1155Item = {
  contractAddress?: string;
  name?: string;
  symbol?: string;
  uriBase?: string;

  // When a user opens the loot box, they will get randomly NFTs with token IDs between
  // this range.
  pickRange: RangeInclusive;

  // Range of token IDs to be deposited to the loot box for rewards
  inputTokenIdRange: RangeInclusive;

  // Fixed token amounts for each token IDS provided in `inputTokenIdRange` (above)
  inputTokenAmountFixed: BigNumberish;

  // Fixed range of NFTs to be picked corresponding to the `inputTokenIdRange`
  // (above) when a user opens a loot box.
  amountPickRangeFixed: RangeInclusive;

  // For example - Let's say you have `inputTokenIdRange` {start: 1, end: 10} with
  // `inputTokenAmountFixed` 20 (for each token ID 1 through 10) and
  // `amountPickRangeFixed` {start: 5, end: 8} (for each token Ids 1 through 10).
  // Then when a user opens a lootbox and gets the token ID 2 as a reward.
  // Then he/she will receive the token amount in the range of 5 to 8 as passed in
  // `amountPickRangeFixed`

  // Array of unique token IDs to be deposited to the loot box for rewards
  inputTokenIds: BigNumberish[];

  // Array of token amounts corresponding to the `inputTokenIds` (above)
  inputTokenAmounts: BigNumberish[];

  // Array of range of NFTs to be picked corresponding to the `inputTokenIds` (above) when a user opens a loot box.
  amountPickRanges: RangeInclusive[];

  // For example - Let's say you have `inputTokenIds` [1, 2, 3] with
  // `inputTokenAmounts` [40, 50, 60] and `amountPickRanges` [{start: 5, end: 6},
  // {start: 7, end: 8}, {start: 9, end: 10}]. Then when a user opens a loot box and get
  // the token ID 2 as a reward. Then he/she will receive the token amount in the range
  // of 7 to 8 as passed in `amountPickRanges`
};

Note - In the above type for the ERC1155 item, either `inputTokenIdRange`, `inputTokenAmountFixed`, `amountPickRangeFixed` or `inputTokenIds`, `inputTokenAmounts`, `amountPickRanges` has to be passed when creating a loot box using the SDK. You cannot pass both `inputTokenIdRange`, `inputTokenAmountFixed`, `amountPickRangeFixed` and `inputTokenIds`, `inputTokenAmounts`, `amountPickRanges`.

enum LBErrorCodes {
  NORMALIZATION_ERROR = "NORMALIZATION_ERROR",
  VALIDATION_ERROR = "VALIDATION_ERROR",
  INITIALIZATION_ERROR = "INITIALIZATION_ERROR",
  LOOTBOX_OPENED_ERROR = "LOOTBOX_OPENED_ERROR",
  LOOTBOX_MULTICALL_OPEN_ERROR = "LOOTBOX_MULTICALL_OPEN_ERROR",
  LOOTBOX_MULTICALL_CLAIM_ERROR = "LOOTBOX_MULTICALL_CLAIM_ERROR",
  TOO_MANY_REQUESTS_ERROR = "TOO_MANY_REQUESTS_ERROR",
  LOOTBOX_REWARDS_CLAIM_ERROR = "LOOTBOX_REWARDS_CLAIM_ERROR",
  RNG_PENDING_ERROR = "RNG_PENDING_ERROR",
  RNG_TIMEOUT_ERROR = "RNG_TIMEOUT_ERROR",
  LOOTBOX_ACTIVE_ERROR = "LOOTBOX_ACTIVE_ERROR",
  LOOTBOX_INACTIVE_ERROR = "LOOTBOX_INACTIVE_ERROR",
  LOOTBOX_PERMANENTLY_STOPPED_ERROR = "LOOTBOX_PERMANENTLY_STOPPED_ERROR",
  APPROVAL_ERROR = "APPROVAL_ERROR",
  LOOTBOX_CREATION_ERROR = "LOOTBOX_CREATION_ERROR",
  MERKLE_ROOT_MISMATCH_ERROR = "MERKLE_ROOT_MISMATCH_ERROR",
  SERVER_ERROR = "SERVER_ERROR",
  LOOTBOX_ACTIVATION_ERROR = "LOOTBOX_ACTIVATION_ERROR",
  LOOTBOX_ITEM_REMOVAL_ERROR = "LOOTBOX_ITEM_REMOVAL_ERROR",
  REMAINING_OPENS_UPDATE_ERROR = "REMAINING_OPENS_UPDATE_ERROR",
  TRUSTED_FORWARDER_UPDATE_ERROR = "TRUSTED_FORWARDER_UPDATE_ERROR",
  TRANSFER_OWNER_ERROR = "TRANSFER_OWNER_ERROR",
}

interface ApprovalData {
  itemAddress: string;
  itemType: ItemType;
  amount?: string;
}

interface CreateLootBoxData {
  lootBoxAddress: string;
  itemAddresses: string[];
}

interface TransactionResponse {
  txn: ContractTransaction;
  receipt: ContractReceipt;
  data?: ApprovalData | CreateLootBoxData;
}
PreviousExample Codes in React & TypeScriptNextExample code for opening a lootbox

Last updated 8 months ago