From 415deac04a7899cbaaaa39fcc1c7351d3af0282e Mon Sep 17 00:00:00 2001
From: Thompson <140930314+Godbrand0@users.noreply.github.com>
Date: Wed, 15 Oct 2025 17:17:13 -0400
Subject: [PATCH 1/3] Add explanatory comments to AMM pool initialization and
swap logic
---
contracts/amm4.clar | 3 +++
1 file changed, 3 insertions(+)
diff --git a/contracts/amm4.clar b/contracts/amm4.clar
index 8527dcb..877dbb1 100644
--- a/contracts/amm4.clar
+++ b/contracts/amm4.clar
@@ -111,6 +111,7 @@
balance-0: u0, ;; initially, balance-0 (x) is 0
balance-1: u0, ;; initially, balance-1 (y) is 0
;; Initialize statistics to 0
+ ;; I made changes to this
total-volume-0: u0,
total-volume-1: u0,
total-fees-collected: u0,
@@ -382,6 +383,7 @@
(balance-1-post-swap (if zero-for-one (- balance-1 output-amount-sub-fees) (+ balance-1 input-amount)))
;; Get current statistics
+ ;; and this
(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))
@@ -407,6 +409,7 @@
(try! (as-contract (contract-call? output-token transfer output-amount-sub-fees THIS_CONTRACT sender none)))
;; update pool balances (x and y) and statistics
+ ;; also updating the pool here
(map-set pools pool-id (merge pool-data {
balance-0: balance-0-post-swap,
balance-1: balance-1-post-swap,
From 97ec5c9462e7732a19499a6866737c099ade1792 Mon Sep 17 00:00:00 2001
From: Thompson <140930314+Godbrand0@users.noreply.github.com>
Date: Wed, 15 Oct 2025 17:47:01 -0400
Subject: [PATCH 2/3] updated the pr
---
frontend/components/pools.tsx | 6 +++++-
frontend/components/transaction-history.tsx | 1 +
frontend/lib/amm.ts | 2 ++
3 files changed, 8 insertions(+), 1 deletion(-)
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..3596831 100644
--- a/frontend/lib/amm.ts
+++ b/frontend/lib/amm.ts
@@ -32,6 +32,7 @@ type ContractEvent = {
};
};
+// also updated these type definitions
type PoolCV = {
"token-0": PrincipalCV;
"token-1": PrincipalCV;
@@ -142,6 +143,7 @@ export async function getAllPools() {
};
// convert the pool data to a Pool object
+ // updated the pool information
const pool: Pool = {
id: poolId,
"token-0": poolInitialData["token-0"].value,
From f5cc4d4042ea9d0b9d6374312b8ee6cc818097a2 Mon Sep 17 00:00:00 2001
From: Thompson <140930314+Godbrand0@users.noreply.github.com>
Date: Thu, 16 Oct 2025 03:05:02 -0400
Subject: [PATCH 3/3] Added comprehensive documentation and comments for pool
statistics tracking
---
contracts/amm4.clar | 58 +++++++++++++++++++++++++--------------------
frontend/lib/amm.ts | 21 +++++++++-------
2 files changed, 45 insertions(+), 34 deletions(-)
diff --git a/contracts/amm4.clar b/contracts/amm4.clar
index 877dbb1..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,12 +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
- ;; I made changes to this
- 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
})
)
@@ -382,18 +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
- ;; and this
+ ;; 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
@@ -408,15 +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
- ;; also updating the pool here
+ ;; 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/lib/amm.ts b/frontend/lib/amm.ts
index 3596831..50d9bfd 100644
--- a/frontend/lib/amm.ts
+++ b/frontend/lib/amm.ts
@@ -32,7 +32,8 @@ type ContractEvent = {
};
};
-// also updated these type definitions
+// Type definitions for pool data structures
+// Updated to include pool statistics tracking
type PoolCV = {
"token-0": PrincipalCV;
"token-1": PrincipalCV;
@@ -40,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;
@@ -54,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
@@ -133,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") {
@@ -142,8 +146,8 @@ export async function getAllPools() {
return 0;
};
- // convert the pool data to a Pool object
- // updated the pool information
+ // 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,
@@ -152,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"]),