[英]What is the use of Uniswap Router Initialization in a Token Contract
我刚开始使用 ETH 和 BSC 构建代币,这是我在许多合约中看到的一种说法。 在 Constructor 方法中,Uniswap 路由器可能与 V2 版本无关。 这个有什么用?
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;
为什么需要这个初始化? 这是什么功能?
感谢有人可以提供帮助。
谢谢
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E);
此行初始化指向0x10ED...
地址的指针,并期望合约(部署在0x10ED...
地址处)实现IUniswapV2Router02
接口。
该接口是在调用者合约源代码的某处定义的。
它允许您执行和调用接口定义的函数,而不是构建低级调用。 它还允许您使用返回的数据类型而不是解析返回的二进制文件。
例子:
pragma solidity ^0.8.5;
interface IRemote {
function foo() external view returns (bool);
}
contract MyContract {
IRemote remote;
constructor() {
remote = IRemote(address(0x123));
}
function getFoo() external view returns (bool) {
bool returnedValue = remote.foo();
return returnedValue;
}
}
你的回答后我仍然感到困惑。 我已经在没有 Uniswap 的情况下构建和部署了许多代币合约,并且能够使用 Uniswap 在线实用程序来设置交换和交易。 这是如何工作的。 它是否会自动创建一个池以及流动性? 我没有看到添加这些代码行的好处。 任何解释都是没有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.