diff --git a/.gitignore b/.gitignore index 8fd3f19..7cca5d1 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ dist dist-ssr *.tgz *.local +demo # Editor directories and files .vscode/* diff --git a/package.json b/package.json index b63fb71..7feb727 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,8 @@ }, "peerDependencies": { "@bluxcc/core": "^0.1.16", + "@tanstack/react-query": "^5.90.11", + "@stellar/stellar-sdk": "^14.1.1", "react": ">=17.0.0", "react-dom": ">=17.0.0" }, diff --git a/src/Provider.tsx b/src/Provider.tsx index 0e15d0b..d297d1b 100644 --- a/src/Provider.tsx +++ b/src/Provider.tsx @@ -2,12 +2,15 @@ import React, { useEffect, useMemo, useRef } from 'react'; import { createConfig, setAppearance } from '@bluxcc/core'; import { IConfig } from '@bluxcc/core/dist/types'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; type BluxProviderProps = { config: IConfig; children: React.ReactNode | any; }; +const queryClient = new QueryClient(); + export const BluxProvider = ({ config, children }: BluxProviderProps) => { const hostRef = useRef(null); @@ -29,5 +32,9 @@ export const BluxProvider = ({ config, children }: BluxProviderProps) => { } }, [hostRef, appearance]); - return
{children}
; + return ( + +
{children}
; +
+ ); }; diff --git a/src/index.ts b/src/index.ts index 9e58487..e9121be 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ export { setAppearance } from '@bluxcc/core'; +export { Asset } from '@stellar/stellar-sdk' export * from './useStellar'; export { useBlux } from './hooks/useBlux'; diff --git a/src/useStellar/index.ts b/src/useStellar/index.ts index b3fd011..22d881a 100644 --- a/src/useStellar/index.ts +++ b/src/useStellar/index.ts @@ -1,21 +1,20 @@ export { networks } from '@bluxcc/core'; export * from './useAccount'; +export * from './useAccounts'; +export * from './useAssets'; +export * from './useBalances'; +export * from './useClaimableBalances'; +export * from './useEffects'; +export * from './useLedgers'; +export * from './useLiquidityPools'; export * from './useNetwork'; -// import useAccounts from './useAccounts'; -// import useAssets from './useAssets'; -// import useBalances from './useBalances'; -// import useClaimableBalances from './useClaimableBalances'; -// import useEffects from './useEffects'; -// import useLedgers from './useLedgers'; -// import useLiquidityPools from './useLiquidityPools'; -// import useNetwork from './useNetwork'; -// import useOffers from './useOffers'; -// import useOperations from './useOperations'; -// import useOrderbook from './useOrderbook'; -// import usePayments from './usePayments'; -// import useStrictReceivePaths from './useStrictReceivePaths'; -// import useStrictSendPaths from './useStrictSendPaths'; -// import useTradeAggregation from './useTradeAggregation'; -// import useTrades from './useTrades'; -// import useTransactions from './useTransactions'; -export { useSwitchNetwork } from './useSwitchNetwork'; +export * from './useOffers'; +export * from './useOperations'; +export * from './useOrderbook'; +export * from './usePayments'; +export * from './useStrictReceivePaths'; +export * from './useStrictSendPaths'; +export * from './useTradeAggregation'; +export * from './useTrades'; +export * from './useTransactions'; +export * from './useSwitchNetwork'; \ No newline at end of file diff --git a/src/useStellar/useAccount.ts b/src/useStellar/useAccount.ts index 142b9ab..e295d23 100644 --- a/src/useStellar/useAccount.ts +++ b/src/useStellar/useAccount.ts @@ -1,38 +1,57 @@ +import { useMemo } from 'react'; import { getAccount } from '@bluxcc/core'; -import { useEffect, useState } from 'react'; import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { GetAccountResult, GetAccountOptions, } from '@bluxcc/core/dist/exports/core/getAccount'; -export type UseAccountResult = { - loading: boolean; - error: Error | null; - result: GetAccountResult; -}; - -export function useAccount(options: GetAccountOptions): UseAccountResult { - // TODO: we need the same exact function as the core function here. - // Take the options.address, options.network, pass it to it, and wait for changes - // In useEffect. - const [result, setResult] = useState(null); - const [error, setError] = useState(null); - const [loading, setLoading] = useState(false); - - useEffect(() => { - setLoading(true); - - getAccount(options) - .then((r) => { - setResult(r); - - setLoading(false); - }) - .catch((e) => { - setError(e); - setLoading(false); - }); - }, [options.address, options.network]); - - return { loading, error, result }; -} +import { getAddress, getNetwork } from '../utils'; + +type R = GetAccountResult; +type O = GetAccountOptions; + +export function useAccount( + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const address = getAddress(options?.address); + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + address, + network, + ]; + + const queryKey = useMemo( + () => ['blux', 'account', network, ...deps], + [network, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + address, + network, + }; + + return getAccount(opts); + }, + [network, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/useAccounts.ts b/src/useStellar/useAccounts.ts index e69de29..58c4507 100644 --- a/src/useStellar/useAccounts.ts +++ b/src/useStellar/useAccounts.ts @@ -0,0 +1,61 @@ +import { useMemo } from 'react'; +import { getAccounts } from '@bluxcc/core'; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetAccountsResult, + GetAccountsOptions, +} from '@bluxcc/core/dist/exports/core/getAccounts'; + +import { getNetwork } from '../utils'; + +type R = GetAccountsResult; +type O = GetAccountsOptions; + +export function useAccounts( + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + options?.forSigner, + options?.forAsset, + options?.sponsor, + options?.forLiquidityPool, + options?.cursor, + options?.limit, + options?.network, + options?.order, + ]; + + const queryKey = useMemo( + () => ['blux', 'accounts', network, ...deps], + [network, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + network, + }; + + return getAccounts(opts); + }, + [network, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/useAssets.ts b/src/useStellar/useAssets.ts index e69de29..d5ddd7c 100644 --- a/src/useStellar/useAssets.ts +++ b/src/useStellar/useAssets.ts @@ -0,0 +1,59 @@ +import { useMemo } from 'react'; +import { getAssets } from "@bluxcc/core"; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetAssetsResult, + GetAssetsOptions, +} from "@bluxcc/core/dist/exports/core/getAssets"; + +import { getNetwork } from '../utils'; + +type R = GetAssetsResult; +type O = GetAssetsOptions; + +export function useAssets( + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + options?.forCode, + options?.forIssuer, + options?.cursor, + options?.limit, + options?.network, + options?.order, + ]; + + const queryKey = useMemo( + () => ['blux', 'assets', network, ...deps], + [network, options, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + network, + }; + + return getAssets(opts); + }, + [network, options, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/useBalances.ts b/src/useStellar/useBalances.ts index e69de29..15734a1 100644 --- a/src/useStellar/useBalances.ts +++ b/src/useStellar/useBalances.ts @@ -0,0 +1,60 @@ +import { useMemo } from 'react'; +import { getBalances } from "@bluxcc/core"; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetBalancesResult, + GetBalancesOptions, +} from "@bluxcc/core/dist/exports/core/getBalances"; + +import { getAddress, getNetwork } from '../utils'; + +type R = GetBalancesResult; +type O = GetBalancesOptions; + +export function useBalances( + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const address = getAddress(options?.address); + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + const includeZeroBalances = options?.includeZeroBalances ?? true + + const deps = [ + address, + network, + includeZeroBalances, + ]; + + const queryKey = useMemo( + () => ['blux', 'balances', network, ...deps], + [network, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + address, + includeZeroBalances, + network, + }; + + return getBalances(opts); + }, + [network, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/useClaimableBalances.ts b/src/useStellar/useClaimableBalances.ts index e69de29..e056f93 100644 --- a/src/useStellar/useClaimableBalances.ts +++ b/src/useStellar/useClaimableBalances.ts @@ -0,0 +1,62 @@ +import { useMemo } from 'react'; +import { getClaimableBalances } from "@bluxcc/core"; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetClaimableBalancesResult, + GetClaimableBalancesOptions, +} from "@bluxcc/core/dist/exports/core/getClaimableBalances"; + +import { getAddress, getNetwork } from '../utils'; + +type R = GetClaimableBalancesResult; +type O = GetClaimableBalancesOptions; + +export function useClaimableBalances( + options: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const claimant = getAddress(options.claimant); + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + claimant, + options?.asset, + options?.sponsor, + options?.cursor, + options?.limit, + options?.network, + options?.order, + ]; + + const queryKey = useMemo( + () => ['blux', 'claimableBalances', network, ...deps], + [network, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + claimant, + network, + }; + + return getClaimableBalances(opts); + }, + [network, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/useEffects.ts b/src/useStellar/useEffects.ts index e69de29..5d86514 100644 --- a/src/useStellar/useEffects.ts +++ b/src/useStellar/useEffects.ts @@ -0,0 +1,62 @@ +import { useMemo } from 'react'; +import { getEffects } from "@bluxcc/core"; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetEffectsResult, + GetEffectsOptions, +} from "@bluxcc/core/dist/exports/core/getEffects"; + +import { getNetwork } from '../utils'; + +type R = GetEffectsResult; +type O = GetEffectsOptions; + +export function useEffects( + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + options?.forAccount, + options?.forLedger, + options?.forTransaction, + options?.forOperation, + options?.forLiquidityPool, + options?.cursor, + options?.limit, + options?.network, + options?.order, + ]; + + const queryKey = useMemo( + () => ['blux', 'effects', network, ...deps], + [network, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + network, + }; + + return getEffects(opts); + }, + [network, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/useLedgers.ts b/src/useStellar/useLedgers.ts index e69de29..6a8f36c 100644 --- a/src/useStellar/useLedgers.ts +++ b/src/useStellar/useLedgers.ts @@ -0,0 +1,58 @@ +import { useMemo } from 'react'; +import { getLedgers } from "@bluxcc/core"; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetLedgersResult, + GetLedgersOptions, +} from "@bluxcc/core/dist/exports/core/getLedgers"; + +import { getNetwork } from '../utils'; + +type R = GetLedgersResult; +type O = GetLedgersOptions; + +export function useLedgers( + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + options?.ledger, + options?.cursor, + options?.limit, + options?.network, + options?.order, + ]; + + const queryKey = useMemo( + () => ['blux', 'ledgers', network, ...deps], + [network, options, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + network, + }; + + return getLedgers(opts); + }, + [network, options, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/useLiquidityPools.ts b/src/useStellar/useLiquidityPools.ts index e69de29..c679401 100644 --- a/src/useStellar/useLiquidityPools.ts +++ b/src/useStellar/useLiquidityPools.ts @@ -0,0 +1,59 @@ +import { useMemo } from 'react'; +import { getLiquidityPools } from "@bluxcc/core"; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetLiquidityPoolsResult, + GetLiquidityPoolsOptions, +} from "@bluxcc/core/dist/exports/core/getLiquidityPools"; + +import { getNetwork } from '../utils'; + +type R = GetLiquidityPoolsResult; +type O = GetLiquidityPoolsOptions; + +export function useLiquidityPools( + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + options?.forAccount, + options?.forAssets, + options?.cursor, + options?.limit, + options?.network, + options?.order, + ]; + + const queryKey = useMemo( + () => ['blux', 'liquidityPools', network, ...deps], + [network, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + network, + }; + + return getLiquidityPools(opts); + }, + [network, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/useOffers.ts b/src/useStellar/useOffers.ts index e69de29..e612d71 100644 --- a/src/useStellar/useOffers.ts +++ b/src/useStellar/useOffers.ts @@ -0,0 +1,63 @@ +import { useMemo } from 'react'; +import { getOffers } from "@bluxcc/core"; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetOffersResult, + GetOffersOptions, +} from "@bluxcc/core/dist/exports/core/getOffers"; + +import { getNetwork } from '../utils'; + +type R = GetOffersResult; +type O = GetOffersOptions; + +export function useOffers( + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + options?.forAccount, + options?.buying, + options?.selling, + options?.sponsor, + options?.seller, + options?.forAccount, + options?.cursor, + options?.limit, + options?.network, + options?.order, + ]; + + const queryKey = useMemo( + () => ['blux', 'offers', network, ...deps], + [network, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + network, + }; + + return getOffers(opts); + }, + [network, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/useOperations.ts b/src/useStellar/useOperations.ts index e69de29..aab80bc 100644 --- a/src/useStellar/useOperations.ts +++ b/src/useStellar/useOperations.ts @@ -0,0 +1,64 @@ +import { useMemo } from 'react'; +import { getOperations } from "@bluxcc/core"; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetOperationsResult, + GetOperationsOptions, +} from "@bluxcc/core/dist/exports/core/getOperations"; + +import { getNetwork } from '../utils'; + +type R = GetOperationsResult; +type O = GetOperationsOptions; + +export function useOperations( + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + options?.forAccount, + options?.includeFailed, + options?.forClaimableBalance, + options?.forLedger, + options?.forTransaction, + options?.forLiquidityPool, + options?.forAccount, + options?.cursor, + options?.limit, + options?.network, + options?.order, + ]; + + const queryKey = useMemo( + () => ['blux', 'operations', network, ...deps], + [network, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + network, + }; + + return getOperations(opts); + }, + [network, options, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/useOrderbook.ts b/src/useStellar/useOrderbook.ts index e69de29..63bb39b 100644 --- a/src/useStellar/useOrderbook.ts +++ b/src/useStellar/useOrderbook.ts @@ -0,0 +1,59 @@ +import { useMemo } from 'react'; +import { getOrderbook } from "@bluxcc/core"; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetOrderbookResult +} from "@bluxcc/core/dist/exports/core/getOrderbook"; + +import { Asset } from '@stellar/stellar-sdk'; +import { CallBuilderOptions } from '../utils'; +import { getNetwork } from '../utils'; + +type R = GetOrderbookResult; +type O = CallBuilderOptions; + +export function useOrderbook( + args: [selling: Asset, buying: Asset], + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + args[0], + args[1], + options?.cursor, + options?.limit, + options?.network, + options?.order, + ]; + + const queryKey = useMemo( + () => ['blux', 'orderbook', network, ...deps], + [network, options, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + network, + }; + + return getOrderbook(args, opts); + }, + [network, options, ...deps], + ); + + return useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); +} diff --git a/src/useStellar/usePayments.ts b/src/useStellar/usePayments.ts index e69de29..8b8cc6a 100644 --- a/src/useStellar/usePayments.ts +++ b/src/useStellar/usePayments.ts @@ -0,0 +1,65 @@ +import { useMemo } from 'react'; +import { getPayments } from "@bluxcc/core"; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetPaymentsOptions, +} from "@bluxcc/core/dist/exports/core/getPayments"; + +import { PaymentCallBuilder } from '@stellar/stellar-sdk/lib/horizon/payment_call_builder'; +import { ServerApi } from '@stellar/stellar-sdk/lib/horizon'; +import { getNetwork } from '../utils'; + +type R = { + builder: PaymentCallBuilder; + response: ServerApi.CollectionPage; +} +type O = GetPaymentsOptions; + +export function usePayments( + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + options?.forAccount, + options?.includeFailed, + options?.forLedger, + options?.forTransaction, + options?.cursor, + options?.limit, + options?.network, + options?.order, + ]; + + const queryKey = useMemo( + () => ['blux', 'payments', network, ...deps], + [network, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + network, + }; + + return getPayments(opts); + }, + [network, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/useStrictReceivePaths.ts b/src/useStellar/useStrictReceivePaths.ts index e69de29..ef9fdc3 100644 --- a/src/useStellar/useStrictReceivePaths.ts +++ b/src/useStellar/useStrictReceivePaths.ts @@ -0,0 +1,66 @@ +import { useMemo } from 'react'; +import { getStrictReceivePaths } from "@bluxcc/core"; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetPaymentPathResult +} from "@bluxcc/core/dist/exports/core/getStrictReceivePaths"; + +import { Asset } from '@stellar/stellar-sdk'; +import { CallBuilderOptions } from '../utils'; +import { getNetwork } from '../utils'; + +type R = GetPaymentPathResult; +type O = CallBuilderOptions; + +export function useStrictReceivePaths( + args: [ + source: string | Asset[], + destinationAsset: Asset, + destinationAmount: string, + ], + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + args[0], + args[1], + args[2], + options?.cursor, + options?.limit, + options?.network, + options?.order, + ]; + + const queryKey = useMemo( + () => ['blux', 'strictReceivePaths', network, ...deps], + [network, options, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + network, + }; + + return getStrictReceivePaths(args, opts); + }, + [network, options, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/useStrictSendPaths.ts b/src/useStellar/useStrictSendPaths.ts index e69de29..1ece36c 100644 --- a/src/useStellar/useStrictSendPaths.ts +++ b/src/useStellar/useStrictSendPaths.ts @@ -0,0 +1,66 @@ +import { useMemo } from 'react'; +import { getStrictSendPaths } from "@bluxcc/core"; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetPaymentPathResult +} from "@bluxcc/core/dist/exports/core/getStrictSendPaths"; + +import { Asset } from '@stellar/stellar-sdk'; +import { CallBuilderOptions } from '../utils'; +import { getNetwork } from '../utils'; + +type R = GetPaymentPathResult; +type O = CallBuilderOptions; + +export function useStrictSendPaths( + args: [ + sourceAsset: Asset, + sourceAmount: string, + destination: string | Asset[], + ], + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + args[0], + args[1], + args[2], + options?.cursor, + options?.limit, + options?.network, + options?.order, + ]; + + const queryKey = useMemo( + () => ['blux', 'strictSendPaths', network, ...deps], + [network, options, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + network, + }; + + return getStrictSendPaths(args, opts); + }, + [network, options, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/useTradeAggregation.ts b/src/useStellar/useTradeAggregation.ts index e69de29..68289ef 100644 --- a/src/useStellar/useTradeAggregation.ts +++ b/src/useStellar/useTradeAggregation.ts @@ -0,0 +1,72 @@ +import { useMemo } from 'react'; +import { getTradeAggregation } from "@bluxcc/core"; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetTradeAggregationResult +} from "@bluxcc/core/dist/exports/core/getTradeAggregation"; + +import { Asset } from '@stellar/stellar-sdk'; +import { CallBuilderOptions } from '../utils'; +import { getNetwork } from '../utils'; + +type R = GetTradeAggregationResult; +type O = CallBuilderOptions; + +export function useTradeAggregation( + args: [ + base: Asset, + counter: Asset, + start_time: number, + end_time: number, + resolution: number, + offset: number, + ], + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + args[0], + args[1], + args[2], + args[3], + args[4], + args[5], + options?.cursor, + options?.limit, + options?.network, + options?.order, + ]; + + const queryKey = useMemo( + () => ['blux', 'tradeAggregation', network, ...deps], + [network, options, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + network, + }; + + return getTradeAggregation(args, opts); + }, + [network, options, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/useTrades.ts b/src/useStellar/useTrades.ts index e69de29..36ccc45 100644 --- a/src/useStellar/useTrades.ts +++ b/src/useStellar/useTrades.ts @@ -0,0 +1,63 @@ +import { useMemo } from 'react'; +import { getTrades } from "@bluxcc/core"; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetTradesResult, + GetTradesOptions, +} from "@bluxcc/core/dist/exports/core/getTrades"; + +import { getNetwork } from '../utils'; + +type R = GetTradesResult; +type O = GetTradesOptions; + +export function useTrades( + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + options?.forAccount, + options?.forAssetPair, + options?.forOffer, + options?.forType, + options?.forLiquidityPool, + options?.forAccount, + options?.cursor, + options?.limit, + options?.network, + options?.order, + ]; + + const queryKey = useMemo( + () => ['blux', 'trades', network, ...deps], + [network, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + network, + }; + + return getTrades(opts); + }, + [network, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/useTransactions.ts b/src/useStellar/useTransactions.ts index e69de29..c9f4eae 100644 --- a/src/useStellar/useTransactions.ts +++ b/src/useStellar/useTransactions.ts @@ -0,0 +1,62 @@ +import { useMemo } from 'react'; +import { getTransactions } from "@bluxcc/core"; +import { + useQuery, + UseQueryResult, + UseQueryOptions, +} from '@tanstack/react-query'; +import type { + GetTransactionsResult, + GetTransactionsOptions, +} from "@bluxcc/core/dist/exports/core/getTransactions"; + +import { getNetwork } from '../utils'; + +type R = GetTransactionsResult; +type O = GetTransactionsOptions; + +export function useTransactions( + options?: O, + queryOptions?: UseQueryOptions, +): UseQueryResult { + const network = getNetwork(options?.network); + const enabled = queryOptions?.enabled ?? true; + + const deps = [ + options?.forAccount, + options?.includeFailed, + options?.forClaimableBalance, + options?.forLedger, + options?.forLiquidityPool, + options?.cursor, + options?.limit, + options?.network, + options?.order, + ]; + + const queryKey = useMemo( + () => ['blux', 'transactions', network, ...deps], + [network, ...deps], + ); + + const queryFn = useMemo( + () => async () => { + const opts: O = { + ...options, + network, + }; + + return getTransactions(opts); + }, + [network, ...deps], + ); + + const result = useQuery({ + queryKey, + queryFn, + enabled, + ...queryOptions, + }); + + return result; +} \ No newline at end of file diff --git a/src/useStellar/utils.ts b/src/useStellar/utils.ts deleted file mode 100644 index dd54f7b..0000000 --- a/src/useStellar/utils.ts +++ /dev/null @@ -1,35 +0,0 @@ -// import { getState } from '../../../core/dist/index.esm'; -// -// export const checkConfigCreated = () => { -// const { stellar } = getState(); -// -// return !!stellar; -// }; -// -// export const getAddress = (address?: string) => { -// const { user } = getState(); -// -// if (!user && !address) { -// throw new Error('Address not found'); -// } -// -// if (address) { -// return address; -// } -// -// return user?.address as string; -// }; -// -// export const getNetwork = (network?: string) => { -// const { stellar } = getState(); -// -// if (!network) { -// if (stellar) { -// return stellar.activeNetwork; -// } -// -// throw new Error('Custom network has no transports.'); -// } -// -// return network; -// }; diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..408579d --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,34 @@ +import { getState } from '@bluxcc/core'; + +export type CallBuilderOptions = { + cursor?: string; + limit?: number; + network?: string; + order?: 'asc' | 'desc'; +}; + +export const checkConfigCreated = () => { + const { stellar } = getState(); + + return !!stellar; +}; + +export const getAddress = (address?: string) => { + const { user } = getState(); + + if (address) { + return address; + } + + return user?.address as string; +}; + +export const getNetwork = (network?: string) => { + const { stellar } = getState(); + + if (!network && stellar) { + return stellar.activeNetwork; + } + + return network; +}; \ No newline at end of file