Original post by Ross Bulat
Zeng Mi, Min Min & AMP; Ah Jian, ethereum lover
Trading options on Uniswap V2
As mentioned above, every transaction pair on Uniswap V1 has ETH. To convert A into B, you must first buy the ETH with the A, and then buy the B with the ETH. Therefore, the trader has to pay two transaction fees and a gas fee.
Uniswap's documentation calls V1 the "prototype" of a decentralized automated exchange. This may be because developers have discovered that V2 provides a more efficient way to execute transactions. Removing ETH as an intermediary token is one of the optimizations.
As shown in the figure below, after the ETH is no longer used as an intermediary token, transaction pairs can be directly traded:

- The two ERC20 tokens are exchanged directly -
This seems reasonable and intuitive, and maybe V1 should work this way. Just call the &Nbsp; NBSP; on the routing contract; swapExactTokensForTokens And & have spent swapTokensForExactTokens Method can be traded.
The & NBSP; in the above method name; Exact Indicates which token you wish to set a limit on in the exchange. If you want to buy a certain amount of ETH with DAI, use NBSP; swapTokensForExactTokens ; On the other hand, if you want to buy ETH with a certain amount of DAI, use NBSP; SwapExactTokensForTokens. Uniswap V2's intelligent contract takes these two approaches.
In addition to direct transactions, users can still choose to use the ETH as an intermediary token to achieve transactions between two tokens. The user does not need to create a liquidity pool directly between the input token and the output token, as long as there is a liquidity pool between the two tokens and the ETH.
For example, ETH is used as an intermediary token to realize the transaction between DAI and LINK. The process is shown in the following figure:

- Indirect interchange with ETH as intermediary token (similar to Uniswp V1) -
The solution to this would be soldesert; swapExactETHForTokens And & have spent SwapETHForExactTokens and NBSP; swapTokensForExactETH And & have spent SwapExactTokensForETH.
The final way to do token trading is to trade between multiple ERC20 tokens until you get the output token you want, which is what the white paper calls "any ERC20 transaction pair." Of course, any token pair can also contain ETH.
The following figure shows how DAI's value can be transferred to the desired output token Link through a series of tokens:

- For tokens without direct trading pairs, a series of intermediary token transactions are available to obtain the output token -
This can be done if there is no pool of liquidity between your input token and the output token you want, and does not necessarily require the ETH as an intermediary token as described above.
Although this trading method is feasible, there is currently no intelligent contract that can automatically implement so many token transactions, so you can only manually write this function into the code of the front-end application and make multiple transaction calls to Uniswap.
This may not be used very often, because after transferring value through one or two intermediary tokens, the extra Gas fee makes roundabout transactions uneconomical. Perhaps it would be cheaper to trade through a centralised exchange. However, this approach will be more widely used once ETH 2.0's main network comes online, with more efficient PoS mechanisms, coupled with extensibility features that make round-about transactions more feasible.
Price information transmission mechanism
Uniswap's price information transfer mechanism allows developers to calculate the average price of a token based on the price volatility of the token over several blocks, which can also be expressed as a time interval between two timestamps.
This time interval can be the first hour, the first 24 hours or more.
Although Uniswap will provide token prices, it will not store any historical prices on the chain. DApp developers are required to calculate the average price of a token over time, based on the cumulative price over a period of time.
These Average prices are called Time Weighted Average prices (TWAP).
The so-called TWAP is to select a block on the chain as the time interval, divide the cumulative price of a token in this block (the price of the token in each block) by the time stamp interval (the time stamp of the ending block minus the time stamp of the starting block), and get the average price of the token in this block. The diagram is as follows:

The TWAP is calculated based on the cumulative token price within a block. -
Twaps are reliable and reflect the price of tokens over a period of time (don't forget, this is based on specific token pairs). This way you can avoid the flash crash and the wild price swings that are common in the cryptocurrency market. When market prices fluctuate, TWAP can more accurately reflect the token situation.
If needed, developers can get pricing information for free through Uniswap's JavaScript SDK. See below for details.
Flash trading
Flash Swap refers to the multi-stage process of borrowing tokens from Uniswap's liquidity pool, using them for an operation, and repaying them in one transaction. If any phase of the process fails, all state changes are undone and the tokens are returned to the corresponding Uniswap flow pool.
The chart below shows how traders can execute flash trades without holding tokens. This is because the value borrowed from the liquidity pool must be returned to the pool, either the transaction fails or the trader returns the borrowed token:

A big use case for flash trading is the carry trade, and the trader must be able to make a profit while returning the value of the previously borrowed scrip to the Uniswap liquidity pool. Traders can easily make a profit each time through carry trades.
Another use case is to use the Uniswap liquidity pool to clear the Maker vault, where you can pay off the debt and take out the ETH (or other token) that is secured in the Maker vault to pay off the Uniswap liquidity pool. It takes less gas than paying with your own scrip.
Flash trading is a relatively new feature on Uniswap, but we'll see more dApp integration soon.
In the case of a trading robot, flash trading can also be used to automate carry trades. Trading robots don't need money to execute trades, they just need to identify arbitrage opportunities and execute flash trades.
New 0.05% agreement on fees and governance
Uniswap V2 also introduced a new fee for protocols, called Protocol Fee, which enables 0.05% of the transaction volume that occurs on Uniswap to be transferred to an address held by Uniswap. This 0.05% is extracted from the 0.3% Uniswap standard fee, so activating this mechanism would result in a loss of interest for the liquidity provider.
At the moment, this mechanism is still closed, and the Uniswap project has not made any public statements about plans to enable this mechanism.
Adding this mechanism seems a bit fishy, but Uniswap developers believe it is a necessary feature to support the growth and sustainability of Uniswap. And they can be defended by the fact that enabling it requires a governance mechanism, with the community voting on whether to activate it or not.
Although community consensus is needed to launch this feature, doing so will give the Uniswap project side a huge benefit and competitive advantage.
On the plus side, this fee can be used to upgrade agreements and the entire ecosystem, increasing acceptance, and ultimately allowing liquidity providers to benefit more. In theory, they could make up for their loss of interest in the system. The whole process is shown in the figure below:

- Positive result of charging 0.05% of the agreement fee -
There is no public information on this governance mechanism, but it is possible to assume that there will be a mechanism to collect votes using ERC20 tokens, similar to Maker's governance and voting mechanism.
For managing the agreement fee mechanism, UNisAP & NBSP; Factory The contract contains NBSP; feeTo And & have spent feeToSetter Method to set the address of the money to be received, and have the right to change it & NBSP; feeTo The account of the address.
JavaScript SDK
This article is intended to avoid being overly technical, but it is worth mentioning Uniswap's JavaScript SDK. This open source library provides JavaScript APIS for NodeJS, JavaScript frameworks, and all browser-based applications to integrate Uniswap.
This is enough to allow third-party applications to integrate Uniswap directly into their own products and services.
Agent Wallet, Switcheo Exchange, and Streamr Marketplace are three of the products that already have direct integration with Uniswap.
The documentation for the JavaScript SDK explains the API for extracting scrip data and scrip address, as well as the API for starting a transaction and getting pricing, where the application can use the price information transfer function described above.
Note that the API does not automatically execute trades, but only prepares you for a trade that takes into account the median market price you are willing to accept and the minimum amount you can exchange (the so-called "slippage point").
Once the transaction is ready, the user must initiate and sign the transaction manually using a wallet software such as MetaMask.
List of tokens for Uniswap
If you're curious about how the list of tokens on Uniswap. Exchange is determined, it's simple, it's manual, and the Uniswap team USES a video conference to discuss which tokens to add. Therefore, even if some kind of ERC20 token pool already exists, it does not mean that this token will appear on the list of tokens on the official Uniswap website.
If you are an issuer of some kind of token and you want your token to appear on the default list of tokens in [UNiswap.Exchange], you need to find it in NBSP; Default Token List repository Submit a Github issue.
Uniswap. Exchange also has a feature: You can find tokens by typing the address of a particular ERC20 into a search box, and then add them to your custom list. So you can trade this token in your dApp, but it doesn't mean that other people can see it, so it doesn't increase the visibility of the token.
Developers can also create custom token lists within their own Uniswap integration by following a simple JSON structure. See Uniswap's Token Lists Repository for more details.
conclusion
This article examines Uniswap V2 in its entirety, revealing its improvements and how it works. Behind these significant improvements -- a more efficient exchange of tokens, a time-weighted price-information identification mechanism, and a lightning exchange function -- is a move to the technical effort to write a contract based on The effort involved in compiling A contract, which allows Uniswap V2 to use the latest opcodes to achieve more efficient execution.
Uniswap V2 maintains its trust-free, decentralized nature. It exists on the Ethereum blockchain and cannot be destroyed unless the underlying protocols are blown up. So far, that has not happened, and given the size of the miners (and the size of the verifiers), it is unlikely to happen in the future.
Ethereum 2.0's test network, Medalla, had 20,000 verifiers when it was launched -- and I estimate that number will be higher when the main network is launched, because the verifiers will be rewarded financially.
Uniswap V2, on the other hand, is not perfect as a token exchange. At the time of writing, Gas charges on the Ethereum network were at their highest in history, making Uniswap extremely expensive. Uniswap doesn't have a real-time order book, so traders still have to go to a centralized exchange to use their trading strategy. As mentioned above, Uniswap needs user arbitrage to keep the exchange token price consistent with the market price, and centralized exchanges still play an irreplaceable role in balancing Uniswap's exchange rate.
Uniswap V2 is a positive step in the right direction, but there is still a long way to go before decentralised exchanges can completely replace centralised exchanges. Nevertheless, the opinion of the cryptographic currency community, and especially the Ethereum ecology, is very optimistic -- it is safe to say that DeFi's progress, both in terms of technical capability and acceptability, will not slow down in the near term.
Welcome to join the official BlockBeats community:
Telegram Subscription Group: https://t.me/theblockbeats
Telegram Discussion Group: https://t.me/BlockBeats_App
Official Twitter Account: https://twitter.com/BlockBeatsAsia