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

# Swap collateral

> Enables safe rebalancing of collateral by withdrawing a selected asset, executing CPIs (e.g. swaps or liquidity provision), and depositing a new asset in its place.



## OpenAPI

````yaml post /markets/creditbook/swap_collateral
openapi: 3.1.3
info:
  title: Loopscale API
  version: 1.0.0
servers:
  - url: https://tars.loopscale.com/v1
    description: Loopscale Production Server
security: []
paths:
  /markets/creditbook/swap_collateral:
    post:
      tags:
        - Markets
      summary: Swap collateral
      description: >-
        Enables safe rebalancing of collateral by withdrawing a selected asset,
        executing CPIs (e.g. swaps or liquidity provision), and depositing a new
        asset in its place.
      operationId: swapCollateral
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SwapCollateralRequest'
      responses:
        '200':
          description: Single versioned transaction message and signatures
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/LoanLockTransactionResponse'
        '400':
          $ref: '#/components/responses/BadRequest'
        '500':
          $ref: '#/components/responses/GeneralError'
components:
  schemas:
    SwapCollateralRequest:
      type: object
      required:
        - loan
        - withdrawCollateral
        - depositCollateral
        - cpiIxs
        - cpiLuts
        - cpiSigners
      properties:
        loan:
          type: string
          description: Loan address to modify
        withdrawCollateral:
          type: array
          items:
            $ref: '#/components/schemas/CollateralWithdraw'
          description: Amount and mint of the collateral to withdraw
        depositCollateral:
          type: array
          items:
            $ref: '#/components/schemas/CollateralDeposit'
          description: Collateral to deposit after intermediate steps
        cpiIxs:
          type: array
          items:
            $ref: '#/components/schemas/CpiInstruction'
          description: Instructions to run between withdrawal and re-deposit
        cpiLuts:
          type: array
          items:
            type: string
          description: Optional Address Lookup Tables for the transaction
        cpiSigners:
          type: array
          items:
            type: string
          description: Optional keypairs required to initialize accounts
    LoanLockTransactionResponse:
      type: object
      properties:
        transactions:
          type: array
          items:
            type: object
            required:
              - message
              - signatures
            properties:
              message:
                type: string
                description: Base64-encoded versioned transaction message
              signatures:
                type: array
                items:
                  type: object
                  required:
                    - publicKey
                    - signature
                  properties:
                    publicKey:
                      type: string
                    signature:
                      type: string
        expectedLoanInfo:
          type: object
          description: Loan post transaction state used for verification
          properties:
            loan:
              type: object
              properties:
                address:
                  type: string
                  description: Onchain loan account
    CollateralWithdraw:
      type: object
      required:
        - amount
        - collateralMint
      properties:
        amount:
          type: number
          description: Collateral to withdraw (lamports)
        collateralMint:
          type: string
          description: Mint of collateral to withdraw
    CollateralDeposit:
      type: object
      required:
        - collateralAmount
        - collateralAssetData
      properties:
        collateralAmount:
          type: number
          description: Collateral to deposit (lamports)
        collateralAssetData:
          $ref: '#/components/schemas/AssetDataInfo'
    CpiInstruction:
      type: object
      required:
        - programId
        - accounts
        - data
      properties:
        programId:
          type: string
          description: Program ID to call
        accounts:
          type: array
          items:
            type: object
            required:
              - pubkey
              - isSigner
              - isWritable
            properties:
              pubkey:
                type: string
              isSigner:
                type: boolean
              isWritable:
                type: boolean
        data:
          type: string
          description: Base64 or hex-encoded instruction data
    AssetDataInfo:
      oneOf:
        - type: object
          required:
            - Spl
          properties:
            Spl:
              $ref: '#/components/schemas/SplAssetData'
        - type: object
          required:
            - StakedSol
          properties:
            StakedSol:
              $ref: '#/components/schemas/StakedSolAssetData'
        - type: object
          required:
            - Orca
          properties:
            Orca:
              $ref: '#/components/schemas/OrcaPositionAssetData'
        - type: object
          required:
            - Meteora
          properties:
            Meteora:
              $ref: '#/components/schemas/MeteoraDlmmPositionAssetData'
        - type: object
          required:
            - Raydium
          properties:
            Raydium:
              $ref: '#/components/schemas/RaydiumPositionAssetData'
      description: >-
        Tagged asset descriptor used for collateral inputs. LP position variants
        can optionally specify `tokenProgram` to override the token program used
        when deriving token accounts.
    SplAssetData:
      type: object
      required:
        - mint
      properties:
        mint:
          type: string
          description: Collateral mint pubkey
    StakedSolAssetData:
      type: object
      required:
        - stakeAccount
        - stakePool
      properties:
        stakeAccount:
          type: string
          description: Stake account pubkey
        stakePool:
          type: string
          description: Stake pool pubkey
    OrcaPositionAssetData:
      type: object
      required:
        - positionMint
        - whirlpool
      properties:
        positionMint:
          type: string
          description: Position mint pubkey
        whirlpool:
          type: string
          description: Whirlpool pubkey
        tokenProgram:
          type: string
          description: Optional token program override for the position mint.
    MeteoraDlmmPositionAssetData:
      type: object
      required:
        - positionAddress
        - lbPair
      properties:
        positionAddress:
          type: string
          description: DLMM position address
        lbPair:
          type: string
          description: Meteora DLMM pair pubkey
        tokenProgram:
          type: string
          description: Optional token program override for the position mint.
    RaydiumPositionAssetData:
      type: object
      required:
        - mint
        - pool
      properties:
        mint:
          type: string
          description: Raydium position mint pubkey
        pool:
          type: string
          description: Raydium pool pubkey
        tokenProgram:
          type: string
          description: Optional token program override for the position mint.
  responses:
    BadRequest:
      description: Bad Request
      content:
        text/plain:
          schema:
            type: string
            example: 'Something went wrong: You have an error in your SQL syntax.'
    GeneralError:
      description: General Server Error
      content:
        text/plain:
          schema:
            type: string
            example: >-
              Failed to deserialize the JSON body into the target type: missing
              field `mints` at line 1 column 1.

````