GithubHelp home page GithubHelp logo

homework-2's Introduction

2024-Spring-HW2

Please complete the report problem below:

Problem 1

Provide your profitable path, the amountIn, amountOut value for each swap, and your final reward (your tokenB balance).

Solution

profitable path: tokenB->tokenA->tokenD->tokenC->tokenB

  • tokenB->tokenA
    • amountIn: 5 tokenB, amountOut: 5.655321988655321988 tokenA
  • tokenA->tokenD
    • amountIn: 5.655321988655321988 tokenA, 2.458781317097933552 tokenD
  • tokenD->tokenC
    • amountIn: 2.458781317097933552 tokenD, 5.088927293301515695 tokenC
  • tokenC->tokenB
    • amountIn: 5.088927293301515695 tokenC, 20.129888944077446732 tokenB

final reward: 20.129888944077446732 tokenB

Problem 2

What is slippage in AMM, and how does Uniswap V2 address this issue? Please illustrate with a function as an example.

Solution

Slippage in AMM happens when traders pay/receive a different price than what they initially requested due to a price movement. We can mitigate slippage through adjusting slippage tolerance.

In function: swapTokensForExactTokens, there is a argument called amountInMax that can let us adjust slippage tolerance. The prototype of the function is as below:

function swapTokensForExactTokens(
  uint amountOut,
  uint amountInMax,
  address[] calldata path,
  address to,
  uint deadline
) external returns (uint[] memory amounts);

Problem 3

Please examine the mint function in the UniswapV2Pair contract. Upon initial liquidity minting, a minimum liquidity is subtracted. What is the rationale behind this design?

Solution

Upon initial liquidity minting, Uniswap V2 uses geometric mean of the amount deposited. If there is no minimum liquidity burned, attacker can control the value of the minimum quantity of liquidity token shares by manipulating the initial liquidity minting. Attackers could make the minimum quantity of liquidity token shares value so high by performing donations to the pool to make it infeasible for small liquidity providers to provide liquidity.

By minimum liquidity, Uniswap V2 can ensure that there is at least quite a number of minimum liquidity tokens, and thus make it harder for attacker to perform the attack.

Problem 4

Investigate the minting function in the UniswapV2Pair contract. When depositing tokens (not for the first time), liquidity can only be obtained using a specific formula. What is the intention behind this?

Solution

The formula is:

liquidity = min(amount0 / reserve0, amount1 / reserve1) * totalSupply

The intention is to maintain the ratio of the two tokens (the relative prices) in the pool. With the specific formula, when liquidity providers provide liquidity that are not of the same ratio of the tokens currently in liquidity pool, liquidity providers will lose money (receive less liquidity token) due to the formula.

Problem 5

What is a sandwich attack, and how might it impact you when initiating a swap?

Solution

The following scenario shows a sandwich attack.

Suppose the victim is swapping A token for B token.

Attacker detects the victim's transaction in the mempool, and swap A token for B token before the victim (front running, possibly by giving higher priority fee), then the amount of token B that one unit of token A will become lower. (value of token A increases). Leading to victim will have a slippage. Then, after the victim's swap transaction, A token's value would raise again. This time, attacker can swap B token for A token, and get more A token then initial.

homework-2's People

Contributors

iceylemon157 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.