logoAcademy

Create a Wrapped Native Token

Wrap the native asset of your blockchain

Creating a wrapped token contract for NAT (myblockchain's native token) involves writing a smart contract that adheres to the ERC-20 standard. This allows NAT to be used in dApps without the need of creating certain behavior for native assets. Here’s a simple example of such a contract written in Solidity:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
contract WrappedNAT {
    string public name = "Wrapped NAT";
    string public symbol = "WNAT";
    uint8 public decimals = 18;
    uint256 public totalSupply;
    
    mapping(address => uint256) private balances;
    mapping(address => mapping(address => uint256)) private allowances;
    
 
    receive() external payable {
        deposit();
    }
 
    function deposit() public payable {
        balances[msg.sender] += msg.value;
        totalSupply += msg.value;
    }
 
    function withdraw(uint256 amount) public {
        require(balances[msg.sender] >= amount, "Insufficient balance");
        balances[msg.sender] -= amount;
        totalSupply -= amount;
        payable(msg.sender).transfer(amount);
    }
 
    function balanceOf(address account) public view returns (uint256) {
        return balances[account];
    }
 
    function transfer(address to, uint256 amount) public returns (bool) {
        require(balances[msg.sender] >= amount, "Insufficient balance");
        balances[msg.sender] -= amount;
        balances[to] += amount;
        return true;
    }
 
    function approve(address spender, uint256 amount) public returns (bool) {
        allowances[msg.sender][spender] = amount;
        return true;
    }
 
    function allowance(address owner, address spender) public view returns (uint256) {
        return allowances[owner][spender];
    }
 
    function transferFrom(address from, address to, uint256 amount) public returns (bool) {
        require(balances[from] >= amount, "Insufficient balance");
        require(allowances[from][msg.sender] >= amount, "Allowance exceeded");
        balances[from] -= amount;
        allowances[from][msg.sender] -= amount;
        balances[to] += amount;
        return true;
    }
}

On this page

No Headings