Skip to content

Quick Start

It's a Quick Start Guide for starting using Multichain SDK.

We will create ERC-721 Lazy Minting NFT and list it in the Ethereum network to start using Multichain SDK. To get more information, use the links in each section.

Installation

Installing Rarible SDK using We3:

yarn add @rarible/sdk
yarn add tslib@2.3.1
yarn add web3@1.5.0

or using Ethers:

yarn add @rarible/sdk
yarn add tslib@2.3.1
yarn add ethers@5.6.2

Check that the SDK is installed correctly:

npm view @rarible/sdk version

Using SDK

Create a project with the JS framework of your choice (we'll be using NextJS here).

To properly set up the Rarible SDK, we need to follow standard Web3 practices.

  1. Grab the Ethereum object out of the global window object.
  2. Use it to create a new instance of Web3.
  3. Create a new instance of EthereumWallet class.
  4. Create Rarible SDK with a new instance of ethereumWallet, created in the previous step.

In code, it looks like that (using TypeScript):

// Imports
import Web3 from "web3"
import { createRaribleSdk } from "@rarible/sdk"
import { EthereumWallet } from "@rarible/sdk-wallet"
import { Blockchain } from "@rarible/api-client"
import { Web3Ethereum } from "@rarible/web3-ethereum"

// Code
const { ethereum } = window as any
const web3 = new Web3(provider)
const web3Ethereum = new Web3Ethereum({ web3 })
const ethWallet = new EthereumWallet(web3Ethereum)
const raribleSdk = createRaribleSdk(ethWallet, "staging")

If using Ethers you should use @rarible/ethers-ethereum library for creating an EthereumWallet. Here is an example of creating all supported providers:

// Imports
import type Wallet from "ethereumjs-wallet"
import Web3 from "web3"
import { ethers } from "ethers"
import { Web3Ethereum } from "@rarible/web3-ethereum"
import { EthersEthereum, EthersWeb3ProviderEthereum } from "@rarible/ethers-ethereum"

// Code
export function createProviders(provider: any, wallet: Wallet) {
    const web3 = new Web3(provider)
    const ethersWeb3Provider = new ethers.providers.Web3Provider(provider)

    return {
        web3,
        providers: [
            new Web3Ethereum({ web3 }),
            new EthersEthereum(new ethers.Wallet(wallet.getPrivateKeyString(), ethersWeb3Provider)),
            new EthersWeb3ProviderEthereum(ethersWeb3Provider),
        ],
    }
}

const ethWallet = new EthereumWallet(ethersEthereumProviderInstance)
const raribleSdk = createRaribleSdk(ethWallet, "staging")

In createRaribleSdk, we have several environment parameters:

  • prod (mainnet)
  • testnet (goerli)

The difference between them is the chain Id and the Rarible API endpoint.

And if you're creating any blockchain application that will interact with users, you'll still need to implement the connect Metamask button to get their wallet connected.

ERC-721 NFT Lazy Minting & Sell

Often users want to list their NFTs on the sale right after creation. For this case, use mintAndSell function, which allows you to do exactly that.

const currentWallet = wallet as EthereumWallet;
const makerAccount = await currentWallet.ethereum.getFrom();
// Price in ETH
const price: number = 1;
const mintRequest: PrepareMintRequest = {
  collectionId: toContractAddress(
    "ETHEREUM:CONTRACT_ADDRESS"
  ),
};
const ethCurrency: EthErc20AssetType = {
  "@type": "ERC20",
  contract: toContractAddress(
    "ETHEREUM:CONTRACT_ADDRESS"
  ),
};
const mintResponse = await sdk.nft.mintAndSell(mintRequest);
const response = await mintResponse.submit({
  uri,
  supply: 1,
  lazyMint: true,
  price,
  creators: [
    {
      account: toUnionAddress(`ETHEREUM:${makerAccount}`),
      value: 10000,
    },
  ],
  currency: ethCurrency,
});
// Response:
// ItemId
// OrderId

See Reference overview page for more information about SDK usage.