Chainstack Self-Hosted is now available! Launch production-grade blockchain nodes on infrastructure you control.    Get started
  • Pricing
  • Docs

Avalanche subnets tutorial series: Avalanche smart contracts

Created Apr 25, 2022 Updated Oct 20, 2022
Avax 4 1024x655 logo

This is the fourth article in the Avalanche subnet tutorial series. Check out the other articles here:

  1. What is a subnet?
  2. Running a local Avalanche node on Fuji testnet.
  3. Creating a subnet, then create a blockchain on it.
  4. Deploying a smart contract. <– You are here.
  5. Indexing subnet with The Graph.

This project has come a long way. At this point, a local node has been set up; then it is staked and is a validator node for the Fuji testnet; then a subnet was created, and the node was assigned to validate the subnet; then a brand new blockchain was established on it.

Just a quick recap:

  • The name of the blockchain is chainstacksubnet;
  • Its ID is 2t27BUokp5Pj8zNQhSNVgqYHChSqrd4k8ewSBrEMXoixxjsXQj. (The ID is unique for every blockchain and subnet).
  • It is a Ethereum Virtual Machine (EVM) blockchain. Based on the genesis block information, 333.33 blockchain token was pre-stored in the address 8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC.

So, the first objective of this tutorial is to transfer some of the tokens out.

How to access your subnet with MetaMask?

Like every other EVM blockchains, subnet EVM too supports the (arguably) most widely used tool-MetaMask. This section guides the reader to configure MetaMask to access a subnet.

Metamask logo

Firstly, open MetaMask, click Add Network:

Image 57 613x1024 logo

Fill in the following information:

Image 58 1024x579 logo
  • Network Name: *any name is ok
  • New RPC URL: http://127.0.0.1:9650/ext/bc/*fill in your blockchain id/rpc Example: http://127.0.0.1:9650/ext/bc/2t27BUokp5Pj8zNQhSNVgqYHChSqrd4k8ewSBrEMXoixxjsXQj/rpc
  • Chain ID: 13213
  • Currency Symbol: *any symbol is ok

Now MetaMask should successfully link to the blockchain. It now can be used to access the initial address. The initial address is defined during creation of genesis block.

Its public address is 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC.

Its private key is 56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027.

This private key is publicly shared, it is also given in the official tutorial. Do not use this account on mainnet or testnet.

Now import the private key into MetaMask.

Click the fox head to open MetaMask. Click on the top right icon, click Import Account:

Image 59 729x1024 logo

Key in the private key and click Import:

Image 60 633x1024 logo

The account should be added into MetaMask. A fresh account should contain 333.3333 tokens.

Image 61 728x1024 logo

Now funds can be sent to another account by clicking on Send button.

Input an address or select Transfer between my accounts. Complete the steps to send 30 tokens to another account. The genesis specified the minimum base fee as 13000000000 which is equivalent to 0.0000000144 native token.

Image 62 668x1024 logo

Once the transaction is finalised, it is shown in the Activity section. The blockchain is successfully deployed and compatible with MetaMask.

Image 63 1024x548 logo

Sending Avalanche smart contracts with Remix

Next step is deploying a smart contract on the blockchain. Remix is used to do that.

Remix is a popular IDE for creating/deploying smart contracts on Ethereum based blockchains. It can be used with Avalanche subnet EVM blockchain too.

First, make sure the previous section is finished successfully and MetaMask is connected.

Log in to Remix. In the left-most pane, click on file explorer logo.

Image 64 1024x637 logo

Right-click on contracts folder, create a new file.

Name the file Gravatar.sol (any name is ok).

Go to Etherscan.io, search for 0x2E645469f354BB4F5c8a05B3b30A929361cf77eC. This is the contract for Gravatar Registry, it will be moved to our subnet.

Scroll down to the lower half of the page, click the Contract tab, copy its source code.

Image 65 1024x569 logo

Go back to Remix, paste it into Gravatar.sol and go to the solidity compiler in the left pane.

Image 66 1024x805 logo

Compile Gravatar.sol. When it is finished, you will see GravatarRegistry in the contract dropdown menu. It means the contract is successfully compiled. Next step is to deploy it to the subnet blockchain.

Image 67 1024x841 logo

Click Deploy tab in the left pane.

Set environment to Injected Web3 to use MetaMask.

Select an account with sufficient funds.

Select the contract GravatarRegistry.

Click Deploy.

Image 68 577x1024 logo

Click confirm on MetaMask pop-up.

Image 69 608x1024 logo

It takes a while to finish the transaction. After the smart contract is successfully deployed, it can be tested in the Remix IDE.

In the Deploy tab, a new section shows up now for Deployed Contracts. The methods from the GravatarRegistry contract are displayed there. It means the contract is successfully deployed. Feel free to play around with all the methods.

Image 70 625x1024 logo

Click on createGravatar, enter a displayName and imageURL, click transact.

Image 71 1024x792 logo

Finish this step in MetaMask by clicking on the Confirm button.

Image 72 468x1024 logo

The transaction shows up in the activity section on MetaMask too:

Image 73 710x1024 logo

Use Remix to test the getGravatar function with the owner’s address, it returns the image URL.

Image 74 1024x705 logo

Now the smart contract is successfully deployed on the subnet.

Conclusion

This is the end of this article. In this article we deployed a smart contract on our blockchain using Remix and MetaMask. Hoping it is useful.

To clarify any question, please feel free to send your message in Chainstack’s discord channel.

Happy coding.

Have you already explored what you can achieve with Chainstack? Get started for free today.

SHARE THIS ARTICLE
Sepolia ETH faucet banner

Sepolia Faucet – Claim Free Sepolia ETH

If you are in need of Sepolia ETH, here is a brief tutorial on how to use the Chainstack faucet in order to get up to 0.5 Sepolia ETH every day.

Dsc 4476 1 150x150 logo
Andrei Popa
Oct 13
Customer Stories

BonusTrade.AI

BonusTrade.AI unlocks real-time crypto market insights with low latency and high-performance Chainstack infrastructure.

Copperx

Making recurring multi-chain billing and payment links accessible.

Trava.Finance

Reliable and high-performance infrastructure across multiple blockchain networks.