Exorium Guide
  • 🟢Introduction
  • âš«Architecture
  • 🟢Governance Model
    • Treasury & Resource Allocation
    • Benefits of Decentralized Governance
    • Future Governance Enhancements
  • âš«Validator
  • 🟢Tokenomics
    • Utility & Use Cases
    • Staking & Reward Mechanisms
    • Sustainability & Growth
  • Getting Started
    • âš«Getting Started
    • 🟢Setup Exorium Network
    • âš«Consensus Engine
    • 🟢Genesis File
      • Example of a Minimal Genesis File
      • Maintenance & Version Control
    • âš«Contributing
    • 🟢Smart Contract
    • âš«ERC721
  • 🟢Roadmap
  • Staking Guide
    • Formula for APY
Powered by GitBook
On this page
  • Sample ERC-20 Token Code
  • Tips & Best Practices
  1. Getting Started

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

  1. IERC20 Interface: Outlines the standard ERC-20 functions/events transfer, approve, allowance, etc.

  2. 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.

  3. Name, Symbol, and Decimals: Adjust them to match your project branding.

  4. Minting & Burning (Optional): For certain use cases, you may add mint (to create new tokens) or burn (to destroy tokens).

Deploying to Exorium Network

  1. 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.

  2. 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.g 1000000 for 1 million tokens).

  3. 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.

  4. 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

  • 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.

The sample contract above provides a straightforward example of an ERC-20 token that’s compatible with the Exorium Network. After deployment, your token will follow a standardized interface, allowing easy integration with wallets, DeFi platforms, or any application that supports EVM-based ERC-20 tokens. Feel free to customize and expand this foundation to match the vision of your project!

PreviousContributingNextERC721

Last updated 5 months ago

Security: Consider using well-audited libraries like to minimize bugs.

🟢
OpenZeppelin