Skip to content

fix(onchain): pay a sat-denominated on-chain request from the Cash wallet - #554

Merged
islandbitcoin merged 2 commits into
mainfrom
fix/onchain-btc-denominated-from-cash
Nov 27, 2026
Merged

islandbitcoin merged 2 commits into
mainfrom
fix/onchain-btc-denominated-from-cash

Conversation

@islandbitcoin

Copy link
Copy Markdown
Contributor

The problem

A user who scans or pastes an on-chain payment request that names its amount in bitcoin cannot pay it from the Cash wallet. The app shows "Failed to calculate fee" and "Currently do not support Bitcoin denomination.", and the confirm button stays disabled. Every merchant invoice and every POS on-chain checkout is this kind of request.

The app is doing the right thing. For a sat-denominated amount it calls onChainUsdTxFeeAsBtcDenominated and onChainUsdPaymentSendAsBtcDenominated. In this fork both were dead:

  • the send was a stub: return new UnsupportedCurrencyError("Currently do not support Bitcoin denomination."), with the old body commented out;
  • the fee query still called Wallets.getOnChainFeeForUsdWalletAndBtcAmount, the pre-IBEX path.

The dollar-denominated pair (onChainUsdTxFee, onChainUsdPaymentSend) has always worked.

The constraint

IBEX sends from a dollar wallet in dollars only: sendToAddressV2 takes "amount in the wallet's currency". Nothing in its API delivers an exact number of sats from a USD or USDT account. So the sats have to be priced in dollars before the send.

What changes

  • usdCentsToDeliverSats (src/app/payments/usd-cents-for-sats.ts): cents = ceil(sats x centsPerSat x (1 + margin)), at the mid rate. It rounds up, never down, so the recipient gets at least what was asked.
  • The margin is onChainWallet.btcDenominatedSendMarginBps, default 50 (0.5%), accepted range 0 to 1000. It covers the price moving between the quote and IBEX executing, and any gap between the mid rate and the rate IBEX converts at.
  • Fee query: prices the sats, then calls Ibex.estimateOnchainFee exactly as onChainUsdTxFee does. The estimate is for the amount the send will use.
  • Send mutation: follows onChainUsdPaymentSend step for step (cash-wallet routing, send guard, wallet amount, payOnChainByWalletId). The send guard runs first, on the sat amount the caller asked for, before the price is read.
  • No schema change. Both operations keep their names, arguments and return types, so every installed app version starts working when this deploys.

Decisions worth a reviewer's eye

  • The payer is debited slightly more than the app shows. The app converts the sats at its own price with no margin; the debit includes up to 0.5% more, plus rounding to a whole cent. The excess goes to the recipient as extra sats. It is not a fee and is not reported as one: the fee query returns IBEX's fee only.
  • The margin is unmeasured. IBEX's conversion rate on this leg is not documented in the client. 0.5% is a starting value, set in config so it can be tuned without a release. One small real payment is the plan to measure what arrives.
  • Whole cents. The dollar amount is rounded up to a whole cent even for a USDT wallet, which could carry fractions. On a very small payment that is proportionally large (1 sat costs 1 cent) and absolutely tiny.
  • Guard amount. The guard is given { currency: "BTC", sats }, so its limit check prices the sats without the margin. The difference is at most the margin.
  • Not touched: getOnChainFeeForUsdWalletAndBtcAmount stays in src/app/wallets/get-on-chain-fee.ts, now unused by this query. onchain-payment-send.ts (BTC wallet on-chain send) is still a stub.

Tests

  • usd-cents-for-sats.spec.ts: the arithmetic; never rounds down; a property check over margins, prices and amounts that the result is at least the marked-up value and less than a cent above it; the configured margin is used; invalid sat amounts refused without reading the price; a price failure passed back; unusable prices refused; span attributes.
  • onchain-btc-denominated-margin.spec.ts: default 50; filled in when a deployment sets the block without the key; not required; bounds enforced at boot.
  • onchain-usd-payment-send-as-sats.spec.ts: the marked-up amount is sent from the routed wallet; the stub's message is gone; guard first, on the sat amount, with call order pinned; guard rejection stops before the price and the payout; zero sats left to the guard; no price, no payout; routing, wallet-amount and payout failures; pending passed through; each bad input; authentication required.
  • on-chain-usd-tx-fee-query-as-sats.spec.ts: IBEX fee for the converted amount; no price, wallet failure, IBEX failure and missing fee all fail; a zero fee is zero; bad inputs; authentication required.
  • Ran locally: these four suites (56 tests) and the GraphQL, payments, prices and config unit suites (68 suites, 819 tests) pass. eslint and prettier clean on the touched files.
  • Not clean locally: two tsc errors in src/servers/graphql-server.ts and graphql-admin-server.ts, untouched here, from older local dependencies. CI is the reference.
  • Not run: a real send. Nothing here has gone through IBEX. Whether the recipient receives the full sat amount at a 0.5% margin is unverified until a real payment is measured.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Gn2VCc25BAQjEu33uLycvP

islandbitcoin and others added 2 commits November 27, 2026 12:00
…llet

An on-chain payment request that names its amount in sats could not be paid
from the Cash wallet. The app asks for the fee and the send "as BTC
denominated", and both were dead in this fork: the send was a stub that
answered "Currently do not support Bitcoin denomination." and the fee query
still called the pre-IBEX path, so the app showed "Failed to calculate fee".

IBEX sends from a dollar wallet in dollars only, so both now price the sats
at the mid rate, round UP with a margin, and use the same IBEX calls as the
dollar-denominated pair. The recipient gets at least the sats asked for,
never less. The margin is onChainWallet.btcDenominatedSendMarginBps, default
50 (0.5%).

The send runs the send guard first, on the sat amount.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Gn2VCc25BAQjEu33uLycvP
- usdCentsPerSatMidPrice gets its own spec. Every caller's spec mocks it, so
  nothing pinned its unit. It now checks cents per sat from a wallet price
  ratio ($60,000 a bitcoin is 0.06), that a price error comes back as the
  same instance, and, through the real getCurrentPriceAsWalletPriceRatio,
  that the price service's dollars per sat are scaled to cents. Dropping that
  scaling now fails the spec (0.0006 against 0.06).
- docs/send-guard.md: the span section names the three guarded on-chain
  rails, onChainUsdPaymentSendAsBtcDenominated among them, instead of
  "either on-chain rail".
- send-guard-wiring.spec.ts: the comment counts nine guarded rails and names
  onchain-usd-payment-send-as-sats.ts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Gn2VCc25BAQjEu33uLycvP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant