feat: M013 CosmWasm fee-router contract scaffold with unit tests#65
feat: M013 CosmWasm fee-router contract scaffold with unit tests#65brawlaphant wants to merge 3 commits intoregen-network:mainfrom
Conversation
Implement the CosmWasm contract scaffold for M013 Value-Based Fee Routing, translating the mechanism spec and JS reference implementation into Rust. Contract structure: - msg.rs: InstantiateMsg, ExecuteMsg (CollectFee, UpdateFeeRate, UpdateDistribution), QueryMsg (FeeConfig, PoolBalances, CalculateFee) with typed responses - state.rs: FeeConfig (rates as Decimal, distribution shares, min_fee) and PoolBalances (burn_pool, validator_fund, community_pool, agent_infra) - error.rs: InvalidFeeRate, ShareSumNotUnity, Unauthorized, ZeroValue, RateExceedsCap - contract.rs: Entry points with fee calculation matching the JS reference impl integer rounding strategy (floor for 3 pools, remainder to validator) Fee calculation: fee = max(value * rate, min_fee) Distribution: burn/community/agent use floor(), validator gets remainder (Fee Conservation invariant: sum of pools == fee collected) 13 unit tests covering: - Valid/invalid instantiation (share sum unity) - All 4 tx type fee calculations matching test vectors - Fee distribution (100M -> 30M burn, 40M validator, 25M community, 5M agent) - Fee conservation across all 5 test vector transactions (total 132M uregen) - Rate cap enforcement (max 10%) - Share update validation - Unauthorized access rejection - Zero value rejection Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary of ChangesHello, 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 introduces the foundational CosmWasm smart contract for implementing value-based fee routing within the Regen Network. It establishes a robust mechanism for calculating and distributing fees across various ecological credit transaction types, ensuring adherence to specified economic models and providing a solid, tested base for future integration. Highlights
Using Gemini Code AssistThe 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
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 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. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new CosmWasm fee-router contract designed to manage transaction fees for ecological credit operations. The contract allows for the configuration of fee rates for various transaction types (credit issuance, transfer, retirement, and marketplace trade) and defines how collected fees are distributed among burn, validator, community, and agent infrastructure pools. It includes functions for contract instantiation, fee collection, and administrative updates to fee rates and distribution shares, with robust validation for rates and share sums. Comprehensive unit tests are also added to ensure correct functionality. Review feedback suggests refactoring a duplicated max_rate constant into a module-level constant for improved maintainability and removing an unused InvalidFeeRate error variant to enhance code clarity.
contracts/fee-router/src/contract.rs
Outdated
| msg: InstantiateMsg, | ||
| ) -> Result<Response, ContractError> { | ||
| // Validate all rates are within [0, 0.10] | ||
| let max_rate = Decimal::raw(100_000_000_000_000_000); // 0.1 |
There was a problem hiding this comment.
The value for max_rate (0.1) is a magic number that is also hardcoded in execute_update_fee_rate on line 147. To improve maintainability and prevent potential inconsistencies, this value should be defined as a module-level constant.
For example, you could add this at the top of the file:
const MAX_RATE: Decimal = Decimal::raw(100_000_000_000_000_000); // 0.1Then, you can use this MAX_RATE constant in both places.
contracts/fee-router/src/error.rs
Outdated
| #[error("Invalid fee rate: rate {rate} exceeds cap of 0.10 (10%)")] | ||
| InvalidFeeRate { rate: String }, | ||
|
|
…mantics - Remove dead `InvalidFeeRate` error variant (only `RateExceedsCap` is used) - Add doc comment on PoolBalances explaining write-only pool design and how downstream mechanisms (M012/M014/M015) handle distribution Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The 0.1 (10%) max rate was defined as a local variable in both instantiate() and execute_update_fee_rate(). Extracted to a module-level constant for single-source-of-truth and clarity. Addresses gemini-code-assist review feedback on PR regen-network#65. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
contracts/fee-router/fee = max(value * rate, min_fee)) and 4-pool distribution matching the JS reference implementation's integer rounding strategy (floor for burn/community/agent, remainder to validator)vector_v0_sample), covering all tx types, fee conservation invariant, share sum unity, rate caps, and authorizationFiles
Cargo.tomlsrc/msg.rssrc/state.rssrc/error.rssrc/contract.rssrc/tests.rs.cargo/config.tomlcargo wasmalias for wasm32 buildsTest vectors validated
Fee conservation: total 132,000,000 uregen = burn 39,600,000 + validator 52,800,000 + community 33,000,000 + agent 6,600,000
Spec references
mechanisms/m013-value-based-fee-routing/SPEC.md(sections 4-5, 9, 13)phase-3/m013-contract-architecture.mdmechanisms/m013-value-based-fee-routing/reference-impl/m013_fee.jsTest plan
cargo testpasses (13/13 tests)Math.floorfor all test vectorscargo wasmbuild + schema generation for integration testing🤖 Generated with Claude Code