简体   繁体   English

如何修改 ERC20 代币的转账 function 以便每次代币转账收取 0.0004 以太币的费用

[英]How to modify a transfer function of an ERC20 token so that it takes a fee equal to 0.0004 ether for every token transfer

As I understand I should make transfer function payable but it says "Overriding function changes state mutability from "payable" to "nonpayable".据我了解,我应该将转账 function 设为应付账款,但它表示“覆盖 function 将 state 的可变性从“应付账款”更改为“非应付账款”。

Solidity currently (v0.8) doesn't allow overriding non-payable methods (eg the OpenZeppelin transfer() function) to payable . Solidity 目前(v0.8)不允许将非支付方法(例如 OpenZeppelin transfer()函数)覆盖到payable So this approach won't work:所以这种方法行不通:

pragma solidity ^0.8;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {
    constructor() ERC20("MyToken", "MyT") {
    }

    // cannot override non-payable function to payable
    function transfer(address to, uint256 amount) override public payable returns (bool) {
    }
}

The ERC-20 standard doesn't say anything about the transfer() function being payable - it doesn't explicitly allow nor disallow it. ERC-20标准没有说明transfer() function 是可支付的——它没有明确允许也没有禁止它。


There are two ways to define a payable transfer() function in your case:在您的情况下,有两种方法可以定义payable transfer() function :

  1. Create a local fork of the OpenZeppelin ERC20.sol contract, as well as its dependencies, and change the declaration of their transfer() function to payable .创建 OpenZeppelin ERC20.sol合约及其依赖项的本地分支,并将其transfer() function 的声明更改为payable Then you can override it in a child contract and implement a custom fee.然后您可以在子合同中覆盖它并实施自定义费用。

  2. Implement the ERC20 standard from scratch in your contract without using OpenZeppelin.在不使用 OpenZeppelin 的情况下,在您的合约中从头开始实施 ERC20 标准。 Then you'll be able define a custom transfer() function with the payable modifier.然后,您将能够使用payable修饰符定义自定义transfer() function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM