diff --git a/README.md b/README.md index ccb5c78..fc8a9bc 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,11 @@ export const socialDBcontractApi = naxiosInstance.contractApi({ contractId: 'v1. export const greetingContractApi = naxiosInstance.contractApi({ contractId: 'dev-1692221685438-15421910364142', }) + +/** + * NEAR RPC API + */ +export const rpcApi = naxiosInstance.rpcApi() ``` #### Opening the Sign-in Wallet Selector Modal @@ -242,6 +247,23 @@ const config: { useCache: true } cachedGreetingContractApi.view('get_greeting', args, config).then((response) => console.log(response)) ``` +### NEAR RPC API + +Naxios also provides access to the NEAR RPC API, so that you can query any data you want. Visit [**NEAR RPC API Docs**](https://docs.near.org/api/rpc/introduction) to get to know how to use it. + +```ts +import { rpcApi } from './web3Api' + +// Viewing account using Near RPC API +rpcApi + .query({ + request_type: 'view_account', + finality: 'final', + account_id: 'wendersonpires.near', + }) + .then((data) => console.log('Account Data:', data)) +``` + ## Utils #### `buildTransaction` @@ -274,6 +296,20 @@ const myData = { age: 22, name: 'user name' } console.log(calculateDepositByDataSize(myData)) // 0.00087 Near (not yocto NEAR) ``` +#### `isClient` + +Simple checker to say if this is running on server or client. + +```ts +import { isClient } from '@wpdas/naxios' + +if (isClient()) { + console.log('Hi from client') +} else { + console.log('Hi from server') +} +``` + ## Contributing Feel free to open issues or pull requests. For major changes, please open an issue first to discuss what you would like to change. diff --git a/package.json b/package.json index d0fe6fc..11c4ee0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@wpdas/naxios", - "version": "2.1.1", + "version": "2.2.0", "description": "Promise based NEAR Contract and NEAR Wallet client for browser", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", @@ -24,6 +24,7 @@ "naxios", "contract", "wallet", + "rpc", "api", "near", "near-protocol", diff --git a/src/managers/rpc-provider-manager.ts b/src/managers/rpc-provider-manager.ts new file mode 100644 index 0000000..90d05e2 --- /dev/null +++ b/src/managers/rpc-provider-manager.ts @@ -0,0 +1,20 @@ +import { providers } from 'near-api-js' +import { RPCProviderManagerConfig } from './types' + +const NETWORK_ENDPOINTS = { + mainnet: 'https://rpc.mainnet.near.org', + testnet: 'https://rpc.testnet.near.org', + localnet: 'http://localhost:3030', +} + +class RPCProviderManager { + private rpcNodeUrl?: RPCProviderManagerConfig['rpcNodeUrl'] + public provider: providers.JsonRpcProvider + + constructor({ rpcNodeUrl, network }: RPCProviderManagerConfig) { + this.rpcNodeUrl = rpcNodeUrl + this.provider = new providers.JsonRpcProvider({ url: this.rpcNodeUrl ?? NETWORK_ENDPOINTS[network] }) + } +} + +export default RPCProviderManager diff --git a/src/managers/types.ts b/src/managers/types.ts index 0e75d90..eec8b1d 100644 --- a/src/managers/types.ts +++ b/src/managers/types.ts @@ -42,6 +42,14 @@ export type WalletManagerConfig = { onInit?: () => void } +export type RPCProviderManagerConfig = { + /** + * A custom RPC endpoint URl. + */ + rpcNodeUrl?: string + network: Network +} + export type ContractManagerConfig = { /** * A custom RPC endpoint URl. diff --git a/src/naxios.ts b/src/naxios.ts index 45d91cc..166a4fd 100644 --- a/src/naxios.ts +++ b/src/naxios.ts @@ -4,6 +4,7 @@ import ContractManager from './managers/contract-manager' import WalletManager from './managers/wallet-manager' import { NaxiosConstructor, Network, ContractApi } from './managers/types' import { isClient } from './utils/isClient' +import RPCProviderManager from './managers/rpc-provider-manager' class naxios { private rpcNodeUrl?: ContractManager['rpcNodeUrl'] @@ -54,6 +55,15 @@ class naxios { cache: config?.cache, }) } + + /** + * NEAR RPC API - Provider + * https://docs.near.org/api/rpc/introduction + * @returns + */ + rpcApi() { + return new RPCProviderManager({ rpcNodeUrl: this.rpcNodeUrl, network: this.network }).provider + } } /** Naxios API */ diff --git a/src/utils/index.ts b/src/utils/index.ts index f921c04..35adc5a 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -2,3 +2,4 @@ export { buildTransaction } from './buildTransaction' export { calculateDepositByDataSize } from './calculateDepositByDataSize' export { pollingAsyncCall } from './pollingAsyncCall' export { validateNearAddress } from './validateNearAddress' +export { isClient } from './isClient'