From 2eb6381f637df58a3d92655147b86021f23cc176 Mon Sep 17 00:00:00 2001 From: Ruben Marcus Date: Thu, 14 Mar 2024 11:32:47 -0300 Subject: [PATCH] rpc fixes (#491) --- package-lock.json | 11 ++++++++++- packages/rpc/README.md | 10 +++++----- packages/rpc/package.json | 2 +- packages/rpc/src/methods/account.ts | 6 +++--- packages/rpc/src/methods/balance.ts | 6 +++--- packages/rpc/src/methods/blockheight.ts | 6 +++--- packages/rpc/src/methods/ftBalance.ts | 6 ++++-- packages/rpc/src/methods/ftMetadata.ts | 6 ++++-- packages/rpc/src/methods/ftStorageBalance.ts | 6 ++++-- packages/rpc/src/methods/getBlockHash.ts | 6 +++--- packages/rpc/src/methods/getGasPrice.ts | 6 +++--- packages/rpc/src/methods/getLatestGasPrice.ts | 6 +++--- packages/rpc/src/methods/keys.ts | 6 +++--- packages/rpc/src/methods/payouts.ts | 6 ++++-- packages/rpc/src/methods/social.ts | 4 +++- packages/rpc/src/methods/txnstatus.ts | 5 +++-- packages/rpc/src/util.ts | 15 +++++++++++---- packages/sdk/src/config/README.md | 1 + packages/sdk/src/constants.ts | 1 - packages/sdk/src/types.ts | 11 +++++++++-- 20 files changed, 80 insertions(+), 46 deletions(-) diff --git a/package-lock.json b/package-lock.json index c5f40dd5..28538193 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27115,7 +27115,7 @@ "version": "0.5.6-beta-prerelease.6", "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" @@ -27124,6 +27124,15 @@ "@types/bn.js": "^5.1.5" } }, + "packages/rpc/node_modules/@mintbase-js/sdk": { + "version": "0.5.6-rpc-fix-1aec566.0", + "resolved": "https://registry.npmjs.org/@mintbase-js/sdk/-/sdk-0.5.6-rpc-fix-1aec566.0.tgz", + "integrity": "sha512-emp3LcFB1wjl5wcNERNfPgfaZGumKlu/mRnzAc8DigXWqW7QoIDzq3M2CvZBO1xklUPU22qGQ89ByvDxmI6kPw==", + "dependencies": { + "bn.js": "5.2.1", + "near-api-js": "^2.1.4" + } + }, "packages/rpc/node_modules/cross-fetch": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-4.0.0.tgz", diff --git a/packages/rpc/README.md b/packages/rpc/README.md index f254779a..a036d49c 100644 --- a/packages/rpc/README.md +++ b/packages/rpc/README.md @@ -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` +### `payouts({ contractId, tokenId, network, rpc }): Promise` Calls a token contract in order to determine the percentage amounts paid out to royalty accounts. -### `getAccessKeys(accountId: string, network?: Network): Promise` +### `getAccessKeys(accountId: string, network?: Network, rpc?: RPC_OPTIONS): Promise` Gets all access keys (public key and permissions object) for a given account. diff --git a/packages/rpc/package.json b/packages/rpc/package.json index a3abc4e6..8eddf316 100644 --- a/packages/rpc/package.json +++ b/packages/rpc/package.json @@ -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" diff --git a/packages/rpc/src/methods/account.ts b/packages/rpc/src/methods/account.ts index 55740f99..2842acbf 100644 --- a/packages/rpc/src/methods/account.ts +++ b/packages/rpc/src/methods/account.ts @@ -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 => { +export const accountExists = async (accountId: string, network?: Network, rpc?: RPC_OPTIONS): Promise => { const response = await requestFromNearRpc({ jsonrpc: '2.0', id: 'dontcare', @@ -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; diff --git a/packages/rpc/src/methods/balance.ts b/packages/rpc/src/methods/balance.ts index ea9420b3..c8f73017 100644 --- a/packages/rpc/src/methods/balance.ts +++ b/packages/rpc/src/methods/balance.ts @@ -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 => { +export const getBalance = async (accountId: string, network?: Network, rpc?: RPC_OPTIONS): Promise => { const res = await requestFromNearRpc({ jsonrpc: '2.0', id: 'dontcare', @@ -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)}`); diff --git a/packages/rpc/src/methods/blockheight.ts b/packages/rpc/src/methods/blockheight.ts index cc7286f7..7e53d7c7 100644 --- a/packages/rpc/src/methods/blockheight.ts +++ b/packages/rpc/src/methods/blockheight.ts @@ -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 => { +export const getBlockHeight = async (network?: Network, rpc?: RPC_OPTIONS): Promise => { 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)}`); diff --git a/packages/rpc/src/methods/ftBalance.ts b/packages/rpc/src/methods/ftBalance.ts index 8d91d741..766097e6 100644 --- a/packages/rpc/src/methods/ftBalance.ts +++ b/packages/rpc/src/methods/ftBalance.ts @@ -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 => { +export const ftBalance = async ({ contractId, accountId, network, rpc }: FTBalanceProps): Promise => { return callViewMethod({ contractId, method: 'ft_balance_of', args: { account_id: accountId }, network: network, + rpc, }); }; diff --git a/packages/rpc/src/methods/ftMetadata.ts b/packages/rpc/src/methods/ftMetadata.ts index 35b6525e..43104f14 100644 --- a/packages/rpc/src/methods/ftMetadata.ts +++ b/packages/rpc/src/methods/ftMetadata.ts @@ -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; @@ -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 @@ -51,11 +52,12 @@ function isStringOrNull(x: any): x is string | null { return false; } -export const ftMetadata = async ({ contractId, network }: FtMetadataProps): Promise => { +export const ftMetadata = async ({ contractId, network, rpc }: FtMetadataProps): Promise => { const res = callViewMethod({ contractId, method: 'ft_metadata', network: network, + rpc: rpc, }); return isFtMetadata(res) ? res : null; diff --git a/packages/rpc/src/methods/ftStorageBalance.ts b/packages/rpc/src/methods/ftStorageBalance.ts index 5da49375..1f789638 100644 --- a/packages/rpc/src/methods/ftStorageBalance.ts +++ b/packages/rpc/src/methods/ftStorageBalance.ts @@ -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 => { +export const ftStorageBalance = async ({ contractId, accountId, network, rpc }: FTStorageProps): Promise => { 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; diff --git a/packages/rpc/src/methods/getBlockHash.ts b/packages/rpc/src/methods/getBlockHash.ts index 5bd94c2d..3a73f953 100644 --- a/packages/rpc/src/methods/getBlockHash.ts +++ b/packages/rpc/src/methods/getBlockHash.ts @@ -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 => { +export const getBlockHash = async (network?: Network, rpc?: RPC_OPTIONS): Promise => { 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)}`); diff --git a/packages/rpc/src/methods/getGasPrice.ts b/packages/rpc/src/methods/getGasPrice.ts index 3299b216..89f5c158 100644 --- a/packages/rpc/src/methods/getGasPrice.ts +++ b/packages/rpc/src/methods/getGasPrice.ts @@ -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 => { +export const getGasPrice = async (hash?: string, network?: Network, rpc?: RPC_OPTIONS): Promise => { 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)}`); diff --git a/packages/rpc/src/methods/getLatestGasPrice.ts b/packages/rpc/src/methods/getLatestGasPrice.ts index fdd3f3c0..09912e25 100644 --- a/packages/rpc/src/methods/getLatestGasPrice.ts +++ b/packages/rpc/src/methods/getLatestGasPrice.ts @@ -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 => { +export const getLatestGasPrice = async (network?: Network, rpc?: RPC_OPTIONS): Promise => { 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)}`); diff --git a/packages/rpc/src/methods/keys.ts b/packages/rpc/src/methods/keys.ts index 69459922..09fd16ef 100644 --- a/packages/rpc/src/methods/keys.ts +++ b/packages/rpc/src/methods/keys.ts @@ -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; @@ -16,7 +16,7 @@ export type AccessKeyPermissions = { }; } -export const getAccessKeys = async (accountId: string, network?: Network): Promise => { +export const getAccessKeys = async (accountId: string, network?: Network, rpc?: RPC_OPTIONS): Promise => { const res = await requestFromNearRpc({ jsonrpc: '2.0', id: 'dontcare', @@ -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; diff --git a/packages/rpc/src/methods/payouts.ts b/packages/rpc/src/methods/payouts.ts index 711b8601..66c2856e 100644 --- a/packages/rpc/src/methods/payouts.ts +++ b/packages/rpc/src/methods/payouts.ts @@ -1,5 +1,5 @@ import { Network } from '@mintbase-js/sdk'; -import { callViewMethod } from '../util'; +import { RPC_OPTIONS, callViewMethod } from '../util'; type Numerator = { numerator: number; @@ -45,9 +45,10 @@ interface PayoutsProps { contractId: string; tokenId: string; network?: Network; + rpc?: RPC_OPTIONS; } -export const payouts = async ({ contractId, tokenId, network }: PayoutsProps): Promise => { +export const payouts = async ({ contractId, tokenId, network, rpc }: PayoutsProps): Promise => { const payout = await callViewMethod({ contractId, method: 'nft_payout', @@ -60,6 +61,7 @@ export const payouts = async ({ contractId, tokenId, network }: PayoutsProps): P max_len_payout: 1000, }, network, + rpc, }, ); diff --git a/packages/rpc/src/methods/social.ts b/packages/rpc/src/methods/social.ts index d007ebb4..253c8344 100644 --- a/packages/rpc/src/methods/social.ts +++ b/packages/rpc/src/methods/social.ts @@ -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/'; @@ -46,6 +46,7 @@ const getImageUrl = (image: ProfileImage): string | null => { export const nearSocialProfile = async ( accountId: string, network?: Network, + rpc?: RPC_OPTIONS, ): Promise => { const finalNetwork = network || mbjs.keys.network; @@ -62,6 +63,7 @@ export const nearSocialProfile = async ( keys: [`${accountId}/profile/**`], }, network, + rpc, }); diff --git a/packages/rpc/src/methods/txnstatus.ts b/packages/rpc/src/methods/txnstatus.ts index 0579850a..0214d2c0 100644 --- a/packages/rpc/src/methods/txnstatus.ts +++ b/packages/rpc/src/methods/txnstatus.ts @@ -1,5 +1,5 @@ import { Network } from '@mintbase-js/sdk'; -import { requestFromNearRpc } from '../util'; +import { RPC_OPTIONS, requestFromNearRpc } from '../util'; export type TxnStatus = 'pending' | 'success' | 'failure'; @@ -7,13 +7,14 @@ export const getTxnStatus = async ( txnHash: string, senderId: string, network?: Network, + rpc?: RPC_OPTIONS, ): Promise => { const res = await requestFromNearRpc({ jsonrpc: '2.0', id: 'dontcare', method: 'tx', params: [txnHash, senderId], - }, network); + }, network, rpc); if (res?.error) { throw res.error; } diff --git a/packages/rpc/src/util.ts b/packages/rpc/src/util.ts index 910d2259..2ec5b9a6 100644 --- a/packages/rpc/src/util.ts +++ b/packages/rpc/src/util.ts @@ -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, network?: string, + rpc?: RPC_OPTIONS, ): Promise | 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', @@ -23,11 +28,13 @@ export const callViewMethod = async ({ method, args, network, + rpc, }: { contractId: string; method: string; args?: Record; network?: string; + rpc?: RPC_OPTIONS; }): Promise => { const args_base64 = args ? Buffer.from(JSON.stringify(args), 'utf-8').toString('base64') @@ -44,7 +51,7 @@ export const callViewMethod = async ({ method_name: method, args_base64, }, - }, network); + }, network, rpc); if (res?.error) { throw res.error; diff --git a/packages/sdk/src/config/README.md b/packages/sdk/src/config/README.md index 8dda956e..bb593dcc 100644 --- a/packages/sdk/src/config/README.md +++ b/packages/sdk/src/config/README.md @@ -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. diff --git a/packages/sdk/src/constants.ts b/packages/sdk/src/constants.ts index 5632c7cb..510a4246 100644 --- a/packages/sdk/src/constants.ts +++ b/packages/sdk/src/constants.ts @@ -55,7 +55,6 @@ export const FT_ADDRESSES: { testnet: FtAddresses; mainnet: FtAddresses } = { }, }; - export const RPC_ENDPOINTS = { lava: LAVA_RPC_ENDPOINTS, near: NEAR_RPC_ENDPOINTS, diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 82838efe..bbc841c3 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -77,6 +77,11 @@ export enum NEAR_RPC_ENDPOINTS { testnet = 'https://rpc.testnet.near.org', } +export enum NEAR_BETA_RPC_ENDPOINTS { + mainnet = 'https://beta.rpc.mainnet.near.org', + testnet = 'https://beta.rpc.testnet.near.org', +} + export enum USDC_ADDRESS { mainnet = 'a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.factory.bridge.near', testnet = 'usdc.fakes.testnet', @@ -98,13 +103,15 @@ export type FtAddresses = { usdt: USDT_ADDRESS; } +export type RPC_OPTIONS = 'lava' | 'near' | 'beta' + export type ConfigOptions = { network?: Network | string; contractAddress?: string; callbackUrl?: string; apiKey?: string; connectProxyAddress?: string; - rpc?: 'lava'| 'near'; + rpc?: RPC_OPTIONS; } export interface ConfigOptionsObj extends ConfigOptions { @@ -112,7 +119,7 @@ export interface ConfigOptionsObj extends ConfigOptions { marketAddress?: MARKET_CONTRACT_ADDRESS | ''; mbContract?: MINTBASE_CONTRACTS; mbContractV2?: MINTBASE_CONTRACTS_V2; - nearRpcUrl: NEAR_RPC_ENDPOINTS | LAVA_RPC_ENDPOINTS | ''; + nearRpcUrl: NEAR_RPC_ENDPOINTS | LAVA_RPC_ENDPOINTS | NEAR_BETA_RPC_ENDPOINTS | ''; debugMode?: boolean; apiKey?: string; connectProxyAddress?: string;