简体   繁体   English

Uniswap v2 swap() function。balance0 和 balance1 是什么?

[英]Uniswap v2 swap() function. What are balance0 & balance1?

I'm learning how the Uniswapv2 contracts work but I can't seem to wrap my mind around the swap() function.我正在学习 Uniswapv2 合约是如何工作的,但我似乎无法全神贯注于swap() function。

Reference: https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Pair.sol#L173参考: https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Pair.sol#L173

Lines 173-174 contain:第 173-174 行包含:

balance0 = IERC20(_token0).balanceOf(address(this));
balance1 = IERC20(_token1).balanceOf(address(this));

My question is, when & whose balances are these?我的问题是,这些余额是什么时候和谁的?

A. These are the same as _reserve0 & _reserve1 after the most recent swap and will be used to synchronize reserves. A. 这些与最近交换后的_reserve0_reserve1相同,将用于同步储备。

B. These are the quantities of each token the user making the swap currently possesses. B. 这些是进行交换的用户当前拥有的每个令牌的数量。

C. None of the above. C 以上都不是。 It's something else.这是另一回事。 Please explain the flow of this function. I cannot find a clear and concise definition anywhere.请解释一下这个function的流程。我在任何地方都找不到清晰简洁的定义。

answer is "C":)答案是“C”:)

balanceOf is a mapping in ERC20 implementation to return the amount that given address holds: balanceOfERC20实现中的一个映射,用于返回给定地址持有的数量:

 // address => holds uint amount
 mapping(address => uint) public balanceOf;

Since current contract is inheriting from UniswapV2ERC20 :由于当前合约继承自UniswapV2ERC20

contract UniswapV2Pair is IUniswapV2Pair, UniswapV2ERC20{}

it can access to UniswapV2ERC20.sol它可以访问UniswapV2ERC20.sol

Since the mapping balanceOf is public, solidity assigns getters to the public variables由于映射balanceOf是公共的,solidity 将 getters 分配给公共变量

In the functions:在函数中:

balance0 = IERC20(_token0).balanceOf(address(this));
balance1 = IERC20(_token1).balanceOf(address(this));

address(this) refers to the current contract which is UniswapV2Pair . address(this)指的是当前合约UniswapV2Pair So balance0 is how much the current contract owns _token0 and balance1 is how much the current contract address owns _token1 .所以balance0是当前合约拥有_token0balance1是当前合约地址拥有_token1 token0 and token1 are contract addresses and each ERC20 token contract, keeps track of addresses and their balances. token0token1是合约地址,每个ERC20币合约都会跟踪地址及其余额。 so you are visiting each token contract and getting how much balance the current contract has.所以你正在访问每个代币合约并获得当前合约的余额。

Think ERC20 contract like a bank.ERC20合约想象成银行。 you have token0 bank and token1 bank.你有token0银行和token1银行。 Each bank keeps track of the balances of their users.每家银行都会跟踪其用户的余额。 balancesOf is where ERC20 tokens store those balances. balancesOfERC20代币存储这些余额的地方。 Your current contract also owns some of those tokens so you just want to get how much tokens the current contract holds您当前的合约也拥有其中一些代币,因此您只想获得当前合约持有的代币数量

swap function will be called by the user. swap function 将被用户调用。 Before executing the swap, contract checks if it has enough funds在执行掉期之前,合约检查它是否有足够的资金

uint amount0In = balance0 > _reserve0 - amount0Out ? balance0 - (_reserve0 - amount0Out) : 0;
uint amount1In = balance1 > _reserve1 - amount1Out ? balance1 - (_reserve1 - amount1Out) : 0;
require(amount0In > 0 || amount1In > 0, 'UniswapV2: INSUFFICIENT_INPUT_AMOUNT');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM