在Remix中的JavaScript VM上创建Uniswap对失败



我正在我的智能合约中实现一个征税功能,该功能将在交易期间将代币交换到ETH(BNB(。为了做到这一点,我在契约的构造函数中初始化IUniswapV2Router02IUniswapV2Factory对象,然后尝试使用IUniswapV2Factory的createPair((函数创建一个对地址。但是addPair(_factory.createPair(address(this), _router.WETH()));会创建一个错误(添加到代码下(。

我试图部署其他发布的具有相同代码和功能的合约,但我得到了完全相同的错误。在搜索了几个小时的修复程序后,我认为错误的原因是我试图在JavaScriptVM上部署。createPair函数会在这个测试环境中工作吗(我的实现是错误的(,还是代码正确,我需要在真正的区块链上测试它?

pragma solidity ^0.8.7;
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
interface IBEP20 {
//...
}
contract BEP20Token is Context, IBEP20, Ownable {
using SafeMath for uint256;
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) private _allowances;

mapping(address => bool) private _pair;

string private _symbol;
string private _name;
uint256 private _totalSupply;
uint8   private _decimals;
uint256 private _developmentTax = 3;
address private constant _factoryAddress = 0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73;
address private constant _routerAddress = 0x10ED43C718714eb63d5aA57B78B54704E256024E;
address private constant _deadAddress = 0x000000000000000000000000000000000000dEaD;
address private _pairAddress;
IUniswapV2Factory private _factory;
IUniswapV2Router02 private _router;
constructor()  {
_name = "My Token";
_symbol = "TKN";
_decimals = 18;
_totalSupply = 1000 * 10 ** 18;
_balances[_msgSender()] = _totalSupply;
_maxTransferLimit = _totalSupply;
_router = IUniswapV2Router02(_routerAddress);
_factory = IUniswapV2Factory(_factoryAddress);   
// it crashes in this line:
addPair(_factory.createPair(address(this), _router.WETH()));

emit Transfer(address(0), msg.sender, _totalSupply);
}
function addPair(address pairAddress) public onlyOwner {
_pair[pairAddress] = true;
_pairAddress = pairAddress;
emit AddPair(pairAddress);
}

}

BEP20Token创建错误:VM错误:revert。

revert事务已恢复到初始状态。注:如果您发送值和您发送的金额应小于您当前的余额。调试交易以获取更多信息。

createPair函数会在这个测试环境中工作吗实现是错误的(,或者代码是正确的,我需要测试它在真正的区块链上?

找到解决方案后,我可以回答这个问题。解决方案是:

  • 我将addPair()的逻辑直接添加到构造函数中
  • 我把它直接部署到BSC主网。它在Remix的测试环境中不起作用

最新更新