EIPs Explained

ERC-7837 · Final(最终) · ERC · 2024-12-07

ERC-7837: Diffusive Tokens

一种同质化代币:transfer 不是转移发送者的代币,而是向接收者铸造新代币,按每个代币收取 ETH 费用支付给发行者,供应上限由所有者设定。

它解决什么问题?

在 ERC-20 代币中,总供应量通过转账重新分配:A 向 B 发送 10 个代币,A 就少了 10 个。ERC-7837 描述了另一种经济模型,即 Diffusive Tokens(DIFF),面向这样的场景:发行者希望代币按需扩散,而不是预先铸造再来回传递。

规范中的例子是制造商为一批实体产品发行代币:每个代币之后可以销毁,以兑换一件商品。持有者通过向他人铸造新代币来"传递"代币,每次都要向发行者支付费用,直到达到最大供应量。

它如何运作

  • transfer 即铸造。 transfer(to, amount) 不会减少发送者的余额,而是向 to 铸造 amount 个新代币并增加 totalSupply。
  • 原生币费用。 发送者必须通过 msg.value 以该链的原生货币支付 transferFee * amount,否则调用会 revert。费用累积在合约中。
  • 供应上限。 会使 totalSupply 超过 maxSupply 的转账会 revert。
  • 销毁。 持有者可以 burn(amount) 来减少自己的余额和总供应量,例如用来兑换对应的商品。
  • 所有者权限。 所有者可以执行 setTransferFee、setMaxSupply(不得低于当前供应量)和 withdrawFees(recipient)。
  • 可选授权。 为兼容而提供 approve/transferFrom;transferFrom 会消耗额度,但仍然向 to 铸造,而不扣减 from。

事件:Transfer、Burn、FeeUpdated、MaxSupplyUpdated。

由于 transfer 和 transferFrom 是 payable 且行为不同,DIFF 仅在接口层面与 ERC-20 兼容,行为上并不兼容。

示例代码

核心逻辑,从规范的参考实现中精简而来:

// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;

contract DiffusiveToken {
    uint256 public totalSupply;
    uint256 public maxSupply;
    uint256 public transferFee;          // wei per token
    address public owner;
    mapping(address => uint256) public balanceOf;

    event Transfer(address indexed from, address indexed to, uint256 amount);
    event Burn(address indexed burner, uint256 amount);

    constructor(uint256 _maxSupply, uint256 _fee) {
        maxSupply = _maxSupply;
        transferFee = _fee;
        owner = msg.sender;
    }

    function transfer(address to, uint256 amount) external payable returns (bool) {
        require(to != address(0) && amount > 0, "bad args");
        require(msg.value >= transferFee * amount, "insufficient fee");
        require(totalSupply + amount <= maxSupply, "cap");
        balanceOf[to] += amount;          // sender keeps their balance
        totalSupply += amount;
        emit Transfer(msg.sender, to, amount);
        return true;
    }

    function burn(uint256 amount) external {
        balanceOf[msg.sender] -= amount;  // reverts on underflow
        totalSupply -= amount;
        emit Burn(msg.sender, amount);
    }

    function withdrawFees(address payable to) external {
        require(msg.sender == owner, "owner");
        (bool ok, ) = to.call{value: address(this).balance}("");
        require(ok, "withdraw failed");
    }
}

需要注意

  • 状态为 Final。
  • 任何人都可以调用 transfer,与自己的余额无关。按规范的写法,向他人铸造代币并不要求持有代币;费用和上限是仅有的限制。
  • DEX、借贷协议和钱包都假定 ERC-20 转账会保持供应量守恒。把 DIFF 代币与它们集成可能产生错误的价格、记账或余额,而且 payable 的转账在不发送 value 的调用方中会失败。
  • 所有者可以随时更改费用并提高上限,这对持有者来说是一个重大的信任假设。
  • 规范中的参考合约若照搬会有 bug(例如它读取了从未声明的 balances 和 allowances 映射);使用任何实现之前都要审查并测试。

#token #fungible #minting #erc-20 #fees

详细信息

作者
Cheng Qian (@jamesavechives) <[email protected]>
状态
Final(最终)
类型
Standards Track / ERC
创建日期
2024-12-07

机器可读数据: Markdown · JSON