Skip to content

Commit

Permalink
feature: allow setNetworkConfig to accept useSpecificApiEndpoint so h…
Browse files Browse the repository at this point in the history
…ost can change rpc, remove the global variable check for rpc
  • Loading branch information
newbreedofgeek committed Nov 11, 2024
1 parent b214ba7 commit 63e731d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 44 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@itheum/sdk-mx-data-nft",
"version": "3.8.0-alpha.3",
"version": "3.8.0-alpha.4",
"description": "SDK for Itheum's Data NFT Technology on MultiversX Blockchain",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down
2 changes: 1 addition & 1 deletion src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ export function getDataFromClientSessionCache(cacheKey: string) {
return false;
} else {
// did it expire? is so, delete it from the cache
if (cacheObject.addedOn - Date.now() > cacheObject.expireAfter) {
if (Date.now() - cacheObject.addedOn > cacheObject.expireAfter) {
console.log('getDataFromClientSessionCache: expired');
delete sessionCache[cacheKey]; // remove it from cache as its expired
return false;
Expand Down
43 changes: 6 additions & 37 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
/*
Host apps like the data dex or explorer, when the MVX dappProvider in initialized (very early in the page)
it calls a utility method that loads the best RPC to use in the ITH_GLOBAL_MVX_RPC_API_SESSION global variable
so we aim to use that if available so this SDK uses the same RPC as the host to talk to the MVX chain
*/
declare const window: {
ITH_GLOBAL_MVX_RPC_API_SESSION: string;
} & Window;

export enum EnvironmentsEnum {
devnet = 'devnet',
testnet = 'testnet',
Expand All @@ -21,28 +12,17 @@ export interface Config {
// note that in all rpc check methods below we check if window === 'undefined' as this is need for tests to pass
const devnetNetworkConfig: Config = {
chainID: 'D',
networkProvider:
typeof window === 'undefined'
? 'https://devnet-api.multiversx.com'
: window.ITH_GLOBAL_MVX_RPC_API_SESSION ||
'https://devnet-api.multiversx.com'
networkProvider: 'https://devnet-api.multiversx.com'
};

const mainnetNetworkConfig: Config = {
chainID: '1',
networkProvider:
typeof window === 'undefined'
? 'https://api.multiversx.com'
: window.ITH_GLOBAL_MVX_RPC_API_SESSION || 'https://api.multiversx.com'
networkProvider: 'https://api.multiversx.com'
};

const testnetNetworkConfig: Config = {
chainID: 'T',
networkProvider:
typeof window === 'undefined'
? 'https://testnet-api.multiversx.com'
: window.ITH_GLOBAL_MVX_RPC_API_SESSION ||
'https://testnet-api.multiversx.com'
networkProvider: 'https://testnet-api.multiversx.com'
};

export const itheumTokenIdentifier: { [key in EnvironmentsEnum]: string } = {
Expand Down Expand Up @@ -85,20 +65,9 @@ export const livelinessStakeContractAddress: {
};

export const apiConfiguration: { [key in EnvironmentsEnum]: string } = {
devnet:
typeof window === 'undefined'
? 'https://devnet-api.multiversx.com'
: window.ITH_GLOBAL_MVX_RPC_API_SESSION ||
'https://devnet-api.multiversx.com',
mainnet:
typeof window === 'undefined'
? 'https://api.multiversx.com'
: window.ITH_GLOBAL_MVX_RPC_API_SESSION || 'https://api.multiversx.com',
testnet:
typeof window === 'undefined'
? 'https://testnet-api.multiversx.com'
: window.ITH_GLOBAL_MVX_RPC_API_SESSION ||
'https://testnet-api.multiversx.com'
devnet: 'https://devnet-api.multiversx.com',
mainnet: 'https://api.multiversx.com',
testnet: 'https://testnet-api.multiversx.com'
};

export const networkConfiguration: { [key in EnvironmentsEnum]: Config } = {
Expand Down
11 changes: 10 additions & 1 deletion src/datanft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ export class DataNft implements DataNftType {
/**
* Sets the network configuration for the DataNft class.
* @param env 'devnet' | 'mainnet' | 'testnet'
* @param useSpecificApiEndpoint optional param to use a specific RPC API endpoint for the env, if not given, defaults to config default value. Value need to start with https://
*/
static setNetworkConfig(env: string) {
static setNetworkConfig(env: string, useSpecificApiEndpoint?: string) {
if (!(env in EnvironmentsEnum)) {
throw new ErrNetworkConfig(
`Invalid environment: ${env}, Expected: 'devnet' | 'mainnet' | 'testnet'`
Expand All @@ -108,6 +109,14 @@ export class DataNft implements DataNftType {
this.env = env;
this.networkConfiguration = networkConfiguration[env as EnvironmentsEnum];
this.apiConfiguration = apiConfiguration[env as EnvironmentsEnum];

if (
useSpecificApiEndpoint &&
useSpecificApiEndpoint.trim().includes('https://')
) {
this.apiConfiguration = useSpecificApiEndpoint.trim();
this.networkConfiguration.networkProvider = useSpecificApiEndpoint.trim();
}
}

/**
Expand Down
4 changes: 0 additions & 4 deletions tests/environment.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
declare const window: {
ITH_GLOBAL_MVX_RPC_API_SESSION: string;
} & Window;

import { ApiNetworkProvider } from '@multiversx/sdk-core/out';
import { DataNftMarket, SftMinter } from '../src/index';

Expand Down

0 comments on commit 63e731d

Please sign in to comment.