fix: fix getUrl

This commit is contained in:
Linkie Link 2024-02-19 13:10:24 +01:00
parent 89d41fa5cf
commit 1a45d64bac
No known key found for this signature in database
GPG Key ID: 5318B0F2564D38EA
5 changed files with 20 additions and 16 deletions

View File

@ -8,6 +8,7 @@ import { MarsParamsQueryClient } from 'types/generated/mars-params/MarsParams.cl
import { MarsPerpsQueryClient } from 'types/generated/mars-perps/MarsPerps.client'
import { MarsRedBankQueryClient } from 'types/generated/mars-red-bank/MarsRedBank.client'
import { MarsSwapperOsmosisQueryClient } from 'types/generated/mars-swapper-osmosis/MarsSwapperOsmosis.client'
import { getUrl } from 'utils/url'
let _cosmWasmClient: Map<string, CosmWasmClient> = new Map()
let _creditManagerQueryClient: Map<string, MarsCreditManagerQueryClient> = new Map()
@ -34,7 +35,7 @@ const getClient = async (rpc: string) => {
const getCreditManagerQueryClient = async (chainConfig: ChainConfig) => {
try {
const contract = chainConfig.contracts.creditManager
const rpc = chainConfig.endpoints.rpc
const rpc = getUrl(chainConfig.endpoints.rpc)
const key = rpc + contract
if (!_creditManagerQueryClient.get(key)) {
@ -51,7 +52,7 @@ const getCreditManagerQueryClient = async (chainConfig: ChainConfig) => {
const getParamsQueryClient = async (chainConfig: ChainConfig) => {
try {
const contract = chainConfig.contracts.params
const rpc = chainConfig.endpoints.rpc
const rpc = getUrl(chainConfig.endpoints.rpc)
const key = rpc + contract
if (!_paramsQueryClient.get(key)) {
@ -68,7 +69,7 @@ const getParamsQueryClient = async (chainConfig: ChainConfig) => {
const getOracleQueryClient = async (chainConfig: ChainConfig) => {
try {
const contract = chainConfig.contracts.oracle
const rpc = chainConfig.endpoints.rpc
const rpc = getUrl(chainConfig.endpoints.rpc)
const key = rpc + contract
if (!_oracleQueryClient.get(key)) {
@ -84,7 +85,7 @@ const getOracleQueryClient = async (chainConfig: ChainConfig) => {
const getVaultQueryClient = async (chainConfig: ChainConfig, address: string) => {
try {
const client = await getClient(chainConfig.endpoints.rpc)
const client = await getClient(getUrl(chainConfig.endpoints.rpc))
return new MarsMockVaultQueryClient(client, address)
} catch (error) {
throw error
@ -94,7 +95,7 @@ const getVaultQueryClient = async (chainConfig: ChainConfig, address: string) =>
const getIncentivesQueryClient = async (chainConfig: ChainConfig) => {
try {
const contract = chainConfig.contracts.incentives
const rpc = chainConfig.endpoints.rpc
const rpc = getUrl(chainConfig.endpoints.rpc)
const key = rpc + contract
if (!_incentivesQueryClient.get(key)) {
const client = await getClient(rpc)
@ -110,7 +111,7 @@ const getIncentivesQueryClient = async (chainConfig: ChainConfig) => {
const getSwapperQueryClient = async (chainConfig: ChainConfig) => {
try {
const contract = chainConfig.contracts.swapper
const rpc = chainConfig.endpoints.rpc
const rpc = getUrl(chainConfig.endpoints.rpc)
const key = rpc + contract
if (!_swapperOsmosisClient.get(key)) {
const client = await getClient(rpc)
@ -126,7 +127,7 @@ const getSwapperQueryClient = async (chainConfig: ChainConfig) => {
const getPerpsQueryClient = async (chainConfig: ChainConfig) => {
try {
const contract = chainConfig.contracts.perps
const rpc = chainConfig.endpoints.rpc
const rpc = getUrl(chainConfig.endpoints.rpc)
const key = rpc + contract
if (!_perpsClient.get(key)) {
const client = await getClient(rpc)
@ -142,7 +143,7 @@ const getPerpsQueryClient = async (chainConfig: ChainConfig) => {
const getRedBankQueryClient = async (chainConfig: ChainConfig) => {
try {
const contract = chainConfig.contracts.redBank
const rpc = chainConfig.endpoints.rpc
const rpc = getUrl(chainConfig.endpoints.rpc)
const key = rpc + contract
if (!_redBankQueryClient.get(key)) {
@ -162,8 +163,8 @@ export {
getIncentivesQueryClient,
getOracleQueryClient,
getParamsQueryClient,
getSwapperQueryClient,
getVaultQueryClient,
getPerpsQueryClient,
getRedBankQueryClient,
getSwapperQueryClient,
getVaultQueryClient,
}

View File

@ -10,6 +10,7 @@ import useChainConfig from 'hooks/useChainConfig'
import useCurrentWallet from 'hooks/useCurrentWallet'
import useToggle from 'hooks/useToggle'
import useStore from 'store'
import { getUrl } from 'utils/url'
interface Props {
providerId?: string
@ -53,7 +54,7 @@ export default function WalletConnecting(props: Props) {
setIsConnecting(true)
try {
const response = await connect({ extensionProviderId, chainId: chainConfig.id })
const cosmClient = await CosmWasmClient.connect(chainConfig.endpoints.rpc)
const cosmClient = await CosmWasmClient.connect(getUrl(chainConfig.endpoints.rpc))
const walletClient: WalletClient = {
broadcast,
cosmWasmClient: cosmClient,
@ -137,7 +138,7 @@ export default function WalletConnecting(props: Props) {
setIsConnecting(true)
try {
await mobileConnect({ mobileProviderId, chainId: chainConfig.id })
const cosmClient = await CosmWasmClient.connect(chainConfig.endpoints.rpc)
const cosmClient = await CosmWasmClient.connect(getUrl(chainConfig.endpoints.rpc))
const walletClient: WalletClient = {
broadcast,
cosmWasmClient: cosmClient,

View File

@ -75,7 +75,7 @@ function Content(props: Props) {
title: (
<FormattedNumber
className='text-xl'
amount={leverage.toNumber()}
amount={isNaN(leverage.toNumber()) ? 1 : leverage.toNumber()}
options={{ suffix: 'x' }}
/>
),

View File

@ -11,6 +11,7 @@ import { MarsParamsQueryClient } from 'types/generated/mars-params/MarsParams.cl
import { MarsPerpsQueryClient } from 'types/generated/mars-perps/MarsPerps.client'
import { MarsRedBankQueryClient } from 'types/generated/mars-red-bank/MarsRedBank.client'
import { MarsSwapperOsmosisQueryClient } from 'types/generated/mars-swapper-osmosis/MarsSwapperOsmosis.client'
import { getUrl } from 'utils/url'
export default function useClients() {
const chainConfig = useChainConfig()
@ -18,7 +19,7 @@ export default function useClients() {
const swr = useSWR(
`chains/${chainConfig.id}/clients`,
async () => {
const client = await CosmWasmClient.connect(chainConfig.endpoints.rpc)
const client = await CosmWasmClient.connect(getUrl(chainConfig.endpoints.rpc))
return {
creditManager: new MarsCreditManagerQueryClient(

View File

@ -1,9 +1,10 @@
export const getUrl = (baseUrl: string, path: string): string => {
export const getUrl = (baseUrl: string, path?: string): string => {
if (!path) path = ''
const isPlaceholder = baseUrl.split('APP_').length > 1
if (isPlaceholder) return baseUrl + '/' + path
const url = new URL(baseUrl)
const url = new URL(baseUrl.split('?')[0])
if (process.env.NEXT_PUBLIC_API_KEY)
return `${url.href}${path}?x-apikey=${process.env.NEXT_PUBLIC_API_KEY}`