diff --git a/layer/Service.ts b/layer/Service.ts index 0730c03a..0469c63c 100644 --- a/layer/Service.ts +++ b/layer/Service.ts @@ -17,7 +17,9 @@ import { IndexerGrpcExplorerApi, IndexerRestSpotChronosApi, IndexerGrpcDerivativesApi, - IndexerGrpcAccountPortfolioApi + IndexerGrpcAccountPortfolioApi, + IndexerRestDerivativesChronosApi, + ChainGrpcStakingApi } from '@injectivelabs/sdk-ts' import { TokenMetaUtilsFactory } from '@injectivelabs/token-metadata' import { MsgBroadcaster, Web3Broadcaster } from '@injectivelabs/wallet-ts' @@ -36,6 +38,7 @@ import { // Services export const bankApi = new ChainGrpcBankApi(ENDPOINTS.grpc) +export const stakingApi = new ChainGrpcStakingApi(ENDPOINTS.grpc) export const wasmApi = new ChainGrpcWasmApi(ENDPOINTS.grpc) export const exchangeApi = new ChainGrpcExchangeApi(ENDPOINTS.grpc) @@ -52,6 +55,10 @@ export const indexerExplorerApi = new IndexerGrpcExplorerApi(ENDPOINTS.indexer) export const indexerRestExplorerApi = new IndexerRestExplorerApi( `${ENDPOINTS.indexer}/api/explorer/v1` ) +export const indexerRestDerivativeChronosApi = + new IndexerRestDerivativesChronosApi( + `${ENDPOINTS.chronos}/api/chronos/v1/derivative` + ) export const indexerRestSpotChronosApi = new IndexerRestSpotChronosApi( `${ENDPOINTS.chronos}/api/chronos/v1/spot` ) diff --git a/layer/plugins/tracking.ts b/layer/plugins/tracking.ts index 3c42099a..6ce99356 100644 --- a/layer/plugins/tracking.ts +++ b/layer/plugins/tracking.ts @@ -1,18 +1,9 @@ import VueGtag from 'vue-gtag' import hotjar from 'vue-hotjar' import { defineNuxtPlugin } from '#imports' -import { init } from '@amplitude/analytics-browser' -import { - HOTJAR_KEY, - AMPLITUDE_KEY, - GOOGLE_ANALYTICS_KEY -} from './../utils/constant' +import { HOTJAR_KEY, GOOGLE_ANALYTICS_KEY } from './../utils/constant' export default defineNuxtPlugin((nuxtApp) => { - if (AMPLITUDE_KEY) { - init(AMPLITUDE_KEY) - } - if (GOOGLE_ANALYTICS_KEY) { nuxtApp.vueApp.use(VueGtag as any, { config: { diff --git a/layer/providers/cacheApi/base.ts b/layer/providers/cacheApi/base.ts index 2384f542..73b5838e 100644 --- a/layer/providers/cacheApi/base.ts +++ b/layer/providers/cacheApi/base.ts @@ -1,4 +1,6 @@ import axios, { AxiosInstance } from 'axios' +import { Pagination, TotalSupply } from '@injectivelabs/sdk-ts' +import { bankApi } from '@/Service' export class BaseCacheApi { client: AxiosInstance diff --git a/layer/providers/cacheApi/derivative.ts b/layer/providers/cacheApi/derivative.ts index bde31443..6291275f 100644 --- a/layer/providers/cacheApi/derivative.ts +++ b/layer/providers/cacheApi/derivative.ts @@ -1,23 +1,45 @@ import { DerivativeMarket, - AllChronosDerivativeMarketSummary + AllChronosDerivativeMarketSummary, + PerpetualMarket, + ExpiryFuturesMarket } from '@injectivelabs/sdk-ts' import { BaseCacheApi } from './base' +import { + indexerDerivativesApi, + indexerRestDerivativeChronosApi +} from '@/Service' export class DerivativeCacheApi extends BaseCacheApi { async fetchMarkets(props?: { marketStatus?: string }) { - const response = await this.client.get( - '/derivatives/markets', - { params: { marketStatus: props?.marketStatus } } - ) - return response.data + try { + const response = await this.client.get( + '/derivatives/markets', + { params: { marketStatus: props?.marketStatus } } + ) + + return response.data + } catch (e) { + const markets = (await indexerDerivativesApi.fetchMarkets()) as Array< + PerpetualMarket | ExpiryFuturesMarket + > + + return markets + } } async fetchMarketsSummary() { - const response = await this.client.get( - '/derivatives/summary' - ) + try { + const response = await this.client.get< + AllChronosDerivativeMarketSummary[] + >('/derivatives/summary') + + return response.data + } catch (e) { + const response = + await indexerRestDerivativeChronosApi.fetchMarketsSummary() - return response.data + return response + } } } diff --git a/layer/providers/cacheApi/spot.ts b/layer/providers/cacheApi/spot.ts index cbbc926b..0c33ab61 100644 --- a/layer/providers/cacheApi/spot.ts +++ b/layer/providers/cacheApi/spot.ts @@ -1,18 +1,31 @@ import { SpotMarket, AllChronosSpotMarketSummary } from '@injectivelabs/sdk-ts' import { BaseCacheApi } from './base' +import { indexerRestSpotChronosApi, indexerSpotApi } from '@/Service' export class SpotCacheApi extends BaseCacheApi { async fetchMarkets() { - const response = await this.client.get('/spot/markets') + try { + const response = await this.client.get('/spot/markets') - return response.data + return response.data + } catch (e) { + const response = await indexerSpotApi.fetchMarkets() + + return response + } } async fetchMarketsSummary() { - const response = await this.client.get( - '/spot/summary' - ) + try { + const response = await this.client.get( + '/spot/summary' + ) + + return response.data + } catch (e) { + const response = await indexerRestSpotChronosApi.fetchMarketsSummary() - return response.data + return response + } } } diff --git a/layer/providers/cacheApi/staking.ts b/layer/providers/cacheApi/staking.ts new file mode 100644 index 00000000..0d643e15 --- /dev/null +++ b/layer/providers/cacheApi/staking.ts @@ -0,0 +1,22 @@ +import { Pagination, Validator } from '@injectivelabs/sdk-ts' +import { BaseCacheApi } from './base' +import { stakingApi } from '@/Service' + +export class StakingCacheApi extends BaseCacheApi { + async fetchValidators(_params?: any) { + try { + const response = await this.client.get<{ + validators: Validator[] + pagination: Pagination + }>('/validators') + + return response.data + } catch (e) { + const { validators, pagination } = await stakingApi.fetchValidators({ + limit: 200 + }) + + return { validators, pagination } + } + } +} diff --git a/layer/providers/cacheApi/token.ts b/layer/providers/cacheApi/token.ts index 99374c81..e3b15b23 100644 --- a/layer/providers/cacheApi/token.ts +++ b/layer/providers/cacheApi/token.ts @@ -1,13 +1,22 @@ import { Pagination, TotalSupply } from '@injectivelabs/sdk-ts' import { BaseCacheApi } from './base' +import { bankApi } from '@/Service' export class TokenCacheApi extends BaseCacheApi { - async fetchTotalSupply() { - const response = await this.client.get<{ - supply: TotalSupply - pagination: Pagination - }>('/tokens') + async fetchTotalSupply(_params: any) { + try { + const response = await this.client.get<{ + supply: TotalSupply + pagination: Pagination + }>('/tokens') - return response.data + return response.data + } catch (e) { + const { supply, pagination } = await bankApi.fetchTotalSupply({ + limit: 2000 + }) + + return { supply, pagination } + } } } diff --git a/layer/utils/constant.ts b/layer/utils/constant.ts index ba817b84..b87f2455 100644 --- a/layer/utils/constant.ts +++ b/layer/utils/constant.ts @@ -82,12 +82,8 @@ export const FEE_PAYER_PUB_KEY = (import.meta.env.VITE_FEE_PAYER_PUB_KEY || '') as string // plugins/tracking.ts -export const AMPLITUDE_KEY_DEV = import.meta.env - .VITE_AMPLITUDE_KEY_DEV as string export const AMPLITUDE_KEY_PROD = import.meta.env .VITE_AMPLITUDE_KEY_PROD as string export const GOOGLE_ANALYTICS_KEY = import.meta.env .VITE_GOOGLE_ANALYTICS_KEY as string export const HOTJAR_KEY = import.meta.env.VITE_HOTJAR_KEY_DEV as string -export const AMPLITUDE_KEY = - IS_DEVNET || IS_TESTNET ? AMPLITUDE_KEY_DEV : AMPLITUDE_KEY_PROD diff --git a/layer/utils/helper.ts b/layer/utils/helper.ts deleted file mode 100644 index e69de29b..00000000 diff --git a/layer/wallet/metamask.ts b/layer/wallet/metamask.ts index e26d31e5..8b280080 100644 --- a/layer/wallet/metamask.ts +++ b/layer/wallet/metamask.ts @@ -1,15 +1,16 @@ import { ErrorType, + GeneralException, MetamaskException, UnspecifiedErrorCode } from '@injectivelabs/exceptions' import { EthereumChainId } from '@injectivelabs/ts-types' -import detectEthereumProvider from '@metamask/detect-provider' import { walletStrategy } from './wallet-strategy' -import { ETHEREUM_CHAIN_ID } from './../utils/constant' +import { ETHEREUM_CHAIN_ID, IS_MAINNET } from './../utils/constant' +import { UtilsWallets, Wallet } from '@injectivelabs/wallet-ts' export const isMetamaskInstalled = async (): Promise => { - const provider = await detectEthereumProvider() + const provider = await UtilsWallets.getMetamaskProvider() return !!provider } @@ -81,4 +82,43 @@ export const validateMetamask = async (address: string) => { } ) } + + const metamaskProvider = await UtilsWallets.getMetamaskProvider() + + if (!metamaskProvider) { + throw new GeneralException( + new Error('You are connected to the wrong wallet. Please use Metamask.'), + { + code: UnspecifiedErrorCode, + type: ErrorType.WalletError + } + ) + } + + if (!metamaskProvider.isMetaMask || metamaskProvider.isPhantom) { + throw new GeneralException( + new Error('You are connected to the wrong wallet. Please use Metamask.'), + { + code: UnspecifiedErrorCode, + type: ErrorType.WalletError + } + ) + } +} + +export const switchToActiveMetamaskNetwork = async ( + wallet: Wallet, + ethereumChainId: EthereumChainId +) => { + if (wallet !== Wallet.Metamask) { + return + } + + const chainId = IS_MAINNET ? EthereumChainId.Mainnet : EthereumChainId.Goerli + + try { + await UtilsWallets.updateMetamaskNetwork(chainId) + } catch (e) { + throw e + } } diff --git a/layer/wallet/wallet.ts b/layer/wallet/wallet.ts index 98f5fe96..ba79b534 100644 --- a/layer/wallet/wallet.ts +++ b/layer/wallet/wallet.ts @@ -41,6 +41,7 @@ export const getAddresses = async (): Promise => { return addresses } +/** @deprecated - we should not use this anymore */ export const confirm = async (address: string) => { return await walletStrategy.confirm(address) }