Links

Deploy a contract

This document describes deploying an ERC721 contract to an Ethereum testnet. The SDK provides support for multiple contract templates. We will use the ERC721Mintable contract template to deploy the NFT contract. View the usage examples to learn how to use some of the other templates.
Ensure you installed the NFT SDK, and have your Infura Web3 authentication details available.

Prerequisites

Steps:

Initialize a new project

mkdir new_project
cd new_project
npm install -D typescript ts-node
npx tsc --init

Install the libraries

Install the Infura NFT API:
npm install -S @infura/sdk
Install the dotenv package to load environment variables from a .env file to store configuration in the environment separate from the code.
npm i dotenv

Create your .env file

Create a .env file at the project root and add the following data:
INFURA_API_KEY=<API-KEY>
INFURA_API_KEY_SECRET=<API-KEY-SECRET>
WALLET_PUBLIC_ADDRESS=<WALLET-PUBLIC-ADDRESS>
WALLET_PRIVATE_KEY=<WALLET-PRIVATE-KEY>
INFURA_IPFS_PROJECT_ID=<PROJECT-ID>
INFURA_IPFS_PROJECT_SECRET=<PROJECT-SECRET
Replace the Infura project credential placeholders with your own.
Add a wallet address and its private key for testing on the supported network the application will run on.
Never disclose your private keys or secret recovery phrases. Do not commit the .env file to your repository if it contains sensitive data. You can create a .gitignore file to prevent accidentally committing the file.

Create your script

touch index.ts

Import the libraries and create an auth object

Import the libraries and load the environment variables.
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.
index.ts
// Import the libraries and load the environment variables.
import { config as loadEnv } from 'dotenv';
import { SDK, Auth, TEMPLATES, Metadata } from '@infura/sdk';
loadEnv();
// Create Auth object
const auth = new Auth({
projectId: process.env.INFURA_API_KEY,
secretId: process.env.INFURA_API_KEY_SECRET,
privateKey: process.env.WALLET_PRIVATE_KEY,
chainId: 5,
ipfs: {
projectId: process.env.INFURA_IPFS_PROJECT_ID,
apiKeySecret: process.env.INFURA_IPFS_PROJECT_SECRET,
},
});
// Instantiate SDK
const sdk = new SDK(auth);

Create NFT metadata

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.
In our example, the metadata looks as follows:
{
"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://myawesomewebsite.net"
}
Upload the contract metadata using the SDK:
index.ts
(async() => {
try {
// CREATE CONTRACT Metadata
const collectionMetadata = Metadata.openSeaCollectionLevelStandard({
name: 'ConsenSys NFT SDK contract demo',
description: "Welcome to the ConsenSys NFT SDK demo store-front.",
image: await sdk.storeFile({
metadata: 'https://bafybeih6oxo5mbvqibbvji3cj7tqs7sitktkeatqwtiynv6ppkze7m6rui.ipfs.infura-ipfs.io/',
}),
external_link: 'https://myawesomewebsite.net',
});
console.log('collectionMetadata ----', collectionMetadata);
const storeMetadata = await sdk.storeMetadata({ metadata: collectionMetadata });
console.log('storeMetadata', storeMetadata);
} catch (error) {
console.log(error);
}
})();

Deploy the NFT contract

The SDK uses the ERC721Mintable contract template to deploy the NFT contract. Add the following code in the async block.
index.ts
const newContract = await sdk.deploy({
template: TEMPLATES.ERC721Mintable,
params: {
name: 'NFT contract',
symbol: 'CNSYS',
contractURI:storeMetadata,
},
});
console.log(`Contract address is: ${newContract.contractAddress}`);

Run the script

The full code sample looks as follows:
index.ts
// Import the libraries and load the environment variables.
import { config as loadEnv } from 'dotenv';
import { SDK, Auth, TEMPLATES, Metadata } from '@infura/sdk';
loadEnv();
// Create Auth object
const auth = new Auth({
projectId: process.env.INFURA_API_KEY,
secretId: process.env.INFURA_API_KEY_SECRET,
privateKey: process.env.WALLET_PRIVATE_KEY,
chainId: 5,
ipfs: {
projectId: process.env.INFURA_IPFS_PROJECT_ID,
apiKeySecret: process.env.INFURA_IPFS_PROJECT_SECRET,
},
});
// Instantiate SDK
const sdk = new SDK(auth);
(async() => {
try {
// CREATE CONTRACT Metadata
const collectionMetadata = Metadata.openSeaCollectionLevelStandard({
name: 'ConsenSys NFT SDK contract demo',
description: "Welcome to the ConsenSys NFT SDK demo store-front.",
image: await sdk.storeFile({
metadata: 'https://bafybeih6oxo5mbvqibbvji3cj7tqs7sitktkeatqwtiynv6ppkze7m6rui.ipfs.infura-ipfs.io/',
}),
external_link: 'https://myawesomewebsite.net',
});
console.log('collectionMetadata ----', collectionMetadata);
const storeMetadata = await sdk.storeMetadata({ metadata: collectionMetadata });
console.log('storeMetadata', storeMetadata);
const newContract = await sdk.deploy({
template: TEMPLATES.ERC721Mintable,
params: {
name: 'NFT contract',
symbol: 'CNSYS',
contractURI:storeMetadata,
},
});
console.log(`Contract address is: ${newContract.contractAddress}`);
} catch (error) {
console.log(error);
}
})();
Run the script:
npx ts-node index.ts
Example output:
collectionMetadata ---- {"name":"ConsenSys NFT SDK contract demo","description":"Welcome to the ConsenSys NFT SDK demo store-front.","image":"ipfs://QmfTxWLFrTVoTQELke5wstM6fEEXUKDUaSd37HUJ9SvjAR","external_link":"https://myawesomewebsite.net"}
storeMetadata ipfs://QmfSQg9ZysC4PkNokxBFy6BAFtBxLwVCKQWEBXHnt5G3Er
Contract address is: 0x6FD6ea9C1EEd65E05D5A9768234587567050BD75
Last modified 2mo ago