Skip to main content

Crate tribewarez_swap

Crate tribewarez_swap 

Source
Expand description

§tribewarez-swap

Automated Market Maker (AMM) Swap Program for Tribewarez DeFi.

This crate implements a constant product AMM (x * y = k) for PTtC token swaps on Solana. It provides liquidity pools, enables decentralized trading, and collects protocol fees with optional tensor network support for dynamic fee discounts based on network coherence metrics.

§Core Features

  • Liquidity Pools: Create pools for trading pairs with constant product formula
  • Token Swaps: Execute swaps with automatic price discovery and slippage protection
  • Liquidity Provision: Add and remove liquidity with LP token minting/burning
  • Fee Collection: Automatic protocol fee collection and distribution
  • Tensor-Aware Fees: v0.2.0 feature for dynamic fee discounts based on network metrics
  • Price Quotes: Query swap quotes before executing trades

§Key Instructions

  • initialize_pool: Create a new liquidity pool for a trading pair
  • add_liquidity: Provide liquidity to a pool and receive LP tokens
  • remove_liquidity: Withdraw liquidity by burning LP tokens
  • swap: Execute a swap with specified slippage protection
  • get_swap_quote: Query the output amount for a given input
  • withdraw_protocol_fees: Withdraw accumulated protocol fees (admin-only)

§Events

This program emits events for pool initialization, liquidity changes, swaps, and fee withdrawals. See the events module for detailed event documentation.

§AMM Formula

The constant product formula ensures: token_a_reserve * token_b_reserve = k

For a swap of amount_in of token A:

  • amount_out = (amount_in * token_b_reserve) / (token_a_reserve + amount_in)
  • Less protocol and LP fees

§Fee Structure

  • Swap Fee: 0.30% to liquidity providers
  • Protocol Fee: 0.05% retained by protocol
  • Tensor Discount: Applied to fees for network participants (v0.2.0)

Modules§

accounts
An Anchor generated module, providing a set of structs mirroring the structs deriving Accounts, where each field is a Pubkey. This is useful for specifying accounts for a client.
events
instruction
An Anchor generated module containing the program’s set of instructions, where each method handler in the #[program] mod is associated with a struct defining the input arguments to the method. These should be used directly, when one wants to serialize Anchor instruction data, for example, when speciying instructions on a client.
program
Module representing the program.
services
tribewarez_swap

Structs§

AddLiquidity
AddLiquidityBumps
GetQuote
GetQuoteBumps
InitializePool
InitializePoolBumps
LiquidityPool
Constant product liquidity pool for token swaps. Maintains reserves of two token types and enables trading via x*y=k formula.
RemoveLiquidity
RemoveLiquidityBumps
Swap
SwapBumps
WithdrawFees
WithdrawFeesBumps

Enums§

SwapError

Statics§

ID
The static program ID

Functions§

check_id
Confirms that a given pubkey is equivalent to the program ID
entry
The Anchor codegen exposes a programming model where a user defines a set of methods inside of a #[program] module in a way similar to writing RPC request handlers. The macro then generates a bunch of code wrapping these user defined methods into something that can be executed on Solana.
entrypoint
Safety
id
Returns the program ID