// This file was generated by `yarn gen:client`. Please, do not modify it.
import { RequestManager, Client } from "@open-rpc/client-js";
import {
  ExecuteTransactionResponseView,
  FunctionCallView,
  AnnotatedFunctionResultView,
  AccessPath,
  AnnotatedStateView,
  RoochAccountAddress,
  RoochStructTag,
  PageView_for_Nullable_BalanceInfoView_and_alloc_vec_Vec_U8Array,
  u64,
  AnnotatedEventResultPageView,
  StateView,
  Hex,
  TransactionView,
  TransactionResultPageView,
  PageView_for_Nullable_AnnotatedStateView_and_alloc_vec_Vec_U8Array,
  StateResultPageView,
} from "./types";

export class JsonRpcClient extends Client {
  constructor(requestManager: RequestManager) {
    super(requestManager);
  }

  async getRpcApiVersion(): Promise<string | undefined> {
    const resp = await this.request({
      method: "rpc.discover",
      params: [],
    });

    return resp.info.version as string;
  }

  // Send the signed transaction in bcs hex format This method blocks waiting for the transaction to be executed.
  async rooch_executeRawTransaction(
    tx_bcs_hex: Uint8Array
  ): Promise<ExecuteTransactionResponseView> {
    const tx_bcs_hex_hex = `0x${Buffer.from(tx_bcs_hex).toString("hex")}`;

    const resp = await this.request({
      method: "rooch_executeRawTransaction",
      params: [tx_bcs_hex_hex],
    });

    return resp as ExecuteTransactionResponseView;
  }

  // Execute a read-only function call The function do not change the state of Application
  async rooch_executeViewFunction(
    function_call: FunctionCallView
  ): Promise<AnnotatedFunctionResultView> {
    const resp = await this.request({
      method: "rooch_executeViewFunction",
      params: [function_call],
    });

    return resp as AnnotatedFunctionResultView;
  }

  // Get the annotated states by access_path The annotated states include the decoded move value of the state
  async rooch_getAnnotatedStates(
    access_path: AccessPath
  ): Promise<AnnotatedStateView | null[]> {
    const resp = await this.request({
      method: "rooch_getAnnotatedStates",
      params: [access_path],
    });

    return resp as AnnotatedStateView | null[];
  }

  // get account balances by AccountAddress
  async rooch_getBalances(
    account_addr: RoochAccountAddress,
    coin_type: RoochStructTag,
    cursor: Uint8Array,
    limit: number
  ): Promise<PageView_for_Nullable_BalanceInfoView_and_alloc_vec_Vec_U8Array> {
    const cursor_hex = `0x${Buffer.from(cursor).toString("hex")}`;

    const resp = await this.request({
      method: "rooch_getBalances",
      params: [account_addr, coin_type, cursor_hex, limit],
    });

    return resp as PageView_for_Nullable_BalanceInfoView_and_alloc_vec_Vec_U8Array;
  }

  async rooch_getChainID(): Promise<u64> {
    const resp = await this.request({
      method: "rooch_getChainID",
      params: [],
    });

    return resp as u64;
  }

  // Get the events by event handle id
  async rooch_getEventsByEventHandle(
    event_handle_type: RoochStructTag,
    cursor: number,
    limit: number
  ): Promise<AnnotatedEventResultPageView> {
    const resp = await this.request({
      method: "rooch_getEventsByEventHandle",
      params: [event_handle_type, cursor, limit],
    });

    return resp as AnnotatedEventResultPageView;
  }

  // Get the states by access_path
  async rooch_getStates(access_path: AccessPath): Promise<StateView | null[]> {
    const resp = await this.request({
      method: "rooch_getStates",
      params: [access_path],
    });

    return resp as StateView | null[];
  }

  async rooch_getTransactionByHash(hash: Hex): Promise<TransactionView> {
    const resp = await this.request({
      method: "rooch_getTransactionByHash",
      params: [hash],
    });

    return resp as TransactionView;
  }

  async rooch_getTransactionsByHash(
    tx_hashes: Hex[]
  ): Promise<TransactionView | null[]> {
    const resp = await this.request({
      method: "rooch_getTransactionsByHash",
      params: [tx_hashes],
    });

    return resp as TransactionView | null[];
  }

  async rooch_getTransactionsByOrder(
    cursor: number,
    limit: number
  ): Promise<TransactionResultPageView> {
    const resp = await this.request({
      method: "rooch_getTransactionsByOrder",
      params: [cursor, limit],
    });

    return resp as TransactionResultPageView;
  }

  // List the annotated states by access_path The annotated states include the decoded move value of the state
  async rooch_listAnnotatedStates(
    access_path: AccessPath,
    cursor: Uint8Array,
    limit: number
  ): Promise<PageView_for_Nullable_AnnotatedStateView_and_alloc_vec_Vec_U8Array> {
    const cursor_hex = `0x${Buffer.from(cursor).toString("hex")}`;

    const resp = await this.request({
      method: "rooch_listAnnotatedStates",
      params: [access_path, cursor_hex, limit],
    });

    return resp as PageView_for_Nullable_AnnotatedStateView_and_alloc_vec_Vec_U8Array;
  }

  // List the states by access_path
  async rooch_listStates(
    access_path: AccessPath,
    cursor: Uint8Array,
    limit: number
  ): Promise<StateResultPageView> {
    const cursor_hex = `0x${Buffer.from(cursor).toString("hex")}`;

    const resp = await this.request({
      method: "rooch_listStates",
      params: [access_path, cursor_hex, limit],
    });

    return resp as StateResultPageView;
  }

  // Send the signed transaction in bcs hex format This method does not block waiting for the transaction to be executed.
  async rooch_sendRawTransaction(tx_bcs_hex: Uint8Array): Promise<Hex> {
    const tx_bcs_hex_hex = `0x${Buffer.from(tx_bcs_hex).toString("hex")}`;

    const resp = await this.request({
      method: "rooch_sendRawTransaction",
      params: [tx_bcs_hex_hex],
    });

    return resp as Hex;
  }
}

// This file was generated by `yarn gen:client`. Please, do not modify it.