πŸ€–Swaps

This explains how to use the aiis.dev swap endpoint for preparing unsigned Solana transactions to swap tokens using our bonding curve. The route supports two operations: a buy swap (wrapping SOL into WSOL and executing a buy instruction) and a sell swap (executing a sell instruction and cleaning up WSOL).


How It Works

  1. Account Setup and Preparation

    • Associated Token Accounts: The route derives and, if necessary, creates the user’s WSOL and target token accounts.

    • WSOL Account Top-Up and Sync: For buy operations, the code calculates whether additional lamports (including a fee buffer) are needed to top up the WSOL account and then syncs the account to update its wrapped SOL balance.

    • Tip Transfer: A jito tip transfer instruction is added at the start to send a small fee for faster landing.

    • Bonding Curve Instruction: Depending on the route ("buy" or "sell"), the corresponding bonding curve instruction is appended.

    • Cleanup: For sell operations, the WSOL account is closed after the swap.

  2. Transaction Building

    • All instructions are assembled into a versioned transaction.

    • The transaction is serialized to a base64-encoded string and returned to the client for signing. The server never signs transactions, ensuring that private keys remain secure on the client side.


How to Use

Endpoint

  • Method: POST

  • Path: /swap/prepare

Request Payload Schema

  • route: Specifies the swap type. Use "buy" for buying tokens or "sell" for selling.

  • lamports: The amount (in lamports) involved in the swap, provided as a string.

  • mint: The mint address of the token being swapped.

  • userPublicKey: The user's wallet public key.

Example Request:

Example Response:

  • unsignedTx: This is the unsigned versioned transaction encoded in base64. The client is expected to sign this transaction before submitting it to the blockchain.

Integration:

Last updated