Monday, April 29, 2024
No menu items!
HomeDatabase ManagementIPFS on AWS, Part 3: Store NFT data on IPFS

IPFS on AWS, Part 3: Store NFT data on IPFS

This series of posts provides a comprehensive introduction to running IPFS (InterPlanetary File System) on AWS:

In Part 1, we introduce the IPFS concepts and test IPFS features on an Amazon Elastic Compute Cloud (Amazon EC2) instance
In Part 2, we propose a reference architecture and build an IPFS cluster on Amazon Elastic Kubernetes Service (Amazon EKS)
In Part 3, we deploy an NFT smart contract using Amazon Managed Blockchain and illustrate the use of IPFS as a way to decentrally store NFT-related data

In this last part of our series, we prepare an NFT smart contract that stores NFT-related data on IPFS and deploy it onto the Ethereum Goerli testnet using Amazon Managed Blockchain. Although you can complete the steps in this post without an IPFS infrastructure, it’s recommended to read at least Part 1 of the series before going further.

Solution overview

For an introduction on the advantages of storing NFT-related data on IPFS and which types of IPFS links to use, refer to Best Practices for Storing NFT Data using IPFS.

In the following sections, we demonstrate how to create a smart contract and Managed Blockchain node, set up MetaMask, deploy our contract on the Goerli testnet, and validate the contract on Etherscan.

Write a smart contract in Remix

We use Remix (an open-source Web3 IDE that can be used directly from your browser) to develop and deploy our smart contract. For simplicity, we also use the OpenZeppelin ERC721 reference implementation.

Download the following contract on your computer.
Open Remix and choose Upload files from the navigation pane to upload the IPFSToken.sol file. Inspect its content in Remix.
Note the following about the solidity code:

All the hard work is done by the ERC721URIStorage class.
The overriding of the _baseURI() function helps save storage space by automatically adding ipfs:// to all token URIs.
The mintToken() function is public. Securing access to such a function is beyond the scope of this post.

In the navigation pane, choose the compiler icon. Choose a compiler version greater than 0.8 and check that the contract compiles without any warning.

Now let’s deploy the contract on the Remix virtual machine (VM).
Choose the deployment icon in the navigation pane, choose the IPFSToken contract, and deploy it locally on the Remix VM.

The IPFSTOKEN contract is now available in the Deployed Contracts in the navigation pane. Let’s mint an NFT token that represents the AWS logo (CID QmRUGmNrjKpZASL4DVjqFXr5groVxPwXByzr9dV2Pyr8Jq) and give it to the local account 2.
Locate and expand the mintToken function.

For tokenOwner, enter the second account address from the list of accounts in the navigation pane.
For tokenURI, insert QmRUGmNrjKpZASL4DVjqFXr5groVxPwXByzr9dV2Pyr8Jq
Choose transact to submit the transaction.

Using the ownerOf and tokenURI functions, we can check that the owner of token 0 is now account 2 and that the token URI is the ipfs URI of the AWS logo.

Create a Managed Blockchain node

Now that we are confident that our smart contract works well (in practice,we would want to write a set of tests and potentially have our code audited before reaching this level of confidence), let’s deploy it on the Goerli test net using Amazon Managed Blockchain. We use the AWS Command Line Interface (AWS CLI) from AWS CloudShell.

Create a node on the Goerli testnet:

AZ=${AWS_REGION}a &&
aws managedblockchain create-node –network-id=n-ethereum-goerli –node-configuration=”{“InstanceType”:”bc.t3.large”, “AvailabilityZone”:”$AZ”}”

Wait for the node to be created (this should take around 30 minutes) and get its ID:

NODE_ID=$(aws managedblockchain list-nodes –network-id=n-ethereum-goerli –query ‘Nodes[?Status == `AVAILABLE`] | [0].Id’ –output text)

There are multiple ways to secure access to the Ethereum node endpoints. We will use the token-based access method. Refer to Using the Ethereum APIs with Amazon Managed Blockchain to select the method that best matches your requirements.

Create an Accessor token:

ACCESSOR_ID=$(aws managedblockchain create-accessor –accessor-type BILLING_TOKEN –query ‘AccessorId’ –output text) &&
BILLING_TOKEN=$(aws managedblockchain get-accessor –accessor-id=$ACCESSOR_ID –query ‘Accessor.BillingToken’ –output text)

You can now assemble the HTTPs endpoint:

ENDPOINT=”https://$NODE_ID.t.ethereum.managedblockchain.$AWS_REGION.amazonaws.com/?billingtoken=$BILLING_TOKEN”

Validate that the endpoint can be accessed by querying the current blockNumber:

curl -X POST -d ‘{“jsonrpc”:”2.0″,”method”:”eth_blockNumber”,”params”:[],”id”: 1}’ “$ENDPOINT”

You should get an answer similar to the following:

{“jsonrpc”:”2.0″,”id”:1,”result”:”0xXXXXXX”}

Set up MetaMask

We will now use the Web3 interface of this endpoint to deploy the smart contract. There are multiple ways to do this. For example, you can compile the smart contract and deploy it with web3.py (see Contract Deployment Example). In our case, we will continue to use Remix, and use MetaMask to connect to the Managed Blockchain endpoint.

Start by downloading and installing MetaMask on your browser.
Choose Get started to setup your wallet:

When your wallet it set up, choose the network drop-down menu (Ethereum Mainnet should be selected) and choose Add network.

Choose Add a network manually and create a new network with the parameters in the following screenshot (New RPC URL must match the endpoint from the previous step):

MetaMask already created an account for you. To get started, we need to fund it with some (Goerli) ETH. There are multiple faucets available for the Goerli testnet (refer to Networks for a complete list), with different authentication requirements. Choose the one of your choice. If you use https://goerlifaucet.com/ for example, creating an account with Alchemy is required.

Deploy a contract on the Goerli testnet

We now connect Remix to MetaMask.

On the Remix webpage, choose the deployment icon in the navigation pane and change the environment to Injected Provider – MetaMask (instead of the default Remix VM).

You might be asked by MetaMask to confirm the connection.
Select your account and choose Next.

Choose Connect.

As in the previous step, choose the contract and deploy it (notice that the Ethereum account created by MetaMask is now selected).

Choose Confirm.

After the transaction is processed by the network, you will see the details of the transaction on the bottom right of the Remix page.

If you choose view on etherscan, you can also analyze the transaction on Etherscan.

Note the address of the IPFSTOKEN contract.

As with the local VM, you can now interact with the contract by minting and transferring tokens. The only difference is that those transactions will be sent on the Goerli testnet by MetaMask through your Managed Blockchain node.

Validate the smart contract on Etherscan

As a final step (and as a courtesy for others who might want to interact with our contract), let’s validate our smart contract on Etherscan using the CONTRACT VERIFICATION – ETHERSCAN Remix plugin (refer to Contract Verification for more details).

In the navigation pane, choose the Plugin manager icon.

Choose Activate next to the CONTRACT VERIFICATION – ETHERSCAN module.
In the navigation pane, choose the Contract Verification – Etherscan icon.

Choose the Settings icon and enter your Etherscan API Key. If you do not have one, you can create an account on Etherscan. Choose Save:

Choose the Home icon to verify the contract.

For Selected Network, Goerli should be already selected

For Contract Name, select IPFSToken
For Contract Address, enter the address of the IPFSTOKEN contract
Choose Verify

Open a new broaswer tab on Etherscan and enter the address of the IPFSTOKEN contract. If all goes well, you should see that the contract source code has been verified:

Anyone with a Web3 browser like MetaMask can now connect and interact directly with the contract through Etherscan:

Congratulations! You successfully deployed an NFT smart contract with tokens referencing objects stored on IPFS.

Conclusion

In this post, we showed how to prepare an NFT smart contract that stores NFT-related data on IPFS and deploy it onto the Ethereum Goerli testnet using Amazon Managed Blockchain.

The smart contract used as an example in this post is very simple. To build a more complex NFT smart contract and create a complete decentralized application to interact with it, refer to the following series of posts:

Develop a Full Stack Serverless NFT Application with Amazon Managed Blockchain – Part 1
Develop a Full Stack Serverless NFT Application with Amazon Managed Blockchain – Part 2

About the Author

Guillaume Goutaudier is a Sr Partner Solution Architect at AWS. He helps companies build strategic technical partnerships with AWS. He is also passionate about blockchain technologies, and a member of the Technical Field Community for blockchain.

Read MoreAWS Database Blog

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments