Deploy a contract
This document describes deploying an ERC721 contract to an Ethereum testnet.
Ensure you installed the NFT SDK, and have your Infura Web3 authentication details available.

Prerequisites

dotenv

Use the dotenv library to manage authentication credentials.
npm install dotenv

Authentication

Create a .env file at the project root and add the following data:
INFURA_PROJECT_ID=<API-KEY>
INFURA_PROJECT_SECRET=<API-KEY-SECRET>
WALLET_PUBLIC_ADDRESS=<WALLET-PUBLIC-ADDRESS>
WALLET_PRIVATE_KEY=<WALLET-PRIVATE-KEY>
Replace the Infura project credential placeholders with your own.
Add a public and private Ethereum address for testing on the supported network the application will run on.

Fund your Ethereum account

Make sure you have enough test ETH to pay the fees.
Try the https://faucet.paradigm.xyz/ faucet to get test ETH.

Create NFT content

Every contract and minted NFT has metadata that describes it.
Metadata information must be accessible via a public URI and resolve to a well-formed json file.

Metadata

The ERC721 contract metadata file contains store-front information for NFT collections.
Create a new file, e.g. metadata.json, and add the details. For example:
{
"name": "ConsenSys NFT SDK contract demo",
"description": "Welcome to the ConsenSys NFT SDK demo store-front.",
"image": "https://bafybeih6oxo5mbvqibbvji3cj7tqs7sitktkeatqwtiynv6ppkze7m6rui.ipfs.infura-ipfs.io/",
"external_link": "https://consensys.net/"
}

NFT content and metadata

Image

We have used the following image for our store-front: https://bafybeih6oxo5mbvqibbvji3cj7tqs7sitktkeatqwtiynv6ppkze7m6rui.ipfs.infura-ipfs.io/ and added the details to the metadata.json file above.
For more information on ERC721 metadata json schemas, check out the OpenZeppelin docs.

metadata.json

We uploaded the metadata.json file to Infura IPFS in the same way: https://bafybeiesrdovwqtcueo2cflwtgkurjkvvvnufwwp2dlnnbsxmnwezdrali.ipfs.infura-ipfs.io/

Deploy the contract

Create a new script

From the project root, create a new script.
touch index.js

Import the libraries and load the environment variables

import { config as loadEnv } from 'dotenv';
import { SDK, Auth, TEMPLATES } from '@infura/sdk';
loadEnv();

Create an Auth object

The SDK wraps authentication parameters in an Auth object which is passed to the constructor.
Check the documentation for the current list of supported networks and add the relevant chainId.
const auth = new Auth({
projectId: process.env.INFURA_PROJECT_ID,
secretId: process.env.INFURA_PROJECT_SECRET,
privateKey: process.env.WALLET_PRIVATE_KEY,
chainId: 5,
});

Instantiate the SDK

The SDK constructor takes an Auth object.
const sdk = new SDK(auth);

Deploy the NFT contract

The SDK uses the ERC721Mintable contract template to deploy the NFT contract.
const newContract = await sdk.deploy({
template: TEMPLATES.ERC721Mintable,
params: {
name: 'NFT contract',
symbol: 'CNSYS',
contractURI: 'https://bafybeiesrdovwqtcueo2cflwtgkurjkvvvnufwwp2dlnnbsxmnwezdrali.ipfs.infura-ipfs.io/',
},
});
console.log(`Contract address is: ${newContract.contractAddress}`);

Run with Node

node index.js
Example output:
Contract address is: 0x5C6464F1a1e9cB4c895F6A85691A80d230d605d4
Last modified 1mo ago
Copy link
On this page
Prerequisites
dotenv
Authentication
Fund your Ethereum account
Create NFT content
Metadata
NFT content and metadata
Deploy the contract
Create a new script
Import the libraries and load the environment variables
Create an Auth object
Instantiate the SDK
Deploy the NFT contract
Run with Node