DEV Community

Jerome Barton
Jerome Barton

Posted on

How to build a Byreal swap and liquidity workflow

Byreal is a decentralized exchange on Solana for swapping tokens and providing liquidity on chain. A reliable workflow ties the correct token mints, a bounded quote and the confirmed result to the same transaction. Verify wallet state, execute a swap with a minimum output, then fund and track a liquidity position using actual on-chain balances.

  1. What should a Byreal integration verify first?Verify each asset’s mint address, token program and decimals before requesting a quote. A ticker is not a token identity: two mints can share a symbol, and 1.25 units of a six-decimal token must be encoded as 1,250,000 base units. Store the mint and decimals with the requested amount so the signing path cannot scale it twice.Check that the wallet has the required source and destination token accounts, plus SOL for transaction fees and any account creation. Native SOL and wrapped SOL need distinct handling; after adding lamports directly to a wrapped SOL account, its token balance needs a SyncNative instruction. SPL Token documentation covers those account mechanics, including the extra behavior some Token-2022 mints attach to transfers.
  2. How do you execute a swap with a bounded output?Get a fresh quote for an exact input amount, then express the minimum acceptable output in the destination mint’s base units. For a Byreal swap, check that the quoted assets and input amount match the trade your user authorized. To execute that trade, use Byreal for the token swap and retain the quote, output floor and resulting signature in your transaction record.Suppose a treasury app needs to sell 10 SOL for USDC before funding a liquidity position. In an illustrative constant-product pool holding 1,000 SOL and 100,000 USDC, that sale returns about 990.10 USDC before fees, rather than the 1,000 USDC implied by the starting mid-price. A further 0.5% tolerance sets the minimum near 985.15 USDC: it covers movement after the quote, while the pool’s price impact is already reflected in the quote.Real execution may use different pool math or more than one pool, so calculate the output floor from the executable quote rather than a chart price. Quote freshness matters when liquidity is thin: another trade can change the reserves between quoting and inclusion, causing the transaction to fail its minimum-output check.
  3. How do you submit and confirm the trade?Simulate the transaction you intend to sign, then inspect its program IDs, writable accounts, fee payer and expected token changes. If your app constructs a legacy or v0 Solana transaction, set its compute-unit limit near simulated use plus roughly 10% headroom. The Solana documentation recommends that margin; the priority fee is charged against the requested limit, so excessive headroom has a cost.Solana documentation lists a base network fee of 5,000 lamports per signature, with an optional priority fee. For example, a 250,000 compute-unit limit priced at 1,000 micro-lamports per unit adds 250 lamports; token-account rent and any trading fee are separate. Treat these as distinct amounts in the estimate shown before signing.Keep the recent blockhash’s lastValidBlockHeight and poll getSignatureStatuses until the signature has no error at your required commitment. A submitted transaction can be delayed or dropped, so reconcile that signature until it lands or expires before building a replacement. The network fee can still be charged when an included transaction fails.
  4. How do you provide Byreal liquidity?Choose the pool and position size by comparing its available depth and deposit rules with the inventory you want to hold. In an illustrative 1,000 SOL to 100,000 USDC constant-product pool, adding 1 SOL proportionally calls for about 100 USDC. If a pool uses concentrated liquidity, select its lower and upper prices in the stated quote convention; a narrower range concentrates exposure around spot and needs closer rebalancing.Record the deposited amounts, pool address, position or share units, starting price and signature. For a 50/50 constant-product position, a 20% relative price move produces roughly 0.4% divergence loss versus holding the starting assets, before earned fees; concentrated positions behave differently and can stop earning swap fees outside their chosen range. Compare the expected fee income with that exposure and the cost of changing or exiting the position.
  5. How do you reconcile costs and trading outcomes?Reconcile each settled swap or liquidity action from on-chain token-account changes, using the signature as an idempotency key in your ledger. Match the mints and credited amounts as well as the debits: a Token-2022 transfer fee, for example, can leave the recipient with less than the nominal transfer amount. A quote and a submitted signature are insufficient evidence of a completed fill.Track Byreal trading fees as an observed cost from the trade’s live terms and result, rather than assuming a fixed rate. If a trading feature presents a limit price, expiry or partial fills, give it an order lifecycle instead of treating it as an immediate swap. Start with one small swap and one small liquidity deposit, then compare their settled token changes and network charges with the bounds your app displayed.

Top comments (0)