Skip to content

Add Stellar Soroban RPC source for on-chain AMM price discovery #36

Description

@prodbycorne

Overview

The current price sources (Stellar DEX orderbook, CoinGecko, CoinMarketCap) miss prices from Stellar's on-chain AMM liquidity pools. AMM pools often have tighter spreads and are the reference price for assets primarily traded on-chain. A Soroban/Horizon AMM source should be added as a fourth price source.

How Stellar AMMs Work

Stellar supports constant-product AMM pools (similar to Uniswap v2). The current price can be derived from:

price_A_in_B = reserve_B / reserve_A

Horizon exposes pool data at:
GET /liquidity_pools?reserves=XLM,USDC:ISSUER

Implementation

Create src/services/sources/stellarAmm.js:

const { Horizon } = require('@stellar/stellar-sdk');

async function fetchFromAMM(assetCode, issuer, horizonUrl) {
  const server = new Horizon.Server(horizonUrl);
  const pools = await server
    .liquidityPools()
    .forAssets(makeAsset(assetCode, issuer), makeAsset('XLM'))
    .call();

  if (!pools.records.length) return null;

  // Find deepest pool (highest total value)
  const best = pools.records.sort((a, b) =>
    parseFloat(b.total_shares) - parseFloat(a.total_shares)
  )[0];

  const [reserveA, reserveB] = best.reserves;
  const price = parseFloat(reserveB.amount) / parseFloat(reserveA.amount);
  return { price_usd: price * XLM_USD_PRICE, source: 'stellar_amm' };
}

Note: XLM_USD_PRICE is fetched from the existing Stellar DEX source.

Acceptance Criteria

  • stellarAmm.js source created and registered in priceOracle.js
  • Selects deepest pool by total shares
  • Returns null if no AMM pool exists for the pair
  • XLM-denominated prices converted to USD using current XLM price
  • Added to fetchFromAllSources array
  • Unit tests with mocked Horizon liquidity pool response
  • AMM source included in circuit breaker system

Metadata

Metadata

Assignees

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial CampaignCampaign: Official CampaignfeatureNew feature or enhancementindexingBlockchain indexing and event streaming

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions