Deploy NFT Smart Contracts without Solidity
Tags: web3, nft, blockchain, javascript
Published: 2023-05-15 20:36:08



If your new to web3, or just getting started with Solidity, building out an end to end ERC721 or 1155 smart contract to deploy your NFT collection is no easy feat. In this blog post, we'll explore how you can use Infura's NFT API/SDK to deploy collection using exclusively javascript. 

Supported Networks:
Infura's NFT SDK supports various networks, including Arbitrum, Avalanche (C-Chain), Ethereum, Palm, and Polygon. It's important to note that the write operations on the Palm Testnet network are only supported by the SDK, while read operations can be performed using REST APIs.

Getting Started:
Before diving into the code, make sure you have the latest version of Node.js installed on your system. Once that's done, let's initialize a new project and install the necessary libraries.

Get Test Funds:
Head over to [faucet.infura.io](url) to grab some Sepolia ETH, we're going to need it to deploy our contract. 

Create a new project directory:
```shell
mkdir new_project
cd new_project
npm init -y
```

Install the required libraries:
```shell
npm install -S @infura/sdk
npm i dotenv
```

Create a .env file:
Create a .env file in the project root directory 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>
```
Replace the placeholders with your own Infura project credentials, and add a wallet address and its private key for testing on the supported network.

Create your ContractURI:
This will just be a link to a pinned chunk of json code that tells our program what the metadata of your collection looks like. From the example below mine looks like this:
{
    "description": "jet", 
    "image": "https://gateway.pinata.cloud/ipfs/QmWtgDewkagKn7ZYTFyHfkJ3uYCnmL3gfdWdSQpXs4VCaN/jet_img3.png", 
    "name": "my airplane nft"
  }
and the image links to a picture of an airplane. My metadata indicates that there is only 1 token in the collection, but you can keep adding to the json file for each token as long as they are unique.  

Code Implementation:
Now that we have the project set up and the required libraries installed, let's dive into the code.

```javascript
// Import the libraries and load the environment variables.
const { SDK, Auth, TEMPLATES } = require('@infura/sdk');
require('dotenv').config();

// 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,
  rpcUrl: process.env.RPC_URL,
  chainId: 11155111,
});

// Instantiate SDK
const sdk = new SDK(auth);

async function deployContract() {
  try {
    const newContract = await sdk.deploy({
      template: TEMPLATES.ERC721Mintable,
      params: {
        name: 'Airplanes',
        symbol: 'JET',
        contractURI: 'https://gateway.pinata.cloud/ipfs/Qmbta8RRbdXjyvXwkuqmbsXkZ8ZRoMezNsP7g6n9Db1Mx1',
      },
    });
    console.log('Contract address:', newContract.contractAddress);
  } catch (error) {
    console.error('An error occurred:', error);
  }
}

deployContract();
```

Explanation:
The code above demonstrates a basic implementation using Infura's NFT SDK. First, we import the necessary libraries and load the environment variables from the .env file. The Auth object is then created using the provided credentials.

Next, we instantiate the SDK with the Auth object. Inside the `deployContract` function, we make use of the `sdk.deploy` method to deploy a new NFT contract. We specify the template as `TEMPLATES.ERC721Mintable` and provide additional parameters like the contract name, symbol, and contractURI.

Upon successful deployment, the contract address is logged to the console. This address represents the newly deployed NFT contract. 

Congratulations! You have successfully deployed an ERC721Mintable NFT and are now are the owner of a token on the Sepolia testnet. Want proof? You can check this out [Sepolia Etherscan](https://sepolia.etherscan.io/)