Skip to content

feat(platform)!: let the contract owner pay the gas of token-paid document actions - #4826

Merged
QuantumExplorer merged 5 commits into
v4.2-devfrom
feat/document-gas-paid-by-contract-owner
Sep 19, 2026
Merged

QuantumExplorer merged 5 commits into
v4.2-devfrom
feat/document-gas-paid-by-contract-owner

Conversation

@QuantumExplorer

@QuantumExplorer QuantumExplorer commented Sep 18, 2026 •

Copy link
Copy Markdown
Member

Issue being fixed or feature implemented

A document type's token cost has carried gasFeesPaidBy (0 DocumentOwner, 1 ContractOwner, 2 PreferContractOwner) since 2.0, and every transition's $tokenPaymentInfo carries the same field, but nothing in the state machine ever read them: the action transformer copied the transition's value onto the action, no production code read it afterwards, and the execution event always debited the signer. So an app could not offer "post for free with the token we gave you", which is what the Yappr free-usage design needs (the optional token cost that lets a user fall back to paying credits comes in a follow-up PR).

This PR makes the field do what it says, from protocol version 14 (unreleased, so the current v14 modules are extended in place).

What was done?

Semantics (GasFeesPaidBy::resolve(offered_by_contract, requested) in rs-dpp): the document owner can always opt out, a preference is accepted by every offer (a document type that offers nothing just leaves the user paying), and an insistence (ContractOwner) is honoured only by a document type that commits to paying. Only a token-paid action can be sponsored, so every sponsored transition is backed by a token the contract owner chose to hand out. PreferContractOwner on the contract side means "I pay when I can, but I refuse to be the reason a transition fails", so it rejects an insisting user.

Pipeline:

  • rs-drive: the base action records the contract's offer next to the user's request; BatchTransitionAction::resolve_gas_payer() names one payer for the whole batch (mixed or two-sponsor batches are refused; latent under the current cap of one transition per batch); ResolvedGasSponsor holds the sponsor's balance.
  • Batch transform v2 resolves the sponsor and reads their balance into the action, billed to the batch. The signer is never their own sponsor.
  • Batch advanced structure v1 refuses a request the document type does not offer (GasFeesPaidByNotAllowedError, 40129) or a batch naming two payers (InconsistentGasFeesPaidByInBatchError, 40130), as paid rejections.
  • The Paid execution event carries gas_sponsor. validate_fees_of_event v1 judges the fee against the sponsor's balance while the signer only funds removed_balance; a sponsor that falls short refuses an insisting batch unpaid (GasSponsorInsufficientBalanceError, 40222) and hands a preferring batch back to the signer's balance. execute_event v1 charges whoever was admitted, on the same estimate, and deducts nothing of a sponsored fee from a budgeted signing key.
  • A failed batch is never sponsored: the sponsor is dropped from the event when validation replaced a transition by a nonce bump, and again when execution carries consensus errors, so the signer pays for the work that ran.
  • The signer's minimum balance pre-check runs before the contracts are loaded; its new v1 (identity_minimum_balance_pre_check, PV14 tables) asks a batch that requests sponsorship for its principal only, so an identity with zero credits gets through check tx on tokens it was given.
  • Such a signer could not pay for a failed batch, and a failed batch is never sponsored. Check tx therefore validates the state of a sponsored batch whose signer is under the fee minimum in full (relies_on_gas_sponsor_to_pay), on the first check and on every recheck, as it already does for masternode votes, so a transition nobody could be charged for never reaches a proposer, and leaves the mempool once the tokens it counted on are spent (mempool policy, no consensus rule). What remains is the race every balance check has: two transitions admitted against the same committed state that spend the same tokens in one block.

Known property: storage refunds still go to the document's owner, so a sponsored document refunds its owner when it is deleted or replaced by a smaller one. Each token the sponsor hands out is worth up to the storage fee of the largest document the type allows, so a sponsored document type should bound its documents' size. Documented in the book.

Docs: book/src/fees/overview.md (new subsection), the v14 version doc, the meta-schema description of gasFeesPaidBy, and the enum and field docs. wasm-dpp maps the three new errors; wasm-dpp2 is code-based and needs nothing.

How Has This Been Tested?

All run locally on this branch (macOS, cargo test debug profile):

  • cargo test -p dpp --lib -- gas_fees_paid_by state_error_discriminants: 10 passed (the resolve table; the frozen StateError discriminants now pin 111 to 113, after the once-per-identity distribution error of feat(platform)!: once-per-identity token distribution #4827).
  • cargo test -p drive --lib -- state_transition_action::batch: 662 passed, 5 new: the resolver on an empty and an unsponsored batch, the strictness the batch asked for, a refused insistence (40129), a batch naming two payers (40130), a nonce bump that names no payer of its own.
  • cargo test -p drive-abci --lib -- gas_sponsorship: 15 new end-to-end tests passed, each asserting credits and gold balances of both identities:
    • a user with zero credits creates a card; the contract owner pays the exact fee, the user nothing, the token moves as before;
    • the same creation is admitted by check tx (FirstTimeCheck), while the unsponsored one from the same user is refused by the pre-check;
    • an insistence the contract owner cannot honour is refused unpaid (40222) both in check tx and in a block, nothing created;
    • a preference falls back to the user when the contract owner cannot pay;
    • a preferring contract owner who can pay is charged;
    • an insistence the document type does not offer (DocumentOwner and PreferContractOwner offers) is a paid rejection (40129) that the user pays for, no token moves;
    • the user can opt out of an offer and pay themselves;
    • a sponsored creation that fails state validation (too few tokens) is paid for by the user, not the sponsor;
    • the same failing creation from a user with zero credits is refused by check tx (40700) instead of reaching a block, where nobody could be charged for it;
    • a recheck keeps an unfunded user's sponsored creation, and drops it (40700) once the gold it counted on is spent, while a user who can pay for their own failure is rechecked as before;
    • an unfunded user's sponsored replacement with a wrong revision is refused by the first check and by a recheck (40106), and the right revision is admitted by both: check tx stays in its own validation mode (CheckTx, RecheckTx) and asks the batch transformer for state validation explicitly, since the mode names where validation runs, not how deep it goes;
    • a contract owner who insists on their own contract pays as the signer;
    • a budgeted signing key is charged nothing of a fee the sponsor paid;
    • at protocol version 13 the field is ignored and the signer pays.
  • cargo test -p drive-abci --lib -- batch::tests check_tx validate_fees_of_event execute_event execution_event identity_balance masternode_vote: 600 passed, 9 ignored (key limits, token-paid creation, contract-bound keys, fee validation and execution suites unchanged).
  • cargo clippy -p dpp -p drive -p drive-abci -p platform-version --all-features --all-targets -- -D warnings: clean. cargo check -p wasm-dpp: clean. cargo fmt applied.

Breaking Changes

Consensus: from protocol version 14 a token-paid document action can be charged to the contract owner, a request the document type does not offer is rejected, and the batch pre-check admits an unfunded signer whose batch asks for sponsorship. Wire format unchanged.

The field has been accepted since 2.0, so a contract already on chain whose token cost says gasFeesPaidBy: 1 or 2 starts paying for the transitions that ask for it when protocol version 14 activates.

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have added "!" to the title and described breaking changes in the corresponding section if my code contains any
  • I have made corresponding changes to the documentation if needed

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added protocol version 14 support for contract-owner gas sponsorship on token-paid document actions.
    • Gas payer selection supports document owners, contract owners, and preference-based fallback.
    • Sponsored batches can reduce the signer’s required minimum balance, while failed batches remain charged to the signer.
  • Bug Fixes
    • Added validation for unsupported payer requests, inconsistent batch payers, and insufficient sponsor balances.
  • Documentation
    • Expanded fee and sponsorship documentation, including payer rules and transition behavior.

Loading
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.

2 participants