UDTswap_scripts/UDTswap_udt_based.c
int create_udtswap_check() {
  int ret = check_tx_input();
  if (ret!=CKB_SUCCESS) {
    return ret;
  }

  uint64_t len=0;
  ret = ckb_load_cell_by_field(NULL, &len, 0, 0, CKB_SOURCE_GROUP_INPUT, CKB_CELL_FIELD_TYPE_HASH);
  if (ret != INDEX_OUT_OF_BOUND_ERROR) {
    return TOO_MANY_GROUP_CELL_ERROR;
  }

  ret = ckb_load_cell_by_field(NULL, &len, 0, 0, CKB_SOURCE_GROUP_OUTPUT, CKB_CELL_FIELD_TYPE_HASH);
  if (ret != CKB_SUCCESS) {
    return NOT_ENOUGH_GROUP_CELL_ERROR;
  }

  ret = ckb_load_cell_by_field(NULL, &len, 0, 1, CKB_SOURCE_GROUP_OUTPUT, CKB_CELL_FIELD_TYPE_HASH);
  if (ret != INDEX_OUT_OF_BOUND_ERROR) {
    return TOO_MANY_GROUP_CELL_ERROR;
  }
  //udtswap group should be no input 1 output

  len = SCRIPT_HASH_SIZE;
  uint8_t current_script_hash_buf[SCRIPT_HASH_SIZE];
  ret = ckb_load_script_hash(current_script_hash_buf, &len, 0);
  if (ret != CKB_SUCCESS) {
    return UDTSWAP_SYSCALL_ERROR - ret;
  }

  ret = check_script_hash(current_script_hash_buf, UDTSWAP_TYPE_CELL_INDEX, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_TYPE_HASH);
  if (ret != CKB_SUCCESS) {
    return ret;
  }

  //udtswap type hash checked

  uint8_t *lock_code_hash_buf;
  uint8_t *udt1_type_script_hash_buf;
  uint8_t *udt2_type_script_hash_buf;

  uint8_t script_buf[UDTSWAP_LOCK_SCRIPT_SIZE];
  len = UDTSWAP_LOCK_SCRIPT_SIZE;
  ret = ckb_load_cell_by_field(script_buf, &len, 0, UDTSWAP_TYPE_CELL_INDEX, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_LOCK);
  if (ret!=CKB_SUCCESS) {
      return UDTSWAP_SYSCALL_ERROR - ret;
  }
  if (len != UDTSWAP_LOCK_SCRIPT_SIZE) {
    return UDTSWAP_LOCK_SCRIPT_SIZE_NOT_CORRECT_ERROR;
  }

  lock_code_hash_buf = &script_buf[CODE_HASH_START];
  udt1_type_script_hash_buf = &script_buf[UDTSWAP_LOCK_ARGS_UDT1_SCRIPT_HASH_START];
  udt2_type_script_hash_buf = &script_buf[UDTSWAP_LOCK_ARGS_UDT2_SCRIPT_HASH_START];

  if (memcmp(lock_code_hash_buf, udtswap_lock_code_hash_buf, CODE_HASH_SIZE) != 0) {
    return CODE_HASH_NOT_MATCH_ERROR;
  }

  //udtswap lock code hash checked

  if (memcmp(udt1_type_script_hash_buf, udt2_type_script_hash_buf, SCRIPT_HASH_SIZE) >= 0) {
    return SAME_UDT_OR_ORDER_ERROR;
  }

  //not same udt, order checked

  uint8_t lock_script_hash_buf[SCRIPT_HASH_SIZE];
  len = SCRIPT_HASH_SIZE;
  ret = ckb_load_cell_by_field(lock_script_hash_buf, &len, 0, UDTSWAP_TYPE_CELL_INDEX, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_LOCK_HASH);
  if(ret!=CKB_SUCCESS) {
    return UDTSWAP_SYSCALL_ERROR - ret;
  }

  ret = check_script_hash(lock_script_hash_buf, UDTSWAP_UDT_LOCK_CELL_INDEX_1, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_LOCK_HASH);
  if(ret!=CKB_SUCCESS) {
    return ret;
  }

  ret = check_script_hash(lock_script_hash_buf, UDTSWAP_UDT_LOCK_CELL_INDEX_2, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_LOCK_HASH);
  if(ret!=CKB_SUCCESS) {
    return ret;
  }

  //udtswap lock hash checked
  int isCKB1 = 0;
  if(memcmp(udt1_type_script_hash_buf, udt_type_ckb_script_hash_buf, SCRIPT_HASH_SIZE)==0) {
    isCKB1 = 1;
    len = 0;
    ret = ckb_load_cell_by_field(NULL, &len, 0, UDTSWAP_UDT_LOCK_CELL_INDEX_1, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_TYPE_HASH);
    if (ret!=ITEM_MISSING_ERROR) {
        return SCRIPT_NOT_MATCH_ERROR;
    }
  } else {
    ret = check_script_hash(udt1_type_script_hash_buf, UDTSWAP_UDT_LOCK_CELL_INDEX_1, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_TYPE_HASH);
    if(ret!=CKB_SUCCESS) {
      return ret;
    }
  }

  int isCKB2 = 0;
  if(memcmp(udt2_type_script_hash_buf, udt_type_ckb_script_hash_buf, SCRIPT_HASH_SIZE)==0) {
    isCKB2 = 1;
    len = 0;
    ret = ckb_load_cell_by_field(NULL, &len, 0, UDTSWAP_UDT_LOCK_CELL_INDEX_2, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_TYPE_HASH);
    if (ret!=ITEM_MISSING_ERROR) {
        return SCRIPT_NOT_MATCH_ERROR;
    }
  } else {
    ret = check_script_hash(udt2_type_script_hash_buf, UDTSWAP_UDT_LOCK_CELL_INDEX_2, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_TYPE_HASH);
    if(ret!=CKB_SUCCESS) {
      return ret;
    }
  }

  //udt type hash checked

  uint8_t udtswap_data_buf[UDTSWAP_DATA_SIZE];
  len = UDTSWAP_DATA_SIZE;
  ret = ckb_load_cell_data(udtswap_data_buf, &len, 0, UDTSWAP_TYPE_CELL_INDEX, CKB_SOURCE_OUTPUT);
  if (ret!=CKB_SUCCESS) {
    return UDTSWAP_SYSCALL_ERROR - ret;
  }
  if(len!=UDTSWAP_DATA_SIZE) {
    return UDTSWAP_DATA_SIZE_NOT_CORRECT_ERROR;
  }
  uint128_t udtswap_udt1_reserve = get_uint128_t(UDTSWAP_DATA_UDT1_RESERVE_START, udtswap_data_buf);
  uint128_t udtswap_udt2_reserve = get_uint128_t(UDTSWAP_DATA_UDT2_RESERVE_START, udtswap_data_buf);
  uint128_t udtswap_total_liquidity = get_uint128_t(UDTSWAP_DATA_TOTAL_LIQUIDITY_START, udtswap_data_buf);

  uint64_t udt_amount_64;
  uint128_t udt1_amount = 0;
  uint8_t udt_data_buf[UDT_AMOUNT_SIZE];
  if(isCKB1) {
    len = 8;
    ret = ckb_load_cell_by_field((uint8_t *)&udt_amount_64, &len, 0, UDTSWAP_UDT_LOCK_CELL_INDEX_1, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_CAPACITY);
    if(ret!=CKB_SUCCESS) {
      return UDTSWAP_SYSCALL_ERROR - ret;
    }
    udt1_amount = (uint128_t)udt_amount_64;
  } else {
    ret = ckb_load_cell_data(udt_data_buf, &len, 0, UDTSWAP_UDT_LOCK_CELL_INDEX_1, CKB_SOURCE_OUTPUT);
    if(ret!=CKB_SUCCESS) {
      return UDTSWAP_SYSCALL_ERROR - ret;
    }
    udt1_amount = get_uint128_t(0, udt_data_buf);
  }

  uint128_t udt2_amount = 0;
  if(isCKB2) {
    len = 8;
    ret = ckb_load_cell_by_field((uint8_t *)&udt_amount_64, &len, 0, UDTSWAP_UDT_LOCK_CELL_INDEX_2, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_CAPACITY);
    if(ret!=CKB_SUCCESS) {
      return UDTSWAP_SYSCALL_ERROR - ret;
    }
    udt2_amount = (uint128_t)udt_amount_64;
  } else {
    ret = ckb_load_cell_data(udt_data_buf, &len, 0, UDTSWAP_UDT_LOCK_CELL_INDEX_2, CKB_SOURCE_OUTPUT);
    if(ret!=CKB_SUCCESS) {
      return UDTSWAP_SYSCALL_ERROR - ret;
    }
    udt2_amount = get_uint128_t(0, udt_data_buf);
  }

  if(
    udtswap_udt1_reserve != udt1_amount ||
    udtswap_udt2_reserve != udt2_amount ||
    udt1_amount != (isCKB1 ? CKB_RESERVE_DEFAULT : UDT_RESERVE_DEFAULT) ||
    udt2_amount != (isCKB2 ? CKB_RESERVE_DEFAULT : UDT_RESERVE_DEFAULT) ||
    udtswap_total_liquidity != 0
  ) {
    return RESULT_NOT_CORRECT_ERROR;
  }
  //udtswap udts reserve, udts amount same checked
  //ckb reserve default checked
  //udt reserve default checked
  //total liquidity default checked

  return CKB_SUCCESS;
}
/*
 * @dev check creating new pool
 * check tx first input
 * check type group
 * check type script hash
 * check lock code hash
 * check first udt and second udt not same
 * check lock script hash
 * check first, second udt type hash
 * check udt reserve, lock amount
 * check empty udt reserve
 * check empty total liquidity
 */
@dev check creating new pool
check tx first input
check type group
check type script hash
check lock code hash
check first udt and second udt not same
check lock script hash
check first, second udt type hash
check udt reserve, lock amount
check empty udt reserve
check empty total liquidity
@dev check creating new pool check tx first input check type group check type script hash check lock code hash check first udt and second udt not same check lock script hash check first second udt type hash check udt reserve lock amount check empty udt reserve check empty total liquidity
int create_udtswap_check() {
  int ret = check_tx_input();
  if (ret!=CKB_SUCCESS) {
    return ret;
  }
  uint64_t len=0;
  ret = ckb_load_cell_by_field(NULL, &len, 0, 0, CKB_SOURCE_GROUP_INPUT, CKB_CELL_FIELD_TYPE_HASH);
  if (ret != INDEX_OUT_OF_BOUND_ERROR) {
    return TOO_MANY_GROUP_CELL_ERROR;
  }
  ret = ckb_load_cell_by_field(NULL, &len, 0, 0, CKB_SOURCE_GROUP_OUTPUT, CKB_CELL_FIELD_TYPE_HASH);
  if (ret != CKB_SUCCESS) {
    return NOT_ENOUGH_GROUP_CELL_ERROR;
  }
  ret = ckb_load_cell_by_field(NULL, &len, 0, 1, CKB_SOURCE_GROUP_OUTPUT, CKB_CELL_FIELD_TYPE_HASH);
  if (ret != INDEX_OUT_OF_BOUND_ERROR) {
    return TOO_MANY_GROUP_CELL_ERROR;
  }
  len = SCRIPT_HASH_SIZE;
  uint8_t current_script_hash_buf[SCRIPT_HASH_SIZE];
  ret = ckb_load_script_hash(current_script_hash_buf, &len, 0);
  if (ret != CKB_SUCCESS) {
    return UDTSWAP_SYSCALL_ERROR - ret;
  }
  ret = check_script_hash(current_script_hash_buf, UDTSWAP_TYPE_CELL_INDEX, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_TYPE_HASH);
  if (ret != CKB_SUCCESS) {
    return ret;
  }
  uint8_t *lock_code_hash_buf;
  uint8_t *udt1_type_script_hash_buf;
  uint8_t *udt2_type_script_hash_buf;
  uint8_t script_buf[UDTSWAP_LOCK_SCRIPT_SIZE];
  len = UDTSWAP_LOCK_SCRIPT_SIZE;
  ret = ckb_load_cell_by_field(script_buf, &len, 0, UDTSWAP_TYPE_CELL_INDEX, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_LOCK);
  if (ret!=CKB_SUCCESS) {
      return UDTSWAP_SYSCALL_ERROR - ret;
  }
  if (len != UDTSWAP_LOCK_SCRIPT_SIZE) {
    return UDTSWAP_LOCK_SCRIPT_SIZE_NOT_CORRECT_ERROR;
  }
  lock_code_hash_buf = &script_buf[CODE_HASH_START];
  udt1_type_script_hash_buf = &script_buf[UDTSWAP_LOCK_ARGS_UDT1_SCRIPT_HASH_START];
  udt2_type_script_hash_buf = &script_buf[UDTSWAP_LOCK_ARGS_UDT2_SCRIPT_HASH_START];
  if (memcmp(lock_code_hash_buf, udtswap_lock_code_hash_buf, CODE_HASH_SIZE) != 0) {
    return CODE_HASH_NOT_MATCH_ERROR;
  }
  if (memcmp(udt1_type_script_hash_buf, udt2_type_script_hash_buf, SCRIPT_HASH_SIZE) >= 0) {
    return SAME_UDT_OR_ORDER_ERROR;
  }
  uint8_t lock_script_hash_buf[SCRIPT_HASH_SIZE];
  len = SCRIPT_HASH_SIZE;
  ret = ckb_load_cell_by_field(lock_script_hash_buf, &len, 0, UDTSWAP_TYPE_CELL_INDEX, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_LOCK_HASH);
  if(ret!=CKB_SUCCESS) {
    return UDTSWAP_SYSCALL_ERROR - ret;
  }
  ret = check_script_hash(lock_script_hash_buf, UDTSWAP_UDT_LOCK_CELL_INDEX_1, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_LOCK_HASH);
  if(ret!=CKB_SUCCESS) {
    return ret;
  }
  ret = check_script_hash(lock_script_hash_buf, UDTSWAP_UDT_LOCK_CELL_INDEX_2, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_LOCK_HASH);
  if(ret!=CKB_SUCCESS) {
    return ret;
  }
  int isCKB1 = 0;
  if(memcmp(udt1_type_script_hash_buf, udt_type_ckb_script_hash_buf, SCRIPT_HASH_SIZE)==0) {
    isCKB1 = 1;
    len = 0;
    ret = ckb_load_cell_by_field(NULL, &len, 0, UDTSWAP_UDT_LOCK_CELL_INDEX_1, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_TYPE_HASH);
    if (ret!=ITEM_MISSING_ERROR) {
        return SCRIPT_NOT_MATCH_ERROR;
    }
  } else {
    ret = check_script_hash(udt1_type_script_hash_buf, UDTSWAP_UDT_LOCK_CELL_INDEX_1, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_TYPE_HASH);
    if(ret!=CKB_SUCCESS) {
      return ret;
    }
  }
  int isCKB2 = 0;
  if(memcmp(udt2_type_script_hash_buf, udt_type_ckb_script_hash_buf, SCRIPT_HASH_SIZE)==0) {
    isCKB2 = 1;
    len = 0;
    ret = ckb_load_cell_by_field(NULL, &len, 0, UDTSWAP_UDT_LOCK_CELL_INDEX_2, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_TYPE_HASH);
    if (ret!=ITEM_MISSING_ERROR) {
        return SCRIPT_NOT_MATCH_ERROR;
    }
  } else {
    ret = check_script_hash(udt2_type_script_hash_buf, UDTSWAP_UDT_LOCK_CELL_INDEX_2, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_TYPE_HASH);
    if(ret!=CKB_SUCCESS) {
      return ret;
    }
  }
  uint8_t udtswap_data_buf[UDTSWAP_DATA_SIZE];
  len = UDTSWAP_DATA_SIZE;
  ret = ckb_load_cell_data(udtswap_data_buf, &len, 0, UDTSWAP_TYPE_CELL_INDEX, CKB_SOURCE_OUTPUT);
  if (ret!=CKB_SUCCESS) {
    return UDTSWAP_SYSCALL_ERROR - ret;
  }
  if(len!=UDTSWAP_DATA_SIZE) {
    return UDTSWAP_DATA_SIZE_NOT_CORRECT_ERROR;
  }
  uint128_t udtswap_udt1_reserve = get_uint128_t(UDTSWAP_DATA_UDT1_RESERVE_START, udtswap_data_buf);
  uint128_t udtswap_udt2_reserve = get_uint128_t(UDTSWAP_DATA_UDT2_RESERVE_START, udtswap_data_buf);
  uint128_t udtswap_total_liquidity = get_uint128_t(UDTSWAP_DATA_TOTAL_LIQUIDITY_START, udtswap_data_buf);
  uint64_t udt_amount_64;
  uint128_t udt1_amount = 0;
  uint8_t udt_data_buf[UDT_AMOUNT_SIZE];
  if(isCKB1) {
    len = 8;
    ret = ckb_load_cell_by_field((uint8_t *)&udt_amount_64, &len, 0, UDTSWAP_UDT_LOCK_CELL_INDEX_1, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_CAPACITY);
    if(ret!=CKB_SUCCESS) {
      return UDTSWAP_SYSCALL_ERROR - ret;
    }
    udt1_amount = (uint128_t)udt_amount_64;
  } else {
    ret = ckb_load_cell_data(udt_data_buf, &len, 0, UDTSWAP_UDT_LOCK_CELL_INDEX_1, CKB_SOURCE_OUTPUT);
    if(ret!=CKB_SUCCESS) {
      return UDTSWAP_SYSCALL_ERROR - ret;
    }
    udt1_amount = get_uint128_t(0, udt_data_buf);
  }
  uint128_t udt2_amount = 0;
  if(isCKB2) {
    len = 8;
    ret = ckb_load_cell_by_field((uint8_t *)&udt_amount_64, &len, 0, UDTSWAP_UDT_LOCK_CELL_INDEX_2, CKB_SOURCE_OUTPUT, CKB_CELL_FIELD_CAPACITY);
    if(ret!=CKB_SUCCESS) {
      return UDTSWAP_SYSCALL_ERROR - ret;
    }
    udt2_amount = (uint128_t)udt_amount_64;
  } else {
    ret = ckb_load_cell_data(udt_data_buf, &len, 0, UDTSWAP_UDT_LOCK_CELL_INDEX_2, CKB_SOURCE_OUTPUT);
    if(ret!=CKB_SUCCESS) {
      return UDTSWAP_SYSCALL_ERROR - ret;
    }
    udt2_amount = get_uint128_t(0, udt_data_buf);
  }
  if(
    udtswap_udt1_reserve != udt1_amount ||
    udtswap_udt2_reserve != udt2_amount ||
    udt1_amount != (isCKB1 ? CKB_RESERVE_DEFAULT : UDT_RESERVE_DEFAULT) ||
    udt2_amount != (isCKB2 ? CKB_RESERVE_DEFAULT : UDT_RESERVE_DEFAULT) ||
    udtswap_total_liquidity != 0
  ) {
    return RESULT_NOT_CORRECT_ERROR;
  }
  return CKB_SUCCESS;
}
int create_udtswap_check ( ) { int ret = check_tx_input ( ) ; if ( ret != CKB_SUCCESS ) { return ret ; } uint64_t len = 0 ; ret = ckb_load_cell_by_field ( NULL , & len , 0 , 0 , CKB_SOURCE_GROUP_INPUT , CKB_CELL_FIELD_TYPE_HASH ) ; if ( ret != INDEX_OUT_OF_BOUND_ERROR ) { return TOO_MANY_GROUP_CELL_ERROR ; } ret = ckb_load_cell_by_field ( NULL , & len , 0 , 0 , CKB_SOURCE_GROUP_OUTPUT , CKB_CELL_FIELD_TYPE_HASH ) ; if ( ret != CKB_SUCCESS ) { return NOT_ENOUGH_GROUP_CELL_ERROR ; } ret = ckb_load_cell_by_field ( NULL , & len , 0 , 1 , CKB_SOURCE_GROUP_OUTPUT , CKB_CELL_FIELD_TYPE_HASH ) ; if ( ret != INDEX_OUT_OF_BOUND_ERROR ) { return TOO_MANY_GROUP_CELL_ERROR ; } len = SCRIPT_HASH_SIZE ; uint8_t current_script_hash_buf [ SCRIPT_HASH_SIZE ] ; ret = ckb_load_script_hash ( current_script_hash_buf , & len , 0 ) ; if ( ret != CKB_SUCCESS ) { return UDTSWAP_SYSCALL_ERROR - ret ; } ret = check_script_hash ( current_script_hash_buf , UDTSWAP_TYPE_CELL_INDEX , CKB_SOURCE_OUTPUT , CKB_CELL_FIELD_TYPE_HASH ) ; if ( ret != CKB_SUCCESS ) { return ret ; } uint8_t * lock_code_hash_buf ; uint8_t * udt1_type_script_hash_buf ; uint8_t * udt2_type_script_hash_buf ; uint8_t script_buf [ UDTSWAP_LOCK_SCRIPT_SIZE ] ; len = UDTSWAP_LOCK_SCRIPT_SIZE ; ret = ckb_load_cell_by_field ( script_buf , & len , 0 , UDTSWAP_TYPE_CELL_INDEX , CKB_SOURCE_OUTPUT , CKB_CELL_FIELD_LOCK ) ; if ( ret != CKB_SUCCESS ) { return UDTSWAP_SYSCALL_ERROR - ret ; } if ( len != UDTSWAP_LOCK_SCRIPT_SIZE ) { return UDTSWAP_LOCK_SCRIPT_SIZE_NOT_CORRECT_ERROR ; } lock_code_hash_buf = & script_buf [ CODE_HASH_START ] ; udt1_type_script_hash_buf = & script_buf [ UDTSWAP_LOCK_ARGS_UDT1_SCRIPT_HASH_START ] ; udt2_type_script_hash_buf = & script_buf [ UDTSWAP_LOCK_ARGS_UDT2_SCRIPT_HASH_START ] ; if ( memcmp ( lock_code_hash_buf , udtswap_lock_code_hash_buf , CODE_HASH_SIZE ) != 0 ) { return CODE_HASH_NOT_MATCH_ERROR ; } if ( memcmp ( udt1_type_script_hash_buf , udt2_type_script_hash_buf , SCRIPT_HASH_SIZE ) >= 0 ) { return SAME_UDT_OR_ORDER_ERROR ; } uint8_t lock_script_hash_buf [ SCRIPT_HASH_SIZE ] ; len = SCRIPT_HASH_SIZE ; ret = ckb_load_cell_by_field ( lock_script_hash_buf , & len , 0 , UDTSWAP_TYPE_CELL_INDEX , CKB_SOURCE_OUTPUT , CKB_CELL_FIELD_LOCK_HASH ) ; if ( ret != CKB_SUCCESS ) { return UDTSWAP_SYSCALL_ERROR - ret ; } ret = check_script_hash ( lock_script_hash_buf , UDTSWAP_UDT_LOCK_CELL_INDEX_1 , CKB_SOURCE_OUTPUT , CKB_CELL_FIELD_LOCK_HASH ) ; if ( ret != CKB_SUCCESS ) { return ret ; } ret = check_script_hash ( lock_script_hash_buf , UDTSWAP_UDT_LOCK_CELL_INDEX_2 , CKB_SOURCE_OUTPUT , CKB_CELL_FIELD_LOCK_HASH ) ; if ( ret != CKB_SUCCESS ) { return ret ; } int isCKB1 = 0 ; if ( memcmp ( udt1_type_script_hash_buf , udt_type_ckb_script_hash_buf , SCRIPT_HASH_SIZE ) == 0 ) { isCKB1 = 1 ; len = 0 ; ret = ckb_load_cell_by_field ( NULL , & len , 0 , UDTSWAP_UDT_LOCK_CELL_INDEX_1 , CKB_SOURCE_OUTPUT , CKB_CELL_FIELD_TYPE_HASH ) ; if ( ret != ITEM_MISSING_ERROR ) { return SCRIPT_NOT_MATCH_ERROR ; } } else { ret = check_script_hash ( udt1_type_script_hash_buf , UDTSWAP_UDT_LOCK_CELL_INDEX_1 , CKB_SOURCE_OUTPUT , CKB_CELL_FIELD_TYPE_HASH ) ; if ( ret != CKB_SUCCESS ) { return ret ; } } int isCKB2 = 0 ; if ( memcmp ( udt2_type_script_hash_buf , udt_type_ckb_script_hash_buf , SCRIPT_HASH_SIZE ) == 0 ) { isCKB2 = 1 ; len = 0 ; ret = ckb_load_cell_by_field ( NULL , & len , 0 , UDTSWAP_UDT_LOCK_CELL_INDEX_2 , CKB_SOURCE_OUTPUT , CKB_CELL_FIELD_TYPE_HASH ) ; if ( ret != ITEM_MISSING_ERROR ) { return SCRIPT_NOT_MATCH_ERROR ; } } else { ret = check_script_hash ( udt2_type_script_hash_buf , UDTSWAP_UDT_LOCK_CELL_INDEX_2 , CKB_SOURCE_OUTPUT , CKB_CELL_FIELD_TYPE_HASH ) ; if ( ret != CKB_SUCCESS ) { return ret ; } } uint8_t udtswap_data_buf [ UDTSWAP_DATA_SIZE ] ; len = UDTSWAP_DATA_SIZE ; ret = ckb_load_cell_data ( udtswap_data_buf , & len , 0 , UDTSWAP_TYPE_CELL_INDEX , CKB_SOURCE_OUTPUT ) ; if ( ret != CKB_SUCCESS ) { return UDTSWAP_SYSCALL_ERROR - ret ; } if ( len != UDTSWAP_DATA_SIZE ) { return UDTSWAP_DATA_SIZE_NOT_CORRECT_ERROR ; } uint128_t udtswap_udt1_reserve = get_uint128_t ( UDTSWAP_DATA_UDT1_RESERVE_START , udtswap_data_buf ) ; uint128_t udtswap_udt2_reserve = get_uint128_t ( UDTSWAP_DATA_UDT2_RESERVE_START , udtswap_data_buf ) ; uint128_t udtswap_total_liquidity = get_uint128_t ( UDTSWAP_DATA_TOTAL_LIQUIDITY_START , udtswap_data_buf ) ; uint64_t udt_amount_64 ; uint128_t udt1_amount = 0 ; uint8_t udt_data_buf [ UDT_AMOUNT_SIZE ] ; if ( isCKB1 ) { len = 8 ; ret = ckb_load_cell_by_field ( ( uint8_t * ) & udt_amount_64 , & len , 0 , UDTSWAP_UDT_LOCK_CELL_INDEX_1 , CKB_SOURCE_OUTPUT , CKB_CELL_FIELD_CAPACITY ) ; if ( ret != CKB_SUCCESS ) { return UDTSWAP_SYSCALL_ERROR - ret ; } udt1_amount = ( uint128_t ) udt_amount_64 ; } else { ret = ckb_load_cell_data ( udt_data_buf , & len , 0 , UDTSWAP_UDT_LOCK_CELL_INDEX_1 , CKB_SOURCE_OUTPUT ) ; if ( ret != CKB_SUCCESS ) { return UDTSWAP_SYSCALL_ERROR - ret ; } udt1_amount = get_uint128_t ( 0 , udt_data_buf ) ; } uint128_t udt2_amount = 0 ; if ( isCKB2 ) { len = 8 ; ret = ckb_load_cell_by_field ( ( uint8_t * ) & udt_amount_64 , & len , 0 , UDTSWAP_UDT_LOCK_CELL_INDEX_2 , CKB_SOURCE_OUTPUT , CKB_CELL_FIELD_CAPACITY ) ; if ( ret != CKB_SUCCESS ) { return UDTSWAP_SYSCALL_ERROR - ret ; } udt2_amount = ( uint128_t ) udt_amount_64 ; } else { ret = ckb_load_cell_data ( udt_data_buf , & len , 0 , UDTSWAP_UDT_LOCK_CELL_INDEX_2 , CKB_SOURCE_OUTPUT ) ; if ( ret != CKB_SUCCESS ) { return UDTSWAP_SYSCALL_ERROR - ret ; } udt2_amount = get_uint128_t ( 0 , udt_data_buf ) ; } if ( udtswap_udt1_reserve != udt1_amount || udtswap_udt2_reserve != udt2_amount || udt1_amount != ( isCKB1 ? CKB_RESERVE_DEFAULT : UDT_RESERVE_DEFAULT ) || udt2_amount != ( isCKB2 ? CKB_RESERVE_DEFAULT : UDT_RESERVE_DEFAULT ) || udtswap_total_liquidity != 0 ) { return RESULT_NOT_CORRECT_ERROR ; } return CKB_SUCCESS ; }
@dev check creating new pool
 check tx first input
 check type group
 check type script hash
 check lock code hash
 check first udt and second udt not same
 check lock script hash
 check first, second udt type hash
 check udt reserve, lock amount
 check empty udt reserve
 check empty total liquidity
@dev check creating new pool check tx first input check type group check type script hash check lock code hash check first udt and second udt not same check lock script hash check first second udt type hash check udt reserve lock amount check empty udt reserve check empty total liquidity
//udtswap group should be no input 1 output //udtswap type hash checked //udtswap lock code hash checked //not same udt, order checked //udtswap lock hash checked //udt type hash checked //udtswap udts reserve, udts amount same checked //ckb reserve default checked //udt reserve default checked //total liquidity default checked