Skip to content

feat: x/precisebank module #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 56 commits into
base: main
Choose a base branch
from
Open

feat: x/precisebank module #69

wants to merge 56 commits into from

Conversation

dudong2
Copy link

@dudong2 dudong2 commented Apr 13, 2025

Description

TL;DR: Introduces x/precisebank to address EVM ↔ SDK precision mismatch and integrates it with x/vm and precompiles.


Problem

  • Cosmos SDK assumes a 6-decimal denomination across all modules.
  • EVM uses 18 decimals, causing precision loss when transferring values or paying gas.
  • Sub-6-decimal values are floored, which can lead to:
    • Loss of small token amounts
    • Arbitrage vulnerabilities in DEXs like Uniswap v2 navite coin -> erc20 senarios due to mismatched balance handling

Purpose

  • Enable precise handling of fractional balances in EVM-related modules.
  • Preserve and manage sub-integer values (e.g., 0.0000001 (= 10^-7) tokens) without loss.
  • Seamlessly integrate precise value tracking into x/vm and precompiles.

Implemented Changes

  • Introduced a new x/precisebank module based on Kava’s implementation.
  • Generalized to support 1~18 decimal assets, not just 6↔18.
  • Added carry/borrow mechanics for converting fractional to integer coins.
  • Replaced direct use of BankKeeper with PreciseBankKeeper in:
    • x/vm
    • precompiles
  • Added invariant check for total supply
  • Added CLI queries for fractional balances and remainder state.
  • Added tests:
    • Edge case handling
    • Fuzz tests (Mint, Burn, Send, Genesis validation)
    • Random value tests
    • Contract tests(simple wrap/unwarp)

Notes

  • For tokens with exactly 18 decimals, using x/precisebank is supported but not necessary — the standard x/bank module is sufficient and recommended.
  • The x/precisebank module is designed to manage only the fractional (sub-integer) balances, while integer balances continue to be managed by the x/bank module..
  • Staking, slashing, distribution, and other Cosmos SDK modules remain unchanged:
    • They all assume 6 decimals and are out of scope for this PR.

Closes: #48


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • tackled an existing issue or discussed with a team member
  • left instructions on how to review the changes
  • targeted the main branch

Reviewers Checklist

All items are required.
Please add a note if the item is not applicable
and please add your handle next to the items reviewed
if you only reviewed selected items.

I have...

  • added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • confirmed all author checklist items have been addressed
  • confirmed that this PR does not change production code
  • reviewed content
  • tested instructions (if applicable)
  • confirmed all CI checks have passed

@dudong2 dudong2 self-assigned this Apr 13, 2025
@dudong2 dudong2 force-pushed the dudong2/feat/precisebank branch from d99ad2b to 1553be2 Compare April 26, 2025 19:53
@dudong2 dudong2 changed the base branch from feat/precisebank to main April 26, 2025 19:54
@dudong2 dudong2 marked this pull request as ready for review April 27, 2025 15:30
@dudong2 dudong2 requested review from vladjdk, dongsam and zsystm April 28, 2025 02:02
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.

Decimal handling (6 and 18)
1 participant