Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/core/src/hooks/useLiquidityPool.ts
Original file line number Diff line number Diff line change
@@ -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<LiquidityPool | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<StellarError | null>(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 };
}
99 changes: 99 additions & 0 deletions packages/core/src/hooks/useLiquidityPoolActions.ts
Original file line number Diff line number Diff line change
@@ -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<StellarError | null>(null);
const [result, setResult] = useState<TransactionResult | null>(null);

const deposit = async (o: {
poolId: string;
maxAmountA: string;
maxAmountB: string;
minPrice: string | { n: number; d: number };
maxPrice: string | { n: number; d: number };
}): Promise<TransactionResult | null> => {
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<TransactionResult | null> => {
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 };
}
13 changes: 8 additions & 5 deletions packages/core/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export interface UseSorobanWriteReturn<T = unknown> {
export interface CustomNetworkConfig {
horizonUrl: string
sorobanUrl: string
networkPassphrase?: string
}

export interface UseSep10AuthOptions {
Expand Down Expand Up @@ -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<Exclude<StellarNetwork, "custom">, string> = {
testnet: "Test SDF Network ; September 2015",
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -599,6 +601,7 @@ export interface UseAccountExistsReturn {
error: StellarError | null
refetch: () => void
}
}

/**
* Represents an open order on the SDEX.
Expand Down
Loading