explaingit

coderofphcity/stableguard

Analysis updated 2026-05-18

0SolidityAudience · developerComplexity · 4/5LicenseSetup · hard

TLDR

A Uniswap v4 hook smart contract that protects liquidity providers by raising trading fees and blocking trades as a pool's token balance becomes skewed by automated trading agents.

Mindmap

mindmap
  root((StableGuard))
    What it does
      Tracks pool skew
      Dynamic fee tiers
      Circuit breaker
    Tech stack
      Solidity
      Uniswap v4
      Foundry
      NestJS
    Use cases
      Protect liquidity providers
      Check risk before swap
      Gasless agent swaps
    Audience
      Smart contract developers
      DeFi builders

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

What do people build with it?

USE CASE 1

Protect a Uniswap v4 liquidity pool from impermanent loss caused by automated trading agents skewing its token balance.

USE CASE 2

Check a pool's live risk level and safe trade size via an API before submitting a swap.

USE CASE 3

Let trading agents swap without a separate token approval transaction using a Permit2-based router.

What is it built with?

SolidityFoundryUniswap v4NestJSethers.js

How does it compare?

coderofphcity/stableguardagus-ops/amphialexzoid-eth/morpho-midnight-fv
Stars000
LanguageSoliditySoliditySolidity
Setup difficultyhardhardmoderate
Complexity4/54/54/5
Audiencedeveloperdeveloperdeveloper

Figures from each repo's GitHub metadata at analysis time.

How do you get it running?

Difficulty · hard Time to first run · 1h+

Requires Foundry for the Solidity contracts and testnet tokens for the deployed chain, the hackathon demo runs on the Arc testnet instead of the originally intended X Layer.

MIT license, free to use, modify, and distribute, including commercially, as long as you keep the copyright notice.

In plain English

StableGuard is a smart contract built for the Uniswap decentralized exchange, made for a hackathon called the OKX.AI Genesis Hackathon. It plugs into Uniswap version 4's hook system, which lets developers add custom logic that runs before and after each trade in a liquidity pool. Its purpose is to protect the people who provide liquidity to a pool from a specific risk: as automated trading agents chase yield across stablecoin pools, their trading can quietly push a pool's balance of tokens out of proportion, exposing liquidity providers to losses. The contract tracks how skewed a pool's balance is after every trade and automatically raises the trading fee as that skew increases, moving through three tiers described as calm, elevated, and defensive. If a trade would push the pool's imbalance past a safety limit, the contract blocks that trade outright rather than letting it through. A separate, simple web service lets other automated trading agents check a pool's current risk level, fee tier, and how much more imbalance it can safely absorb before they submit a trade, so they can decide in advance whether a swap is safe. The project also includes a router that lets agents swap without a separate approval transaction first, using a token approval standard called Permit2. The README is upfront about two simplifications in the current version: instead of partially filling an oversized trade, the circuit breaker currently blocks it entirely, and the imbalance calculation uses an approximate measure rather than exact reserve math. The author notes the contracts were meant for a different blockchain network called X Layer, but were deployed to a testnet called Arc instead due to trouble obtaining test tokens, with plans to extend to more chains later. The project is licensed under MIT.

Copy-paste prompts

Prompt 1
Explain how StableGuard's dynamic fee tiers (calm, elevated, defensive) work as a pool becomes more skewed.
Prompt 2
Walk me through calling the /protection-status endpoint to check a pool's risk before submitting a trade.
Prompt 3
How does the circuit breaker in StableGuardHook.sol decide when to block a trade entirely?
Prompt 4
What would it take to extend the circuit breaker to allow partial fills instead of reverting oversized trades?

Frequently asked questions

What is stableguard?

A Uniswap v4 hook smart contract that protects liquidity providers by raising trading fees and blocking trades as a pool's token balance becomes skewed by automated trading agents.

What language is stableguard written in?

Mainly Solidity. The stack also includes Solidity, Foundry, Uniswap v4.

What license does stableguard use?

MIT license, free to use, modify, and distribute, including commercially, as long as you keep the copyright notice.

How hard is stableguard to set up?

Setup difficulty is rated hard, with roughly 1h+ to a first successful run.

Who is stableguard for?

Mainly developer.

Open on GitHub → Explain another repo

This repo across BitVibe Labs

Verify against the repo before relying on details.