Skip to content

Commit

Permalink
rpc fixes (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenmarcus committed Mar 14, 2024
1 parent d58aea9 commit 2eb6381
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 46 deletions.
11 changes: 10 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions packages/rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ This module provides a wrapper around common RPC calls used to fetch on-chain da

`type Network: 'testnet' | 'mainnet'`

### `getBalance(account: string, network?: Network): BN`
### `getBalance(account: string, network?: Network, rpc?: RPC_OPTIONS): BN`

Fetches the balance of a NEAR account (in yocto) by address.

### `getBlockHeight(network?: Network): number`
### `getBlockHeight(network?: Network, rpc?: RPC_OPTIONS): number`

Returns the current block height for the configured network.

### `getTxnStatus(txnHash: string, senderId: string,network?: Network): TxnStatus`
### `getTxnStatus(txnHash: string, senderId: string,network?: Network, rpc?: RPC_OPTIONS): TxnStatus`

For a transaction hash, determine the status of a transaction on the configured network: `pending`, `success`, or `failure`

### `payouts({ contractId, tokenId, network }): Promise<UiPayout>`
### `payouts({ contractId, tokenId, network, rpc }): Promise<UiPayout>`

Calls a token contract in order to determine the percentage amounts paid out to royalty accounts.

### `getAccessKeys(accountId: string, network?: Network): Promise<AccessKey>`
### `getAccessKeys(accountId: string, network?: Network, rpc?: RPC_OPTIONS): Promise<AccessKey>`

Gets all access keys (public key and permissions object) for a given account.

Expand Down
2 changes: 1 addition & 1 deletion packages/rpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"@mintbase-js/sdk": "^0.5.6-beta-prerelease.6",
"@mintbase-js/sdk": "0.5.6-rpc-fix-1aec566.0",
"@types/node": "18.11.9",
"bn.js": "^5.2.1",
"cross-fetch": "^4.0.0"
Expand Down
6 changes: 3 additions & 3 deletions packages/rpc/src/methods/account.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Network } from '@mintbase-js/sdk';
import { requestFromNearRpc } from '../util';
import { RPC_OPTIONS, requestFromNearRpc } from '../util';

export const accountExists = async (accountId: string, network?: Network): Promise<boolean> => {
export const accountExists = async (accountId: string, network?: Network, rpc?: RPC_OPTIONS): Promise<boolean> => {
const response = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
Expand All @@ -11,7 +11,7 @@ export const accountExists = async (accountId: string, network?: Network): Promi
finality: 'final',
account_id: accountId,
},
}, network);
}, network, rpc);

if (response?.error) {
return false;
Expand Down
6 changes: 3 additions & 3 deletions packages/rpc/src/methods/balance.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import BN from 'bn.js';
import { requestFromNearRpc } from '../util';
import { RPC_OPTIONS, requestFromNearRpc } from '../util';
import { Network } from '@mintbase-js/sdk';

export const getBalance = async (accountId: string, network?: Network): Promise<BN> => {
export const getBalance = async (accountId: string, network?: Network, rpc?: RPC_OPTIONS): Promise<BN> => {
const res = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
Expand All @@ -12,7 +12,7 @@ export const getBalance = async (accountId: string, network?: Network): Promise<
finality: 'final',
account_id: accountId,
},
}, network);
}, network, rpc);
const balanceString = res?.result?.amount;
if (!balanceString) {
throw new Error(`Malformed response: ${JSON.stringify(res)}`);
Expand Down
6 changes: 3 additions & 3 deletions packages/rpc/src/methods/blockheight.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Network } from '@mintbase-js/sdk';
import { requestFromNearRpc } from '../util';
import { RPC_OPTIONS, requestFromNearRpc } from '../util';

export const getBlockHeight = async (network?: Network): Promise<number> => {
export const getBlockHeight = async (network?: Network, rpc?: RPC_OPTIONS): Promise<number> => {
const res = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
method: 'status',
params: [],
}, network);
}, network, rpc);
const blockHeight = res?.result?.sync_info?.latest_block_height;
if (!blockHeight) {
throw new Error(`Malformed response: ${JSON.stringify(res)}`);
Expand Down
6 changes: 4 additions & 2 deletions packages/rpc/src/methods/ftBalance.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { Network } from '@mintbase-js/sdk';
import { callViewMethod } from '../util';
import { RPC_OPTIONS, callViewMethod } from '../util';

interface FTBalanceProps {
contractId: string;
accountId: string;
network?: Network;
rpc?: RPC_OPTIONS;
}

export const ftBalance = async ({ contractId, accountId, network }: FTBalanceProps): Promise<string> => {
export const ftBalance = async ({ contractId, accountId, network, rpc }: FTBalanceProps): Promise<string> => {
return callViewMethod<string>({
contractId,
method: 'ft_balance_of',
args: { account_id: accountId },
network: network,
rpc,
});
};
6 changes: 4 additions & 2 deletions packages/rpc/src/methods/ftMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Network } from '@mintbase-js/sdk';
import { callViewMethod } from '../util';
import { RPC_OPTIONS, callViewMethod } from '../util';

export type FtMetadata = {
spec: string;
Expand All @@ -15,6 +15,7 @@ export type FtMetadata = {
interface FtMetadataProps {
contractId: string;
network?: Network;
rpc?: RPC_OPTIONS;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -51,11 +52,12 @@ function isStringOrNull(x: any): x is string | null {
return false;
}

export const ftMetadata = async ({ contractId, network }: FtMetadataProps): Promise<FtMetadata | null> => {
export const ftMetadata = async ({ contractId, network, rpc }: FtMetadataProps): Promise<FtMetadata | null> => {
const res = callViewMethod<FtMetadata>({
contractId,
method: 'ft_metadata',
network: network,
rpc: rpc,
});

return isFtMetadata(res) ? res : null;
Expand Down
6 changes: 4 additions & 2 deletions packages/rpc/src/methods/ftStorageBalance.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { Network } from '@mintbase-js/sdk';
import { callViewMethod } from '../util';
import { RPC_OPTIONS, callViewMethod } from '../util';

interface FTStorageProps {
contractId: string;
accountId: string;
network?: Network;
rpc?: RPC_OPTIONS;
}

export const ftStorageBalance = async ({ contractId, accountId, network }: FTStorageProps): Promise<string | null> => {
export const ftStorageBalance = async ({ contractId, accountId, network, rpc }: FTStorageProps): Promise<string | null> => {
const balance = await callViewMethod<{ total: string; available: string } | null>({
contractId,
method: 'storage_balance_of',
args: { account_id: accountId },
network: network,
rpc: rpc,
});

return balance?.total || null;
Expand Down
6 changes: 3 additions & 3 deletions packages/rpc/src/methods/getBlockHash.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Network } from '@mintbase-js/sdk';
import { requestFromNearRpc } from '../util';
import { RPC_OPTIONS, requestFromNearRpc } from '../util';

export const getBlockHash = async (network?: Network): Promise<number> => {
export const getBlockHash = async (network?: Network, rpc?: RPC_OPTIONS): Promise<number> => {
const res = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
method: 'status',
params: [],
}, network);
}, network, rpc);
const blockHeight = res?.result?.sync_info?.latest_block_hash;
if (!blockHeight) {
throw new Error(`Malformed response: ${JSON.stringify(res)}`);
Expand Down
6 changes: 3 additions & 3 deletions packages/rpc/src/methods/getGasPrice.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Network } from '@mintbase-js/sdk';
import { requestFromNearRpc } from '../util';
import { RPC_OPTIONS, requestFromNearRpc } from '../util';

export const getGasPrice = async (hash?: string, network?: Network): Promise<number> => {
export const getGasPrice = async (hash?: string, network?: Network, rpc?: RPC_OPTIONS): Promise<number> => {
const res = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
method: 'gas_price',
params: [hash || null],
}, network);
}, network, rpc);
const gasPrice = res?.result?.gas_price;
if (!gasPrice) {
throw new Error(`Malformed response: ${JSON.stringify(res)}`);
Expand Down
6 changes: 3 additions & 3 deletions packages/rpc/src/methods/getLatestGasPrice.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Network } from '@mintbase-js/sdk';
import { requestFromNearRpc } from '../util';
import { RPC_OPTIONS, requestFromNearRpc } from '../util';

export const getLatestGasPrice = async (network?: Network): Promise<string> => {
export const getLatestGasPrice = async (network?: Network, rpc?: RPC_OPTIONS): Promise<string> => {
const res = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
method: 'gas_price',
params: [null],
}, network);
}, network, rpc);
const blockHeight = res?.result?.gas_price;
if (!blockHeight) {
throw new Error(`Malformed response: ${JSON.stringify(res)}`);
Expand Down
6 changes: 3 additions & 3 deletions packages/rpc/src/methods/keys.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Network } from '@mintbase-js/sdk';
import { requestFromNearRpc } from '../util';
import { RPC_OPTIONS, requestFromNearRpc } from '../util';

export type AccessKey = {
public_key: string;
Expand All @@ -16,7 +16,7 @@ export type AccessKeyPermissions = {
};
}

export const getAccessKeys = async (accountId: string, network?: Network): Promise<AccessKey[]> => {
export const getAccessKeys = async (accountId: string, network?: Network, rpc?: RPC_OPTIONS): Promise<AccessKey[]> => {
const res = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
Expand All @@ -26,7 +26,7 @@ export const getAccessKeys = async (accountId: string, network?: Network): Promi
finality: 'final',
account_id: accountId,
},
}, network);
}, network, rpc);

const accessKeys = res?.result?.keys;

Expand Down
6 changes: 4 additions & 2 deletions packages/rpc/src/methods/payouts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Network } from '@mintbase-js/sdk';
import { callViewMethod } from '../util';
import { RPC_OPTIONS, callViewMethod } from '../util';

type Numerator = {
numerator: number;
Expand Down Expand Up @@ -45,9 +45,10 @@ interface PayoutsProps {
contractId: string;
tokenId: string;
network?: Network;
rpc?: RPC_OPTIONS;
}

export const payouts = async ({ contractId, tokenId, network }: PayoutsProps): Promise<UiPayout> => {
export const payouts = async ({ contractId, tokenId, network, rpc }: PayoutsProps): Promise<UiPayout> => {
const payout = await callViewMethod<NepPayout>({
contractId,
method: 'nft_payout',
Expand All @@ -60,6 +61,7 @@ export const payouts = async ({ contractId, tokenId, network }: PayoutsProps): P
max_len_payout: 1000,
},
network,
rpc,
},
);

Expand Down
4 changes: 3 additions & 1 deletion packages/rpc/src/methods/social.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { callViewMethod } from '../util';
import { RPC_OPTIONS, callViewMethod } from '../util';
import { Network, mbjs } from '@mintbase-js/sdk';

export const NEAR_SOCIAL_IPFS_GATEWAY = 'https://ipfs.near.social/ipfs/';
Expand Down Expand Up @@ -46,6 +46,7 @@ const getImageUrl = (image: ProfileImage): string | null => {
export const nearSocialProfile = async (
accountId: string,
network?: Network,
rpc?: RPC_OPTIONS,
): Promise<NearSocialProfile> => {

const finalNetwork = network || mbjs.keys.network;
Expand All @@ -62,6 +63,7 @@ export const nearSocialProfile = async (
keys: [`${accountId}/profile/**`],
},
network,
rpc,
});


Expand Down
5 changes: 3 additions & 2 deletions packages/rpc/src/methods/txnstatus.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { Network } from '@mintbase-js/sdk';
import { requestFromNearRpc } from '../util';
import { RPC_OPTIONS, requestFromNearRpc } from '../util';

export type TxnStatus = 'pending' | 'success' | 'failure';

export const getTxnStatus = async (
txnHash: string,
senderId: string,
network?: Network,
rpc?: RPC_OPTIONS,
): Promise<TxnStatus> => {
const res = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
method: 'tx',
params: [txnHash, senderId],
}, network);
}, network, rpc);
if (res?.error) {
throw res.error;
}
Expand Down
15 changes: 11 additions & 4 deletions packages/rpc/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { mbjs, RPC_ENDPOINTS } from '@mintbase-js/sdk';
import { mbjs, RPC_ENDPOINTS, NEAR_RPC_ENDPOINTS } from '@mintbase-js/sdk';
import fetch from 'cross-fetch';


export type RPC_OPTIONS = 'lava' | 'near' | 'beta'


export const requestFromNearRpc = async (
body: Record<string, any>,
network?: string,
rpc?: RPC_OPTIONS,
): Promise<Record<string, any> | undefined> => {

const fetchUrl = RPC_ENDPOINTS[mbjs.keys.network] || mbjs.keys.nearRpcUrl;
const rpcAddress = network ? RPC_ENDPOINTS[network] : fetchUrl;
const fetchUrl = mbjs.keys.nearRpcUrl || RPC_ENDPOINTS[mbjs.keys.rpc][mbjs.keys.network] || NEAR_RPC_ENDPOINTS[mbjs.keys.network];
const rpcAddress = network && rpc ? RPC_ENDPOINTS[rpc][network] : fetchUrl;

const res = await fetch(rpcAddress, {
method: 'POST',
Expand All @@ -23,11 +28,13 @@ export const callViewMethod = async <T>({
method,
args,
network,
rpc,
}: {
contractId: string;
method: string;
args?: Record<string, any>;
network?: string;
rpc?: RPC_OPTIONS;
}): Promise<T> => {
const args_base64 = args
? Buffer.from(JSON.stringify(args), 'utf-8').toString('base64')
Expand All @@ -44,7 +51,7 @@ export const callViewMethod = async <T>({
method_name: method,
args_base64,
},
}, network);
}, network, rpc);

if (res?.error) {
throw res.error;
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Most @mintbase-js/data and @mintbase-js/sdk methods will accept network as an op
NEAR_NETWORK=testnet
CALLBACK_URL=https://mintbase.xyz/success
CONTRACT_ADDRESS=buddha.mintspace2.testnet
RPC=lava
```

These variables will only work server-side due to limitations of process.env on client-side, however depending on what bundler you are using you may be able to set env variables at build time.
Expand Down
Loading

0 comments on commit 2eb6381

Please sign in to comment.