Skip to content

Commit

Permalink
@mintbase-js/rpc: add rpcUrl mandatory param (#521)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Smith <bh2smith@users.noreply.github.com>
Co-authored-by: mintbaseteam <eng@mintbase.io>
Co-authored-by: Till <31419678+tifrel@users.noreply.github.com>
  • Loading branch information
4 people authored Aug 21, 2024
1 parent c514a1e commit f7538bb
Show file tree
Hide file tree
Showing 48 changed files with 2,845 additions and 5,732 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"packages": [
"packages/*"
]
}
}
7,954 changes: 2,592 additions & 5,362 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,10 @@
"eslint-plugin-simple-import-sort": "^5.0.3",
"fetch-mock": "^9.11.0",
"graphql": "^16.6.0",
"isomorphic-fetch": "^3.0.0",
"jest": "^29.1.1",
"jest-environment-jsdom": "^29.1.2",
"lerna": "^7.0.0",
"lerna": "^8.0.0",
"lerna-jest": "^0.5.4",
"serve": "^14.0.1",
"superagent": "^8.0.3",
"ts-jest": "^29.0.3",
"typescript": "^4.8.4",
"@near-wallet-selector/wallet-utils": "^8.9.7"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ export const tokenOwnersByMetadataIdMock = {
},
},
};

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { META_SERVICE_HOST_TESTNET } from '../../constants';
import { OWNED_MINTED_ORDER_BY, ParsedDataReturn, UserTokensFilter, UserTokensQueryResult } from '../../types';
import { OWNED_MINTED_ORDER_BY, ParsedDataReturn, UserTokensQueryResult } from '../../types';
import fetchMock from 'fetch-mock';
import { Network, mbjs } from '@mintbase-js/sdk';
import { getUserMintedTokens } from '../userMintedTokens/userMintedTokens';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { META_SERVICE_HOST_TESTNET } from '../../constants';
import { OWNED_MINTED_ORDER_BY, ParsedDataReturn, UserTokensFilter, UserTokensQueryResult } from '../../types';
import { OWNED_MINTED_ORDER_BY, ParsedDataReturn, UserTokensQueryResult } from '../../types';

import fetchMock from 'fetch-mock';
import { Network, mbjs } from '@mintbase-js/sdk';
Expand Down
3 changes: 2 additions & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"dependencies": {
"@mintbase-js/wallet": "0.6.1-update-packages-24058ee.0",
"@mintbase-js/data": "0.6.1-update-packages-24058ee.0",
"@near-wallet-selector/core": "8.9.12",
"@near-wallet-selector/here-wallet": "8.9.12",
"@near-wallet-selector/meteor-wallet": "8.9.12",
Expand All @@ -32,4 +33,4 @@
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}
}
2 changes: 1 addition & 1 deletion packages/react/src/MintbaseWalletContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
// import { invokeContractMethod } from './transactions';
import React from 'react';
import { render, screen, act, waitFor } from '@testing-library/react';
import { act, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { useMbWallet, MintbaseWalletContextProvider } from './MintbaseWalletContext';
import {
Expand Down
32 changes: 0 additions & 32 deletions packages/react/src/hooks/methods/nearPrice.ts

This file was deleted.

9 changes: 0 additions & 9 deletions packages/react/src/hooks/methods/nearPrice.types.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/react/src/hooks/useNearPrice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useState } from 'react';
import { nearPrice } from './methods/nearPrice';
import { nearPrice } from '@mintbase-js/data';

type UseNearPriceReturn = {
nearPrice: number;
Expand Down
17 changes: 8 additions & 9 deletions packages/rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,30 @@ 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 on each method, so you can pass your own RPC regarding one of those on the list:
https://docs.near.org/api/rpc/providers

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

### `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: 0 additions & 1 deletion packages/rpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"author": "",
"license": "MIT",
"dependencies": {
"@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
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';
4 changes: 2 additions & 2 deletions packages/rpc/src/methods/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('accountExists', () => {
});

expect(
await accountExists('benipsen.near'),
await accountExists({accountId: 'root.near', rpcUrl: 'rpc'}),
).toBe(true);
});

Expand All @@ -26,7 +26,7 @@ describe('accountExists', () => {
});

expect(
await accountExists('notreal.near'),
await accountExists({accountId: 'xyz&&&.near', rpcUrl: 'rpc'}),
).toBe(false);
});
});
17 changes: 8 additions & 9 deletions packages/rpc/src/methods/account.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { Network } from '@mintbase-js/sdk';
import { RPC_OPTIONS, requestFromNearRpc } from '../util';
import { AccountParams } from '../types';
import { callNearRpc } from '../util';

export const accountExists = async (accountId: string, network?: Network, rpc?: RPC_OPTIONS): Promise<boolean> => {
const response = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
method: 'query',
export const accountExists = async ({accountId, rpcUrl}: AccountParams): Promise<boolean> => {

const res = await callNearRpc({
params: {
request_type: 'view_account',
finality: 'final',
account_id: accountId,
},
}, network, rpc);
method: 'query',
rpcUrl})

if (response?.error) {
if (res?.error) {
return false;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc/src/methods/balance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('getBalance', () => {
}),
});

const b = await getBalance('foo.near');
const b = await getBalance({accountId:'foo.near', rpcUrl:'rpcUrl'});
expect(b instanceof BN).toBe(true);
expect(b.eq(new BN(123))).toBe(true);
});
Expand Down
26 changes: 14 additions & 12 deletions packages/rpc/src/methods/balance.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import BN from 'bn.js';
import { RPC_OPTIONS, requestFromNearRpc } from '../util';
import { Network } from '@mintbase-js/sdk';
import { callNearRpc, } from '../util';
import { AccountParams } from '../types';

export const getBalance = async (accountId: string, network?: Network, rpc?: RPC_OPTIONS): Promise<BN> => {
const res = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
method: 'query',
export const getBalance = async ({ accountId, rpcUrl }: AccountParams): Promise<BN> => {

const res = await callNearRpc({
params: {
request_type: 'view_account',
finality: 'final',
account_id: accountId,
},
}, network, rpc);
request_type: 'view_account',
finality: 'final',
account_id: accountId,
},
method:'query',
rpcUrl
})

const balanceString = res?.result?.amount as string;
if (!balanceString) {
throw new Error(`Malformed response: ${JSON.stringify(res)}`);
}
return new BN(balanceString);
};

2 changes: 1 addition & 1 deletion packages/rpc/src/methods/blockheight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('getBlockHeight', () => {
}),
});

const h = await getBlockHeight();
const h = await getBlockHeight('https://rpc.testnet.near.org');
expect(typeof h).toBe('number');
expect(h > 0).toBe(true);
});
Expand Down
15 changes: 7 additions & 8 deletions packages/rpc/src/methods/blockheight.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Network } from '@mintbase-js/sdk';
import { RPC_OPTIONS, requestFromNearRpc } from '../util';
import { callNearRpc } from '../util';

export const getBlockHeight = async (network?: Network, rpc?: RPC_OPTIONS): Promise<number> => {
const res = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
method: 'status',
export const getBlockHeight = async (rpcUrl: string): Promise<number> => {

const res = await callNearRpc({
params: [],
}, network, rpc);
method: 'status',
rpcUrl})

const blockHeight = (res?.result?.sync_info as {latest_block_height: number})?.latest_block_height;
if (!blockHeight) {
throw new Error(`Malformed response: ${JSON.stringify(res)}`);
Expand Down
16 changes: 4 additions & 12 deletions packages/rpc/src/methods/ftBalance.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { Network } from '@mintbase-js/sdk';
import { RPC_OPTIONS, callViewMethod } from '../util';
import { FTParams } from '../types';
import { callViewMethod } from '../util';

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

export const ftBalance = async ({ contractId, accountId, network, rpc }: FTBalanceProps): Promise<string> => {
export const ftBalance = async ({ contractId, accountId, rpcUrl }: FTParams): Promise<string> => {
return callViewMethod<string>({
contractId,
method: 'ft_balance_of',
args: { account_id: accountId },
network: network,
rpc,
rpcUrl,
});
};
11 changes: 4 additions & 7 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,7 @@ export type FtMetadata = {

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

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

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

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

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

export const ftStorageBalance = async ({ contractId, accountId, network, rpc }: FTStorageProps): Promise<string | null> => {
export const ftStorageBalance = async ({ contractId, accountId, rpcUrl }: FTParams): 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,
rpcUrl,
});

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

export const getBlockHash = async (network?: Network, rpc?: RPC_OPTIONS): Promise<string> => {
const res = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
export const getBlockHash = async (rpcUrl: string): Promise<string> => {
const res = await callNearRpc({
method: 'status',
params: [],
}, network, rpc);
rpcUrl
}, );

const blockHash = (res?.result?.sync_info as {latest_block_hash: string})?.latest_block_hash;

if (!blockHash) {
throw new Error(`Malformed response: ${JSON.stringify(res)}`);
}

return blockHash;
};
Loading

0 comments on commit f7538bb

Please sign in to comment.