我正在尝试在 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。
编辑:我现在已经为这两个函数测试了死钱包地址 0x0000000000000000000000000000000000000000 。两者的结果不同:
balanceOf 回报:10491075026009005720565 balanceOf_tOwned 回报:115792089237316195423570985008687907853269984665640000000000000000000000000000
我再次对这意味着什么感到困惑,但可能是映射实际上是从 BNB 将转换返回到我的令牌?即它的 BNB 价值为 380,000 美元(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);
}
}