Burn

向合約添加刻錄功能

  • June 8, 2022

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.11;

interface IBEP20 {
//functions
 function name() external view returns (string memory);
 function symbol() external view returns (string memory);
 function decimals() external view returns (uint8);
 function totalSupply() external view returns (uint256);
 function balanceOf(address _owner) external view returns (uint256);
 function getOwner() external view returns (address);
 function transfer(address _to, uint256 _value) external returns (bool);
 function transferFrom(address _from, address _to, uint256 _value) external returns (bool);
 function approve(address _spender, uint256 _value) external returns (bool);
 function allowance(address _owner, address _spender) external view returns (uint256);
 function burn(address _owner, address _spender) external returns (uint256);


//events
 event Transfer(address indexed _from, address indexed _to, uint256 _value);
 event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}

/* This contract created by YazilimNedir.com */
contract YazilimNedir is IBEP20{
   using SafeMath for uint256;

   address private _ownerOfContract;
   string private _name = "Yaztoken";
   string private _symbol = "YZTKN";
   uint8 private _decimals = 4;
   uint256 private _totalSupply = 10000000000000;

   mapping(address=>uint256) private _balances;
   mapping (address => mapping (address => uint256)) private _allowances;

   uint8 private _commissionPercent = 3;
   address private constant _commissionWallet = 0x792b262e797B77eD8d2eEcEF2Be4e540648f5c51;

   constructor()
   {
       _ownerOfContract = msg.sender;
       _balances[_ownerOfContract] = _totalSupply;
       emit Transfer(msg.sender, _ownerOfContract, _totalSupply);
   }

   function name() external view returns (string memory){
       return _name;
   }

   function symbol() external view returns (string memory){
       return _symbol;
   }

   function decimals() external view returns (uint8){
       return _decimals;
   }

   function totalSupply() external view returns (uint256){
       return _totalSupply;
   }

   function balanceOf(address _owner) external view returns (uint256){
       return _balances[_owner];
   }

   function getOwner() external view returns (address){
       return _ownerOfContract;
   }

   function transfer(address _to, uint256 _value) external returns (bool){
       _transfer(msg.sender, _to, _value);
       return true;
   }
    
       function burn(address _to(0), address owner, uint256 _value) external returns (bool){
       _transfer(msg.sender, _to, _value);
       return true;
   }



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

       if(_takeCommission(sender, recipient)){
           _transferWithCommission(sender, recipient, amount);
       } 
       else{
           _transferWOCommission(sender, recipient, amount);
       }
   }

   function transferFrom(address _from, address _to, uint256 _value) external returns (bool){
       _transfer(_from, _to, _value);
       _approve(_from, msg.sender, _allowances[_from][msg.sender].sub(_value, "BEP20: transfer amount exceeds allowance"));
       return true;
   }

   function approve(address _spender, uint256 _value) external returns (bool){
       _approve(msg.sender, _spender, _value);
       return true;
   }

   function _approve(address owner, address spender, uint256 amount) internal {
       require(owner != address(0), "BEP20: approve from the zero address");
       require(spender != address(0), "BEP20: approve to the zero address");

       _allowances[owner][spender] = amount;
       emit Approval(owner, spender, amount);
   }

   function allowance(address _owner, address _spender) external view returns (uint256){
       return _allowances[_owner][_spender];
   }

   function _transferWithCommission(address sender, address recipient, uint256 amount) private
   {
        _balances[sender] = _balances[sender].sub(amount, "BEP20: transfer amount exceeds balance");
        uint256 _fee = _calcCommission(amount, _commissionPercent);
        _balances[_commissionWallet] = _balances[_commissionWallet].add(_fee);
        _balances[recipient] = _balances[recipient].add(amount.sub(_fee));
        emit Transfer(sender, recipient, amount.sub(_fee));
   }

   function _calcCommission(uint256 value, uint8 commissionPercent) private pure returns(uint256)
   {
       return value.mul(commissionPercent).div(100);
   }

   function _transferWOCommission(address sender, address recipient, uint256 amount) private
   {
       _balances[sender] = _balances[sender].sub(amount, "BEP20: transfer amount exceeds balance");
       _balances[recipient] = _balances[recipient].add(amount);
       emit Transfer(sender, recipient, amount);
   }

   function _takeCommission(address sender,address recipient) private pure returns(bool)
   {
       if(sender == _commissionWallet || recipient == _commissionWallet){
           return false;
       }
       else{
           return true;
       }
   }

}

library SafeMath {
   /**
    * @dev Returns the addition of two unsigned integers, reverting on
    * overflow.
    *
    * Counterpart to Solidity's `+` operator.
    *
    * Requirements:
    *
    * - Addition cannot overflow.
    */
   function add(uint256 a, uint256 b) internal pure returns (uint256) {
       uint256 c = a + b;
       require(c >= a, "SafeMath: addition overflow");

       return c;
   }

   /**
    * @dev Returns the subtraction of two unsigned integers, reverting on
    * overflow (when the result is negative).
    *
    * Counterpart to Solidity's `-` operator.
    *
    * Requirements:
    *
    * - Subtraction cannot overflow.
    */
   function sub(uint256 a, uint256 b) internal pure returns (uint256) {
       return sub(a, b, "SafeMath: subtraction overflow");
   }

   /**
    * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
    * overflow (when the result is negative).
    *
    * Counterpart to Solidity's `-` operator.
    *
    * Requirements:
    *
    * - Subtraction cannot overflow.
    */
   function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
       require(b <= a, errorMessage);
       uint256 c = a - b;

       return c;
   }

   /**
    * @dev Returns the multiplication of two unsigned integers, reverting on
    * overflow.
    *
    * Counterpart to Solidity's `*` operator.
    *
    * Requirements:
    *
    * - Multiplication cannot overflow.
    */
   function mul(uint256 a, uint256 b) internal pure returns (uint256) {
       // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
       // benefit is lost if 'b' is also tested.
       // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
       if (a == 0) {
           return 0;
       }

       uint256 c = a * b;
       require(c / a == b, "SafeMath: multiplication overflow");

       return c;
   }

   /**
    * @dev Returns the integer division of two unsigned integers. Reverts on
    * division by zero. The result is rounded towards zero.
    *
    * Counterpart to Solidity's `/` operator. Note: this function uses a
    * `revert` opcode (which leaves remaining gas untouched) while Solidity
    * uses an invalid opcode to revert (consuming all remaining gas).
    *
    * Requirements:
    *
    * - The divisor cannot be zero.
    */
   function div(uint256 a, uint256 b) internal pure returns (uint256) {
       return div(a, b, "SafeMath: division by zero");
   }

   /**
    * @dev Returns the integer division of two unsigned integers. Reverts with custom message on
    * division by zero. The result is rounded towards zero.
    *
    * Counterpart to Solidity's `/` operator. Note: this function uses a
    * `revert` opcode (which leaves remaining gas untouched) while Solidity
    * uses an invalid opcode to revert (consuming all remaining gas).
    *
    * Requirements:
    *
    * - The divisor cannot be zero.
    */
   function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
       require(b > 0, errorMessage);
       uint256 c = a / b;
       // assert(a == b * c + a % b); // There is no case in which this doesn't hold

       return c;
   }

   /**
    * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
    * Reverts when dividing by zero.
    *
    * Counterpart to Solidity's `%` operator. This function uses a `revert`
    * opcode (which leaves remaining gas untouched) while Solidity uses an
    * invalid opcode to revert (consuming all remaining gas).
    *
    * Requirements:
    *
    * - The divisor cannot be zero.
    */
   function mod(uint256 a, uint256 b) internal pure returns (uint256) {
       return mod(a, b, "SafeMath: modulo by zero");
   }

   /**
    * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
    * Reverts with custom message when dividing by zero.
    *
    * Counterpart to Solidity's `%` operator. This function uses a `revert`
    * opcode (which leaves remaining gas untouched) while Solidity uses an
    * invalid opcode to revert (consuming all remaining gas).
    *
    * Requirements:
    *
    * - The divisor cannot be zero.
    */
   function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
       require(b != 0, errorMessage);
       return a % b;
   }
}

你好親愛的solidity專家。我在這份契約中添加了刻錄功能。但我收到錯誤。如果您能指出我的錯誤並予以糾正,我將不勝感激。這些是我添加的程式碼。


       function burn(address _to(0), address owner, uint256 _value) external returns (bool){
       _transfer(msg.sender, _to, _value);
       return true;
   }
 function burn(address _owner, address _spender) external returns (uint256);

錯誤:ypeError:契約“YazilimNedir”應標記為抽象。–> token.sol:26:1: | 26 | 契約 YazilimNedir 是 IBEP20{ | ^(相關原始碼部分從這裡開始並跨越多行)。注意:缺少實現:–> token.sol:17:3: | 17 | 函式燒錄(地址_owner,地址_spender)外部返回(uint256);| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^

不確定這是否是有意的,但您使用的是一個非常粗略的契約,它不符合 ERC20 標準(或 BEP20 標準),並且其中有一個硬編碼的佣金地址。

我強烈建議不要使用此程式碼,而是要找到問題的根源:

你的介面聲明了一個函式

 function burn(address _owner, address _spender) external returns (uint256);

然而,您已經(錯誤地)實現了具有不同參數的函式。

function burn(address _to(0), address owner, uint256 _value) external returns (bool){
       _transfer(msg.sender, _to, _value);
       return true;
   }

實現介面的函式需要實現所有函式,否則應聲明為抽象(即聲明為“不完整”且缺少函式)。

您的界面實際上應該聲明一個函式(遵循 OpenZeppelin 的 ERC20Burnable 擴展)

 function burn(address from, uint256 amount) external;

程式碼類似於:

function burn(address from, uint256 amount) public virtual {
       _balances[from] = _balances[from].sub(amount);
       _totalSupply = _totalSupply.sub(amount);

       emit Transfer(from, address(0), amount);
   }

事實上,我什至不會使用 BEP20(你可能不會橋接到幣安鏈,getOwner如果你願意,仍然可以實現一個功能),而只是使用受信任的程式碼庫,比如OpenZeppelin 的 ERC20Burnable

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