Skip to content

Commit

Permalink
Add rpc env logic to enable rpc selector (#490)
Browse files Browse the repository at this point in the history
Co-authored-by: Ruben Marcus <[email protected]>
  • Loading branch information
sainthiago and rubenmarcus authored Mar 14, 2024
1 parent fd25f81 commit e3c4169
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/sdk/src/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ This method will work on both server side and client side.
const config = {
network: 'testnet',
callbackUrl: 'https://mintbase.xyz/success',
contractAddress: 'buddha.mintspace2.testnet'
contractAddress: 'buddha.mintspace2.testnet',
rpc: 'near'
}

mbjs.config(config)
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/config/config.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GRAPHQL_ENDPOINTS, MARKET_CONTRACT_ADDRESS, MINTBASE_CONTRACTS, MINTBASE_CONTRACTS_V2, NEAR_NETWORKS, RPC_ENDPOINTS, USDC_ADDRESS, USDT_ADDRESS } from '../types';
import { GRAPHQL_ENDPOINTS, MARKET_CONTRACT_ADDRESS, MINTBASE_CONTRACTS, MINTBASE_CONTRACTS_V2, NEAR_NETWORKS, NEAR_RPC_ENDPOINTS, USDC_ADDRESS, USDT_ADDRESS } from '../types';

export const TESTNET_MOCK = {
apiKey: process.env.MINTBASE_API_KEY,
Expand All @@ -10,7 +10,7 @@ export const TESTNET_MOCK = {
marketAddress: MARKET_CONTRACT_ADDRESS.testnet,
mbContract: MINTBASE_CONTRACTS.testnet,
mbContractV2: MINTBASE_CONTRACTS_V2.testnet,
nearRpcUrl: RPC_ENDPOINTS.testnet,
nearRpcUrl: NEAR_RPC_ENDPOINTS.testnet,
network: NEAR_NETWORKS.TESTNET,
connectProxyAddress: null,
ftAddresses: { usdc: USDC_ADDRESS.testnet, usdt: USDT_ADDRESS.testnet },
Expand All @@ -28,7 +28,7 @@ export const MAINNET_MOCK = {
marketAddress: MARKET_CONTRACT_ADDRESS.mainnet,
mbContract: MINTBASE_CONTRACTS.mainnet,
mbContractV2: MINTBASE_CONTRACTS_V2.mainnet,
nearRpcUrl: RPC_ENDPOINTS.mainnet,
nearRpcUrl: NEAR_RPC_ENDPOINTS.mainnet,
network: NEAR_NETWORKS.MAINNET,
connectProxyAddress: null,
ftAddresses: { usdc: USDC_ADDRESS.mainnet, usdt: USDT_ADDRESS.mainnet },
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/src/config/config.server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('test mbjs namespace', () => {

it('should set configuration with process.env TESTNET', () => {
process.env.NEAR_NETWORK = NEAR_NETWORKS.TESTNET;
process.env.RPC = 'near';
process.env.CONTRACT_ADDRESS = TESTNET_MOCK.contractAddress;
process.env.CALLBACK_URL = TESTNET_MOCK.callbackUrl;

Expand All @@ -22,6 +23,7 @@ describe('test mbjs namespace', () => {

it('should set configuration with process.env MAINNET', () => {
process.env.NEAR_NETWORK = NEAR_NETWORKS.MAINNET;
process.env.RPC = 'near';
process.env.CONTRACT_ADDRESS = MAINNET_MOCK.contractAddress;
process.env.CALLBACK_URL = MAINNET_MOCK.callbackUrl;

Expand Down
14 changes: 10 additions & 4 deletions packages/sdk/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* mbjs Namespace to set the config vars on all mintbase-js packages, as also retrieve the global keys in any part of the application.
*/
import { FT_ADDRESSES } from '../constants';
import { FT_ADDRESSES, RPC_ENDPOINTS } from '../constants';
import {
MbJsKeysObject,
MARKET_CONTRACT_ADDRESS,
Expand All @@ -11,26 +11,30 @@ import {
NEAR_NETWORKS,
ConfigOptions,
GRAPHQL_ENDPOINTS,
RPC_ENDPOINTS,
NEAR_RPC_ENDPOINTS,
} from '../types';

// to create a new key you have to specify here on the configuration and MbJsKeysObject + add on the setGlobalEnv

export const isProcessEnv = Boolean(typeof window == 'undefined' && process?.env?.NEAR_NETWORK);
export const hasRpcEnv = Boolean(typeof window == 'undefined' && process?.env?.RPC);
export const isDebugMode = Boolean(typeof window == 'undefined' && process?.env?.NEAR_NETWORK === NEAR_NETWORKS.TESTNET);
export const hasContractAddress = Boolean(typeof window == 'undefined' && process?.env?.CONTRACT_ADDRESS);
export const hasCallbackUrl = Boolean(typeof window == 'undefined' && process?.env?.CALLBACK_URL);

const RPC_noRPCenv = isProcessEnv ? NEAR_RPC_ENDPOINTS[process.env.NEAR_NETWORK] : NEAR_RPC_ENDPOINTS[NEAR_NETWORKS.MAINNET];

export const DEFAULT_API_KEY = 'anon';


const defaultContractAddress = isProcessEnv ? MINTBASE_CONTRACTS[process.env.NEAR_NETWORK] : MINTBASE_CONTRACTS[NEAR_NETWORKS.MAINNET];

// set startup defaults
// if users set vars on process.env it will come by default setting up the config on the server.
const startupConfig: MbJsKeysObject = {
network: isProcessEnv ? process.env.NEAR_NETWORK : NEAR_NETWORKS.MAINNET,
graphqlUrl: isProcessEnv ? GRAPHQL_ENDPOINTS[process.env.NEAR_NETWORK] : GRAPHQL_ENDPOINTS[NEAR_NETWORKS.MAINNET],
nearRpcUrl: isProcessEnv ? RPC_ENDPOINTS[process.env.NEAR_NETWORK] : RPC_ENDPOINTS[NEAR_NETWORKS.MAINNET],
nearRpcUrl: isProcessEnv && hasRpcEnv ? RPC_ENDPOINTS[process.env.RPC][process.env.NEAR_NETWORK] : RPC_noRPCenv,
callbackUrl: hasCallbackUrl ? process.env.CALLBACK_URL : '',
// will check for process.env.CONTRACT_ADDRESS if not will setup mintbase contract according to the network, if not will setup testnet contract
contractAddress: hasContractAddress ? process.env.CONTRACT_ADDRESS : defaultContractAddress,
Expand All @@ -54,7 +58,8 @@ export const setGlobalEnv = (configObj: ConfigOptions): MbJsKeysObject => {
const globalConfig: MbJsKeysObject = {
network: configObj.network as Network,
graphqlUrl: GRAPHQL_ENDPOINTS[configObj.network],
nearRpcUrl: RPC_ENDPOINTS[configObj.network],
rpc: configObj.rpc,
nearRpcUrl: configObj.rpc ? RPC_ENDPOINTS[configObj.rpc][configObj.network]: NEAR_RPC_ENDPOINTS[configObj.network],
callbackUrl: configObj.callbackUrl,
contractAddress: configObj.contractAddress ?? MINTBASE_CONTRACTS[configObj.network],
marketAddress: MARKET_CONTRACT_ADDRESS[configObj.network],
Expand All @@ -69,6 +74,7 @@ export const setGlobalEnv = (configObj: ConfigOptions): MbJsKeysObject => {
};

config.network = globalConfig.network;
config.rpc = globalConfig.rpc;
config.graphqlUrl = globalConfig.graphqlUrl;
config.callbackUrl = globalConfig.callbackUrl;
config.contractAddress = globalConfig.contractAddress;
Expand Down
8 changes: 7 additions & 1 deletion packages/sdk/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BN from 'bn.js';
import { FtAddresses, USDC_ADDRESS, USDT_ADDRESS } from './types';
import { FtAddresses, LAVA_RPC_ENDPOINTS, NEAR_RPC_ENDPOINTS, USDC_ADDRESS, USDT_ADDRESS } from './types';

export const GAS = '200000000000000';
export const MAX_GAS = '300000000000000';
Expand Down Expand Up @@ -54,3 +54,9 @@ export const FT_ADDRESSES: { testnet: FtAddresses; mainnet: FtAddresses } = {
usdt: USDT_ADDRESS.mainnet,
},
};


export const RPC_ENDPOINTS = {
lava: LAVA_RPC_ENDPOINTS,
near: NEAR_RPC_ENDPOINTS,
};
10 changes: 8 additions & 2 deletions packages/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,16 @@ export enum GRAPHQL_ENDPOINTS {
testnet = 'https://interop-testnet.hasura.app/v1/graphql',
}

export enum RPC_ENDPOINTS {
export enum LAVA_RPC_ENDPOINTS {
mainnet = 'https://g.w.lavanet.xyz:443/gateway/near/rpc-http/f538cb3b0a85aafdb9996886d004ee0a',
testnet = 'https://g.w.lavanet.xyz:443/gateway/neart/rpc-http/f538cb3b0a85aafdb9996886d004ee0a',
}

export enum NEAR_RPC_ENDPOINTS {
mainnet = 'https://rpc.mainnet.near.org',
testnet = 'https://rpc.testnet.near.org',
}

export enum USDC_ADDRESS {
mainnet = 'a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.factory.bridge.near',
testnet = 'usdc.fakes.testnet',
Expand Down Expand Up @@ -99,14 +104,15 @@ export type ConfigOptions = {
callbackUrl?: string;
apiKey?: string;
connectProxyAddress?: string;
rpc?: 'lava'| 'near';
}

export interface ConfigOptionsObj extends ConfigOptions {
graphqlUrl?: GRAPHQL_ENDPOINTS | '';
marketAddress?: MARKET_CONTRACT_ADDRESS | '';
mbContract?: MINTBASE_CONTRACTS;
mbContractV2?: MINTBASE_CONTRACTS_V2;
nearRpcUrl: RPC_ENDPOINTS | '';
nearRpcUrl: NEAR_RPC_ENDPOINTS | LAVA_RPC_ENDPOINTS | '';
debugMode?: boolean;
apiKey?: string;
connectProxyAddress?: string;
Expand Down

0 comments on commit e3c4169

Please sign in to comment.