Introduction
In this article, we learned how to set up the Avalanche blockchain on the DevNet and transfer tokens between wallets. We followed a step-by-step process to install and set up the Avalanche CLI, Docker, and Hardhat—tools needed to work with Avalanche. We also showed how to deploy a smart contract on the local Avalanche C-Chain. Finally, we explained how to transfer tokens from one wallet to another, helping you understand how smart contracts work in a local testing environment.
1. Requirements
System Requirements
- OS: Linux / macOS / Windows with WSL
- RAM: 4 GB minimum
- Disk: 10 GB available
Install Docker
Docker is an essential tool used for quickly deploy and scale applications into any environment, and you know your code will run. (These commands are for Linux).
sudo apt update
sudo apt install docker.io
sudo systemctl enable docker
sudo systemctl start docker
These commands will update your system package list, install Docker, enable it, and start the Docker service.
Install Avalanche CLI
Avalanche CLI is a command-line tool provided by Ava Labs that helps developers interact with Avalanche blockchains.
curl -s https://raw.githubusercontent.com/ava-labs/avalanche-cli/main/scripts/install.sh | bash
Then, restart your terminal and verify.
avalanche --version
![Avalanche CLI]()
Initialize Avalanche CLI
This step lets you configure your CLI for a preferred network and other settings like key management and analytics.
avalanche configure
Create Wallets
To interact with the blockchain and perform transactions, you need wallets.
avalanche key create wallet1
avalanche key create wallet2
This command creates two wallets. You can view and manage their addresses and private keys for contract deployment and token transfers.
Hardhat Setup
Create a Project Directory.
mkdir my-hardhat-project
cd my-hardhat-project
Initialize the Node.js Project -Initializes a package.json
file, which tracks dependencies and configurations.
npm init -y
Install Hardhat
Hardhat is a powerful development framework for Ethereum-compatible blockchains. It allows you to compile contracts, deploy them, test them, and debug in a local environment.
npm install --save-dev hardhat
Create a Hardhat Project -Hardhat is the framework you'll use for writing, testing, and deploying Ethereum-compatible smart contracts.
npx hardhat
- Upon running the above command, you'll be prompted with a few options. Choose.
- "Create a basic sample project."
- Accept the default values (hit Enter).
- Once initialized, your directory will look like this.
![Directory]()
2. Smart Contract
This smart contract, which we place inside the contracts folder, defines an ERC-20 token named MyToken (MTK) using OpenZeppelin’s standard implementation. It mints an initial supply of tokens to the deployer’s address. We use OpenZeppelin, a library that provides predefined, secure smart contracts which we can import and use directly in our projects. The smart Contract is present inside the Contracts folder (Contracts/MyToken.sol).
// contracts/MyToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor(uint256 initialSupply) ERC20("MyToken", "MTK") {
_mint(msg.sender, initialSupply);
}
}
Compile Smart Contracts: This command compiles the smart contracts written in Solidity. I used the force option with the script to bypass warnings and constraints. As you can see, it successfully compiled seven Solidity files.
npx hardhat compile --force
![Smart Contracts]()
3. Hardhat Configuration
This is the Hardhat configuration file that sets up the environment for deploying and interacting with smart contracts. It uses the @nomicfoundation/hardhat-toolbox for extended functionality. It defines a custom network called localavalanche, pointing to a local Avalanche C-Chain RPC URL. It includes a chainId - 43112, which is the chainId of the Avalanche C-chain. We also mentioned the two private keys for the transaction.
require("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: "0.8.28", // Recommend using 0.8.19 if that's what Hardhat+OpenZeppelin support best
networks: {
localavalanche: {
url: "http://127.0.0.1:9650/ext/bc/C/rpc", // 127.0.0.1 is safer than localhost in some setups
timeout: 60000,
chainId: 43112,
accounts: [
"0x2******************************************c40d2e4a20249ab66f094",
"0x6******************************************0ba38a62c3ea9faba5d73"
]
}
}
};
4. Deploying the Smart Contract
This script is used to deploy an ERC-20 token smart contract (named MyToken) using Hardhat. It defines the initial token supply using ethers.parseUnits, and then uses the contract factory to deploy the smart contract by passing the initial value to the constructor. It waits until the deployment is confirmed and then logs the deployed contract’s address.
After obtaining the token address, we need to copy it and paste it into the scripts/transfer.js file.
// my-avalanche-token/scripts/deploy.js
const { ethers } = require("hardhat");
async function main() {
console.log("⏳ Starting deployment...");
const initialSupply = ethers.parseUnits("1000000", 18);
console.log("Initial supply parsed:", initialSupply.toString());
let Token;
console.log("🔍 Attempting to get contract factory...");
try {
Token = await ethers.getContractFactory("MyToken");
console.log("✅ Contract factory fetched");
} catch (err) {
console.error("❌ Could not get contract factory:", err);
process.exit(1);
}
console.log("🚀 Deploying contract...");
try {
const token = await Token.deploy(initialSupply);
console.log("📡 Deploy transaction sent");
await token.waitForDeployment();
console.log("✅ Contract deployed to:", token.target);
} catch (err) {
console.error("❌ Error during deployment:", err);
process.exit(1);
}
}
// Call the main function
main().catch((error) => {
console.error("❌ Unexpected error:", error);
process.exit(1);
});
npx hardhat run scripts/deploy.js
This command deploys the compiled smart contract to the local Hardhat node. As you can see, after running the deployment script, the process starts by parsing the initial supply. The contract is then deployed to the address 0x5fbDB231567afecb367f032d93F642f64180aa3. We need this token address for transferring tokens—simply copy and paste it into the transfer.js script in place of the token address.
![Token address]()
5. Transferring the Token
This script is used for transferring tokens from one address to another. It starts by importing ethers from Hardhat, which provides all the necessary utilities for handling Ethereum-based operations, then accesses two wallets from Hardhat using ethers.getSigners(), which represent the sender and the receiver. It uses ethers.getContractAt(), the script connects to the deployed MyToken contract at the specified address. It calls the transfer
method to transfer tokens from the sender to the receiver, specifying the amount to transfer (100 tokens in this case). After initiating the transfer, the script waits for the transaction to be confirmed using await tx.wait().
const { ethers } = require("hardhat");
async function main() {
const [sender, receiver] = await ethers.getSigners();
const tokenAddress = "0x5Fb******************************4180aa3";
const Token = await ethers.getContractAt("MyToken", tokenAddress);
const tx = await Token.transfer(receiver.address, ethers.parseEther("100"));
await tx.wait();
console.log(
`✅ Transferred 100 tokens from ${sender.address} to ${receiver.address}`
);
}
main().catch((error) => {
console.error("❌ Error:", error);
process.exit(1);
});
This command runs a script that interacts with the deployed smart contract to transfer tokens. The script transfers tokens from the sender's wallet receiver's wallet. As you can see, it successfully transfers 100 tokens.
npx hardhat run scripts/transfer.js
![Transfer tokens]()
Conclusion
In this article, we learned about the complete process of setting up a local Avalanche Devnet and deploying an ERC-20 token using Hardhat. We went through how to write, compile, and deploy smart contracts on a local Avalanche C-Chain. We also learned how to interact with those contracts using scripts, including how to transfer tokens between wallets. To make the development process easier, safer, and more efficient, we used tools like Hardhat and OpenZeppelin. By following these steps, you can gain hands-on experience with smart contract development on the Avalanche network.