# Using off-chain data

This page shows how to enrich your Mimic functions with **off‑chain data**. You’ll learn three common patterns:

1. **Pricing** convert token balances to USD
2. **Discovery of relevant tokens** for a user across a chain and bulk‑transfer them
3. **Custom subgraph queries** (e.g., fetch a Uniswap pool price)

We’ll walk through each pattern, the inputs they require, and implementation details you should keep in mind (precision, slippage, and fees).

> Pre-reqs: You’ve already read the basic function guide and can `mimic codegen`, `mimic compile`, and `mimic deploy`.

***

### Use price feeds to act on a USD threshold

**Goal:** Top up a recipient if their token balance (in USD) falls below a threshold.

{% @github-files/github-code-block url="<https://github.com/mimic-protocol/examples/blob/main/examples/04-transfer-balance-threshold-with-oracles/src/function.ts>" visible="false" %}

[Github link](https://github.com/mimic-protocol/examples/blob/main/examples/04-transfer-balance-threshold-with-oracles/src/function.ts)

**Notes**

* Convert all human‑readable decimals using `BigInt.fromStringDecimal(value, decimals)` to avoid precision loss.
* `maxFee` is specified in token units here; pass a USD‑denominated cap instead by using `TokenAmount.fromStringDecimal(DenominationToken.USD(), ...)` (see next example).

***

### Find relevant tokens and send them in one go

**Goal**: Detect which tokens a user actually holds on a chain and transfer any non‑zero balances to a recipient, paying a single USD‑capped fee.

{% @github-files/github-code-block url="<https://github.com/mimic-protocol/examples/blob/main/examples/08-relevant-tokens-query/src/function.ts>" visible="false" %}

[Github link](https://github.com/mimic-protocol/examples/blob/main/examples/08-relevant-tokens-query/src/function.ts)

**Notes**

* `getRelevantTokens` helps you focus on balances that matter. Supply an allow‑list if you want strict control over which tokens are considered.
* The fee cap is set in **USD** via `DenominationToken.USD()`; the runtime handles conversion.

***

### Query a subgraph for price and swap with slippage

**Goal**: Fetch a Uniswap pool price from a subgraph, compute expected output, apply slippage in BPS, and submit a swap intent.

{% @github-files/github-code-block url="<https://github.com/mimic-protocol/examples/blob/main/examples/09-subgraph-query/src/function.ts>" visible="false" %}

[Github link](https://github.com/mimic-protocol/examples/blob/main/examples/09-subgraph-query/src/function.ts)

**Why `PRICE_PRECISION = 40`?**

* Subgraph prices are strings with decimal precision. We parse them into a big integer using a high fixed precision (40) to minimize rounding error before scaling to token decimals. Match this with your downstream math so `upscale/downscale` lands on correct integer units.

**Slippage in BPS**

* `slippageBps = 50` → 0.50% buffer. We compute `minAmountOut = expectedOut * (1 − bps/10_000)`.

**Edge cases**

* Ensure the pool exists (`data.pools.length > 0`).
* Consider stable pools whose price may deviate
* Handle tokens with non‑standard decimals (e.g., 6, 8).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mimic.fi/examples/using-off-chain-data.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
