> ## Documentation Index
> Fetch the complete documentation index at: https://docs.compasslabs.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Swap tokens

> Trade one token for another from inside your Risk Yield Account.

Mostly used to reach the ratio a range needs before opening a position:
a range fixes the proportion of the two assets, and a caller holding one of
them has to swap to it.

There is no aggregator on this chain, so the route is a Uniswap v3 pool. The
API quotes every enabled fee tier and takes whichever returns the most — a
1% pool with depth beats a 0.05% pool with none, which here is the common
case rather than the corner one.

The transaction is simulated before it is returned. A failing simulation is
a 422 rather than something to sign: on this chain a token may tax
transfers, block an address or be paused, and none of that is visible until
the call is actually made.



## OpenAPI

````yaml /v2/combined_spec.json post /v2/risk_yield/swap
openapi: 3.1.0
info:
  title: Compass API
  description: Compass Labs DeFi API
  version: 0.0.1
servers:
  - url: https://api.compasslabs.ai
    description: Production server
security:
  - ApiKeyAuth: []
paths:
  /v2/risk_yield/swap:
    post:
      tags:
        - Risk Yield
      summary: Swap tokens
      description: >-
        Trade one token for another from inside your Risk Yield Account.


        Mostly used to reach the ratio a range needs before opening a position:

        a range fixes the proportion of the two assets, and a caller holding one
        of

        them has to swap to it.


        There is no aggregator on this chain, so the route is a Uniswap v3 pool.
        The

        API quotes every enabled fee tier and takes whichever returns the most —
        a

        1% pool with depth beats a 0.05% pool with none, which here is the
        common

        case rather than the corner one.


        The transaction is simulated before it is returned. A failing simulation
        is

        a 422 rather than something to sign: on this chain a token may tax

        transfers, block an address or be paused, and none of that is visible
        until

        the call is actually made.
      operationId: v2_risk_yield_swap
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RiskYieldSwapRequest'
        required: true
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RiskYieldSwapResponse'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
      x-codeSamples:
        - lang: python
          label: Python (SDK)
          source: |-
            from compass_api_sdk import CompassAPI, models


            with CompassAPI(
                api_key_auth="<YOUR_API_KEY_HERE>",
            ) as compass_api:

                res = compass_api.risk_yield.risk_yield_swap(owner="0x06A9aF046187895AcFc7258450B15397CAc67400", token_in="USDG", token_out="WETH", amount_in="100", chain=models.RiskYieldSwapRequestChain.ROBINHOOD, gas_sponsorship=False, preview=False, slippage_pct="0.5", deadline_seconds=300)

                # Handle response
                print(res)
        - lang: typescript
          label: Typescript (SDK)
          source: |-
            import { CompassApiSDK } from "@compass-labs/api-sdk";

            const compassApiSDK = new CompassApiSDK({
              apiKeyAuth: "<YOUR_API_KEY_HERE>",
            });

            async function run() {
              const result = await compassApiSDK.riskYield.riskYieldSwap({
                owner: "0x06A9aF046187895AcFc7258450B15397CAc67400",
                chain: "robinhood",
                tokenIn: "USDG",
                tokenOut: "WETH",
                amountIn: "100",
              });

              console.log(result);
            }

            run();
components:
  schemas:
    RiskYieldSwapRequest:
      properties:
        owner:
          type: string
          title: Owner
          description: The wallet that owns the Risk Yield Account.
          default: '0x06A9aF046187895AcFc7258450B15397CAc67400'
          examples:
            - '0x06A9aF046187895AcFc7258450B15397CAc67400'
        chain:
          type: string
          enum:
            - robinhood
          title: Chain
          description: Risk Yield is available on Robinhood Chain only.
          default: robinhood
          examples:
            - robinhood
        gas_sponsorship:
          type: boolean
          title: Gas Sponsorship
          description: >-
            Return EIP-712 typed data for the owner to sign instead of a
            transaction, so a sponsor can broadcast it.
          default: false
        preview:
          type: boolean
          title: Preview
          description: >-
            Return the plan and its simulation without building a transaction.
            Nothing is signed and nothing can be broadcast.
          default: false
        slippage_pct:
          anyOf:
            - type: number
              maximum: 10
              exclusiveMinimum: 0
            - type: string
              pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$
          title: Slippage Pct
          description: >-
            How far the executed amounts may fall short of the quote before the
            transaction reverts. Memecoin pools move between the quote and the
            block that includes the transaction.
          default: '0.5'
        deadline_seconds:
          type: integer
          maximum: 3600
          minimum: 30
          title: Deadline Seconds
          description: How long the transaction stays valid once built.
          default: 300
        token_in:
          type: string
          title: Token
          description: What you are spending.
          examples:
            - USDC
            - WETH
            - '0xA0b86a33E6441ccF30EE5DdEF1E9b652C91ac1c8'
          default: USDG
        token_out:
          type: string
          title: Token
          description: What you want.
          examples:
            - USDC
            - WETH
            - '0xA0b86a33E6441ccF30EE5DdEF1E9b652C91ac1c8'
          default: WETH
        amount_in:
          anyOf:
            - type: number
              exclusiveMinimum: 0
            - type: string
              pattern: ^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$
          title: Amount In
          description: In token units, not wei.
          default: '100'
          examples:
            - '100'
        fee_ppm:
          anyOf:
            - type: integer
              maximum: 1000000
              minimum: 0
            - type: 'null'
          title: Fee Ppm
          description: >-
            Pin the route to one fee tier. Leave unset to let the API pick the
            tier that returns the most.
      type: object
      required:
        - owner
        - token_in
        - token_out
        - amount_in
      title: RiskYieldSwapRequest
      description: >-
        Swap one token for another from inside the Risk Yield Account.


        There is no aggregator on this chain, so the route is a Uniswap v3 pool.

        The API quotes every enabled fee tier and takes whichever returns the
        most —

        a 1% pool with depth beats a 0.05% pool with none, and here that is the

        common case.
      default:
        owner: '0x06A9aF046187895AcFc7258450B15397CAc67400'
        chain: robinhood
        token_in: USDG
        token_out: WETH
        amount_in: '100'
    RiskYieldSwapResponse:
      properties:
        transaction:
          anyOf:
            - $ref: '#/components/schemas/UnsignedTransaction'
            - type: 'null'
          description: Null when `preview` was set, or when the plan is a no-op.
        swap:
          anyOf:
            - $ref: '#/components/schemas/SwapPlan'
            - type: 'null'
        steps:
          items:
            $ref: '#/components/schemas/PlanStep'
          type: array
          title: Steps
          description: What the single transaction does, in order.
        simulation:
          anyOf:
            - $ref: '#/components/schemas/Simulation'
            - type: 'null'
          description: >-
            The result of executing this against current state. A failure here
            is the only warning you get about a token that taxes transfers,
            blocks an address, or is paused.
        warnings:
          items:
            type: string
          type: array
          title: Warnings
      type: object
      title: RiskYieldSwapResponse
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    UnsignedTransaction:
      properties:
        chainId:
          type: string
          title: Chainid
          description: The chain id of the transaction
        data:
          type: string
          title: Data
          description: The data of the transaction
        from:
          type: string
          title: From
          description: The sender of the transaction
        gas:
          anyOf:
            - type: string
            - type: 'null'
          title: Gas
          description: The gas of the transaction
        to:
          type: string
          title: To
          description: The recipient of the transaction
        value:
          type: string
          title: Value
          description: The value of the transaction
        nonce:
          type: string
          title: Nonce
          description: The nonce of the address
        maxFeePerGas:
          type: string
          title: Maxfeepergas
          description: The max fee per gas of the transaction
        maxPriorityFeePerGas:
          type: string
          title: Maxpriorityfeepergas
          description: The max priority fee per gas of the transaction
      type: object
      required:
        - chainId
        - data
        - from
        - gas
        - to
        - value
        - nonce
        - maxFeePerGas
        - maxPriorityFeePerGas
      title: UnsignedTransaction
      example:
        chainId: '0x2105'
        data: >-
          0x1688f0b900000000000000000000000029fcb43b46531bca003ddc8fcb67ffe91900c762000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000675f4a3d
        from: '0x4A83b4413CF41C3244027e1590E35a0F48403F0c'
        gas: '0x7a120'
        maxFeePerGas: '0x59682f00'
        maxPriorityFeePerGas: '0x3b9aca00'
        nonce: '0x5'
        to: '0x4e1DCf7AD4e460CfD30791CCC4F9c8a4f820ec67'
        value: '0x0'
    SwapPlan:
      properties:
        token_in:
          type: string
          title: Token In
        token_out:
          type: string
          title: Token Out
        amount_in:
          type: string
          title: Amount In
        amount_out_quoted:
          type: string
          title: Amount Out Quoted
        amount_out_min:
          type: string
          title: Amount Out Min
          description: The floor the transaction enforces, after slippage.
        price_impact_pct:
          anyOf:
            - type: string
            - type: 'null'
          title: Price Impact Pct
        pool_fee_ppm:
          anyOf:
            - type: integer
            - type: 'null'
          title: Pool Fee Ppm
      type: object
      required:
        - token_in
        - token_out
        - amount_in
        - amount_out_quoted
        - amount_out_min
      title: SwapPlan
      description: The swap leg of an entry or a rebalance.
    PlanStep:
      properties:
        kind:
          $ref: '#/components/schemas/PlanStepKind'
        description:
          type: string
          title: Description
        contract:
          anyOf:
            - type: string
            - type: 'null'
          title: Contract
        token:
          anyOf:
            - type: string
            - type: 'null'
          title: Token
        amount:
          anyOf:
            - type: string
            - type: 'null'
          title: Amount
      type: object
      required:
        - kind
        - description
      title: PlanStep
      description: >-
        One leg of a bundled transaction, in the order it executes.


        Every transact endpoint returns a single transaction; this is what is
        inside

        it, so a caller can show a person what they are about to sign.
    Simulation:
      properties:
        ok:
          type: boolean
          title: Ok
        error:
          anyOf:
            - type: string
            - type: 'null'
          title: Error
        gas_used:
          anyOf:
            - type: integer
            - type: 'null'
          title: Gas Used
      type: object
      required:
        - ok
      title: Simulation
      description: >-
        The result of executing the transaction against current state.


        Meme tokens on this chain carry transfer taxes, blacklists and pause

        switches, none of which are visible from a pool's parameters. Simulating
        is

        the only way to find out before the owner signs.
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError
    PlanStepKind:
      type: string
      enum:
        - APPROVE
        - PERMIT2_APPROVE
        - SWAP
        - MINT
        - INCREASE
        - DECREASE
        - COLLECT
        - BURN
        - TRANSFER
      title: PlanStepKind
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: x-api-key
      description: >-
        Your Compass API Key. Get your key
        [here](https://www.compasslabs.ai/dashboard).

````