Repository navigation
Calculate transaction fees automatically from their cost - #1027
Merged
Merged
Conversation
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
…balance can be spent
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
Users are getting
INVALID_FEE_TOO_CLOSE_TO_ZERObecause 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
build_with_fee!macro incrates/sage/src/utils/fees.rs. It's a macro because the futures of borrowing async closures aren'tSend, which the RPC server and apps bridge need.spend_bundle_conditionsfrom 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.take_offer,cancel_offer(s)andcreate_transaction, takes an optionalfee_per_cost. Without it nothing changes. With it,feeis the least the transaction pays.make_offertakespre_split_fee_per_costfor 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.send_xchandmint_optiontakededuct_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.UI
allowAuto={false}). The split fee of a pre-split offer can be automatic, and follows the default fee.Decisions to confirm
These were my calls; each is easy to change.
fee_per_costis a JSON number (f64) on the requests above. An integer rate would be too coarse for market rates later.get_fee_ratereturnsfee_per_cost(recommended) andminimum_fee_per_cost, andTransactionSummary.costis optional (missing for one side of an offer).make_offergainspre_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.auto,auto:<rate>or an amount, stored under a newdefaultNetworkFeekey; a positive olddefaultFeeis carried over, an old 0 isn't.Not in this PR
--feeoption, which still takes an amount.pnpm extractat the next release.Testing
crates/sage-rpc/src/tests/fees.rs, checking fees against a cost worked out independently of the wallet:create_transactionadds a fee action only when needed.src/lib/fees.test.ts.cargo fmt --check,cargo clippy --workspace --all-features --all-targets -- -D warningsandcargo macheteare clean, andcargo test --workspace --all-featurespasses (572 tests).pnpm buildsucceeds, andpnpm generate:bindingsandpnpm generate:app-docsleave the tree unchanged.pnpm lint,pnpm prettier:checkandtscare clean.pnpm test:unitpasses.Note
Medium Risk
Every transaction endpoint now goes through the fee loop. Requests without
fee_per_costbuild 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.utils/fees.rs),fee_per_coston transaction and offer-taking requests,pre_split_fee_per_costonmake_offer,deduct_feeonsend_xchandmint_option,get_fee_rate,costin the summary, SDK 0.39.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.