Solidity

為什麼在 Erc20 Contract 中包含 UniSwapRouterV2?

  • December 28, 2021

許多像 SafeMoon 這樣的 ERC20 代幣都包含UniSwapRouterV2在他們的合約中。這樣做有什麼好處?不包括路由器可以嗎?

例如 SafeMoon 的路由器程式碼:

interface IUniswapV2Router01 {
function factory() external pure returns (address);
function WETH() external pure returns (address);

function addLiquidity(
   address tokenA,
   address tokenB,
   uint amountADesired,
   uint amountBDesired,
   uint amountAMin,
   uint amountBMin,
   address to,
   uint deadline
) external returns (uint amountA, uint amountB, uint liquidity);
function addLiquidityETH(
   address token,
   uint amountTokenDesired,
   uint amountTokenMin,
   uint amountETHMin,
   address to,
   uint deadline
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
function removeLiquidity(
   address tokenA,
   address tokenB,
   uint liquidity,
   uint amountAMin,
   uint amountBMin,
   address to,
   uint deadline
) external returns (uint amountA, uint amountB);
function removeLiquidityETH(
   address token,
   uint liquidity,
   uint amountTokenMin,
   uint amountETHMin,
   address to,
   uint deadline
) external returns (uint amountToken, uint amountETH);
function removeLiquidityWithPermit(
   address tokenA,
   address tokenB,
   uint liquidity,
   uint amountAMin,
   uint amountBMin,
   address to,
   uint deadline,
   bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountA, uint amountB);
function removeLiquidityETHWithPermit(
   address token,
   uint liquidity,
   uint amountTokenMin,
   uint amountETHMin,
   address to,
   uint deadline,
   bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountToken, uint amountETH);
function swapExactTokensForTokens(
   uint amountIn,
   uint amountOutMin,
   address[] calldata path,
   address to,
   uint deadline
) external returns (uint[] memory amounts);
function swapTokensForExactTokens(
   uint amountOut,
   uint amountInMax,
   address[] calldata path,
   address to,
   uint deadline
) external returns (uint[] memory amounts);
function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
   external
   payable
   returns (uint[] memory amounts);
function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
   external
   returns (uint[] memory amounts);
function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
   external
   returns (uint[] memory amounts);
function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
   external
   payable
   returns (uint[] memory amounts);

function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
}



// pragma solidity >=0.6.2;

interface IUniswapV2Router02 is IUniswapV2Router01 {
function removeLiquidityETHSupportingFeeOnTransferTokens(
   address token,
   uint liquidity,
   uint amountTokenMin,
   uint amountETHMin,
   address to,
   uint deadline
) external returns (uint amountETH);
function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
   address token,
   uint liquidity,
   uint amountTokenMin,
   uint amountETHMin,
   address to,
   uint deadline,
   bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountETH);

function swapExactTokensForTokensSupportingFeeOnTransferTokens(
   uint amountIn,
   uint amountOutMin,
   address[] calldata path,
   address to,
   uint deadline
) external;
function swapExactETHForTokensSupportingFeeOnTransferTokens(
   uint amountOutMin,
   address[] calldata path,
   address to,
   uint deadline
) external payable;
function swapExactTokensForETHSupportingFeeOnTransferTokens(
   uint amountIn,
   uint amountOutMin,
   address[] calldata path,
   address to,
   uint deadline
) external;
}

通常這樣做是為了在 Uniswap 中創建一個令牌對。

代幣需要允許代幣對進行一些特權操作,例如重新平衡流動性。

一個有點奇怪的問題。許多 ERC20 代幣合約涉及 IUniswapV2Router01 和 IUniswapV2Router02 介面。它們呼叫介面函式與 Uniswap DEX 協議互動。許多代幣像 Safemoon 一樣實現自動流動性獲取。他們通過呼叫addLiquidityUniswap 路由器合約的函式來做到這一點。他們還可以呼叫交換函式,例如swapTokensForExactETHor swapTokensForExactTokens。因此,他們應該在他們的智能合約中包含路由器介面。

引用自:https://ethereum.stackexchange.com/questions/100219