Skip to content

Commit

Permalink
Merge pull request #5 from wpdas/feat/rpc-api
Browse files Browse the repository at this point in the history
NEAR RPC Api Provider
  • Loading branch information
wpdas authored Aug 12, 2024
2 parents 6e272e3 + f37dacf commit 63823dd
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -242,6 +247,23 @@ const config: { useCache: true }
cachedGreetingContractApi.view<string>('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`
Expand Down Expand Up @@ -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.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -24,6 +24,7 @@
"naxios",
"contract",
"wallet",
"rpc",
"api",
"near",
"near-protocol",
Expand Down
20 changes: 20 additions & 0 deletions src/managers/rpc-provider-manager.ts
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions src/managers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions src/naxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { buildTransaction } from './buildTransaction'
export { calculateDepositByDataSize } from './calculateDepositByDataSize'
export { pollingAsyncCall } from './pollingAsyncCall'
export { validateNearAddress } from './validateNearAddress'
export { isClient } from './isClient'

0 comments on commit 63823dd

Please sign in to comment.