Guides
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
ACTIVEaccount with balance in thesourceAsset.
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).
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"
}'{
"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.
| Error | When |
|---|---|
400 INVALID_DATA MISSING_AMOUNT | Neither sourceAmount nor targetAmount |
400 INVALID_DATA MUTUALLY_EXCLUSIVE_AMOUNTS | Both amounts |
400 INVALID_DATA AMOUNT_MUST_BE_POSITIVE | Zero amount |
400 INVALID_DATA INVALID_AMOUNT_PRECISION | Too many decimal places for the asset |
404 RESOURCE_NOT_FOUND | Unknown 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:
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.
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"}.
| Error | When |
|---|---|
422 QUOTE_EXPIRED | Quote expired (details.expiredAt) |
422 QUOTE_ALREADY_CONSUMED | Quote already used (details.consumedByOperationId) |
422 SAME_ASSET_SWAP | Quote between the same asset (Mimicr's choice) |
422 QUOTE_ACCOUNT_MISMATCH | Quote 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.
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 +1mand create the swap; the response is422 QUOTE_EXPIRED. - Manual settlement: turn
autoSettleOperationsoff and useoperation.settleoroperation.fail. - Waiting in a test:
npx mimicr wait trace object id=<operation-id> status=COMPLETED.
Next steps
- Withdrawal: send the converted funds out.
- Money and dates: each asset's precision.