🟢Smart Contract
Sample ERC-20 Token Code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title ExoriumToken
* @dev A simple ERC-20 Token example on the Exorium Network.
*/
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);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract ExoriumToken is IERC20 {
string public name = "Exorium Sample Token";
string public symbol = "EXT";
uint8 public decimals = 18;
uint256 private _totalSupply;
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
/**
* @dev The constructor assigns all initial supply to the deployer (contract owner).
* Adjust total supply or distribution mechanics as needed.
*/
constructor(uint256 initialSupply) {
_totalSupply = initialSupply * (10 ** uint256(decimals));
_balances[msg.sender] = _totalSupply;
// Emit a Transfer event from address(0) to indicate new tokens are minted.
emit Transfer(address(0), msg.sender, _totalSupply);
}
/**
* @dev Returns the total amount of tokens in circulation.
*/
function totalSupply() public view override returns (uint256) {
return _totalSupply;
}
/**
* @dev Returns the token balance of a given account.
*/
function balanceOf(address account) public view override returns (uint256) {
return _balances[account];
}
/**
* @dev Transfers tokens to a specified recipient.
*/
function transfer(address recipient, uint256 amount) public override returns (bool) {
_transfer(msg.sender, recipient, amount);
return true;
}
/**
* @dev Returns the remaining number of tokens that spender can spend on behalf of owner.
*/
function allowance(address owner, address spender)
public
view
override
returns (uint256)
{
return _allowances[owner][spender];
}
/**
* @dev Approves the spender to use a specified amount of tokens on the caller's behalf.
*/
function approve(address spender, uint256 amount) public override returns (bool) {
_approve(msg.sender, spender, amount);
return true;
}
/**
* @dev Transfers tokens on behalf of `sender` to `recipient`, given sufficient allowance.
*/
function transferFrom(
address sender,
address recipient,
uint256 amount
) public override returns (bool) {
_transfer(sender, recipient, amount);
uint256 currentAllowance = _allowances[sender][msg.sender];
require(
currentAllowance >= amount,
"ExoriumToken: transfer amount exceeds allowance"
);
_approve(sender, msg.sender, currentAllowance - amount);
return true;
}
/**
* @dev Internal function to manage token transfers between addresses.
*/
function _transfer(
address sender,
address recipient,
uint256 amount
) internal {
require(sender != address(0), "ExoriumToken: transfer from the zero address");
require(recipient != address(0), "ExoriumToken: transfer to the zero address");
require(_balances[sender] >= amount, "ExoriumToken: transfer amount exceeds balance");
_balances[sender] -= amount;
_balances[recipient] += amount;
emit Transfer(sender, recipient, amount);
}
/**
* @dev Internal function to set an allowance for a spender.
*/
function _approve(
address owner,
address spender,
uint256 amount
) internal {
require(owner != address(0), "ExoriumToken: approve from the zero address");
require(spender != address(0), "ExoriumToken: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
}
Brief Explanation
IERC20 Interface: Outlines the standard ERC-20 functions/events transfer, approve, allowance, etc.
ExoriumToken Contract:
Implements the ERC-20 interface.
Stores
totalSupply
, balances, and allowances in_balances
and_allowances
.The constructor takes an
initialSupply
parameter, allocating all tokens to the deployer by default._transfer
handles the internal transfer logic,_approve
manages allowances.
Name, Symbol, and Decimals: Adjust them to match your project branding.
Minting & Burning (Optional): For certain use cases, you may add
mint
(to create new tokens) orburn
(to destroy tokens).
Deploying to Exorium Network
Add Exorium to Your Wallet/Toolkit
Make sure you’ve added Exorium (mainnet or testnet) in MetaMask or in Hardhat/Truffle config files.
Use the appropriate RPC URL, Chain ID, and other parameters.
Compile & Deploy
If using Remix (remix.ethereum.org), copy this code into a
.sol
file.Select Solidity compiler (version ^0.8.x) and compile.
In Deploy & Run, choose Injected Web3 (or your local environment), ensuring you’re connected to Exorium.
Click Deploy and provide the
initialSupply
(e.g1000000
for 1 million tokens).
Verify the Contract
Navigate to the Exorium block explorer (e.g Exoscan), find your deployed contract address.
Follow the contract verification steps if available so others can verify and interact with your token easily.
Test Transfers
Once deployed, you can transfer tokens to another address using Remix or your wallet.
Check balances and transaction details via the Exoscan explorer.
Tips & Best Practices
Security: Consider using well-audited libraries like OpenZeppelin to minimize bugs.
Gas Optimization: If your contract will be called frequently, implement gas-efficient patterns.
Upgradeability: For more complex projects, explore proxy patterns (UUPS, Transparent Proxy) or on-chain governance.
Integration: For DeFi scenarios, you may add modules like snapshot voting, timelocks, or DEX integration.
Last updated