# [tokens] FeeERC20.sol

> ERC20トークンに取引手数料（fee）機能を追加した拡張トークンです。

- URL: https://cardene.dev/books/encrypted-erc/e1e0aa
- 本: Encrypted ERC (https://cardene.dev/books/encrypted-erc.md)
- 章: 21 / 32
- 前の章: https://cardene.dev/books/encrypted-erc/734c0f.md
- 次の章: https://cardene.dev/books/encrypted-erc/2b6eb3.md
- 公開日: 2025-07-07
- 更新日: 2025-07-07
- トピック: Blockchain
- タグ: ブロックチェーン, solidity, smartcontract, avalanch

## 概要

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

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

## コード

https://github.com/ava-labs/EncryptedERC/blob/main/contracts/tokens/FeeERC20.sol

```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`)
    * トータルで送るトークンの量（このうち一部が手数料として差し引かれます）。
