【发布时间】:2021-12-27 19:09:56
【问题描述】:
我正在我的智能合约中实施一项征税功能,该功能将在交易期间将代币兑换为 ETH (BNB)。为了做到这一点,我在合约的构造函数中初始化 IUniswapV2Router02 和 IUniswapV2Factory 对象,然后尝试使用 IUniswapV2Factory 的 createPair() 函数创建一个配对地址。但是addPair(_factory.createPair(address(this), _router.WETH())); 会产生错误(添加在代码下)。
我尝试部署其他具有相同代码和功能的已发布合约,但我得到了完全相同的错误。经过几个小时的搜索修复后,我假设错误的原因是我尝试在 JavaScript VM 上进行部署。 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 事务已恢复到初始状态。笔记: 如果您发送值和值,则应支付被调用的函数 您发送的金额应少于您当前的余额。调试 交易以获取更多信息。
【问题讨论】:
-
function addPair(address pairAddress) public onlyOwner payable {有什么作用吗? -
@Darcys22 不。在另一个主题中,我得到了不要从构造函数中调用它的建议。而是将该逻辑放入构造函数中。我做到了,它似乎工作。我将它部署到生产中。在 Remix 的测试环境中,它无论如何都不起作用。