Skip to content

Latest commit

 

History

History
41 lines (32 loc) · 2.31 KB

File metadata and controls

41 lines (32 loc) · 2.31 KB

Program: barter-dex-program

Purpose

This program implements an oracle-based DEX. Unlike a traditional AMM, it does not use an internal formula (like x*y=k) to determine prices. Instead, it relies on a trusted off-chain service (price-keeper-bot) to push AI-generated exchange rates on-chain via the update_oracle_price instruction. Swaps are then executed at this externally-provided oracle price.

State Accounts

1. LiquidityPool

  • PDA Seeds: ["liquidity_pool", mint_a_pubkey, mint_b_pubkey]
  • Purpose: Stores the core data for a liquidity pool, including the AI-provided price.
  • Fields:
    • mint_a: Pubkey - The mint address of the first token in the pair.
    • mint_b: Pubkey - The mint address of the second token in the pair.
    • oracle_authority: Pubkey - The public key of the trusted bot authorized to call update_oracle_price.
    • oracle_price: u64 - The AI-generated price of token A in terms of token B, with 9 decimals of precision.
    • last_oracle_update: i64 - The Unix timestamp of the last price update to prevent stale prices.
    • vault_a_bump: u8 - The bump seed for this pool's token vault for mint_a.
    • vault_b_bump: u8 - The bump seed for this pool's token vault for mint_b.

Instructions

1. create_pool

  • Description: Initializes a new liquidity pool, setting the oracle_authority.
  • Parameters: oracle_authority: Pubkey

2. add_liquidity

  • Description: Allows a user to deposit tokens into the vaults to provide liquidity for swaps.
  • Parameters: amount_a: u64, amount_b: u64

3. update_oracle_price

  • Description: This is a permissioned instruction. Only the oracle_authority can call it. It updates the oracle_price on-chain with a new value generated by the AI.
  • Parameters: new_price: u64

4. swap

  • Description: Allows a user to swap tokens at the current oracle_price. The transaction fails if liquidity is insufficient or the oracle price is considered stale.
  • Parameters: amount_in: u64, min_amount_out: u64

Errors

  • SlippageExceeded, InsufficientLiquidity, Overflow, InvalidMint
  • InvalidOracleAuthority: The signer of update_oracle_price is not the authorized authority.
  • OraclePriceStale: The price has not been updated within the allowed time window.