Skip to content

Commit

Permalink
re-arrange request types
Browse files Browse the repository at this point in the history
  • Loading branch information
m-aboelenein committed Feb 26, 2024
1 parent aab4c9e commit fe5ebb8
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/request/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getProviderById } from '../provider';
import { Request } from './types/requests';
import { Request } from './types';

export const request: Request = async (method, params, providerId?: string) => {
let provider = window.XverseProviders?.BitcoinProvider;
Expand Down
4 changes: 2 additions & 2 deletions src/request/types/btc.ts → src/request/types/btcMethods.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, AddressPurpose } from 'src/addresses';
import { MethodParamsAndResult } from './rpc';
import { Address, AddressPurpose } from '../../addresses';
import { MethodParamsAndResult } from '../../types';

type GetInfoResult = {
version: Number | String;
Expand Down
37 changes: 34 additions & 3 deletions src/request/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
export * from './requests';
export * from './rpc';
export * from './stx';
import { GetAddresses, GetInfo, SendTransfer, SignMessage, SignPsbt } from './btcMethods';
import {
ContractCall,
ContractDeploy,
SignStructuredMessage,
SignStxMessage,
SignTransaction,
TransferStx,
} from './stxMethods';

export interface Requests {
getInfo: GetInfo;
getAddresses: GetAddresses;
signMessage: SignMessage;
sendTransfer: SendTransfer;
signPsbt: SignPsbt;
stx_contractCall: ContractCall;
stx_transferStx: TransferStx;
stx_signMessage: SignStxMessage;
stx_signStructuredMessage: SignStructuredMessage;
stx_contractDeploy: ContractDeploy;
stx_signTransaction: SignTransaction;
}

export type Return<Method> = Method extends keyof Requests ? Requests[Method]['result'] : unknown;
export type Params<Method> = Method extends keyof Requests ? Requests[Method]['params'] : unknown;

export type Request = <Method extends keyof Requests>(
requestMethod: Method,
params: Params<Method>
) => Promise<Return<Method>>;

export * from './stxMethods';
export * from './btcMethods';
31 changes: 0 additions & 31 deletions src/request/types/requests.ts

This file was deleted.

34 changes: 0 additions & 34 deletions src/request/types/rpc.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MethodParamsAndResult } from './rpc';
import { MethodParamsAndResult } from '../../types';

interface FunctionArgs {
/**
Expand Down
37 changes: 37 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,40 @@ export interface RequestOptions<Payload extends RequestPayload, Response> {
payload: Payload;
getProvider?: () => Promise<BitcoinProvider | undefined>;
}

// RPC Types

export type RpcId = string | number | null | undefined;

export interface RpcBase {
jsonrpc: '2.0';
id: RpcId;
}
export interface RpcRequest<T extends string, U> extends RpcBase {
method: T;
params: U;
}

export interface MethodParamsAndResult<TParams, TResult> {
params: TParams;
result: TResult;
}

export enum RpcErrorCode {
PARSE_ERROR = -32700, // Parse error Invalid JSON
INVALID_REQUEST = -32600, // The JSON sent is not a valid Request object.
METHOD_NOT_FOUND = -32601, // The method does not exist/is not available.
INVALID_PARAMS = -32602, // Invalid method parameter(s)
INTERNAL_ERROR = -32603, // Internal JSON-RPC error
USER_REJECTION = -32000, // user rejected/canceled the request
METHOD_NOT_SUPPORTED = -32001, // method is not supported for the address provided
}

export interface RpcError extends RpcBase {
code: number | RpcErrorCode;
message: string;
}

export interface RpcErrorResponse<TError extends RpcError = RpcError> {
error: TError;
}

0 comments on commit fe5ebb8

Please sign in to comment.