Skip to content

Commit

Permalink
Merge branch 'master' into gc-apps
Browse files Browse the repository at this point in the history
  • Loading branch information
nohaapav authored Mar 13, 2024
2 parents f5e8cfa + b47020b commit 480f088
Show file tree
Hide file tree
Showing 48 changed files with 293 additions and 158 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@radix-ui/react-slider": "^1.0.0",
"@radix-ui/react-switch": "^1.0.0",
"@radix-ui/react-toast": "^1.0.0",
"@radix-ui/react-tooltip": "^1.0.2",
"@radix-ui/react-tooltip": "^1.0.7",
"@sentry/react": "^7.16.0",
"@sentry/tracing": "^7.16.0",
"@talismn/connect-wallets": "^1.2.1",
Expand Down
4 changes: 2 additions & 2 deletions src/api/accountBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface PalletBalancesAccountDataCustom extends PalletBalancesAccountData {
}

export const useAccountBalances = (id: Maybe<AccountId32 | string>) => {
const api = useApiPromise()
const { api } = useApiPromise()
return useQuery(
QUERY_KEYS.accountBalances(id),
!!id ? getAccountBalances(api, id) : undefinedNoop,
Expand Down Expand Up @@ -104,7 +104,7 @@ const getTokenAccountBalancesList =
export const useTokenAccountBalancesList = (
pairs: Array<[address: AccountId32 | string, assetId: u32 | string]>,
) => {
const api = useApiPromise()
const { api } = useApiPromise()

return useQuery(
QUERY_KEYS.tokenAccountBalancesList(pairs),
Expand Down
4 changes: 2 additions & 2 deletions src/api/addLiquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ interface AddLiquidityAsset {
}

export function useAddLiquidityPaymentInfo(assetA: string, assetB: string) {
const api = useApiPromise()
const { api } = useApiPromise()
return usePaymentInfo(api.tx.xyk.addLiquidity(assetA, assetB, "0", "0"))
}

export function useAddLiquidityMutation(onClose: () => void) {
const api = useApiPromise()
const { api } = useApiPromise()
const { createTransaction } = useStore()
const { account } = useAccountStore()

Expand Down
4 changes: 2 additions & 2 deletions src/api/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getAssetLogo } from "components/AssetIcon/AssetIcon"
import { u32 } from "@polkadot/types"
import { Maybe, useQueryReduce, useQuerySelect } from "utils/helpers"
import { TradeRouter } from "@galacticcouncil/sdk"
import { useTradeRouter } from "utils/api"
import { useApiPromise } from "utils/api"
import { useQuery } from "@tanstack/react-query"
import { QUERY_KEYS } from "utils/queryKeys"

Expand Down Expand Up @@ -34,7 +34,7 @@ export const useUsdPeggedAsset = () => {
}

export const useTradeAssets = () => {
const tradeRouter = useTradeRouter()
const { tradeRouter } = useApiPromise()
return useQuery(QUERY_KEYS.tradeAssets, getTradeAssets(tradeRouter))
}

Expand Down
4 changes: 2 additions & 2 deletions src/api/assetDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { PalletAssetRegistryAssetType } from "@polkadot/types/lookup"
import { BN_0 } from "utils/constants"

export const useAssetDetails = (id: Maybe<u32 | string>) => {
const api = useApiPromise()
const { api } = useApiPromise()
return useQuery(QUERY_KEYS.assets, getAssetDetails(api), {
select: (data) => data.find((i) => i.id === id?.toString()),
})
Expand All @@ -26,7 +26,7 @@ export const useAssetDetailsList = (
assetType: ["Token"],
},
) => {
const api = useApiPromise()
const { api } = useApiPromise()

const normalizedIds = ids?.filter(isNotNil).map(normalizeId)

Expand Down
4 changes: 2 additions & 2 deletions src/api/assetMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { u32, u8 } from "@polkadot/types"
import { Maybe } from "utils/helpers"

export const useAssetMeta = (id: Maybe<u32 | string>) => {
const api = useApiPromise()
const { api } = useApiPromise()
return useQuery(QUERY_KEYS.assetsMeta, getAllAssetMeta(api), {
select: (data) => data.find((i) => i.id === id?.toString()),
})
}

export const useAssetMetaList = (ids: Array<Maybe<u32 | string>>) => {
const api = useApiPromise()
const { api } = useApiPromise()

const normalizedIds = ids
.filter((x): x is u32 | string => !!x)
Expand Down
8 changes: 4 additions & 4 deletions src/api/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const useTokenBalance = (
id: Maybe<string | u32>,
address: Maybe<AccountId32 | string>,
) => {
const api = useApiPromise()
const { api } = useApiPromise()

return useQuery(
QUERY_KEYS.tokenBalance(id, address),
Expand All @@ -80,7 +80,7 @@ export function useTokensBalances(
tokenIds: (string | u32)[],
address: Maybe<AccountId32 | string>,
) {
const api = useApiPromise()
const { api } = useApiPromise()

return useQueries({
queries: tokenIds.map((id) => ({
Expand All @@ -97,15 +97,15 @@ const getExistentialDeposit = (api: ApiPromise) => {
}

export function useExistentialDeposit() {
const api = useApiPromise()
const { api } = useApiPromise()
return useQuery(QUERY_KEYS.existentialDeposit, async () => {
const existentialDeposit = await getExistentialDeposit(api)
return existentialDeposit.toBigNumber()
})
}

export const useTokensLocks = (ids: Maybe<u32 | string>[]) => {
const api = useApiPromise()
const { api } = useApiPromise()
const { account } = useAccountStore()

const normalizedIds = ids?.reduce<string[]>((memo, item) => {
Expand Down
6 changes: 3 additions & 3 deletions src/api/bestTrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { TradeRouter } from "@galacticcouncil/sdk"
import BigNumber from "bignumber.js"
import { useQuery } from "@tanstack/react-query"
import { QUERY_KEYS } from "utils/queryKeys"
import { useTradeRouter } from "utils/api"
import { useApiPromise } from "utils/api"

export const useBestBuy = (props: BestTradeProps) => {
const tradeRouter = useTradeRouter()
const { tradeRouter } = useApiPromise()

return useQuery(QUERY_KEYS.bestBuy(props), getBestBuy(tradeRouter, props))
}

export const useBestSell = (props: BestTradeProps) => {
const tradeRouter = useTradeRouter()
const { tradeRouter } = useApiPromise()

return useQuery(QUERY_KEYS.bestSell(props), getBestSell(tradeRouter, props))
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useApiPromise } from "utils/api"
import { QUERY_KEYS } from "utils/queryKeys"

export const useBestNumber = (disable?: boolean) => {
const api = useApiPromise()
const { api } = useApiPromise()
return useQuery(
QUERY_KEYS.bestNumber,
async () => {
Expand Down
8 changes: 4 additions & 4 deletions src/api/deposits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export type DepositNftType = Awaited<
>[number]

export const useDeposits = (poolId?: string) => {
const api = useApiPromise()
const { api } = useApiPromise()
return useQuery(QUERY_KEYS.deposits(poolId), getDeposits(api, poolId))
}

export const useAllDeposits = (poolIds?: string[]) => {
const api = useApiPromise()
const { api } = useApiPromise()
const ids = poolIds?.filter((id): id is string => !!id) ?? []

return useQueries({
Expand All @@ -29,7 +29,7 @@ export const useAllDeposits = (poolIds?: string[]) => {
}

export const useDeposit = (id: Maybe<u128>) => {
const api = useApiPromise()
const { api } = useApiPromise()
return useQuery(
QUERY_KEYS.deposit(id),
id != null ? getDeposit(api, id) : undefinedNoop,
Expand All @@ -40,7 +40,7 @@ export const useDeposit = (id: Maybe<u128>) => {
export const useAccountDepositIds = (
accountId: Maybe<AccountId32 | string>,
) => {
const api = useApiPromise()
const { api } = useApiPromise()
return useQuery(
QUERY_KEYS.accountDepositIds(accountId),
accountId != null ? getAccountDepositIds(api, accountId) : undefinedNoop,
Expand Down
2 changes: 1 addition & 1 deletion src/api/exchangeFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import BN from "bignumber.js"
import { TRADING_FEE } from "utils/constants"

export const useExchangeFee = () => {
const api = useApiPromise()
const { api } = useApiPromise()

return useQuery(QUERY_KEYS.exchangeFee, getExchangeFee(api))
}
Expand Down
12 changes: 6 additions & 6 deletions src/api/farms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { isNotNil, useQueryReduce } from "utils/helpers"
import { QUERY_KEYS } from "utils/queryKeys"

export const useYieldFarms = (ids: FarmIds[]) => {
const api = useApiPromise()
const { api } = useApiPromise()
return useQuery(QUERY_KEYS.yieldFarms(ids), getYieldFarms(api, ids))
}

Expand All @@ -16,14 +16,14 @@ export const useYieldFarm = (ids: {
globalFarmId: u32 | string
yieldFarmId: u32 | string
}) => {
const api = useApiPromise()
const { api } = useApiPromise()
return useQuery(QUERY_KEYS.yieldFarm(ids), getYieldFarm(api, ids), {
enabled: !!ids,
})
}

export const useActiveYieldFarms = (poolIds: (AccountId32 | string)[]) => {
const api = useApiPromise()
const { api } = useApiPromise()
return useQueries({
queries: poolIds.map((poolId) => ({
queryKey: QUERY_KEYS.activeYieldFarms(poolId),
Expand All @@ -33,19 +33,19 @@ export const useActiveYieldFarms = (poolIds: (AccountId32 | string)[]) => {
}

export const useGlobalFarms = (ids: u32[]) => {
const api = useApiPromise()
const { api } = useApiPromise()
return useQuery(QUERY_KEYS.globalFarms(ids), getGlobalFarms(api, ids), {
enabled: !!ids.length,
})
}

export const useGlobalFarm = (id: u32) => {
const api = useApiPromise()
const { api } = useApiPromise()
return useQuery(QUERY_KEYS.globalFarm(id), getGlobalFarm(api, id))
}

export const useInactiveYieldFarms = (poolIds: (AccountId32 | string)[]) => {
const api = useApiPromise()
const { api } = useApiPromise()
return useQueries({
queries: poolIds.map((poolId) => ({
queryKey: QUERY_KEYS.inactiveYieldFarms(poolId),
Expand Down
6 changes: 3 additions & 3 deletions src/api/payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const getAcceptedCurrency = (api: ApiPromise, id: u32 | string) => async () => {
}

export const useAcceptedCurrencies = (ids: Maybe<string | u32>[]) => {
const api = useApiPromise()
const { api } = useApiPromise()

return useQueries({
queries: ids.map((id) => ({
Expand All @@ -34,7 +34,7 @@ export const useAcceptedCurrencies = (ids: Maybe<string | u32>[]) => {
}

export const useSetAsFeePayment = () => {
const api = useApiPromise()
const { api } = useApiPromise()
const { account } = useAccountStore()
const { createTransaction } = useStore()
const queryClient = useQueryClient()
Expand Down Expand Up @@ -75,7 +75,7 @@ const getAccountCurrency =
}

export const useAccountCurrency = (address: Maybe<string | AccountId32>) => {
const api = useApiPromise()
const { api } = useApiPromise()
return useQuery(
QUERY_KEYS.accountCurrency(address),
!!address ? getAccountCurrency(api, address) : undefinedNoop,
Expand Down
8 changes: 4 additions & 4 deletions src/api/pools.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useQueries, useQuery } from "@tanstack/react-query"
import { QUERY_KEYS } from "utils/queryKeys"
import { useApiPromise, useTradeRouter } from "utils/api"
import { useApiPromise } from "utils/api"
import { ApiPromise } from "@polkadot/api"
import { PoolBase, TradeRouter } from "@galacticcouncil/sdk"
import { AccountId32 } from "@polkadot/types/interfaces"
Expand All @@ -12,12 +12,12 @@ import { useAccountStore } from "../state/store"
import { useQueryReduce } from "utils/helpers"

export const usePools = () => {
const tradeRouter = useTradeRouter()
const { tradeRouter } = useApiPromise()
return useQuery(QUERY_KEYS.pools, getPools(tradeRouter))
}

export const usePoolShareToken = (poolId: string) => {
const api = useApiPromise()
const { api } = useApiPromise()

return useQuery(
QUERY_KEYS.poolShareToken(poolId),
Expand Down Expand Up @@ -59,7 +59,7 @@ export const usePoolsWithShareTokens = () => {
}

export const usePoolShareTokens = (poolIds: (string | AccountId32)[]) => {
const api = useApiPromise()
const { api } = useApiPromise()

return useQueries({
queries: poolIds.map((id) => ({
Expand Down
30 changes: 11 additions & 19 deletions src/api/provider.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
import { useQuery } from "@tanstack/react-query"
import { QUERY_KEYS } from "utils/queryKeys"
import { ApiPromise, WsProvider } from "@polkadot/api"
//import * as definitions from "@galacticcouncil/api-augment/basilisk/interfaces/voting/definitions"
import * as definitions from "interfaces/voting/definitions"

import { WsProvider } from "@polkadot/api"
import { create } from "zustand"
import { persist } from "zustand/middleware"
import { SubstrateApis } from "@galacticcouncil/xcm-sdk"
import { PoolService, PoolType, TradeRouter } from "@galacticcouncil/sdk"

const fetchApi = async (api: string) => {
const provider = await new Promise<WsProvider>((resolve, reject) => {
const provider = new WsProvider(api)
const fetchApi = async (url: string) => {
const provider = new WsProvider(url)

provider.on("connected", () => {
resolve(provider)
})
const apiPool = SubstrateApis.getInstance()
const api = await apiPool.api(provider.endpoint)

provider.on("error", () => {
provider.disconnect()
reject("disconnected")
})
const poolService = new PoolService(api)
const tradeRouter = new TradeRouter(poolService, {
includeOnly: [PoolType.XYK],
})

const types = Object.values(definitions).reduce(
(res, { types }): object => ({ ...res, ...types }),
{},
)
return ApiPromise.create({ provider, types })
return { api, tradeRouter }
}

export const useProvider = (rpcUrl?: string) => {
Expand Down
6 changes: 3 additions & 3 deletions src/api/spotPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { u32 } from "@polkadot/types"
import { TradeRouter } from "@galacticcouncil/sdk"
import { BN_1, BN_10, BN_NAN } from "utils/constants"
import BN from "bignumber.js"
import { useTradeRouter } from "utils/api"
import { useApiPromise } from "utils/api"
import { Maybe } from "utils/helpers"

export const useSpotPrice = (
assetA: Maybe<u32 | string>,
assetB: Maybe<u32 | string>,
) => {
const tradeRouter = useTradeRouter()
const { tradeRouter } = useApiPromise()
const tokenIn = assetA?.toString() ?? ""
const tokenOut = assetB?.toString() ?? ""

Expand All @@ -26,7 +26,7 @@ export const useSpotPrices = (
assetsIn: Maybe<u32 | string>[],
assetOut: Maybe<u32 | string>,
) => {
const tradeRouter = useTradeRouter()
const { tradeRouter } = useApiPromise()

const assets = assetsIn
.filter((a): a is u32 | string => !!a)
Expand Down
2 changes: 1 addition & 1 deletion src/api/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function useTimestamp(
blockNumber?: Maybe<u32 | BigNumber>,
enabled = true,
) {
const api = useApiPromise()
const { api } = useApiPromise()
return useQuery(
QUERY_KEYS.timestamp(blockNumber),
() =>
Expand Down
Loading

0 comments on commit 480f088

Please sign in to comment.