UDTswap_scripts/UDTswap_udt_based.c
int add_liquidity(
  uint128_t u_r1,
  uint128_t u_r2,
  uint128_t u_r_a1,
  uint128_t u_r_a2,
  uint128_t t_l,
  uint128_t t_l_a
) {
  struct bn
    temp2,
    temp3,
    udt1_reserve,
    udt2_reserve,
    udt1_amount,
    udt2_amount,
    user_liquidity,
    total_liquidity,
    one
  ;

  bignum_init(&temp2);
  bignum_init(&temp3);
  bignum_init(&udt1_reserve);
  bignum_init(&udt2_reserve);
  bignum_init(&udt1_amount);
  bignum_init(&udt2_amount);
  bignum_init(&user_liquidity);
  bignum_init(&total_liquidity);
  bignum_init(&one);

  uint128_t_to_bignum(u_r1, &udt1_reserve);
  uint128_t_to_bignum(u_r2, &udt2_reserve);
  uint128_t_to_bignum(u_r_a1 - u_r1, &udt1_amount);
  uint128_t_to_bignum(u_r_a2 - u_r2, &udt2_amount);
  uint128_t_to_bignum(t_l_a - t_l, &user_liquidity);
  uint128_t_to_bignum(t_l, &total_liquidity);

  bignum_from_uint64_t(&one, 0x0000000000000001);

  bignum_mul(&udt2_reserve, &udt1_amount, &temp2);
  if (bignum_is_zero(&udt1_reserve) == 1) {
    return DIVIDE_ZERO_ERROR;
  }
  bignum_div(&temp2, &udt1_reserve, &temp3);
  bignum_add(&temp3, &one, &temp2);
  if (bignum_cmp(&temp2, &udt2_amount) != EQUAL) {
    return ADD_LIQUIDITY_NOT_CORRECT_ERROR;
  }
  //udt amount to add liquidity

  bignum_mul(&total_liquidity, &udt1_amount, &temp2);
  bignum_div(&temp2, &udt1_reserve, &temp3); //already checked above
  if (bignum_cmp(&temp3, &user_liquidity) != EQUAL) {
    return LIQUIDITY_NOT_CORRECT_ERROR;
  }
  //user's liquidity amount
  return CKB_SUCCESS;
}
/*
 * @dev check adding liquidity
 * second udt amount check with first udt amount
 * receiving user's liquidity check
 *
 * @param u_r1 first udt reserve before adding liquidity
 * @param u_r2 second udt reserve before adding liquidity
 * @param u_r_a1 first udt reserve after adding liquidity
 * @param u_r_a2 second udt reserve after adding liquidity
 * @param t_l total liquidity before adding liquidity
 * @param t_l_a total liquidity after adding liquidity
 */
@dev check adding liquidity
second udt amount check with first udt amount
receiving user's liquidity check


@dev check adding liquidity second udt amount check with first udt amount receiving user ' s liquidity check
int add_liquidity(
  uint128_t u_r1,
  uint128_t u_r2,
  uint128_t u_r_a1,
  uint128_t u_r_a2,
  uint128_t t_l,
  uint128_t t_l_a
) {
  struct bn
    temp2,
    temp3,
    udt1_reserve,
    udt2_reserve,
    udt1_amount,
    udt2_amount,
    user_liquidity,
    total_liquidity,
    one
  ;
  bignum_init(&temp2);
  bignum_init(&temp3);
  bignum_init(&udt1_reserve);
  bignum_init(&udt2_reserve);
  bignum_init(&udt1_amount);
  bignum_init(&udt2_amount);
  bignum_init(&user_liquidity);
  bignum_init(&total_liquidity);
  bignum_init(&one);
  uint128_t_to_bignum(u_r1, &udt1_reserve);
  uint128_t_to_bignum(u_r2, &udt2_reserve);
  uint128_t_to_bignum(u_r_a1 - u_r1, &udt1_amount);
  uint128_t_to_bignum(u_r_a2 - u_r2, &udt2_amount);
  uint128_t_to_bignum(t_l_a - t_l, &user_liquidity);
  uint128_t_to_bignum(t_l, &total_liquidity);
  bignum_from_uint64_t(&one, 0x0000000000000001);
  bignum_mul(&udt2_reserve, &udt1_amount, &temp2);
  if (bignum_is_zero(&udt1_reserve) == 1) {
    return DIVIDE_ZERO_ERROR;
  }
  bignum_div(&temp2, &udt1_reserve, &temp3);
  bignum_add(&temp3, &one, &temp2);
  if (bignum_cmp(&temp2, &udt2_amount) != EQUAL) {
    return ADD_LIQUIDITY_NOT_CORRECT_ERROR;
  }
  bignum_mul(&total_liquidity, &udt1_amount, &temp2);
  bignum_div(&temp2, &udt1_reserve, &temp3); 
  if (bignum_cmp(&temp3, &user_liquidity) != EQUAL) {
    return LIQUIDITY_NOT_CORRECT_ERROR;
  }
  return CKB_SUCCESS;
}
int add_liquidity ( uint128_t u_r1 , uint128_t u_r2 , uint128_t u_r_a1 , uint128_t u_r_a2 , uint128_t t_l , uint128_t t_l_a ) { struct bn temp2 , temp3 , udt1_reserve , udt2_reserve , udt1_amount , udt2_amount , user_liquidity , total_liquidity , one ; bignum_init ( & temp2 ) ; bignum_init ( & temp3 ) ; bignum_init ( & udt1_reserve ) ; bignum_init ( & udt2_reserve ) ; bignum_init ( & udt1_amount ) ; bignum_init ( & udt2_amount ) ; bignum_init ( & user_liquidity ) ; bignum_init ( & total_liquidity ) ; bignum_init ( & one ) ; uint128_t_to_bignum ( u_r1 , & udt1_reserve ) ; uint128_t_to_bignum ( u_r2 , & udt2_reserve ) ; uint128_t_to_bignum ( u_r_a1 - u_r1 , & udt1_amount ) ; uint128_t_to_bignum ( u_r_a2 - u_r2 , & udt2_amount ) ; uint128_t_to_bignum ( t_l_a - t_l , & user_liquidity ) ; uint128_t_to_bignum ( t_l , & total_liquidity ) ; bignum_from_uint64_t ( & one , 0x0000000000000001 ) ; bignum_mul ( & udt2_reserve , & udt1_amount , & temp2 ) ; if ( bignum_is_zero ( & udt1_reserve ) == 1 ) { return DIVIDE_ZERO_ERROR ; } bignum_div ( & temp2 , & udt1_reserve , & temp3 ) ; bignum_add ( & temp3 , & one , & temp2 ) ; if ( bignum_cmp ( & temp2 , & udt2_amount ) != EQUAL ) { return ADD_LIQUIDITY_NOT_CORRECT_ERROR ; } bignum_mul ( & total_liquidity , & udt1_amount , & temp2 ) ; bignum_div ( & temp2 , & udt1_reserve , & temp3 ) ; if ( bignum_cmp ( & temp3 , & user_liquidity ) != EQUAL ) { return LIQUIDITY_NOT_CORRECT_ERROR ; } return CKB_SUCCESS ; }
@dev check adding liquidity
 second udt amount check with first udt amount
 receiving user's liquidity check
@dev check adding liquidity second udt amount check with first udt amount receiving user ' s liquidity check
//udt amount to add liquidity //already checked above //user's liquidity amount