Skip to content

Commit

Permalink
feat: minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Dec 28, 2023
1 parent fc039c5 commit 5572a51
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 40 deletions.
9 changes: 8 additions & 1 deletion layer/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)

Expand All @@ -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`
)
Expand Down
11 changes: 1 addition & 10 deletions layer/plugins/tracking.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
2 changes: 2 additions & 0 deletions layer/providers/cacheApi/base.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
42 changes: 32 additions & 10 deletions layer/providers/cacheApi/derivative.ts
Original file line number Diff line number Diff line change
@@ -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<DerivativeMarket[]>(
'/derivatives/markets',
{ params: { marketStatus: props?.marketStatus } }
)
return response.data
try {
const response = await this.client.get<DerivativeMarket[]>(
'/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<AllChronosDerivativeMarketSummary[]>(
'/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
}
}
}
25 changes: 19 additions & 6 deletions layer/providers/cacheApi/spot.ts
Original file line number Diff line number Diff line change
@@ -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<SpotMarket[]>('/spot/markets')
try {
const response = await this.client.get<SpotMarket[]>('/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<AllChronosSpotMarketSummary[]>(
'/spot/summary'
)
try {
const response = await this.client.get<AllChronosSpotMarketSummary[]>(
'/spot/summary'
)

return response.data
} catch (e) {
const response = await indexerRestSpotChronosApi.fetchMarketsSummary()

return response.data
return response
}
}
}
22 changes: 22 additions & 0 deletions layer/providers/cacheApi/staking.ts
Original file line number Diff line number Diff line change
@@ -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 }
}
}
}
21 changes: 15 additions & 6 deletions layer/providers/cacheApi/token.ts
Original file line number Diff line number Diff line change
@@ -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 }
}
}
}
4 changes: 0 additions & 4 deletions layer/utils/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Empty file removed layer/utils/helper.ts
Empty file.
46 changes: 43 additions & 3 deletions layer/wallet/metamask.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> => {
const provider = await detectEthereumProvider()
const provider = await UtilsWallets.getMetamaskProvider()

return !!provider
}
Expand Down Expand Up @@ -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
}
}
1 change: 1 addition & 0 deletions layer/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const getAddresses = async (): Promise<string[]> => {
return addresses
}

/** @deprecated - we should not use this anymore */
export const confirm = async (address: string) => {
return await walletStrategy.confirm(address)
}

0 comments on commit 5572a51

Please sign in to comment.