Skip to content
Open
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
55 changes: 32 additions & 23 deletions contracts/amm4.clar
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)

Expand Down Expand Up @@ -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
})
)

Expand Down Expand Up @@ -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
Expand All @@ -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 })
Expand Down
6 changes: 5 additions & 1 deletion frontend/components/pools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@ export interface PoolsListProps {

export function PoolsList({ pools }: PoolsListProps) {
return (
//also updated the pool here
<div className="flex flex-col">
<div className="grid grid-cols-7 place-items-center w-full bg-gray-900 justify-between p-4 font-semibold text-sm">
<span>ID</span>
<span>Token Pair</span>
<span>Fee</span>
<span>Liquidity</span>
<span>Total Volume</span>

<span>Total Volume</span>
<span>Fees Collected</span>
<span>Swaps</span>
</div>
{pools.map((pool) => (
<PoolListItem
key={`pool-${pool["token-0"]}-${pool["token-1"]}`}
pool={pool}

/>
))}
</div>
);

}

export function PoolListItem({ pool }: { pool: Pool }) {
Expand Down
1 change: 1 addition & 0 deletions frontend/components/transaction-history.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface TransactionHistoryProps {
userAddress: string;
}

// this is the history component.
export function TransactionHistory({ userAddress }: TransactionHistoryProps) {
const [transactions, setTransactions] = useState<Transaction[]>([]);
const [loading, setLoading] = useState(true);
Expand Down
19 changes: 13 additions & 6 deletions frontend/lib/amm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ type ContractEvent = {
};
};

// Type definitions for pool data structures
// Updated to include pool statistics tracking
type PoolCV = {
"token-0": PrincipalCV;
"token-1": PrincipalCV;
fee: UIntCV;
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;
Expand All @@ -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
Expand Down Expand Up @@ -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") {
Expand All @@ -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,
Expand All @@ -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"]),
Expand Down