diff --git a/packages/core/src/hooks/useLiquidityPool.ts b/packages/core/src/hooks/useLiquidityPool.ts new file mode 100644 index 0000000..8045309 --- /dev/null +++ b/packages/core/src/hooks/useLiquidityPool.ts @@ -0,0 +1,36 @@ +import { useState, useCallback, useEffect } from "react"; +import { useStellar } from "../providers/StellarProvider"; +import { LiquidityPool, StellarError } from "../types"; + +export function useLiquidityPool(poolId: string) { + const { server } = useStellar(); + const [pool, setPool] = useState(null); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + + const refetch = useCallback(async () => { + if (!server || !poolId) return; + setLoading(true); + setError(null); + try { + const res = await server.liquidityPools().liquidityPoolId(poolId).call(); + setPool({ + id: res.id, + fee_bp: res.fee_bp, + type: res.type, + total_trustlines: res.total_trustlines, + total_shares: res.total_shares, + reserves: res.reserves, + }); + } catch (err) { + setError(err as StellarError); + } finally { +ts } + }, [server, poolId]); + + useEffect(() => { + refetch(); + }, [refetch]); + + return { pool, loading, error, refetch }; +} \ No newline at end of file diff --git a/packages/core/src/hooks/useLiquidityPoolActions.ts b/packages/core/src/hooks/useLiquidityPoolActions.ts new file mode 100644 index 0000000..5ef7b66 --- /dev/null +++ b/packages/core/src/hooks/useLiquidityPoolActions.ts @@ -0,0 +1,99 @@ +import { useState } from "react"; +import { Operation } from "@stellar/stellar-sdk"; +import { useStellar } from "../providers/StellarProvider"; +import { StellarError, TransactionResult } from "../types"; + +export function useLiquidityPoolActions() { + const { server, adapter, publicKey } = useStellar(); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + const [result, setResult] = useState(null); + + const deposit = async (o: { + poolId: string; + maxAmountA: string; + maxAmountB: string; + minPrice: string | { n: number; d: number }; + maxPrice: string | { n: number; d: number }; + }): Promise => { + if (!server || !adapter || !publicKey) { + setError(new Error("StellarProvider not initialized") as StellarError); + return null; + } + + setLoading(true); + setError(null); + try { + const account = await server.loadAccount(publicKey); + const op = Operation.liquidityPoolDeposit({ + liquidityPoolId: o.poolId, + maxAmountA: o.maxAmountA, + maxAmountB: o.maxAmountB, + minPrice: o.minPrice, + maxPrice: o.maxPrice, + }); + + const { signedTx } = await adapter.signTransaction({ + transaction: op, + account, + }); + + const res = await server.submitTransaction(signedTx); + const txResult = { ...res, status: res.successful ? "success" : "error" } as TransactionResult; + setResult(txResult); + return txResult; + } catch (err) { + setError(err as StellarError); + return null; + } finally { + setLoading(false); + } + }; + + const withdraw = async (o: { + poolId: string; + amount: string; + minAmountA: string; + minAmountB: string; + }): Promise => { + if (!server || !adapter || !publicKey) { + setError(new Error("StellarProvider not initialized") as StellarError); + return null; + } + + setLoading(true); + setError(null); + try { + const account = await server.loadAccount(publicKey); + const op = Operation.liquidityPoolWithdraw({ + liquidityPoolId: o.poolId, + amount: o.amount, + minAmountA: o.minAmountA, + minAmountB: o.minAmountB, + }); + + const { signedTx } = await adapter.signTransaction({ + transaction: op, + account, + }); + + const res = await server.submitTransaction(signedTx); + const txResult = { ...res, status: res.successful ? "success" : "error" } as TransactionResult; + setResult(txResult); + return txResult; + } catch (err) { + setError(err as StellarError); + return null; + } finally { + setLoading(false); + } + }; + + const reset = () => { + setError(null); + setResult(null); + setLoading(false); + }; + + return { deposit, withdraw, loading, error, result, reset }; +} \ No newline at end of file diff --git a/packages/core/src/types/index.ts b/packages/core/src/types/index.ts index ee9f932..eda3654 100644 --- a/packages/core/src/types/index.ts +++ b/packages/core/src/types/index.ts @@ -58,7 +58,6 @@ export interface UseSorobanWriteReturn { export interface CustomNetworkConfig { horizonUrl: string sorobanUrl: string - networkPassphrase?: string } export interface UseSep10AuthOptions { @@ -153,10 +152,7 @@ export interface UseCreateAccountReturn { } /** - * The passphrase for each network this library ships defaults for. - * - * `custom` is deliberately absent — there is no such thing as a default - * passphrase for a network we know nothing about. + * Pre-defined configurations for supported Stellar networks. */ export const NETWORK_PASSPHRASES: Record, string> = { testnet: "Test SDF Network ; September 2015", @@ -285,6 +281,12 @@ export interface AssetMetadata extends IssuedAsset { */ export type Asset = NativeAsset | IssuedAsset | "liquidity_pool_shares" +/** + * Represents a Stellar AMM Liquidity Pool. + */ + */ +export type Asset = NativeAsset | IssuedAsset | "liquidity_pool_shares" + /** * Represents a Stellar AMM Liquidity Pool. */ @@ -599,6 +601,7 @@ export interface UseAccountExistsReturn { error: StellarError | null refetch: () => void } +} /** * Represents an open order on the SDEX.