Skip to content

Calculate transaction fees automatically from their cost - #1027

Merged
Rigidity merged 13 commits into
mainfrom
rigidity/automatic-fees
Nov 17, 2026
Merged

Rigidity merged 13 commits into
mainfrom
rigidity/automatic-fees

Conversation

@Rigidity

@Rigidity Rigidity commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Purpose

Users are getting INVALID_FEE_TOO_CLOSE_TO_ZERO because Sage's fees aren't tied to a transaction's cost. A full mempool rejects anything under 5 mojos per cost, and from chia-blockchain 2.7.6 farmers skip transactions under 4 mojos per cost by default (chia-blockchain#21649). This PR lets Sage price a transaction from its cost, and makes that the default in the UI.

The rate is fixed at 5 mojos per cost for now. A market estimate from Coinset, with peers as a fallback, can come later behind the same endpoint.

Changes

Backend

  • Fee loop: adding a coin to pay the fee raises the cost, which raises the fee. So a transaction is rebuilt with a higher fee until the fee it pays covers its cost at the rate, usually in 2–3 attempts. The fee only grows between attempts, so the coins selected only grow, and it settles. It's the build_with_fee! macro in crates/sage/src/utils/fees.rs. It's a macro because the futures of borrowing async closures aren't Send, which the RPC server and apps bridge need.
  • What gets priced: the cost is the mempool's own calculation (spend_bundle_conditions from SDK 0.39, run once per attempt), at the current peak height so the 3.0 hard fork is accounted for. The fee never goes down between attempts, so if a later attempt costs a little less, the fee can end up slightly over the rate (about 2% in a taken-offer test). The fee is what the bundle actually pays (removals minus additions). So when taking an offer, the maker's spends and any fee they already pay count too.
  • API: every transaction request, plus take_offer, cancel_offer(s) and create_transaction, takes an optional fee_per_cost. Without it nothing changes. With it, fee is the least the transaction pays. make_offer takes pre_split_fee_per_cost for the transaction that splits its coins, which is complete on its own; the offer's own fee stays an amount, since one side of an offer can't be costed.
  • Spending a whole balance: send_xch and mint_option take deduct_fee, which takes the fee out of the amount inside the fee loop, so a whole balance can be sent or locked up when the fee isn't known ahead of time. For an option, the mojo the option itself is made of comes out of it too.
  • get_fee_rate: returns the recommended rate (5 mojos per cost) and the minimum rate (5), below which a transaction may never be confirmed. The RPC doesn't warn about a low rate: callers choose it.
  • TransactionSummary.cost: the summary now includes the transaction's cost, when it's a complete transaction.
  • SDK: bumped chia-wallet-sdk to 0.39.0 and chia crates to 0.54.

UI

  • Fee input: the fee input defaults to Auto at the recommended rate. The rate can be edited in place, and a rate below the minimum shows a warning under the input. A button next to it switches between automatic and a custom amount. Every transaction form uses it.
  • Offers and WalletConnect: an offer's own fee and WalletConnect requests keep an entered fee (allowAuto={false}). The split fee of a pre-split offer can be automatic, and follows the default fee.
  • Default fee setting: it can now be Automatic, at the recommended or a chosen rate. Automatic at the recommended rate is the new default. It's stored under a new key, and only a positive fee someone chose before is carried over: the old default of 0 is now too low.
  • Max button: for an XCH send to one recipient, or an XCH option underlying, Max switches the amount to "Balance minus fee" instead of filling in a number. The exact amount appears on the confirmation screen. An offer's Max already leaves no change, so it needs no split fee and is unchanged.
  • Confirmation dialog: shows the rate the fee pays under the fee amount. It warns when that's below the minimum, which also covers a custom amount that's too low.

Decisions to confirm

These were my calls; each is easy to change.

  • fee_per_cost is a JSON number (f64) on the requests above. An integer rate would be too coarse for market rates later.
  • get_fee_rate returns fee_per_cost (recommended) and minimum_fee_per_cost, and TransactionSummary.cost is optional (missing for one side of an offer).
  • make_offer gains pre_split_fee_per_cost, so a pre-split offer's split no longer defaults to a zero fee. The offer's own fee stays an amount.
  • UI fee values are auto, auto:<rate> or an amount, stored under a new defaultNetworkFee key; a positive old defaultFee is carried over, an old 0 isn't.
  • The confirmation dialog warns whenever a transaction's fee pays below the minimum rate, including custom amounts and dApp requests that use it.

Not in this PR

  • A market fee estimate. The rate is fixed at the minimum.
  • Warnings in the WalletConnect and Sage Apps dialogs when a dApp's fee is below the minimum.
  • The CLI's --fee option, which still takes an amount.
  • Translation catalogs need pnpm extract at the next release.

Testing

  • New RPC tests in crates/sage-rpc/src/tests/fees.rs, checking fees against a cost worked out independently of the wallet:
    • The fee covers the cost at the rate without overshooting, and the transaction confirms.
    • A second coin is selected to pay the fee and is included in the cost.
    • A taken offer counts the maker's fee; create_transaction adds a fee action only when needed.
    • An issued CAT is recorded from the priced transaction, and a pre-split offer's split is priced.
    • A higher given fee is paid as is, no rate means no change, and a fee beyond the balance or a negative rate is an error.
    • A whole balance is sent, or locked up in an option, with the fee taken out of it.
  • Frontend tests for the fee helpers and validation messages, the default fee migration, the fee input (Auto default, amount-only inputs, the low-rate warning), and the offer processor's split fee.
  • In the sandbox, sent a whole 100 TXCH balance with Max and an automatic fee (99.99969555501 sent plus 0.00030444499 fee), minted an option from a whole balance (locked amount plus fee plus 1 mojo equalled the balance), and made an offer of a whole balance with an automatic split fee (no split was needed).
  • In the sandbox, sent 1 TXCH with an automatic fee. It was priced at 0.0000519039 TXCH, and the balance fell by exactly the amount plus that fee. At a rate of 2, both warnings showed, and the fee was priced at 2 mojos per cost.
  • Unit tests for parsing and validating fee rates in src/lib/fees.test.ts.
  • cargo fmt --check, cargo clippy --workspace --all-features --all-targets -- -D warnings and cargo machete are clean, and cargo test --workspace --all-features passes (572 tests).
  • pnpm build succeeds, and pnpm generate:bindings and pnpm generate:app-docs leave the tree unchanged.
  • Two review rounds (correctness, tests, simplification); the second found nothing above minor.
  • pnpm lint, pnpm prettier:check and tsc are clean.
  • pnpm test:unit passes.

Note

Medium Risk
Every transaction endpoint now goes through the fee loop. Requests without fee_per_cost build exactly as before, but the UI now sends a rate by default, so most transactions the UI creates are rebuilt 2–3 times and pay at least 5 mojos per cost. That's limited by the new tests and the fact that the confirmation dialog shows the exact fee before anything is signed.

  • Backend: fee loop and cost rules (utils/fees.rs), fee_per_cost on transaction and offer-taking requests, pre_split_fee_per_cost on make_offer, deduct_fee on send_xch and mint_option, get_fee_rate, cost in the summary, SDK 0.39.
  • Frontend: automatic mode with an editable rate in FeeAmountInput, a whole-balance mode for Max, fee params at every transaction call site, the default fee migration, and the fee rate and low-fee warnings.

Transactions can now take a fee rate in mojos per cost. With one, the
transaction is rebuilt until its fee covers its own cost, including the
coins that pay the fee. The wallet recommends 5 mojos per cost, the
minimum for blocks and a full mempool, and the UI uses it by default.
The automatic fee's rate can be edited in the fee input, starting from
the recommended rate. A rate below the minimum is warned about there,
and the confirmation dialog warns when a transaction's fee pays less
than the minimum per cost, however it was chosen.
- Let the split fee of a pre-split offer be automatic in the UI
- Check fees against a cost worked out independently in the tests, and test a
  second fee coin, a maker's fee, create_transaction, an issued CAT's id and a
  priced split
- Share the fee schema with a balance check and the fee rates hook
- Treat an empty rate as the recommended one in one place
- Test the default fee migration, automatic fee params and the fee input
- Report an invalid fee amount's own message instead of a generic one
- Work out the cost shown in the summary the same way the fee is priced
- Reuse the JSON coin spend conversions in the fee tests, and bound the
  split fee from above
- Test that the split fee's rate reaches make_offer, and that only the split
  fee input allows an automatic fee
- Reject an invalid stored automatic fee rate
- Share the whole balance state between Send and Mint Option in a hook, which
  stops for good when the balance runs out instead of coming back by itself
- Keep Mint Option's underlying balance current as the wallet syncs
- Test that the fee can't be taken out of a CAT underlying
- Drop Mint Option's unreachable XCH max that subtracted a typed fee
@Rigidity
Rigidity enabled auto-merge November 17, 2026 22:18
@Rigidity
Rigidity merged commit d3cb5f0 into main Nov 17, 2026
9 checks passed
@Rigidity
Rigidity deleted the rigidity/automatic-fees branch November 17, 2026 22:22
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