# The statement and a payout

Calls go to https://supply.coronium.ai, signed in as in [enrolment](/guides/device-enrol.md), in the folder its step 1 made. The blocks use curl and jq and keep every answer in a file only you can read. [Device states](/guides/device-states.md) says what each state means and when a payout is paid.

The statement the partner agreement names is `GET /app/api/earnings`: the lines of one month, the totals and the terms in force. The buyer's statement, `GET /app/api/billing/statement`, is another read and says nothing about a device.

## Steps

1. Read the statement.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
cd "${CORONIUM_DIR:-$HOME/coronium}" || exit
curl -sS -b coronium.cookies "$SUPPLY/app/api/earnings" -o coronium-earnings.json
jq 'if .code then {code, cause, fix} else {month, earned: .earned.usdMicros, held: .held.usdMicros, available: .available.usdMicros, requested: .requested.usdMicros, paid: .paid.usdMicros, payoutMinimumUsdMicros, terms} end' coronium-earnings.json
```

Expect: this month's totals and `terms`. For another month add `?month=YYYY-MM` to the address. If it prints a `code`, do what its `fix` names: `sign_in_required` means sign in again, as step 1 of enrolment does. Safe to repeat: yes.

What the answer holds:

- An amount is an object: `usdMicros`, whole micro-USD as a decimal string, and `n`, the exact units, of which `nPerUsdMicro` make one micro-USD.
- `lines`: one line for each `day`, `deviceId`, `country` and `shareBp`, with `bytesUp` and `bytesDown`, `buyersPaid`, `earned`, `releaseOn` and `voided`.
- `buyersPaid` is what buyers paid for the traffic that device carried that day, and `shareBp` is the share of it in basis points: `earned` and `voided` together are `buyersPaid` times `shareBp` over `10000`. The share applies to nothing else. `voided` is the part Coronium voided while it was still held, which is never paid. Free grants, unpaid traffic and an account's own traffic earn nothing, and so do Coronium's own exit measurements.
- `releaseOn` is the day a line's amount leaves `held` and joins `available`: `terms.holdDays` after the day the ledger recorded it.
- `earned`, `held`, `available`, `requested` and `paid` are the totals: everything earned, the part still held, the part a payout may ask for, the part asked for and waiting, and the part paid.
- `terms.standing` is `new`, `established` or `operator`, and sets `holdDays` and `shareBp`. `terms.deviceLimit` is how many devices the account may hold that are not withdrawn, or null without a limit.
- `payoutMinimumUsdMicros` is the least a payout may ask for.

<!-- facts:begin part.earns -->
While `terms.charging` of `GET /app/api/earnings` is false, buyers are not charged, `terms.shareBp` is null and traffic earns nothing.
<!-- facts:end part.earns -->

2. Work out what one GB would earn, from the two reads that state it. Derived, and an estimate only: the statement's `earned` is the amount.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
cd "${CORONIUM_DIR:-$HOME/coronium}" || exit
curl -sS "$SUPPLY/app/api/prices" -o coronium-prices.json
jq -r --slurpfile e coronium-earnings.json '($e[0].terms.shareBp // 0) as $share | .tiers[] | ((.usdMicrosPerGb | tonumber) * $share / 10000 | floor) as $earns | "tier \(.tier) \(.name): buyers pay \(.usdMicrosPerGb) micro-USD a GB; at shareBp \($share) a device earns \($earns) micro-USD a GB"' coronium-prices.json
```

Expect: one line for each price tier. A buyer pays the price of the tier of the exit country, which `countries` of each tier lists, so a device earns `shareBp` of that price for each GB of paid buyer traffic it carried. A device that is not `admitted` or `established` carries none, whatever the line reads. Safe to repeat: yes.

3. Read the devices the statement counts.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
cd "${CORONIUM_DIR:-$HOME/coronium}" || exit
curl -sS -b coronium.cookies "$SUPPLY/app/api/devices?limit=50" -o coronium-devices.json
jq -c 'if .code then {code, cause, fix} else .devices[] | {deviceId, status, state, reason, requests, bytesUp, bytesDown} end' coronium-devices.json
```

Expect: one line for each device. `requests`, `bytesUp` and `bytesDown` include Coronium's own exit measurements, which earn nothing. A longer list answers `nextCursor`: send it back as `cursor`. `counts` holds the devices by state. Safe to repeat: yes.

4. Read the payout requests of the account.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
cd "${CORONIUM_DIR:-$HOME/coronium}" || exit
curl -sS -b coronium.cookies "$SUPPLY/app/api/payouts" | jq -c 'if .code then {code, cause, fix} else .payouts[] | {payoutId, usdMicros, payTo, state, requestedAt, decidedAt, reason} end'
```

Expect: nothing for an account that never asked, else one line for each request, newest first. Safe to repeat: yes.

5. Ask for a payout. Only your person decides this: the amount, the asset, the network and the address are theirs, and a payment to a wrong address is not taken back. They write `coronium-payout.json` as `{"usdMicros": "<the amount>", "asset": "USDC", "network": "<the network of the address>", "address": "<the address>"}`. `asset` is `USDC` or `USDT`. The amount is whole cents, a multiple of `10000`, at least `payoutMinimumUsdMicros` and at most `available.usdMicros` of step 1.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
cd "${CORONIUM_DIR:-$HOME/coronium}" || exit
[ -s coronium-payout-request.json ] || { jq -c --arg id "$(od -An -tx1 -N8 /dev/urandom | tr -d ' \n')" --slurpfile a coronium-account.json '. + {requestId: $id, proof: {code: $a[0].accessCode}}' coronium-payout.json > coronium-payout-request.tmp && mv coronium-payout-request.tmp coronium-payout-request.json || exit; }
curl -sS -b coronium.cookies -H 'Content-Type: application/json' -d @- "$SUPPLY/app/api/payouts" < coronium-payout-request.json | jq '{outcome, usdMicros, state, payTo, code, cause, fix}'
```

Expect: `state` `requested` and `payTo`, the one line the operator pays from. `coronium-payout-request.json` holds the access code: delete it once the answer reads `requested`. If refused, do what `fix` names: `proof_required` (the access code in `coronium-account.json` is not the current one), `not_whole_cents`, `below_minimum` or `nothing_available` (correct the amount in `coronium-payout.json`, delete `coronium-payout-request.json`, repeat), `payout_pending` (one request waits at a time: read step 4), `buyer_in_debt` (the account owes for traffic it sent as a buyer: your person adds funds on the buyer site), `amount_changed` (this `requestId` was sent with other values: delete `coronium-payout-request.json` for a new request). Safe to repeat: yes, with the same file; one `requestId` asks once.
