Skip to content

Add missing convenience functions for RPC query request types #78

@petersalomonsen

Description

@petersalomonsen

Problem

The RpcQueryRequestSchema supports 8 different request types, but we only have convenience functions for 3 of them.

Current Status

Have convenience functions for:

  • view_accountviewAccount()
  • view_access_keyviewAccessKey()
  • call_functionviewFunction()

Missing convenience functions for:

  • view_code
  • view_state
  • view_access_key_list
  • view_global_contract_code
  • view_global_contract_code_by_account_id

Proposed Solution

Option 1: Manual Implementation

Add the missing convenience functions to /packages/jsonrpc-client/src/convenience.ts following the existing pattern.

Option 2: Auto-generation (Recommended)

Since all convenience functions follow the same pattern, they could be auto-generated:

  1. Parse RpcQueryRequestSchema to extract all request types
  2. For each request type, generate a convenience function that:
    • Takes appropriate parameters based on the schema
    • Constructs query params with blockId/finality logic
    • Calls the generic query() function
    • Returns the properly typed result

Benefits of auto-generation:

  • Ensures complete coverage of all request types
  • Maintains consistency across all convenience functions
  • Automatically stays in sync with OpenAPI spec updates
  • Reduces manual maintenance

Implementation Notes

The pattern for all convenience functions is:

export async function viewSomething(
  client: NearRpcClient,
  params: {
    // specific params for this request type
    finality?: 'final' | 'near-final' | 'optimistic';
    blockId?: string | number;
  }
): Promise<SpecificReturnType> {
  const queryParams = params.blockId
    ? { requestType: 'view_something', ...specificParams, blockId: params.blockId }
    : { requestType: 'view_something', ...specificParams, finality: params.finality || 'final' };
  
  return query(client, queryParams) as Promise<SpecificReturnType>;
}

The generator would need to:

  1. Map request types to function names (e.g., view_access_key_listviewAccessKeyList)
  2. Extract required parameters from the schema
  3. Map to appropriate return types from RpcQueryResponseSchema

Related Files

  • /packages/jsonrpc-client/src/convenience.ts - Current convenience functions
  • /packages/jsonrpc-types/src/schemas.ts - RpcQueryRequestSchema definition (line 2634)
  • /tools/codegen/generate.ts - Could be extended to generate convenience functions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions