diff --git a/contracts/amm4.clar b/contracts/amm4.clar index 8527dcb..56748f9 100644 --- a/contracts/amm4.clar +++ b/contracts/amm4.clar @@ -31,11 +31,11 @@ balance-0: uint, balance-1: uint, - ;; Pool statistics - total-volume-0: uint, - total-volume-1: uint, - total-fees-collected: uint, - swap-count: uint + ;; Pool statistics - track trading activity and performance + total-volume-0: uint, ;; Total volume traded of token-0 + total-volume-1: uint, ;; Total volume traded of token-1 + total-fees-collected: uint, ;; Total fees collected by the pool + swap-count: uint ;; Number of swaps executed in this pool } ) @@ -110,11 +110,13 @@ liquidity: u0, ;; initially, liquidity is 0 balance-0: u0, ;; initially, balance-0 (x) is 0 balance-1: u0, ;; initially, balance-1 (y) is 0 - ;; Initialize statistics to 0 - total-volume-0: u0, - total-volume-1: u0, - total-fees-collected: u0, - swap-count: u0 + + ;; Initialize pool statistics to zero + ;; These will be updated as swaps occur + total-volume-0: u0, ;; no trading volume yet + total-volume-1: u0, ;; no trading volume yet + total-fees-collected: u0, ;; no fees collected yet + swap-count: u0 ;; no swaps executed yet }) ) @@ -381,17 +383,23 @@ (balance-0-post-swap (if zero-for-one (+ balance-0 input-amount) (- balance-0 output-amount-sub-fees))) (balance-1-post-swap (if zero-for-one (- balance-1 output-amount-sub-fees) (+ balance-1 input-amount))) - ;; Get current statistics + ;; Retrieve current pool statistics before updating (current-volume-0 (get total-volume-0 pool-data)) (current-volume-1 (get total-volume-1 pool-data)) (current-fees (get total-fees-collected pool-data)) (current-swap-count (get swap-count pool-data)) - ;; Update statistics based on swap direction - (new-volume-0 (if zero-for-one (+ current-volume-0 input-amount) current-volume-0)) - (new-volume-1 (if zero-for-one current-volume-1 (+ current-volume-1 input-amount))) - (new-fees (+ current-fees fees)) - (new-swap-count (+ current-swap-count u1)) + ;; Calculate updated statistics based on swap direction + ;; If zero-for-one is true, user is selling token-0 for token-1 + ;; If zero-for-one is false, user is selling token-1 for token-0 + (new-volume-0 (if zero-for-one + (+ current-volume-0 input-amount) + current-volume-0)) + (new-volume-1 (if zero-for-one + current-volume-1 + (+ current-volume-1 input-amount))) + (new-fees (+ current-fees fees)) ;; Accumulate total fees + (new-swap-count (+ current-swap-count u1)) ;; Increment swap counter ) ;; make sure user is swapping >0 tokens @@ -406,14 +414,15 @@ ;; transfer output token from pool to user (try! (as-contract (contract-call? output-token transfer output-amount-sub-fees THIS_CONTRACT sender none))) - ;; update pool balances (x and y) and statistics + ;; Update pool state with new balances and statistics + ;; This persists the swap results and tracks pool performance metrics (map-set pools pool-id (merge pool-data { - balance-0: balance-0-post-swap, - balance-1: balance-1-post-swap, - total-volume-0: new-volume-0, - total-volume-1: new-volume-1, - total-fees-collected: new-fees, - swap-count: new-swap-count + balance-0: balance-0-post-swap, ;; Updated token-0 balance + balance-1: balance-1-post-swap, ;; Updated token-1 balance + total-volume-0: new-volume-0, ;; Cumulative volume for token-0 + total-volume-1: new-volume-1, ;; Cumulative volume for token-1 + total-fees-collected: new-fees, ;; Total fees earned by LPs + swap-count: new-swap-count ;; Total number of swaps })) (print { action: "swap", pool-id: pool-id, input-amount: input-amount }) diff --git a/frontend/components/pools.tsx b/frontend/components/pools.tsx index 4365076..52036c0 100644 --- a/frontend/components/pools.tsx +++ b/frontend/components/pools.tsx @@ -7,13 +7,15 @@ export interface PoolsListProps { export function PoolsList({ pools }: PoolsListProps) { return ( + //also updated the pool here
ID Token Pair Fee Liquidity - Total Volume + + Total Volume Fees Collected Swaps
@@ -21,10 +23,12 @@ export function PoolsList({ pools }: PoolsListProps) { ))}
); + } export function PoolListItem({ pool }: { pool: Pool }) { diff --git a/frontend/components/transaction-history.tsx b/frontend/components/transaction-history.tsx index 40e09bf..3e23456 100644 --- a/frontend/components/transaction-history.tsx +++ b/frontend/components/transaction-history.tsx @@ -6,6 +6,7 @@ interface TransactionHistoryProps { userAddress: string; } +// this is the history component. export function TransactionHistory({ userAddress }: TransactionHistoryProps) { const [transactions, setTransactions] = useState([]); const [loading, setLoading] = useState(true); diff --git a/frontend/lib/amm.ts b/frontend/lib/amm.ts index d55bf0a..50d9bfd 100644 --- a/frontend/lib/amm.ts +++ b/frontend/lib/amm.ts @@ -32,6 +32,8 @@ type ContractEvent = { }; }; +// Type definitions for pool data structures +// Updated to include pool statistics tracking type PoolCV = { "token-0": PrincipalCV; "token-1": PrincipalCV; @@ -39,6 +41,7 @@ type PoolCV = { liquidity: UIntCV; "balance-0": UIntCV; "balance-1": UIntCV; + // Pool statistics fields from smart contract "total-volume-0": UIntCV; "total-volume-1": UIntCV; "total-fees-collected": UIntCV; @@ -53,10 +56,11 @@ export type Pool = { liquidity: number; "balance-0": number; "balance-1": number; - "total-volume-0": number; - "total-volume-1": number; - "total-fees-collected": number; - "swap-count": number; + // Pool performance metrics + "total-volume-0": number; // Cumulative trading volume for token-0 + "total-volume-1": number; // Cumulative trading volume for token-1 + "total-fees-collected": number; // Total fees earned by liquidity providers + "swap-count": number; // Number of swaps executed in this pool }; // getAllPools @@ -132,7 +136,8 @@ export async function getAllPools() { const poolData = poolDataResult.value.value.value as any; - // Helper to safely parse uint values + // Helper function to safely parse uint clarity values + // Returns 0 if the value is undefined or not a uint type const parseUintCV = (cv: any): number => { if (!cv) return 0; if (cv.type === "uint") { @@ -141,7 +146,8 @@ export async function getAllPools() { return 0; }; - // convert the pool data to a Pool object + // Convert the pool data from Clarity values to JavaScript Pool object + // Includes both core pool data and statistics tracking fields const pool: Pool = { id: poolId, "token-0": poolInitialData["token-0"].value, @@ -150,6 +156,7 @@ export async function getAllPools() { liquidity: Number(poolData["liquidity"].value), "balance-0": Number(poolData["balance-0"].value), "balance-1": Number(poolData["balance-1"].value), + // Parse pool statistics - safely handle undefined values "total-volume-0": parseUintCV(poolData["total-volume-0"]), "total-volume-1": parseUintCV(poolData["total-volume-1"]), "total-fees-collected": parseUintCV(poolData["total-fees-collected"]),