# Computing position rewards \[Compute a wallet's earned rewards on a Conduit from on-chain data, including in-flight operations]

Value what the wallet holds, treat every unsettled operation as a claim (committed minus delivered), and net that against the assets that flowed in and out. That holds at every stage of an async deposit or redemption.

:::info
The [Railnet REST API](https://docs.api.railnet.org) computes `total_rewards_in_asset` this way. Call it if you just need the number.
:::

## Inputs

All of it comes from the [indexer GraphQL API](/developers/api).

| Input | Source |
|---|---|
| Share balance | `ConduitBalance.value` |
| Movements per operation | `ConduitQuery.flows`, from the Conduit's `Pulled` and `Pushed` events |
| Settled or in flight | The query's STEAM state; `SETTLED` and `REJECTED` are final |
| Share price, now and at creation | `convert()` and `ConduitQuery.creationRate` |
| Fee manager address | `Conduit.feeManager` |

A deposit pulls assets in and pushes shares out on settlement; a redemption pulls shares in and pushes assets out. Rejected operations push the input back, so refunds need no special case.

## The formula

```text
rewards = balance value + pending deposits + pending redemptions - assets in + assets out
```

* **balance value**: the share balance at the current price.
* **assets in**: every asset amount ever pulled from the wallet, settled or not.
* **assets out**: every asset amount ever pushed out, settled or not, except legs paid to the fee manager. Those are fees, not the user's money.
* **pending**: one claim per in-flight operation.

Without the pending terms a redemption in flight reads as a loss: the shares burn on request, the payout arrives later.

### Valuing a claim

```text
still owed = max(0, committed - delivered)
```

For a deposit, committed is the assets pulled in and delivered is the shares minted so far at the current price. For a redemption, committed is the shares burnt and delivered is the tranches paid out. Netting off what has arrived stops a tranche counting twice, and a deposit's minted shares already sit in the balance term. Clamp each claim at zero on its own, so one over-delivered operation cannot mask another's remainder.

:::warning
Never filter delivered amounts by the user's own address. Settlement pays whoever holds the operation then, and a wrapped operation may have changed hands, so those payouts never net out. Exclude the fee manager's legs only.
:::

### Price a pending redemption at the request-time rate

```text
min(share price now, share price when the redemption was requested)
```

A redemption's value crystallizes when the shares burn: an Allocation Strategy's redeem queue caps each request at its request-time price, so marking at a later, higher price shows yield the user never receives. A fall is the opposite: queues socialise losses into pending requests.

Sources pricing at settlement only delay when growth shows. A missing `creationRate` falls back to the current price.

## Worked example

A user deposits 5 USDC at a price of 1.00, then redeems everything.

| Moment | balance value | pending | assets in | assets out | rewards |
|---|---|---|---|---|---|
| Deposits 5, waiting to settle | 0 | 5.00 | 5.00 | 0 | **0** |
| Deposit settles, 5 shares minted | 5.00 | 0 | 5.00 | 0 | **0** |
| Yield accrues, price now 1.02 | 5.10 | 0 | 5.00 | 0 | **+0.10** |
| Requests full redemption, shares burnt | 0 | 5.10 | 5.00 | 0 | **+0.10** |
| Queue pays a first tranche of 3.00 | 0 | 2.10 | 5.00 | 3.00 | **+0.10** |
| Final tranche of 2.10 arrives, settled | 0 | 0 | 5.00 | 5.10 | **+0.10** |

Rewards never dip negative while the money is in flight, and never jump from double counting. Once settled, the claim is zero and the number comes from real flows alone. Had the price risen to 1.10 during the wait, the request stays stamped at 1.02 and pending at 5.10.

## Pseudo-code

One wallet, one Conduit. Amounts are minimum units; `value(shares, rate)` converts at assets per share.

```python [Pseudo-code]
balance  = ConduitBalance(owner: wallet, conduit: conduit).value
queries  = ConduitQuery(owner: wallet, conduit: conduit) {
               query { mode state }
               creationRate { assetsPerShare }
               flows { flowType asset amount account }
           }
fee_mgr  = Conduit.feeManager.address
asset    = Conduit.asset
price    = convert(1 share)                         # assets per share

assets_in, assets_out, pending = 0, 0, 0

for q in queries:
    committed_assets = committed_shares = 0
    delivered_assets = delivered_shares = 0

    for f in q.flows:
        if f.flowType == PULLED and f.account == wallet:
            if f.asset == asset:            # deposit input
                assets_in        += f.amount
                committed_assets += f.amount
            if f.asset == conduit:          # burnt shares
                committed_shares += f.amount

        if f.flowType == PUSHED and f.account != fee_mgr:
            if f.asset == asset:            # payout or refund
                assets_out       += f.amount
                delivered_assets += f.amount
            if f.asset == conduit:          # minted shares
                delivered_shares += f.amount

    if q.state in (SETTLED, REJECTED):
        continue                            # flows already tell the story

    if q.mode == DEPOSIT:
        pending += max(0, committed_assets - value(delivered_shares, price))

    if q.mode == REDEEM:
        rate = min(price, q.creationRate or price)   # request-time cap
        pending += max(0, value(committed_shares, rate) - delivered_assets)

rewards = value(balance, price) + pending - assets_in + assets_out
```
