UDTswap_scripts/UDTswap_udt_based.c
int swap(
  uint128_t i_r,
  uint128_t o_r,
  uint128_t i_r_a,
  uint128_t o_r_a
) {
  int flag = 0;
  struct bn
    temp2,
    temp3,
    temp4,
    temp5,
    input_amount,
    output_amount,
    input_reserve,
    output_reserve,
    thousand,
    except_fee,
    one
  ;

  bignum_init(&temp2);
  bignum_init(&temp3);
  bignum_init(&temp4);
  bignum_init(&temp5);
  bignum_init(&input_amount);
  bignum_init(&input_reserve);
  bignum_init(&output_amount);
  bignum_init(&output_reserve);
  bignum_init(&thousand);
  bignum_init(&except_fee);
  bignum_init(&one);

  uint128_t_to_bignum(i_r_a - i_r, &input_amount);
  uint128_t_to_bignum(i_r, &input_reserve);
  uint128_t_to_bignum(o_r - o_r_a, &output_amount);
  uint128_t_to_bignum(o_r, &output_reserve);

  bignum_from_uint64_t(&thousand, 0x00000000000003e8);
  bignum_from_uint64_t(&except_fee, LIQUIDITY_POOL_EXCEPT_FEE);
  bignum_from_uint64_t(&one, 0x0000000000000001);

  bignum_mul(&input_reserve, &thousand, &temp2);
  bignum_mul(&temp2, &output_amount, &temp3);
  if (bignum_cmp(&output_reserve, &output_amount) == SMALLER) {
    return SUBTRACT_ERROR;
  }
  bignum_sub(&output_reserve, &output_amount, &temp4);
  bignum_mul(&temp4, &except_fee, &temp5);
  if (bignum_is_zero(&temp5) == 1) {
    return DIVIDE_ZERO_ERROR;
  }
  bignum_div(&temp3, &temp5, &temp4);
  bignum_add(&temp4, &one, &temp5);
  if (bignum_cmp(&temp5, &input_amount) != EQUAL) {
    flag = 1;
  }
  //input amount by output amount

  bignum_mul(&input_amount, &except_fee, &temp3);
  bignum_mul(&temp3, &output_reserve, &temp4);
  bignum_add(&temp2, &temp3, &temp5);
  if (bignum_is_zero(&temp5) == 1) {
    return DIVIDE_ZERO_ERROR;
  }
  bignum_div(&temp4, &temp5, &temp2);
  if (bignum_cmp(&temp2, &output_amount) != EQUAL && flag==1) {
    return SWAP_NOT_CORRECT_ERROR;
  }
  //output amount by input amount
  return CKB_SUCCESS;
}
/*
 * @dev check swapping
 * check input amount calculated by output amount
 * check output amount calculated by input amount
 * if more than one of above are correct, success
 *
 * @param i_r input udt reserve before swapping
 * @param o_r output udt reserve before swapping
 * @param i_r_a input udt reserve after swapping
 * @param o_r_a output udt reserve after swapping
 */
@dev check swapping
check input amount calculated by output amount
check output amount calculated by input amount
if more than one of above are correct, success

@param i_r input udt reserve before swapping
@param o_r output udt reserve before swapping
@param i_r_a input udt reserve after swapping
@param o_r_a output udt reserve after swapping
@dev check swapping check input amount calculated by output amount check output amount calculated by input amount if more than one of above are correct success @param i_r input udt reserve before swapping @param o_r output udt reserve before swapping @param i_r_a input udt reserve after swapping @param o_r_a output udt reserve after swapping
int swap(
  uint128_t i_r,
  uint128_t o_r,
  uint128_t i_r_a,
  uint128_t o_r_a
) {
  int flag = 0;
  struct bn
    temp2,
    temp3,
    temp4,
    temp5,
    input_amount,
    output_amount,
    input_reserve,
    output_reserve,
    thousand,
    except_fee,
    one
  ;
  bignum_init(&temp2);
  bignum_init(&temp3);
  bignum_init(&temp4);
  bignum_init(&temp5);
  bignum_init(&input_amount);
  bignum_init(&input_reserve);
  bignum_init(&output_amount);
  bignum_init(&output_reserve);
  bignum_init(&thousand);
  bignum_init(&except_fee);
  bignum_init(&one);
  uint128_t_to_bignum(i_r_a - i_r, &input_amount);
  uint128_t_to_bignum(i_r, &input_reserve);
  uint128_t_to_bignum(o_r - o_r_a, &output_amount);
  uint128_t_to_bignum(o_r, &output_reserve);
  bignum_from_uint64_t(&thousand, 0x00000000000003e8);
  bignum_from_uint64_t(&except_fee, LIQUIDITY_POOL_EXCEPT_FEE);
  bignum_from_uint64_t(&one, 0x0000000000000001);
  bignum_mul(&input_reserve, &thousand, &temp2);
  bignum_mul(&temp2, &output_amount, &temp3);
  if (bignum_cmp(&output_reserve, &output_amount) == SMALLER) {
    return SUBTRACT_ERROR;
  }
  bignum_sub(&output_reserve, &output_amount, &temp4);
  bignum_mul(&temp4, &except_fee, &temp5);
  if (bignum_is_zero(&temp5) == 1) {
    return DIVIDE_ZERO_ERROR;
  }
  bignum_div(&temp3, &temp5, &temp4);
  bignum_add(&temp4, &one, &temp5);
  if (bignum_cmp(&temp5, &input_amount) != EQUAL) {
    flag = 1;
  }
  bignum_mul(&input_amount, &except_fee, &temp3);
  bignum_mul(&temp3, &output_reserve, &temp4);
  bignum_add(&temp2, &temp3, &temp5);
  if (bignum_is_zero(&temp5) == 1) {
    return DIVIDE_ZERO_ERROR;
  }
  bignum_div(&temp4, &temp5, &temp2);
  if (bignum_cmp(&temp2, &output_amount) != EQUAL && flag==1) {
    return SWAP_NOT_CORRECT_ERROR;
  }
  return CKB_SUCCESS;
}
int swap ( uint128_t i_r , uint128_t o_r , uint128_t i_r_a , uint128_t o_r_a ) { int flag = 0 ; struct bn temp2 , temp3 , temp4 , temp5 , input_amount , output_amount , input_reserve , output_reserve , thousand , except_fee , one ; bignum_init ( & temp2 ) ; bignum_init ( & temp3 ) ; bignum_init ( & temp4 ) ; bignum_init ( & temp5 ) ; bignum_init ( & input_amount ) ; bignum_init ( & input_reserve ) ; bignum_init ( & output_amount ) ; bignum_init ( & output_reserve ) ; bignum_init ( & thousand ) ; bignum_init ( & except_fee ) ; bignum_init ( & one ) ; uint128_t_to_bignum ( i_r_a - i_r , & input_amount ) ; uint128_t_to_bignum ( i_r , & input_reserve ) ; uint128_t_to_bignum ( o_r - o_r_a , & output_amount ) ; uint128_t_to_bignum ( o_r , & output_reserve ) ; bignum_from_uint64_t ( & thousand , 0x00000000000003e8 ) ; bignum_from_uint64_t ( & except_fee , LIQUIDITY_POOL_EXCEPT_FEE ) ; bignum_from_uint64_t ( & one , 0x0000000000000001 ) ; bignum_mul ( & input_reserve , & thousand , & temp2 ) ; bignum_mul ( & temp2 , & output_amount , & temp3 ) ; if ( bignum_cmp ( & output_reserve , & output_amount ) == SMALLER ) { return SUBTRACT_ERROR ; } bignum_sub ( & output_reserve , & output_amount , & temp4 ) ; bignum_mul ( & temp4 , & except_fee , & temp5 ) ; if ( bignum_is_zero ( & temp5 ) == 1 ) { return DIVIDE_ZERO_ERROR ; } bignum_div ( & temp3 , & temp5 , & temp4 ) ; bignum_add ( & temp4 , & one , & temp5 ) ; if ( bignum_cmp ( & temp5 , & input_amount ) != EQUAL ) { flag = 1 ; } bignum_mul ( & input_amount , & except_fee , & temp3 ) ; bignum_mul ( & temp3 , & output_reserve , & temp4 ) ; bignum_add ( & temp2 , & temp3 , & temp5 ) ; if ( bignum_is_zero ( & temp5 ) == 1 ) { return DIVIDE_ZERO_ERROR ; } bignum_div ( & temp4 , & temp5 , & temp2 ) ; if ( bignum_cmp ( & temp2 , & output_amount ) != EQUAL && flag == 1 ) { return SWAP_NOT_CORRECT_ERROR ; } return CKB_SUCCESS ; }
@dev check swapping
 check input amount calculated by output amount
 check output amount calculated by input amount
 if more than one of above are correct, success
@dev check swapping check input amount calculated by output amount check output amount calculated by input amount if more than one of above are correct success
//input amount by output amount //output amount by input amount