Skip to content

Commit

Permalink
rpc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenmarcus committed Aug 20, 2024
1 parent cbd48f8 commit a130c4c
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 38 deletions.
18 changes: 9 additions & 9 deletions packages/rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<UiPayout>`
### `payouts({ contractId, tokenId, rpcUrl? }): Promise<UiPayout>`

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<AccessKey>`
### `getAccessKeys(accountId: string,rpcUrl?: string): Promise<AccessKey>`

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

Expand Down
1 change: 1 addition & 0 deletions packages/rpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './methods/keys';
export * from './methods/ftBalance';
export * from './methods/ftStorageBalance';
export * from './methods/ftMetadata';
export * from './util';
5 changes: 1 addition & 4 deletions packages/rpc/src/methods/ftBalance.ts
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down
7 changes: 2 additions & 5 deletions packages/rpc/src/methods/ftMetadata.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,8 +13,6 @@ export type FtMetadata = {

interface FtMetadataProps {
contractId: string;
network?: Network;
rpc?: RPC_OPTIONS;
rpcUrl?: string
}

Expand Down Expand Up @@ -53,7 +50,7 @@ function isStringOrNull(x: any): x is string | null {
return false;
}

export const ftMetadata = async ({ contractId, rpcUrl }: FtMetadataProps): Promise<FtMetadata | null> => {
export const ftMetadata = async ({ contractId, rpcUrl }: FtMetadataProps): Promise<FtMetadata | null> => {
const res = callViewMethod<FtMetadata>({
contractId,
method: 'ft_metadata',
Expand Down
5 changes: 1 addition & 4 deletions packages/rpc/src/methods/ftStorageBalance.ts
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion packages/rpc/src/methods/getGasPrice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { requestFromNearRpc } from '../util';

export const getGasPrice = async (hash?: string, rpcUrl?: string): Promise<number> => {
export const getGasPrice = async (hash: string, rpcUrl?: string): Promise<number> => {
const res = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
Expand Down
5 changes: 1 addition & 4 deletions packages/rpc/src/methods/payouts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Network } from '@mintbase-js/sdk';
import { RPC_OPTIONS, callViewMethod } from '../util';
import { callViewMethod } from '../util';

type NepPayout = {
payout: Record<string, string>;
Expand Down Expand Up @@ -33,8 +32,6 @@ const nepToUi = (nepPayout: NepPayout, tokenId: string): UiPayout => {
interface PayoutsProps {
contractId: string;
tokenId: string;
network?: Network;
rpc?: RPC_OPTIONS;
rpcUrl?: string
}

Expand Down
59 changes: 48 additions & 11 deletions packages/rpc/src/util.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>,
rpcUrl?: string): Promise<{ result: Record<string, unknown>, 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 };
}
};


Expand Down Expand Up @@ -48,8 +85,8 @@ export const callViewMethod = async <T>({
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;
};

0 comments on commit a130c4c

Please sign in to comment.