Skip to content

Add m013 Value-Based Fee Routing mechanism spec#23

Open
CShear wants to merge 2 commits intoregen-network:mainfrom
CShear:feat/m013-fee-routing
Open

Add m013 Value-Based Fee Routing mechanism spec#23
CShear wants to merge 2 commits intoregen-network:mainfrom
CShear:feat/m013-fee-routing

Conversation

@CShear
Copy link
Contributor

@CShear CShear commented Feb 18, 2026

Summary

  • Adds full mechanism spec for m013 (Value-Based Fee Routing) — replaces flat gas fees with value-proportional fees on credit transactions
  • Fee schedule: Credit Issuance 2%, Transfer 0.1%, Retirement 0.5%, Marketplace Trade 1%
  • 4-pool distribution (Model A): Burn 30%, Validator Fund 40%, Community Pool 25%, Agent Infrastructure 5%
  • Min fee floor (1 REGEN) prevents zero-fee transactions on low-value credits
  • State machine: FLAT_GAS → TRANSITION (dual-fee) → VALUE_BASED
  • Includes SPEC.md (13 sections), JSON schemas (fee event, fee config, KPI), reference implementation with deterministic self-test, replay datasets
  • Documents open questions OQ-M013-1 through OQ-M013-5 (distribution model, burn debate, fee denomination)
  • Updates verify.mjs with m013 validation and MECHANISM_CONSUMERS.md with consumer mappings
  • Foundation mechanism — M012, M014, and M015 all depend on M013 fee revenue

Test plan

  • node mechanisms/m013-value-based-fee-routing/reference-impl/m013_fee.js — self-test PASS (5 vectors, fee conservation verified)
  • npm run verify — PASS
  • m010 regression — PASS
  • Review fee rates against phase-2/2.6 source material
  • Validate distribution model against WG consensus

🤖 Generated with Claude Code

Adds full mechanism spec for m013 following the established pattern:
- SPEC.md with 13 sections covering value-proportional fees on credit
  transactions, 4-pool distribution (burn/validator/community/agent),
  and FLAT_GAS → TRANSITION → VALUE_BASED state machine
- JSON schemas for fee events, fee config, and KPI metrics
- Reference implementation (m013_fee.js, m013_kpi.js) with deterministic
  self-test covering min_fee floor clamping and fee conservation
- Fee schedule: Issuance 2%, Transfer 0.1%, Retirement 0.5%, Marketplace 1%
- Distribution (Model A): Burn 30%, Validator 40%, Community 25%, Agent 5%
- Updates verify.mjs with m013 validation checks
- Updates MECHANISM_CONSUMERS.md with m013 consumer mappings

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @CShear, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request integrates the m013 Value-Based Fee Routing mechanism, fundamentally altering how fees are calculated and distributed for ecological credit transactions. It shifts from a flat gas model to a value-proportional system, ensuring protocol revenue scales with the value facilitated. The change establishes a structured approach to funding critical network operations and incentives through a multi-pool distribution model, supported by detailed specifications, schemas, and a reference implementation for validation.

Highlights

  • Value-Based Fee Routing (m013) Introduction: This pull request introduces the full mechanism specification for m013, which replaces flat gas fees with value-proportional fees on ecological credit transactions. This mechanism is foundational, with M012, M014, and M015 depending on its fee revenue.
  • Detailed Fee Schedule and Distribution: A comprehensive fee schedule is defined for various transaction types: Credit Issuance (2%), Transfer (0.1%), Retirement (0.5%), and Marketplace Trade (1%). Fees are distributed across four pools: Burn (30%), Validator Fund (40%), Community Pool (25%), and Agent Infrastructure (5%). A minimum fee floor of 1 REGEN is implemented to prevent zero-fee transactions.
  • State Machine and Governance: The mechanism outlines a state machine for transitioning from FLAT_GAS to TRANSITION (dual-fee) and finally to VALUE_BASED, with governance approval and specific conditions for each transition. Key parameters like fee rates and distribution shares are governance-controlled.
  • Comprehensive Documentation and Reference Implementation: The PR includes a detailed SPEC.md with 13 sections covering problem, actors, token flows, security invariants, and acceptance tests. It also provides JSON schemas for fee events, fee configuration, and KPI metrics, along with a reference implementation in JavaScript with deterministic self-tests and replay datasets.
  • Open Questions for Working Group Resolution: Several open questions are documented, including methods for credit value estimation for non-marketplace transactions, fee denomination, and the governance model for the Agent Infrastructure fund, indicating areas for further discussion and decision-making.
Changelog
  • README.md
    • Added m013 to the mechanism index.
  • docs/MECHANISM_CONSUMERS.md
    • Added a new section for m013, detailing its canonical specification, output schemas, datasets, and known consumers.
  • mechanisms/m013-value-based-fee-routing/README.md
    • Added a new README file explaining the purpose, outputs, limitations (v0), referencing guidelines, replay datasets, and schemas for m013.
  • mechanisms/m013-value-based-fee-routing/SPEC.md
    • Added the complete specification document for m013, outlining the problem, fee schedule, calculation, distribution, token flows, participants, state transitions, security invariants, governance parameters, open questions, implementation notes, and acceptance tests.
  • mechanisms/m013-value-based-fee-routing/datasets/README.md
    • Added a README file for m013 datasets, describing their use as deterministic inputs for KPI computation and listing relevant files.
  • mechanisms/m013-value-based-fee-routing/datasets/fixtures/v0_sample.json
    • Added a sample JSON fixture containing fee configuration and various fee events for testing and replay purposes.
  • mechanisms/m013-value-based-fee-routing/datasets/schema.json
    • Added a JSON schema for m013 replay datasets.
  • mechanisms/m013-value-based-fee-routing/reference-impl/README.md
    • Added a README file for the m013 reference implementation, detailing inputs, outputs, self-test instructions, and default parameters.
  • mechanisms/m013-value-based-fee-routing/reference-impl/m013_fee.js
    • Added a JavaScript module for computing fees and their distribution for individual credit transactions, including default rates, shares, and a self-test harness.
  • mechanisms/m013-value-based-fee-routing/reference-impl/m013_kpi.js
    • Added a JavaScript module for computing aggregate Key Performance Indicator (KPI) metrics from m013 fee events.
  • mechanisms/m013-value-based-fee-routing/reference-impl/test_vectors/vector_v0_sample.expected.json
    • Added expected output data for m013 fee computation test vectors.
  • mechanisms/m013-value-based-fee-routing/reference-impl/test_vectors/vector_v0_sample.input.json
    • Added input data for m013 fee computation test vectors.
  • mechanisms/m013-value-based-fee-routing/schemas/README.md
    • Added a README file for m013 output schemas, explaining their purpose and listing the schema files.
  • mechanisms/m013-value-based-fee-routing/schemas/m013_fee_config.schema.json
    • Added a JSON schema for m013 fee configuration parameters.
  • mechanisms/m013-value-based-fee-routing/schemas/m013_fee_event.schema.json
    • Added a JSON schema for a single m013 fee collection event.
  • mechanisms/m013-value-based-fee-routing/schemas/m013_kpi.schema.json
    • Added a JSON schema for m013 Key Performance Indicator (KPI) output.
  • package.json
    • Updated the package configuration to specify 'type': 'module'.
  • scripts/verify.mjs
    • Updated the verification script to include checks for m013 core files, schema validation, and the m013 reference implementation self-test.
Activity
  • The self-test for m013_fee.js in the reference implementation passed.
  • The npm run verify command executed successfully.
  • m010 regression tests passed.
  • Review of fee rates against phase-2/2.6 source material is pending.
  • Validation of the distribution model against Working Group consensus is pending.
  • The pull request content was generated with Claude Code.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the comprehensive specification and reference implementation for M013 Value-Based Fee Routing. The documentation, schemas, and test data are well-structured and thorough. My review found a critical issue in the fee distribution logic of the reference implementation that could lead to a loss of funds due to rounding errors, violating the 'Fee Conservation' security invariant. I have provided a code suggestion to fix this. I also suggest updating the specification to clarify the rounding logic to prevent ambiguity in other implementations, aligning with repository guidelines for explicit transition logic.

Comment on lines +57 to +62
const distribution = {
burn: Math.floor(feeAmount * shares.burn),
validator: Math.floor(feeAmount * shares.validator),
community: Math.floor(feeAmount * shares.community),
agent: Math.floor(feeAmount * shares.agent)
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The current fee distribution logic uses Math.floor() for each pool's share independently. This can lead to rounding errors where the sum of the distributed amounts is less than the total feeAmount, causing a loss of funds (dust). This violates the Fee Conservation security invariant defined in SPEC.md (section 9), which states fee_collected = sum(pool_distributions). To fix this, the remainder from rounding should be deterministically allocated to one of the pools. The suggested implementation calculates three of the pool amounts using Math.floor() and allocates the entire remaining amount to the fourth pool (validator), ensuring that the total distributed amount exactly equals feeAmount.

  const d_burn = Math.floor(feeAmount * shares.burn);
  const d_community = Math.floor(feeAmount * shares.community);
  const d_agent = Math.floor(feeAmount * shares.agent);
  const d_validator = feeAmount - d_burn - d_community - d_agent;

  const distribution = {
    burn: d_burn,
    validator: d_validator,
    community: d_community,
    agent: d_agent
  };

Comment on lines +76 to +79
burn_pool += fee_amount * burn_share
validator_fund += fee_amount * validator_share
community_pool += fee_amount * community_share
agent_infra += fee_amount * agent_share
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The pseudo-code for fee distribution is ambiguous about how to handle rounding with integer-based amounts. This can lead to implementations that violate the Fee Conservation invariant (fee_collected = sum(pool_distributions)), as flooring each share's calculation can result in lost "dust".

The specification should be explicit about a deterministic rounding strategy to ensure no funds are lost. For example, you could specify that N-1 shares are calculated with flooring, and the remainder is assigned to the last (or largest) share to ensure the sum is always correct.

References
  1. When specifying transition logic between different mechanisms (e.g., multipliers), explicitly define the function (e.g., max()) and the state-based conditions (phase-gating) that govern the transition to prevent implementation ambiguity.

Floor 3 pools (burn, community, agent) and derive validator as remainder
so fee_collected = sum(pool_distributions) always holds. Clarify rounding
strategy in SPEC.md pseudo-code. Fixes Gemini review on PR regen-network#23.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@glandua
Copy link
Contributor

glandua commented Mar 18, 2026

Review: M013 Value-Based Fee Routing

Thorough review of the full PR — SPEC.md (13 sections), reference implementation, schemas, datasets, test vectors, and verify.mjs integration. Overall this is a well-structured mechanism spec that follows the m010 precedent cleanly.


1. Technical Accuracy (Cosmos SDK Fit)

Mostly sound, with caveats:

  • The x/feerouter module intercepting x/ecocredit messages is a reasonable Cosmos SDK pattern using AnteHandler or message-level decorator hooks.
  • SPEC.md Section 8 says "disable legacy flat gas for credit transactions" in the VALUE_BASED state. This needs clarification: does gas go to zero for credit txs (unusual — gas still serves anti-spam), or does gas remain but value-based fee replaces the revenue function of gas? If gas is truly eliminated for credit txs, that opens a spam vector. Recommend clarifying that gas remains for DoS protection while value fee is additive for revenue.
  • The min_fee of 1 REGEN is quite high as a floor. A low-value transfer of 0.5 REGEN would pay a 1 REGEN fee (200% effective rate). Document whether the intent is to discourage micro-transactions or whether the floor should be revisited.
  • Note that on-chain implementation MUST use sdk.Dec or integer math with basis-point rates, not the float arithmetic in the reference impl (which is fine for readability).

2. Completeness

  • Value estimation (OQ-M013-2) is the critical unresolved dependency. For issuance, credits have no marketplace history — marketplace_reference may return zero/stale data for new credit classes. The SPEC should explicitly address the cold-start case.
  • Missing: batch/multi-msg transaction handling. A single Cosmos SDK tx can contain multiple messages. How are fees calculated? Per-message? Sum of values?
  • Missing: refund/failure semantics. If a credit transaction fails after fee deduction, is the fee refunded? AnteHandlers deduct before execution.
  • Missing: upgrade/migration path. The state machine defines FLAT_GAS → TRANSITION → VALUE_BASED but doesn't specify the chain upgrade mechanism.

3. Consistency with M012/M014/M015

  • M012 (Burn): Burn pool at 30% feeding M012 — dependency documented in Sections 6 and 12. Clean.
  • M014 (PoA): Validator Fund at 40% for "fixed compensation." Consistent with PoA, but clarify whether "fixed" means per-validator or as a pool divided among validators.
  • M015 (Community): Community Pool at 25% → M015. Model B alternative (50-60%) is a significant governance question, well-framed.
  • Agent Infrastructure Fund (5%): Not referenced by any other mechanism. Risk of becoming an unaccountable pool without a consuming mechanism. Define concretely what "AI agent operational costs" means.

4. Issues Found

  • Dataset/test vector mismatch: Replay dataset has 8 fee events but test vectors cover only 5. The 3 missing events need coverage or the subset rationale documented.
  • fee_rate and min_fee_applied are optional in fee event schema but are essential audit fields — should be required.
  • Self-test runs unconditionally on import in m013_fee.js. Any code importing computeFee triggers file reads and potential process.exit(1). Gate behind main-module check.
  • SPEC Section 5 pseudocode variable name inconsistency — validator remainder line uses pool share names but should use accumulator variable names.

5. Open Questions Assessment

All 5 OQs are well-framed. OQ-M013-2 (value estimation) is the most critical — add a decision matrix for the cold-start case. OQ-M013-5 (burn debate) should inline a brief pro/con summary so the SPEC is self-contained.

Recommendations

  1. Clarify gas + value fee coexistence in Section 8
  2. Add cold-start handling for OQ-M013-2
  3. Add multi-msg transaction semantics
  4. Add failure/refund semantics
  5. Gate the self-test behind main-module check
  6. Align test vectors with dataset
  7. Make fee_rate and min_fee_applied required in schema
  8. Note integer arithmetic requirement for on-chain implementation

Verdict

Solid foundation. The mechanism design is sound and the spec is thorough for v0. Main concerns are (a) gas elimination ambiguity, (b) missing multi-msg and failure semantics, (c) self-test-on-import pattern. None blocking for draft spec merge, but items 1-4 should be addressed before moving beyond draft status.


Review generated with Claude Code, grounded in KOI and Cosmos SDK technical documentation.

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