Guides
Journeys

Swap

Convert balance between BRL, USDT and USDC within the same account using a quote.

A swap converts balance between assets of the same account, for example BRL to USDT or USDC to BRL. Every swap uses a quote, which locks the rate. Once executed, a swap cannot be reversed.

Prerequisites

  • An ACTIVE account with balance in the sourceAsset.

1. Create the quote

Send the source asset, the target asset and one of the amounts: how much you want to spend (sourceAmount) or how much you want to receive (targetAmount).

bash
curl --request POST \
  --url https://trace.mimicr.dev/v1/quotes \
  --header 'Authorization: Bearer <token>' \
  --header 'X-Idempotency-Key: <uuid>' \
  --header 'Content-Type: application/json' \
  --data '{
    "accountId": "<account-id>",
    "sourceAsset": "BRL",
    "targetAsset": "USDT",
    "sourceAmount": "100.00"
  }'
json
{
  "id": "6441857b-d9cf-4596-837d-5f5f64bbae5e",
  "sourceAmount": { "value": "100.00", "asset": "BRL", "decimals": 2 },
  "targetAmount": { "value": "20.000000", "asset": "USDT", "decimals": 6 },
  "effectiveRate": { "base": "BRL", "quote": "USDT", "value": "0.20" },
  "expiresAt": "2026-11-05T20:12:02Z"
}

effectiveRate.value is how many quote (target) units you get per base (source) unit. The quote expires in 30 seconds and serves a single operation.

ErrorWhen
400 INVALID_DATA MISSING_AMOUNTNeither sourceAmount nor targetAmount
400 INVALID_DATA MUTUALLY_EXCLUSIVE_AMOUNTSBoth amounts
400 INVALID_DATA AMOUNT_MUST_BE_POSITIVEZero amount
400 INVALID_DATA INVALID_AMOUNT_PRECISIONToo many decimal places for the asset
404 RESOURCE_NOT_FOUNDUnknown account

Rates in Mimicr

Trace does not publish sandbox rates. In Mimicr, 1 USDT and 1 USDC are worth 5.00 BRL by default and USDT ↔ USDC is 1:1. The derived side is rounded down to the asset's precision, fees is empty and the rate is printed with 2 to 8 decimals. To change the price:

bash
curl --request PUT \
  --url https://api.mimicr.dev/v1/trace/rates \
  --header 'Authorization: Bearer mk_...' \
  --header 'Content-Type: application/json' \
  --data '{ "USDT": "5.37", "USDC": "5.35" }'

Reference: Create a quote.

2. Create the swap

Assets and amounts come from the quote, so the account and the quote are all you need.

bash
curl --request POST \
  --url https://trace.mimicr.dev/v1/operations/swaps \
  --header 'Authorization: Bearer <token>' \
  --header 'X-Idempotency-Key: <uuid>' \
  --header 'Content-Type: application/json' \
  --data '{
    "externalId": "swap-1",
    "accountId": "<account-id>",
    "quoteId": "<quote-id>"
  }'

The response is 201 with the operation in REQUESTED and intent: {"type": "SWAP"}.

ErrorWhen
422 QUOTE_EXPIREDQuote expired (details.expiredAt)
422 QUOTE_ALREADY_CONSUMEDQuote already used (details.consumedByOperationId)
422 SAME_ASSET_SWAPQuote between the same asset (Mimicr's choice)
422 QUOTE_ACCOUNT_MISMATCHQuote from another account (Mimicr's choice)

Reference: Create a swap.

3. Track the swap

The lifecycle is the same as a withdrawal: about 1 s later PROCESSING (with a balance check; a short balance gives FAILED with INSUFFICIENT_BALANCE) and about 3 s later COMPLETED, with a DEBIT transaction of the source asset and a CREDIT of the target asset.

bash
curl --url https://trace.mimicr.dev/v1/operations/<operation-id> \
  --header 'Authorization: Bearer <token>'

Swaps only move forward. Check the quote's effectiveRate before creating the swap.

Test it

  • Expired quote without waiting: create the quote, run npx mimicr clock trace +1m and create the swap; the response is 422 QUOTE_EXPIRED.
  • Manual settlement: turn autoSettleOperations off and use operation.settle or operation.fail.
  • Waiting in a test: npx mimicr wait trace object id=<operation-id> status=COMPLETED.

Next steps