概要

ERC20トークンに取引手数料(fee)機能を追加した拡張トークンです。 OpenZeppelinのERC20コントラクトを継承するSimpleERC20コントラクトをさらに拡張しており、transferFrom 処理において送金額から一定割合の手数料を差し引き、その分を指定された手数料受取人(feeCollector)に送金します。

このようなトークンは、取引時に自動でネットワーク手数料を徴収したいDEXや課金制プラットフォームなどで有効に活用されます。

コード

solidity
// (c) 2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// SPDX-License-Identifier: Ecosystem

pragma solidity 0.8.27;

import {SimpleERC20} from "./SimpleERC20.sol";

/**
 * @title FeeERC20
 * @dev ERC20 token with a fee mechanism for testing
 */
contract FeeERC20 is SimpleERC20 {
    uint256 public feeRate;
    address public feeCollector;

    constructor(
        string memory name,
        string memory symbol,
        uint8 decimal,
        uint256 feeRates,
        address feeCollectors
    ) SimpleERC20(name, symbol, decimal) {
        feeRate = feeRates;
        feeCollector = feeCollectors;
    }

    /**
     * @dev Override transferFrom to apply a fee
     * @param sender The address to transfer from
     * @param recipient The address to transfer to
     * @param amount The amount to transfer
     * @return A boolean that indicates if the operation was successful
     */
    function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) public virtual override returns (bool) {
        address spender = _msgSender();

        // Calculate fee
        uint256 fee = (amount * feeRate) / 100;
        uint256 amountAfterFee = amount - fee;

        // Deduct allowance
        _spendAllowance(sender, spender, amount);

        // Transfer amount after fee to recipient
        _transfer(sender, recipient, amountAfterFee);

        // Transfer fee to fee collector
        if (fee > 0) {
            _transfer(sender, feeCollector, fee);
        }

        return true;
    }
}

State

solidity
uint256 public feeRate;
address public feeCollector;
  • feeRate

    • 手数料率を表す数値(単位:パーセンテージ)。
    • 例えば、feeRate = 2 の場合、送金額の2%が手数料として差し引かれます。
  • feeCollector

    • 差し引かれた手数料を受け取るアドレス。
    • 通常はプラットフォームの収益用ウォレットなどが指定されます。

External Functions

constructor

solidity
constructor(
    string memory name,
    string memory symbol,
    uint8 decimal,
    uint256 feeRates,
    address feeCollectors
) SimpleERC20(name, symbol, decimal) {
    feeRate = feeRates;
    feeCollector = feeCollectors;
}

デプロイ時にトークンの基本情報(名前・シンボル・小数桁数)に加え、手数料率と手数料受取アドレスを設定します。

引数

  • name (string)
    • トークン名(例: "MyToken")。
  • symbol (string)
    • トークンの略称(例: "MTK")。
  • decimal (uint8)
    • 小数桁数(例: 18)。
  • feeRates (uint256)
    • 送金額に対する手数料の割合(%)。
  • feeCollectors (address)
    • 手数料を受け取るアドレス。

transferFrom

solidity
function transferFrom(
    address sender,
    address recipient,
    uint256 amount
) public virtual override returns (bool) {
    address spender = _msgSender();

    uint256 fee = (amount * feeRate) / 100;
    uint256 amountAfterFee = amount - fee;

    _spendAllowance(sender, spender, amount);
    _transfer(sender, recipient, amountAfterFee);

    if (fee > 0) {
        _transfer(sender, feeCollector, fee);
    }

    return true;
}

ERC20標準の transferFrom を上書きして、手数料処理を加えた送金処理を追加した関数。。

通常の transferFrom の動作に加えて、送金額から feeRate % の手数料を計算し、差し引いた金額を recipient に送金して残りの手数料分を feeCollector に送金します。

引数

  • sender (address)
    • トークンを送る元のアドレス。
  • recipient (address)
    • トークンを受け取るアドレス。
  • amount (uint256)
    • トータルで送るトークンの量(このうち一部が手数料として差し引かれます)。