Liquidswap 코드 분석

JJVoiture·2023년 2월 20일
0

기능 요약

router

  • register_pool
    X, Y 유동성 풀 등록

  • add_liquidity, mint
    X/Y 유동성 풀에 유동성 공급

  • remove_liquidity, burn
    LP 코인을 소각하고 X,Y 코인을 환급

  • swap

    • swap_exact_coin_for_coin
      일정량의 코인 X를 코인 Y로 스왑
    • swap_coin_for_exact_coin
      코인 X를 일정량의 코인 Y로 스왑하기 위해 코인 X 최대량을 입력
    • swap_coin_for_coin_unchecked
      input/output 양을 체크하지 않고 코인 X를 코인 Y로 스왑
  • get_decimals_scales
    stable curve의 코인 X와 코인 Y의 decimal scale을 반환

  • get_cumulative_prices
    X/Y 유동성 풀의 현재 누적 가격(cumulative price)를 구함
    return (X price, Y price, block_timestamp);

  • get_reserves_size
    유동성 풀 X/Y의 reserve(충당금) 반환

  • fee

    • get_fees_config
      특정 풀의 fee를 분자, 분모 형태로 반환
      return (numerator, denominator);
      numerator: 분자, denominator: 분모
    • get_fee
      특정 풀의 fee를 반환
    • get_dao_fees_config
      특정 풀의 DAO fee를 분자 분모 형태로 반환
    • get_dao_fee
      특정 풀의 DAO fee를 반환
  • is_swap_exists
    X, Y 페어의 swap이 존재하는지 체크, boolean 값 반환

  • Math

    • calc_optimal_coin_values
      새로운 유동성 공급을 위해 추가해야 하는 X,Y의 적정량 반환
      input

      • x_desired, y_desired: 공급된 양
      • x_min, y_min: 기대 최소량
    • convert_with_current_price
      coin_in(스왑할 양)을 위한 LP의 크기
      input

      • coin_in: 스왑할 양
      • reserve_in: 스왑할 코인의 리저브
      • reserve_out: 얻을 코인의 리저브
    • get_reserves_for_lp_coins
      LP 코인을 X,Y로 변환

    • get_amount_out
      코인 X를 amount_in만큼 넣어서 얻는 코인 Y의 양

    • get_amount_in
      코인 X의 amount_out만큼 넣어서 얻는 코인 X의 양

    • get_coin_out_with_fees
      coin_in만큼의 코인을 넣어서 얻는 coin_out 양

      input

      • coin_in
      • reserve_in: 스왑할 코인의 리저브
      • reserve_out: 얻을 코인의 리저브
      • scale_in: 스왑할 코인의 log_10 스케일
      • scale_out: 얻을 코인의 log_10 스케일
    • get_coin_in_with_fees
      coin_out을 얻기 위해 필요한 coin_in

      input

      • coin_out
      • reserve_in: 스왑할 코인의 리저브
      • reserve_out: 얻을 코인의 리저브
      • scale_in: 스왑할 코인의 log_10 스케일
      • scale_out: 얻을 코인의 log_10 스케일
     공식
      y = x * (fee_scale - fee_pct) * ry / (rx + x * (fee_scale - fee_pct))
     
     
      x = y * rx / ((ry - y) * (fee_scale - fee_pct)) or
      x = y * rx * (fee_scale) / ((ry - y) * (fee_scale - fee_pct))

    liquidity_pool

    /// Liquidity pool with reserves.
      struct LiquidityPool<phantom X, phantom Y, phantom Curve> has key {
          coin_x_reserve: Coin<X>,
          coin_y_reserve: Coin<Y>,
          last_block_timestamp: u64,
          last_price_x_cumulative: u128,
          last_price_y_cumulative: u128,
          lp_mint_cap: coin::MintCapability<LP<X, Y, Curve>>,
          lp_burn_cap: coin::BurnCapability<LP<X, Y, Curve>>,
          // Scales are pow(10, token_decimals).
          x_scale: u64,
          y_scale: u64,
          locked: bool,
          fee: u64,           // 1 - 100 (0.01% - 1%)
          dao_fee: u64,       // 0 - 100 (0% - 100%)
      }
  • loan

    • flashloan
      특정 풀에 대한 loan 발급
    • pay_flashloan
      flashloan 값기
  • 체크(assert)

    • assert_lp_value_is_increased
      스왑 전후의 LP value를 비교.
    • assert_pool_unlocked, is_pool_locked
      풀의 lock 여부 체크
    • is_pool_exists
      유동성 풀이 존재하는지 체크
  • new_reserves_after_fees_scaled
    fee를 빼고 난 이후의 새로 들어온 코인 양을 합한 reserve 값.

  • split_fee_to_dao
    fee의 일부를 DAO storage에 지급

  • update_oracle
    현재 cumulative price를 업데이트

  • get_cumulative_prices
    현재 cumulative price를 get

profile
안녕하세요.

0개의 댓글