diff --git a/packages/rpc/README.md b/packages/rpc/README.md index a036d49c..6ff3102b 100644 --- a/packages/rpc/README.md +++ b/packages/rpc/README.md @@ -11,31 +11,31 @@ This module provides a wrapper around common RPC calls used to fetch on-chain da ## Available Modules & Methods -`type Network: 'testnet' | 'mainnet'` +We now accepts rpcUrl in each method, so you can pass your own RPC regarding one of those on the list: +`packages/rpc/src/util.ts#9` -### `getBalance(account: string, network?: Network, rpc?: RPC_OPTIONS): BN` +if **rpcUrl** not passed, it will fallback to near rpc mainnet. + +### `getBalance(account: string, rpcUrl?: string): BN` Fetches the balance of a NEAR account (in yocto) by address. -### `getBlockHeight(network?: Network, rpc?: RPC_OPTIONS): number` +### `getBlockHeight(rpcUrl?: string): number` Returns the current block height for the configured network. -### `getTxnStatus(txnHash: string, senderId: string,network?: Network, rpc?: RPC_OPTIONS): TxnStatus` +### `getTxnStatus(txnHash: string, senderId: string,rpcUrl?: string): TxnStatus` For a transaction hash, determine the status of a transaction on the configured network: `pending`, `success`, or `failure` -### `payouts({ contractId, tokenId, network, rpc }): Promise` +### `payouts({ contractId, tokenId, rpcUrl? }): Promise` Calls a token contract in order to determine the percentage amounts paid out to royalty accounts. -### `getAccessKeys(accountId: string, network?: Network, rpc?: RPC_OPTIONS): Promise` +### `getAccessKeys(accountId: string,rpcUrl?: string): Promise` Gets all access keys (public key and permissions object) for a given account. -## Configuration - -Before calling these methods the near network should be configured using the [config SDK method](https://docs.mintbase.io/dev/mintbase-sdk-ref/sdk/config) ## Future diff --git a/packages/rpc/src/index.ts b/packages/rpc/src/index.ts index 366d97bc..2f33ec9e 100644 --- a/packages/rpc/src/index.ts +++ b/packages/rpc/src/index.ts @@ -11,3 +11,4 @@ export * from './methods/keys'; export * from './methods/ftBalance'; export * from './methods/ftStorageBalance'; export * from './methods/ftMetadata'; +export * from './util'; \ No newline at end of file diff --git a/packages/rpc/src/methods/ftBalance.ts b/packages/rpc/src/methods/ftBalance.ts index dec90956..2d98895b 100644 --- a/packages/rpc/src/methods/ftBalance.ts +++ b/packages/rpc/src/methods/ftBalance.ts @@ -1,11 +1,8 @@ -import { Network } from '@mintbase-js/sdk'; -import { RPC_OPTIONS, callViewMethod } from '../util'; +import { callViewMethod } from '../util'; interface FTBalanceProps { contractId: string; accountId: string; - network?: Network; - rpc?: RPC_OPTIONS; rpcUrl?: string } diff --git a/packages/rpc/src/methods/ftMetadata.ts b/packages/rpc/src/methods/ftMetadata.ts index 51c95ead..ea395ece 100644 --- a/packages/rpc/src/methods/ftMetadata.ts +++ b/packages/rpc/src/methods/ftMetadata.ts @@ -1,5 +1,4 @@ -import { Network } from '@mintbase-js/sdk'; -import { RPC_OPTIONS, callViewMethod } from '../util'; +import { callViewMethod } from '../util'; export type FtMetadata = { spec: string; @@ -14,8 +13,6 @@ export type FtMetadata = { interface FtMetadataProps { contractId: string; - network?: Network; - rpc?: RPC_OPTIONS; rpcUrl?: string } @@ -53,7 +50,7 @@ function isStringOrNull(x: any): x is string | null { return false; } -export const ftMetadata = async ({ contractId, rpcUrl }: FtMetadataProps): Promise => { +export const ftMetadata = async ({ contractId, rpcUrl }: FtMetadataProps): Promise => { const res = callViewMethod({ contractId, method: 'ft_metadata', diff --git a/packages/rpc/src/methods/ftStorageBalance.ts b/packages/rpc/src/methods/ftStorageBalance.ts index 13603fc7..971f3495 100644 --- a/packages/rpc/src/methods/ftStorageBalance.ts +++ b/packages/rpc/src/methods/ftStorageBalance.ts @@ -1,11 +1,8 @@ -import { Network } from '@mintbase-js/sdk'; -import { RPC_OPTIONS, callViewMethod } from '../util'; +import { callViewMethod } from '../util'; interface FTStorageProps { contractId: string; accountId: string; - network?: Network; - rpc?: RPC_OPTIONS; rpcUrl?: string } diff --git a/packages/rpc/src/methods/getGasPrice.ts b/packages/rpc/src/methods/getGasPrice.ts index 1647ef46..10398c3a 100644 --- a/packages/rpc/src/methods/getGasPrice.ts +++ b/packages/rpc/src/methods/getGasPrice.ts @@ -1,6 +1,6 @@ import { requestFromNearRpc } from '../util'; -export const getGasPrice = async (hash?: string, rpcUrl?: string): Promise => { +export const getGasPrice = async (hash: string, rpcUrl?: string): Promise => { const res = await requestFromNearRpc({ jsonrpc: '2.0', id: 'dontcare', diff --git a/packages/rpc/src/methods/payouts.ts b/packages/rpc/src/methods/payouts.ts index 459447bb..1fd66234 100644 --- a/packages/rpc/src/methods/payouts.ts +++ b/packages/rpc/src/methods/payouts.ts @@ -1,5 +1,4 @@ -import { Network } from '@mintbase-js/sdk'; -import { RPC_OPTIONS, callViewMethod } from '../util'; +import { callViewMethod } from '../util'; type NepPayout = { payout: Record; @@ -33,8 +32,6 @@ const nepToUi = (nepPayout: NepPayout, tokenId: string): UiPayout => { interface PayoutsProps { contractId: string; tokenId: string; - network?: Network; - rpc?: RPC_OPTIONS; rpcUrl?: string } diff --git a/packages/rpc/src/util.ts b/packages/rpc/src/util.ts index 2d06daa3..e2827063 100644 --- a/packages/rpc/src/util.ts +++ b/packages/rpc/src/util.ts @@ -1,19 +1,56 @@ -import { mbjs, RPC_ENDPOINTS, NEAR_RPC_ENDPOINTS } from '@mintbase-js/sdk'; import fetch from 'cross-fetch'; +export type RPC_OPTIONS = 'lava' | 'near' | 'beta' | 'fastnear' | 'pagoda' -export type RPC_OPTIONS = 'lava' | 'near' | 'beta' | 'fastnear' +type RpcNodes = { + [key in RPC_OPTIONS]?: string; +}; + +export const rpcNodes: { + mainnet: RpcNodes; + testnet: RpcNodes; +} = { + mainnet: { + lava: "https://g.w.lavanet.xyz:443/gateway/near/rpc-http/f538cb3b0a85aafdb9996886d004ee0a", + near: "https://rpc.mainnet.near.org", + fastnear: "https://free.rpc.fastnear.com/", + pagoda: "https://rpc.mainnet.pagoda.co" + }, + testnet: { + lava: "https://g.w.lavanet.xyz:443/gateway/neart/rpc-http/f538cb3b0a85aafdb9996886d004ee0a", + near: "https://rpc.testnet.near.org", + pagoda: "https://rpc.testnet.pagoda.co", + fastnear: + "https://test.rpc.fastnear.com", + }, +}; export const requestFromNearRpc = async ( body: Record, rpcUrl?: string): Promise<{ result: Record, error: unknown } | undefined> => { - const res = await fetch(rpcUrl, { - method: 'POST', - body: JSON.stringify(body), - headers: { 'Content-type': 'application/json' }, - }); - return res.json(); + const validUrls = Object.values(rpcNodes.mainnet).concat(Object.values(rpcNodes.testnet)); + + // Set default rpcUrl if not provided + if (!rpcUrl) { + rpcUrl = rpcNodes.mainnet.near; + } + + if (rpcUrl && !validUrls.includes(rpcUrl)) { + throw new Error('Invalid rpcUrl'); + } + + try { + const res = await fetch(rpcUrl, { + method: 'POST', + body: JSON.stringify(body), + headers: { 'Content-type': 'application/json' }, + }); + + return res.json(); + } catch (error) { + return { result: {}, error }; + } }; @@ -48,8 +85,8 @@ export const callViewMethod = async ({ if (res?.error) { throw res.error; } - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - const parsed = JSON.parse(Buffer.from(res?.result?.result).toString()); + + const resultBuffer = Buffer.from(res?.result?.result as string); + const parsed = JSON.parse(resultBuffer.toString()); return parsed as T; };