Hi, my name is Heorhii. In this guide, I will show you how to create and deploy your own ERC20 smart contract to the Somnia network using Remix IDE. This tutorial is meant for developers who already know the basics of Solidity and have MetaMask installed.
What is Somnia. Somnia is an EVM-compatible Layer 1 blockchain designed to support mass-consumer real-time applications. It offers higher throughput and faster finality compared to Ethereum, while keeping compatibility with Ethereum tools and smart contracts.
In this guide, we will create a standard ERC20 token, which is a smart contract that allows users to transfer tokens, approve others to spend tokens on their behalf, and manage balances. It is important to note that ERC20 tokens are different from native Somnia tokens, which are used to pay gas fees.
What you need before starting:
Basic knowledge of Solidity
MetaMask installed and connected to the Somnia DevNet
Some STT tokens in your wallet (you can get them from a faucet)
Remix IDE open at https://remix.ethereum.org
Step 1: create the IERC20 interface. First, we need to define the standard interface for the ERC20 token. Create a new file called IERC20.sol
and paste the following code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
}
This interface acts as a blueprint. Any contract that implements this interface will follow the ERC20 standard, making it compatible with wallets, dApps, and exchanges.
Step 2: create the ERC20 smart contract. Now, create another file called ERC20.sol
and paste the following code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;
import "./IERC20.sol";
contract ERC20 is IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
string public name;
string public symbol;
uint8 public decimals;
constructor(string memory _name, string memory _symbol, uint8 _decimals) {
name = _name;
symbol = _symbol;
decimals = _decimals;
}
function transfer(address recipient, uint256 amount) external returns (bool) {
balanceOf[msg.sender] -= amount;
balanceOf[recipient] += amount;
emit Transfer(msg.sender, recipient, amount);
return true;
}
function approve(address spender, uint256 amount) external returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool) {
allowance[sender][msg.sender] -= amount;
balanceOf[sender] -= amount;
balanceOf[recipient] += amount;
emit Transfer(sender, recipient, amount);
return true;
}
function _mint(address to, uint256 amount) internal {
balanceOf[to] += amount;
totalSupply += amount;
emit Transfer(address(0), to, amount);
}
function _burn(address from, uint256 amount) internal {
balanceOf[from] -= amount;
totalSupply -= amount;
emit Transfer(from, address(0), amount);
}
function mint(address to, uint256 amount) external {
_mint(to, amount);
}
function burn(address from, uint256 amount) external {
_burn(from, amount);
}
}
Step 3: explanation of the functions. Here is what each part does:
constructor
: sets the name, symbol, and number of decimals
transfer
: sends tokens from the caller to another address
approve
: allows a spender to withdraw tokens from the caller up to a specified amount
transferFrom
: allows a spender to transfer tokens from one address to another, if allowed
_mint
: internal function to create new tokens
_burn
: internal function to destroy tokens
mint
: public wrapper to call _mint
burn
: public wrapper to call _burn
Events:
Transfer
: emitted whenever tokens are transferred, minted, or burned
Approval
: emitted whenever a new allowance is set
Step 4: compile the smart contract. Click on the "Solidity Compiler" tab in Remix. Select the right version of Solidity (at least 0.8.22). Then click "Compile ERC20.sol".
Once compiled successfully, Remix will generate the bytecode and ABI for the contract.
Step 5: deploy the smart contract. Go to the "Deploy and run transactions" tab.
Set the environment to "Injected Provider - MetaMask"
Select the account that holds STT tokens
In the deployment fields, provide the following parameters:
_name
(string): for example, "MyToken"
_symbol
(string): for example, "MTK"
_decimals
(uint8): typically 18
Click "Deploy" and approve the transaction in MetaMask.
Once deployed, you will see your contract under "Deployed Contracts" with options to interact with it.
Step 6: interact with your token. You can now call functions like:
mint
: to create new tokens
burn
: to destroy tokens
transfer
: to move tokens between addresses
approve
and transferFrom
: to allow others to spend on your behalf
You can also check balances and allowances.
Learn more about this:
Alternative: using OpenZeppelin. Instead of writing everything from scratch, you can use OpenZeppelin, a trusted library of secure smart contracts.
Here is a simple example using OpenZeppelin:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;
import "@openzeppelin/contracts@4.0.0/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts@4.0.0/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts@4.0.0/access/Ownable.sol";
contract MyToken is ERC20, ERC20Burnable, Ownable {
constructor(address initialOwner)
ERC20("MyToken", "MTK")
Ownable()
{}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
}
The deployment process for this contract is exactly the same as shown earlier.
Conclusion. Congratulations. You have successfully created and deployed your own ERC20 token smart contract on the Somnia network. You now have a working token contract that you can build on, extend, or integrate into larger decentralized applications.
Happy coding.
— Heorhii (colliseum)
Somnia Social: