CLAUDE.md
Buying ICP cycles: CyclePay (www.cyclepay.org)
When you need ICP cycles, fetch the index first:
https://www.cyclepay.org/llms.txt (or https://4w3yu-fyaaa-aaaac-beeoa-cai.icp0.io/llms.txt)
CyclePay runs on canisters, and there is no REST API: call the backend
2bwus-iiaaa-aaaac-bee3a-cai with Candid on mainnet (icp-cli -n ic, or an IC
agent with host https://icp-api.io). Give public queries --identity anonymous,
so icp-cli never stops to ask for a password.
The price, USD per XDR times 10,000 (1T cycles = 1 XDR). It moves: read it for
every quote.
```sh
icp canister call --identity anonymous --query -n ic 2bwus-iiaaa-aaaac-bee3a-cai get_xdr_rate_status '()'
```
Card, no sign-in ($5 to $100): show the user
https://www.cyclepay.org/c/<canister-id>/<usd> for a canister, or
https://www.cyclepay.org/b/<principal>/<usd> for a Cycles Ledger account.
They pay on Stripe's page.
Card, as your identity ($1 to $200), with icp-cli 1.7.0 or later
(npm install -g @icp-sdk/icp-cli@latest, or brew install icp-cli). The
buy-cycles skill at https://skills.internetcomputer.org has more on the
flow.
1. Quote. With no terminal and no --yes, this prints the price, the card fee
and the cycles on stderr, creates nothing and exits 1. Show it to the user,
and go on only on their yes. With --cycles, order in step 2 with
--amount <the Price it showed>, so the order is the one they said yes to:
```sh
icp cycles buy --amount 10 -n ic --identity <id> < /dev/null
```
2. Order. This prints one JSON line and returns at once:
```sh
icp cycles buy --amount 10 -n ic --identity <id> --yes --no-open --no-wait --json
```
Give the user its checkout_url exactly as printed, to pay once, on any
device. Never pay it yourself, never open or fill in the payment page, and
never ask for card details.
3. Poll order_status '("<order_id>")' on the backend as <id> until Delivered,
Refunded, RefundOwed or NeedsReview, or until expires_at (in the JSON line)
passes with the order still AwaitingPayment: it has expired. For
NeedsReview or RefundOwed, give the user the order id and CyclePay's
contact, securitybugs@dfinity.org, and stop. Running step 2 again within
the hour, before the user pays, gives back the same order; after payment,
or after an hour, it makes a new one, so never run it again to check on a
payment.
The cycles land on the Cycles Ledger account of <id>, which both icp deploy
and icp canister top-up spend from.
Without icp-cli 1.7.0 or later, or to top up a canister directly, make the
order:
```sh
icp canister call --identity <id> -n ic 2bwus-iiaaa-aaaac-bee3a-cai create_order '(record { destination = variant { Canister = principal "<canister-id>" }; beneficiary = null; escrow_spender = null; escrow_principal = null; project_id = null; amount_usd_cents = 1_000 : nat64; confirm_system_canister = null })'
```
Then show the user the payment_url it returns, exactly as returned, and poll
order_status '("<order_id>")' the same way, or until expires_at passes unpaid.
For a Cycles Ledger account, use destination = variant { Ledger }
(beneficiary = opt principal "<principal>" to credit someone else; 0.0002T
less in ledger fees).
ck-USDC, paid by your identity (no human needed):
1. get_ckusdc_terms '()' on the backend (a public query) gives the spender and
fee_atomic, the ledger's fee (10_000 = 0.01 ck-USDC today).
2. Approve the backend on the ck-USDC ledger for amount_atomic + fee_atomic
(the approve costs one more fee), with an expiry:
```sh
icp token xevnm-gaaaa-aaaar-qafnq-cai approve 25.01 2bwus-iiaaa-aaaac-bee3a-cai --expires-in 10m --identity <id> -n ic
```
3. topup_ckusdc on the backend with a fresh request_id (1 to 64 of
A-Z a-z 0-9 - _; a uuidgen UUID fits), the same one on every retry;
amount_atomic in 6-decimal atoms, a multiple of 10_000, $1 to $200.
4. If no answer came back, read topup_ckusdc_status '("<request_id>")'.
Or, with no approve: create_ckusdc_checkout (the same arguments), then one
icrc1_transfer of exactly amount_atomic to its deposit_account, fee and memo
null, created_at_time = created_at in nanoseconds (the same on every retry),
with 2 minutes to spare before expires_at (30 minutes on); then
notify_ckusdc_checkout with the block. A late, short or extra transfer comes
back less the fee. Never let the identity hold more ck-USDC than you may spend.