Skip to content

Commit

Permalink
Merge pull request #2 from akaia-io/main
Browse files Browse the repository at this point in the history
feat: Custom RPC endpoint
  • Loading branch information
wpdas authored Apr 25, 2024
2 parents 0900a40 + a3dc10c commit 770fc01
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ It's super easy to get a Wallet and/or Contract API in place all at once. Take a
import naxios from '@wpdas/naxios'

const naxiosInstance = new naxios({
rpcNodeUrl: 'free.rpc.fastnear.com', // optional
contractId: CONTRACT_ID,
network: 'testnet',
})
Expand Down
6 changes: 4 additions & 2 deletions src/managers/contract-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import { pollingAsyncCall } from '..'
type ResultType = QueryResponseKind & { result: any }

class ContractManager {
private rpcNodeUrl?: ContractManagerConfig['rpcNodeUrl']
private walletManager: WalletManager
private cache?: MemoryCache | StorageCache
private contractId: string

constructor({ walletManager, cache, contractId }: ContractManagerConfig) {
constructor({ rpcNodeUrl, walletManager, cache, contractId }: ContractManagerConfig) {
this.rpcNodeUrl = rpcNodeUrl
this.walletManager = walletManager
this.cache = cache
this.contractId = contractId || walletManager.contractId
Expand Down Expand Up @@ -83,7 +85,7 @@ class ContractManager {
}

const { network } = this.walletManager.walletSelector.options
const provider = new providers.JsonRpcProvider({ url: network.nodeUrl })
const provider = new providers.JsonRpcProvider({ url: this.rpcNodeUrl ?? network.nodeUrl })

const res = (await provider.query({
request_type: 'call_function',
Expand Down
6 changes: 5 additions & 1 deletion src/managers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export type WalletManagerConfig = {
}

export type ContractManagerConfig = {
/**
* A custom RPC endpoint URl.
*/
rpcNodeUrl?: string
walletManager: WalletManager
contractId?: string
cache?: MemoryCache | StorageCache
Expand All @@ -62,7 +66,7 @@ export type BuildViewInterfaceConfig = {
}

// Naxios Constructor
export type NaxiosConstructor = {
export type NaxiosConstructor = Pick<ContractManagerConfig, 'rpcNodeUrl'> & {
contractId: string
network: Network
walletSelectorModules?: WalletModuleFactory[]
Expand Down
4 changes: 4 additions & 0 deletions src/naxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import WalletManager from './managers/wallet-manager'
import { NaxiosConstructor, Network, ContractApi } from './managers/types'

class naxios {
private rpcNodeUrl?: ContractManager['rpcNodeUrl']
private contractId: string
private network: Network
private walletSelectorModules: WalletModuleFactory[] = [setupMyNearWallet()]
private walletManager!: WalletManager

constructor(config: NaxiosConstructor) {
this.rpcNodeUrl = config.rpcNodeUrl
this.contractId = config.contractId
this.network = config.network

if (config.walletSelectorModules) {
this.walletSelectorModules = config.walletSelectorModules
}
Expand All @@ -39,6 +42,7 @@ class naxios {
*/
contractApi(config?: ContractApi) {
return new ContractManager({
rpcNodeUrl: this.rpcNodeUrl,
walletManager: this.walletManager,
contractId: config?.contractId,
cache: config?.cache,
Expand Down

0 comments on commit 770fc01

Please sign in to comment.