BSC测试网部署SafeMoonDemo合约遇execution reverted错误求助
BSC测试网部署SafeMoonDemo合约报错:execution reverted
部署SafeMoonDemo合约到BSC测试网时,Gas估算触发以下错误,交易大概率执行失败:
内部JSON-RPC错误。{"code": -32000, "message": "execution reverted"}
错误截图:
合约代码如下:
contract SafemoonDemo is Context, IERC20, Ownable { using SafeMath for uint256; using Address for address; mapping (address => uint256) private _rOwned; mapping (address => uint256) private _tOwned; mapping (address => mapping (address => uint256)) private _allowances; mapping (address => bool) private _isExcludedFromFee; mapping (address => bool) private _isExcluded; address[] private _excluded; uint256 private constant MAX = ~uint256(0); uint256 private _tTotal = 1000 * 10**6 * 10**9; uint256 private _rTotal = (MAX - (MAX % _tTotal)); uint256 private _tFeeTotal; string private _name = "SafeMoonDemo"; string private _symbol = "SFD"; uint8 private _decimals = 9; uint256 public _taxFee = 5; uint256 private _previousTaxFee = _taxFee; uint256 public _liquidityFee = 5; uint256 private _previousLiquidityFee = _liquidityFee; IUniswapV2Router02 public immutable uniswapV2Router; address public immutable uniswapV2Pair; bool inSwapAndLiquify; bool public swapAndLiquifyEnabled = true; uint256 public _maxTxAmount = 5000 * 10**6 * 10**9; uint256 private numTokensSellToAddToLiquidity = 5000 * 10**6 * 10**9; event MinTokensBeforeSwapUpdated(uint256 minTokensBeforeSwap); event SwapAndLiquifyEnabledUpdated(bool enabled); event SwapAndLiquify( uint256 tokensSwapped, uint256 ethReceived, uint256 tokensIntoLiqudity ); modifier lockTheSwap { inSwapAndLiquify = true; _; inSwapAndLiquify = false; } constructor () public { _rOwned[_msgSender()] = _rTotal; IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E); // Create a uniswap pair for this new token uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory()) .createPair(address(this), _uniswapV2Router.WETH()); // set the rest of the contract variables uniswapV2Router = _uniswapV2Router; //exclude owner and this contract from fee _isExcludedFromFee[owner()] = true; _isExcludedFromFee[address(this)] = true; emit Transfer(address(0), _msgSender(), _tTotal); } 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 account) public view override returns (uint256) { if (_isExcluded[account]) return _tOwned[account]; return tokenFromReflection(_rOwned[account]); } 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; } }
问题原因及解决方法
- 缺失核心函数实现:代码中调用了
tokenFromReflection、_transfer、_approve等关键函数,但未提供具体实现,合约部署时执行到相关逻辑会直接触发revert。必须补充这些函数的完整代码才能正常部署。 - Router地址错误:当前使用的
0x10ED43C718714eb63d5aA57B78B54704E256024E是BSC主网的PancakeSwap Router地址,BSC测试网对应的Router地址应为0xD99D1c33F9fC3444f8101754aBC46c52416550D1,替换后再尝试部署。 - immutable变量初始化顺序问题:Solidity中
immutable变量需要在构造函数中按正确顺序赋值,确保uniswapV2Router先被赋值,再调用其factory()方法创建交易对。 - Gas Limit不足:部署带有Uniswap交互逻辑的合约需要更高的Gas,手动将Gas Limit调整至3000000左右,再尝试发送交易。
内容的提问来源于stack exchange,提问作者Nitesh Mehra
相关产品推荐
相关产品推荐

