返回

ethereum-.balance 方法返回非零,而映射 (address => uint256) 返回非零

发布时间:2022-03-03 18:35:18 397

我试图在BSC上创建一个无需思考的代币,一个简单的固定费用用于开发钱包,另一个费用用于流动性。

每次调用以下函数时,我都会遇到一个错误:

function _transferStandard(address sender, address recipient, uint256 tAmount) private {
        require(balanceOf(sender) > tAmount, "Insuficient balance for transaction.");
        (uint256 tTransferAmount, uint256 fee) = _getValues(tAmount);
        _tOwned[sender] = _tOwned[sender].sub(tAmount);
        _tOwned[devWallet] = _tOwned[devWallet].add(fee);
        _tOwned[recipient] = _tOwned[recipient].add(tTransferAmount);       
        emit Transfer(sender, recipient, tTransferAmount);
    }

每当从发件人钱包中取出代币时,减法就会溢出。经过进一步分析,我发现以下功能:

function balanceOf(address _owner) public view returns (uint256 balance){
    return _owner.balance;
}

将返回业主余额的正确值1721391030000000000(合同部署后,将最大供应量转移给业主)。然而,以下函数返回0,因为在钱包中没有余额,人们期望从中获得相同的输出。

function balanceOf_tOwned(address _owner) public view returns (uint256 balance){
    return _tOwned[_owner];
}

_另一方面,tOwned是一个简单的映射变量。

mapping (address => uint256) private _tOwned;

我不明白为什么使用_tOwned时余额显示为0。

编辑:我现在已经测试了两个功能的死钱包地址0x0000000000000000000000000000。这两方面的结果都不同:

收益余额:10491075026009005720565收益余额:11579208923731619542357098500868790785326998465640000000000000000

我再次困惑于这意味着什么,但可能是映射实际上正在从BNB返回到我的令牌的转换?即其价值为38万美元(以BNB计)(https://bscscan.com/address/0x0000000000000000000000000000000000000000).

完整合同代码:

// SPDX-License-Identifier: Unlicensed
pragma solidity ^0.8.12;

abstract contract Context {
    function _msgSender() internal view virtual returns (address payable) {
        return payable(msg.sender);
    }
    function _msgData() internal view virtual returns (bytes memory) {
        this; 
        return msg.data;
    }
}

interface IERC20 {
    function totalSupply() external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}
library SafeMath {
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");
        return c;
    }
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        return sub(a, b, "SafeMath: subtraction overflow");
    }
    function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b <= a, errorMessage);
        uint256 c = a - b;
        return c;
    }
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }
        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");
        return c;
    }
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        return div(a, b, "SafeMath: division by zero");
    }
    function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        uint256 c = a / b;
        return c;
    }
    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        return mod(a, b, "SafeMath: modulo by zero");
    }
    function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b != 0, errorMessage);
        return a % b;
    }
}
library Address {
    function isContract(address account) internal view returns (bool) {
        bytes32 codehash;
        bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
        assembly { codehash := extcodehash(account) }
        return (codehash != accountHash && codehash != 0x0);
    }
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");
        (bool success, ) = recipient.call{ value: amount }("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
      return functionCall(target, data, "Address: low-level call failed");
    }
    function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
        return _functionCallWithValue(target, data, 0, errorMessage);
    }
    function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }
    function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        return _functionCallWithValue(target, data, value, errorMessage);
    }
    function _functionCallWithValue(address target, bytes memory data, uint256 weiValue, string memory errorMessage) private returns (bytes memory) {
        require(isContract(target), "Address: call to non-contract");
        (bool success, bytes memory returndata) = target.call{ value: weiValue }(data);
        if (success) {
            return returndata;
        } else {
            if (returndata.length > 0) {
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
}

// The actual contract
contract Ownable is Context {
    address private _owner;
    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    constructor () {
        address msgSender = _msgSender();
        _owner = msgSender;
        renounceOwnership();
    }

    function owner() public view returns (address payable) {
        return payable(_owner);
    }
    modifier onlyOwner() {
        require(_owner == _msgSender(), "Ownable: caller is not the owner");
        _;
    }
    function renounceOwnership() public virtual onlyOwner {
        emit OwnershipTransferred(_owner, address(0));
        _owner = address(0);
    }
}
contract COOLCONTRACT is Context, IERC20, Ownable {
    using SafeMath for uint256;
    using Address for address;
    mapping (address => uint256) private _tOwned;
    mapping (address => mapping (address => uint256)) private _allowances;
    uint256 private constant MAX = ~uint256(0);
    uint256 _tTotal = 10 * 10**8 * 10**18;
    uint256 private _rTotal = (MAX - (MAX % _tTotal));
    string private _name = 'igb';
    string private _symbol = 'yiibg';
    uint8 private _decimals = 9;

    constructor () {
        _tOwned[owner()] = _rTotal;
    }
    // 80% donated, 20% used to fund the project
    address payable private devWallet = payable(0x40C31d8F17338841FA1c8Af2F619B21428B1f358);

    function name() public view returns (string memory) {
        return _name;
    }
    function symbol() public view returns (string memory) {
        return _symbol;
    }
    function decimals() public view returns (uint8) {
        return _decimals;
    }
    function totalSupply() public view override returns (uint256) {
        return _tTotal;
    }

    function balanceOf(address _owner) public view returns (uint256 balance){
        return _owner.balance;
    }

    function shiet(address _owner) public view returns (uint256 balance){
        return _tOwned[_owner];
    }

    function transfer(address recipient, uint256 amount) public override returns (bool) {
        _transfer(_msgSender(), recipient, amount);
        return true;
    }
    function allowance(address owner, address spender) public view override returns (uint256) {
        return _allowances[owner][spender];
    }
    function approve(address spender, uint256 amount) public override returns (bool) {
        _approve(_msgSender(), spender, amount);
        return true;
    }
    function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
        _transfer(sender, recipient, amount);
        _approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
        return true;
    }
    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
        return true;
    }
    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
        return true;
    }
    
    function _approve(address owner, address spender, uint256 amount) private {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");
        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }
    function _transfer(address sender, address recipient, uint256 amount) private {
        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");
        require(amount > 0, "Transfer amount must be greater than zero");
        _transferStandard(sender, recipient, amount);
    }
    function _transferStandard(address sender, address recipient, uint256 tAmount) private {
        require(balanceOf(sender) > tAmount, "Insuficient balance for transaction.");
        (uint256 tTransferAmount, uint256 fee) = _getValues(tAmount);
        _tOwned[sender] = _tOwned[sender].sub(tAmount);
        _tOwned[devWallet] = _tOwned[devWallet].add(fee);
        _tOwned[recipient] = _tOwned[recipient].add(tTransferAmount);       
        emit Transfer(sender, recipient, tTransferAmount);
    }
    
    function _getValues(uint256 tAmount) private pure returns (uint256, uint256) {
        uint256 fee = tAmount.div(100).mul(10); 
        uint256 totalTransac = tAmount.sub(fee);
        return (totalTransac, fee);
    }
}
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
相关帖子
下一篇
c#读取HttpModule中的HTTP请求正文 2022-03-03 17:55:38