Solidity

從 _transfer 函式的費用中排除地址

  • January 10, 2022

我打算不讓合約的部署者支付 9% 的費用,這筆費用是在每次轉賬時支付的。我試圖通過向*_transfer*添加一個 if 語句來檢查發件人是否與部署者俱有相同的地址來做到這一點。然而,在 pancakeswap 上測試後,部署者仍然需要付費。

contract token is ERC20, Ownable {
   using SafeMath for uint256;
   uint feePercentage = 9;
   address public deployer;

   constructor(uint256 initialSupply) ERC20("token", "tkn") {
       _mint(msg.sender, initialSupply);
       deployer = msg.sender;
   }

   function _transfer(
       address sender,
       address recipient,
       uint256 amount
   ) internal override {
       if (sender != deployer) {
           uint256 FeeAmount = amount.mul(feePercentage) / 100;
           super._transfer(sender, deployer, FeeAmount);
           amount -= FeeAmount;
       }
       super._transfer(sender, recipient, amount);
   }
} 

有一個名為 owner() 的內置變數,它就是用來做這樣的事情的。(因為您的契約是可擁有的。)

所以你的程式碼看起來像這樣

contract token is ERC20, Ownable {
       using SafeMath for uint256;
       uint taxPercentage = 9;
   
       constructor(uint256 initialSupply) ERC20("token", "tkn") {
           _mint(msg.sender, initialSupply);
       }
   
       function _transfer(
           address sender,
           address recipient,
           uint256 amount
       ) internal override {
           if (sender != owner()) {
               uint256 FeeAmount = amount.mul(taxPercentage) / 100;
               super._transfer(sender, owner(), FeeAmount); //Maybe this should be recipient instead owner/deployer?? I added owner based on your code
               amount -= FeeAmount;
           }
           super._transfer(sender, recipient, amount);
       }
   }

可能的答案:

我沒有檢查發件人是否 == 部署者,而是選擇檢查收件人 == 部署者是否按預期工作(似乎)。我認為這是由於交易所在交換期間如何處理轉移?

引用自:https://ethereum.stackexchange.com/questions/118488