如何将"transaction fee"添加到ERC20或BEP20智能合约中?



有新的令牌"收取交易费";除了通常的ETH汽油费。我说的是从代币本身减去的交易费用。假设100个代币被转移。1个令牌作为"1"进入中央账户;交易费用";。这是可行的,因为像$GRUMPY这样的硬币有它,但我在代码中找不到允许我这样做的函数。

也在此处插入。我有一些简单的东西,请参阅下面的transfer函数。问题是,它将产生2笔交易和更多的天然气费用。不知道你是否拆分并将费用发送到合同本身,如果这样可以减少费用。

这只是一个想法,而不是测试集。对来自社区的其他解决方案感兴趣。

function _transfer(address sender, address recipient, uint256 amount) internal virtual {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");

address FeeReceiverAdr = 0xE037D395bB24C69bD0347F4e1652199BF60a41d1;
address payable FeeReceiver = payable(FeeReceiverAdr); // Correct since Solidity >= 0.6.0

_beforeTokenTransfer(sender, recipient, amount);

uint bpsfee = 1618;
uint bps = 100000;

uint256 senderBalance = _balances[sender];
require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
_balances[sender] = senderBalance - amount;
_balances[recipient] += taxedValue;

_balances[FeeReceiver] +=  fee;

emit Transfer(sender, recipient, taxedValue);
emit Transfer(sender, FeeReceiver, fee);
}

我找到了一个可能有帮助的解决方案!以防有人还在四处寻找线索。以下是启用ERC20%分发的代码片段。代码尚未经过审核,因此请自行承担使用风险,但这应该足以开始试验。

// SPDX-License-Identifier: MIT

实用主义稳固性^0.6.0;

contract TransfertTokenAndPercentageToTargetAddress{

// pay 1% of all transactions to target address
//test address = testnet
address payable target = 0x326Da5Aa845675a1967e1F7E31A32a4412eD47DD;
// state variables for your token to track balances and to test
mapping (address => uint) public balanceOf;
uint public totalSupply;
// create a token and assign all the tokens to the creator to test
constructor(uint _totalSupply) public {
totalSupply = _totalSupply;
balanceOf[msg.sender] = totalSupply;
}
// the token transfer function with the addition of a 1% share that
// goes to the target address specified above
function transfer(address _to, uint amount) public {
// calculate the share of tokens for your target address
uint shareForX = amount/100;
// save the previous balance of the sender for later assertion
// verify that all works as intended
uint senderBalance = balanceOf[msg.sender];

// check the sender actually has enough tokens to transfer with function 
// modifier
require(senderBalance >= amount, 'Not enough balance');

// reduce senders balance first to prevent the sender from sending more 
// than he owns by submitting multiple transactions
balanceOf[msg.sender] -= amount;

// store the previous balance of the receiver for later assertion
// verify that all works as intended
uint receiverBalance = balanceOf[_to];
// add the amount of tokens to the receiver but deduct the share for the
// target address
balanceOf[_to] += amount-shareForX;

// add the share to the target address
balanceOf[target] += shareForX;
// check that everything works as intended, specifically checking that
// the sum of tokens in all accounts is the same before and after
// the transaction. 
assert(balanceOf[msg.sender] + balanceOf[_to] + shareForX ==
senderBalance + receiverBalance);
}

}

相关内容

  • 没有找到相关文章

最新更新