Taxable Token Development on Polygon

Posted By : Siddharth

Nov 30, 2023

In this blog, we will explore the inner workings of a custom ERC-20 token named MyToken. This token not only adheres to the ERC-20 standard but also incorporates a fee mechanism on each transfer. Let's delve into the code and understand how this innovative token is implemented. Also, visit smart contract development services on various blockchains like Ethereum, Solana, and Polygon. 


Smart Contract Essentials


The Solidity smart contract language is at the core of decentralized applications (DApps) on the Ethereum blockchain. In this project, the contract is written in Solidity version ^0.8.20, emphasizing the use of a specific compiler version.


Token Features

 

Fee Mechanism:

 

One of the standout features of MyToken is the incorporation of a fee mechanism on each transfer. This is achieved through the following steps:

 

Fee Variable:

 

The uint16 fee; variable is introduced to store the fee percentage. This can be adjusted by the owner of the contract.

 

Code - uint16 fee;

 

Transfer Function

 

The standard transfer function from ERC-20 is overridden to include the fee calculation. The fee is deducted from the transferred amount, and the remainder is sent to the intended recipient.

 

Code - function transfer(address to, uint256 amount) public virtual override returns (bool) {
    uint256 feeAmount = amount * fee / 100;
    uint256 transferAmount = amount - feeAmount;
    super.transfer(owner(), feeAmount);
    return super.transfer(to, transferAmount);
}

 

Owner Receives Fee 

 

The deducted fee is transferred to the contract owner. This ensures a seamless revenue stream for the owner proportional to the token transfers.

 

Code - super.transfer(owner(), feeAmount);

 

Constructor Initialization:

 

The contract's constructor initializes crucial parameters during deployment:

 

Code - constructor(string memory _name, string memory _symbol, uint16 _fee) 
    ERC20(_name, _symbol) 
    Ownable(msg.sender) {
    fee = _fee;
    _mint(msg.sender, 1000000000 * 10 ** decimals());
}

 

Token Name and Symbol

 

The name and symbol of the token are set, providing a unique identity on the Ethereum blockchain.

 

Code - ERC20(_name, _symbol)

 

Initial Fee and Total Supply

 

The initial fee percentage is set, and an initial supply of 1,000,000,000 tokens is minted and assigned to the contract deployer.

 

Code - fee = _fee;
_mint(msg.sender, 1000000000 * 10 ** decimals());

 

Updating the Fee

 

To maintain flexibility, the contract includes a function to update the fee. The updateFee function can only be called by the owner of the contract and ensures that the fee remains within a reasonable range (less than or equal to 100%).

 

Code - require(_fee <= 100, "Fee should be less than 100");
fee = _fee;


Conclusion


The MyToken smart contract showcases the power and flexibility of Ethereum smart contracts. By adhering to the ERC-20 standard and incorporating a fee mechanism, this token opens up possibilities for various use cases, from incentivizing token holders to supporting sustainable blockchain projects. As you continue your journey into the world of decentralized finance (DeFi), remember that innovation knows no bounds in the realm of smart contracts and blockchain technology.

 

If you're interested in developing ERC-20 token on Ethereum or Polygon, connect with our blockchain developers to get started. 


Complete Code -

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyToken is ERC20, Ownable {
   uint16 fee;
   constructor(string memory _name, string memory _symbol, uint16 _fee) 
       ERC20(_name, _symbol) 
       Ownable(msg.sender) {
       fee = _fee;
       _mint(msg.sender, 1000000000 * 10 ** decimals());
   }
   function transfer(address to, uint256 amount) public virtual override returns (bool) {
       uint256 feeAmount = amount * fee / 100;
       uint256 transferAmount = amount - feeAmount;
       super.transfer(owner(), feeAmount);
       return super.transfer(to, transferAmount);
   }
   function updateFee(uint16 _fee) public onlyOwner {
       require(_fee <= 100, "Fee should be less than 100");
       fee = _fee;
   }
}

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

May 3, 2024 at 03:58 am

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text