Skip to content

Commit

Permalink
feat: provider options and missing types
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-haynes committed Sep 16, 2024
1 parent 843de04 commit 53c2d27
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 20 deletions.
8 changes: 8 additions & 0 deletions packages/client/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ export const PAGODA_RPC_ENDPOINTS_MAINNET = [
'https://rpc.mainnet.pagoda.co',
];

export const PAGODA_RPC_ARCHIVAL_ENDPOINTS_MAINNET = [
'https://archival-rpc.near.org',
];

export const PAGODA_RPC_ENDPOINTS_TESTNET = [
'https://rpc.testnet.near.org',
'https://rpc.testnet.pagoda.co',
];

export const PAGODA_RPC_ARCHIVAL_ENDPOINTS_TESTNET = [
'https://archival-rpc.testnet.near.org',
];

export const KITWALLET_FUNDED_TESTNET_ACCOUNT_ENDPOINT = 'https://testnet-api.kitwallet.app/account';
15 changes: 2 additions & 13 deletions packages/client/src/interfaces/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import type {
BlockReference,
BlockResult,
FinalExecutionOutcome,
QueryResponseKind,
} from '@near-js/types';
import type { SignedTransaction } from '@near-js/transactions';
import type { PublicKey } from '@near-js/crypto';

import type { RpcQueryProvider } from './providers';

export interface Dependent<T> {
deps: T;
}

export interface RpcQueryProvider {
block(block: BlockReference): Promise<BlockResult>;
query<T extends QueryResponseKind>(...args: any[]): Promise<T>;
sendTransaction(transaction: SignedTransaction): Promise<FinalExecutionOutcome>;
}

export interface MessageSigner {
getPublicKey(): Promise<PublicKey>;
signMessage(m: Uint8Array): Promise<Uint8Array>;
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './dependencies';
export * from './providers';
export * from './transactions';
export * from './view';
25 changes: 25 additions & 0 deletions packages/client/src/interfaces/providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type {
BlockReference,
BlockResult,
ChunkId,
ChunkResult,
FinalExecutionOutcome,
QueryResponseKind,
TxExecutionStatus,
} from '@near-js/types';
import type { SignedTransaction } from '@near-js/transactions';

interface GetTransactionParams {
transactionHash: string;
account: string;
includeReceipts?: boolean;
waitUntil?: TxExecutionStatus;
}

export interface RpcQueryProvider {
block(block: BlockReference): Promise<BlockResult>;
chunk(chunkId: ChunkId): Promise<ChunkResult>;
getTransaction(params: GetTransactionParams): Promise<FinalExecutionOutcome>;
sendTransaction(transaction: SignedTransaction): Promise<FinalExecutionOutcome>;
query<T extends QueryResponseKind>(...args: any[]): Promise<T>;
}
35 changes: 29 additions & 6 deletions packages/client/src/providers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { FailoverRpcProvider, JsonRpcProvider } from '@near-js/providers';
import type { Finality } from '@near-js/types';

import type { ViewBaseParams } from './interfaces';
import { PAGODA_RPC_ENDPOINTS_MAINNET, PAGODA_RPC_ENDPOINTS_TESTNET } from './constants';
import type { RpcQueryProvider, ViewBaseParams } from './interfaces';
import {
PAGODA_RPC_ARCHIVAL_ENDPOINTS_TESTNET,
PAGODA_RPC_ENDPOINTS_MAINNET,
PAGODA_RPC_ENDPOINTS_TESTNET,
} from './constants';

interface DefaultFinality {
defaultFinality?: Finality;
Expand Down Expand Up @@ -44,28 +48,40 @@ export function getEndpointsByNetwork(network: string) {
* Initialize a failover RPC provider capable of retrying requests against a set of endpoints
* @param urls RPC endpoint URLs
*/
export function getFailoverRpcProvider(urls: string[]) {
export function createRpcClientWrapper(urls: string[]): RpcQueryProvider {
if (!urls) {
throw new Error('at least one RPC endpoint URL required');
}

return new FailoverRpcProvider(urls.map((url) => new JsonRpcProvider({ url })));
const provider = new FailoverRpcProvider(urls.map((url) => new JsonRpcProvider({ url })));
return {
block: (block) => provider.block(block),
chunk: (chunkId) => provider.chunk(chunkId),
getTransaction: ({ transactionHash, account, includeReceipts, waitUntil}) => {
if (includeReceipts) {
return provider.txStatusReceipts(transactionHash, account, waitUntil);
}
return provider.txStatus(transactionHash, account, waitUntil);
},
sendTransaction: (transaction) => provider.sendTransaction(transaction),
query: (params) => provider.query(params),
};
}

/**
* Initialize a failover RPC provider for the given network
* @param network target blockchain network (e.g. `mainnet`)
*/
export function getProviderByNetwork(network: string) {
return getFailoverRpcProvider(getEndpointsByNetwork(network));
return createRpcClientWrapper(getEndpointsByNetwork(network));
}

/**
* Initialize a failover RPC provider for a set of RPC endpoint URLs
* @param urls RPC endpoint URLs
*/
export function getProviderByEndpoints(...urls: string[]) {
return getFailoverRpcProvider(urls);
return createRpcClientWrapper(urls);
}

/**
Expand All @@ -75,6 +91,13 @@ export function getTestnetRpcProvider() {
return getProviderByNetwork('testnet');
}

/**
* Initialize a testnet archival RPC provider
*/
export function getTestnetRpcArchivalProvider() {
return createRpcClientWrapper(PAGODA_RPC_ARCHIVAL_ENDPOINTS_TESTNET);
}

/**
* Initialize a mainnet RPC provider
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/providers/src/failover-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export class FailoverRpcProvider extends Provider {
const result = await getResult(this.currentProvider);

if (result) return result;
} catch {
} catch (e) {
console.error(e);
this.switchToNextProvider();
}
}
Expand Down
22 changes: 22 additions & 0 deletions packages/types/src/provider/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ export interface ExecutionOutcome {
status: ExecutionStatus | ExecutionStatusBasic;
}

export type ReceiptAction =
{ Transfer: { deposit: string }};

export interface ExecutionOutcomeReceiptDetail {
predecessor_id: string;
receipt: {
Action: ExecutionOutcomeReceiptAction
};
receipt_id: string;
receiver_id: string;
}

export interface ExecutionOutcomeReceiptAction {
actions: ReceiptAction[];
gas_price: string;
input_data_ids: string[];
output_data_receivers: string[];
signer_id: string;
signer_public_key: string;
}

export interface ExecutionOutcomeWithIdView {
proof: MerklePath;
block_hash: string;
Expand All @@ -59,6 +80,7 @@ export interface FinalExecutionOutcome {
transaction: any;
transaction_outcome: ExecutionOutcomeWithId;
receipts_outcome: ExecutionOutcomeWithId[];
receipts?: ExecutionOutcomeReceiptDetail[];
}

export interface QueryResponseKind {
Expand Down

0 comments on commit 53c2d27

Please sign in to comment.