简体   繁体   English

erc20 智能合约中的交易费用

[英]Transction Fees in erc20 smart contract

I ran into an issue...我遇到了一个问题...

I built a erc20 smart contract but dont know how to add features such as a transaction fees on each transaction.我构建了一个 erc20 智能合约,但不知道如何在每笔交易中添加交易费用等功能。

How to code a transaction fees on each transaction?如何编码每笔交易的交易费用?

Not sure exactly what you're asking - but if you want to take a cut of the transaction whenever an ERC20 is transferred you could do this:.不确定您到底要问什么 - 但如果您想在 ERC20 转移时从交易中分一杯羹,您可以这样做:。

In the ERC20 contract: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol在 ERC20 合约中: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol

Add owner to state, and update the constructor to be:将所有者添加到 state,并将构造函数更新为:

    address contractDeployer;
    uint txCharge = 1 // As we can't store decimals (1 = 1%)

    constructor(string memory name_, string memory symbol_) {
        _name = name_;
        _symbol = symbol_;
        contractDeployer= msg.sender;
    }

Then, modify the transfer function to be:然后,将转帐 function 修改为:

function transfer(address to, uint256 amount) public virtual override returns (bool) {
        address owner = _msgSender();
        // Transfers 1% of the amount to send to the contract deployer (you)
        _transfer(owner, contractDeployer, amount * txCharge / 100); 
        // Transfers the remaining 99% to the actual recipient
        _transfer(owner, to, amount - (amount * txCharge/100);
        return true;
    }

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

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