Shorting a token through an AMM
An Anchor lending pool that lets a trader short a token by selling borrowed tokens through Meteora's DAMM v2 by CPI, and liquidates positions below a threshold set per pool.
- Status
- Delivered, not audited
- When
- Oct to Dec 2025
- Role
- Wrote the lending-pool program and its tests
- Team
- Solo
- Stack
- Rust, Anchor, TypeScript, Mocha
- Links
- Source on GitHub
- 41
- instructions: initialize a pool, open a short, close it, liquidate it
- ~9001
- lines of Rust added; Meteora's AMM left untouched apart from 2 lines
- 61
- end-to-end tests over the full cycle, plus 10 simulated scenarios
Summary
A client wanted traders to be able to short a Solana token against a constant-product pool. I forked Meteora's DAMM v2, left its AMM program alone, and wrote a separate Anchor program beside it: a lending pool that lends the token, sells it through the AMM in the same transaction and liquidates positions whose collateral falls too low.
The work ran from October to December 2025 in 24 commits on top of Meteora's history1.
How a short opens
Opening a short is one instruction. The trader deposits collateral, the lending pool lends the token being shorted out of its own vault, and the program sells that token straight into the AMM with a cross-program call, protected by a minimum output the trader sets. The proceeds land back in the lending pool's vault, and the position records the collateral, the debt and the pool's price at entry.
1Deposits collateral; a protocol fee is taken
2Lends the shorted token out of its vault
3Swaps it by CPI, with the trader's minimum out
4Pays the proceeds into the lending pool's vault
5Records collateral, debt and the entry price
Closing buys the token back and repays the debt. Each pool sets its own minimum collateral ratio, liquidation threshold and protocol fee, and liquidate only succeeds when a position's ratio is below its pool's threshold.
Verification
- Six Mocha tests run the full cycle against the real programs: create the AMM pool, create the lending pool, seed liquidity, open a short, close it and liquidate one.
- A simulation script walks ten scenarios through the same maths.
- Not audited, and the implementation notes say so.
What I would change
Collateral is valued from the AMM pool's own spot price, with no outside oracle. That kept the program self-contained, but a spot price can be pushed around within a single block, which makes it a poor input for liquidations on a thin pool. Before real money goes in, I would value positions from a time-weighted price or an oracle and cap how far a single swap can move the ratio used for liquidation.
Sources
- 1.takshakmudgal/duo-liquidity-damm, the lending-pool program and its tests. Diffed against Meteora's damm-v2 at release 0.1.4.