# Pay with gamma

Green Compute takes subnet 110 **gamma**, following Bittensor's [Subnet services standard](https://www.bittensor.com/docs/guides/services). Lium (SN51), engy (SN53) and SayGM (SN28) take gamma the same way. Gamma is a dollar credit minted by burning a subnet's alpha: every gamma you spend here is $1 of SN110 alpha that was bought and burned when you minted it.

You need no account, email or API key. A hotkey that has spent gamma at our service is an account, and it signs its own requests.

> **Status:** gamma payments start when gamma is switched on for subnet 110 and our service is listed on chain. Check `GET https://api.green-compute.com/platform/gamma`: it says `"accepting": true` once both are done.

## How it works

1. **Make a hotkey for it.** This hotkey becomes your account here. Use btcli's default key type (sr25519): a hashed hotkey can't sign requests or sign in here.

   ```bash
   btcli wallet new-hotkey -H API_KEY
   ```

2. **Mint gamma into it.** Minting buys SN110 alpha with TAO and burns it, and you get 1 gamma for each dollar burned.

   ```bash
   btcli gamma mint --netuid 110 --hotkey API_KEY --amount-tao 1
   ```

3. **Spend it at our service hotkey.** Spending has no fee. The service hotkey is in `GET /platform/gamma` and in our [services.json](https://api.green-compute.com/gamma/services.json), and `btcli query services --netuid 110` lists it from the chain.

   ```bash
   btcli gamma spend --netuid 110 --service-hotkey 5CwckYmAV3fET8V9KTKfqds6BnRGACAgofECQqdYfa2Q9rvp --amount-gamma 5 -H API_KEY
   ```

4. **Wait about a minute.** Once the spend is finalized, the hotkey's account is credited **$1 per gamma**. Check it with:

   ```bash
   curl "https://api.green-compute.com/gamma/balance?hotkey=<your hotkey ss58>"
   ```

   ```json
   {"hotkey": "5F…", "credited_usd": "5.00", "used_usd": "1.20", "remaining_usd": "3.80", "as_of_block": 9577120}
   ```

   `credited_usd` is public, because the chain shows your spends to anyone. `used_usd` and `remaining_usd` are shown when the account holds nothing but that hotkey's gamma, or when the hotkey signs the request itself. Otherwise they are `null`, so nobody can read an account's balance from a wallet address.

5. **Use it.** Sign each request with the hotkey ([below](#signed-requests-btauth1)), or [sign in with the hotkey](#sign-in-with-the-hotkey) to get an API key.

Rules the chain sets: a spend is at least 0.1 gamma unless it empties the deposit, and the coldkey that funded the deposit can cap what the hotkey spends per day (`btcli gamma set-cap`). We never send a transaction: we read what your hotkey has spent at our service hotkey from the chain, at the finalized head. Spent gamma is burned, so it can't be sent back.

## Signed requests (btauth/1)

Requests signed with the hotkey that spent use Bittensor's [signed request format](https://www.bittensor.com/docs/guides/signed-requests). They act as that hotkey's account, with no API key. Send these headers:

| Header | Value |
|---|---|
| `X-Bittensor-Version` | `1` |
| `X-Bittensor-Hotkey` | your hotkey (ss58) |
| `X-Bittensor-Receiver` | our service hotkey, `5CwckYmAV3fET8V9KTKfqds6BnRGACAgofECQqdYfa2Q9rvp` |
| `X-Bittensor-Nonce` | the current unix time in nanoseconds, new for every request |
| `X-Bittensor-Signature` | `0x` and the hex sr25519 signature |

The signature is over these eight lines, joined with `\n` and with no newline at the end: `btauth/1`, `sr25519`, the method in capitals, the path with its `?query` exactly as sent, the hex SHA-256 of the exact body bytes, the nonce, your hotkey and our service hotkey. A nonce is accepted from 10 seconds old to 2 seconds ahead, and only once. Sign every attempt, retries too. Only sr25519 hotkeys (what `btcli` makes) are accepted, so leave out `X-Bittensor-Crypto`.

When these headers are present, any `Authorization` header is ignored, so the OpenAI SDK can send a placeholder key:

```python
import hashlib, time
import bittensor as bt                      # or any sr25519 keypair, e.g. substrateinterface.Keypair
import httpx
from openai import OpenAI

SERVICE = "5CwckYmAV3fET8V9KTKfqds6BnRGACAgofECQqdYfa2Q9rvp"
hotkey = bt.Wallet(name="my_coldkey", hotkey="API_KEY").hotkey

def sign(request: httpx.Request) -> None:
    body = request.read()
    nonce = str(time.time_ns())
    payload = "\n".join([
        "btauth/1", "sr25519", request.method, request.url.raw_path.decode(),
        hashlib.sha256(body).hexdigest(), nonce, hotkey.ss58_address, SERVICE,
    ]).encode()
    request.headers.update({
        "X-Bittensor-Version": "1",
        "X-Bittensor-Hotkey": hotkey.ss58_address,
        "X-Bittensor-Receiver": SERVICE,
        "X-Bittensor-Nonce": nonce,
        "X-Bittensor-Signature": "0x" + hotkey.sign(payload).hex(),
    })

http = httpx.Client(event_hooks={"request": [sign]})
client = OpenAI(base_url="https://api.green-compute.com/v1", api_key="unused", http_client=http)
print(http.get("https://api.green-compute.com/platform/billing/balance").json())
```

On bittensor 11, `bt.http_auth.sign(wallet, method=..., path=..., body=..., receiver_ss58=SERVICE)` returns the same headers.

A signed request can **rent GPUs, call models and read the account**: the `rentals`, `inference` and `read` scopes. It can't add credit by card or crypto, change account limits or manage API keys. For those, [sign in with the hotkey](#sign-in-with-the-hotkey) and use the key you get.

If a signed request is refused, the reply's `error.code` says why:

| Code | Status | Meaning |
|---|---|---|
| `signed_request_invalid` | 401 | A check failed. `detail.reason` is `malformed`, `wrong_receiver`, `stale`, `bad_signature`, `replayed` or `unsupported_crypto`. |
| `gamma_payment_required` | 402 | The hotkey has no account yet. Spend gamma at our service hotkey first. `detail.pay` has the commands. |
| `wallet_sign_in_off` | 403 | The hotkey is linked to an account for payments only. See [Pay into an account you already have](#pay-into-an-account-you-already-have). |
| `scope_required` | 403 | Signed requests can't do this. Sign in with the hotkey for an API key. |

## Sign in with the hotkey

A hotkey with an account signs in like any [Bittensor wallet](https://www.green-compute.com/docs/agents#sign-in-with-a-wallet), and gets an API key:

1. `POST /platform/auth/wallet/challenge` with `{"address": "<your hotkey>"}`.
2. Sign the `message` with the hotkey: `btcli wallet sign --wallet-name my_coldkey --wallet-hotkey API_KEY --use-hotkey --message "<message>"`.
3. `POST /platform/auth/wallet/verify` with the `nonce` and the `signature`. You get an API key with full access to the account.

## Pay into an account you already have

To have gamma credited to the account you already use, verify the hotkey **before it first spends**. Do it on the [Billing](https://www.green-compute.com/billing) page under **Verified wallets**, signing the message with `btcli wallet sign --use-hotkey`, or [over the API](https://www.green-compute.com/docs/agents#option-a-verify-your-wallet-once-then-just-pay). From then on, gamma that hotkey spends at our service is added to your balance. To also sign requests with the hotkey, turn on **Sign in with this wallet** for it.

A hotkey that spends before it is verified gets an account of its own: a wallet can belong to one account only.

## Good to know

- New wallet accounts have [first-week limits](https://www.green-compute.com/docs/agents#sign-in-with-a-wallet): 2 rentals at once and $50 of spend per day. They lift once $20 has been paid in crypto, and gamma counts.
- We check every hotkey that pays against public sanctions lists, and the coldkey that funded its deposit while the deposit is open. Gamma from a listed wallet isn't credited.
- Totals at our service are public: `GET https://api.green-compute.com/gamma/spent`. The source of truth is the chain's `GammaSpent` at our service hotkey, which anyone can read.
- Machine-readable: [services.json](https://api.green-compute.com/gamma/services.json), the `btauth` security scheme in [openapi.json](https://api.green-compute.com/openapi.json), and `GET /platform/gamma`.
