Skip to content

fix(swap-verification): stop inflating MAYAChain CACAO amounts 100x - #55

Merged
kaladinlight merged 1 commit into
developfrom
fix/mayachain-cacao-midgard-precision
Aug 27, 2026
Merged

fix(swap-verification): stop inflating MAYAChain CACAO amounts 100x#55
kaladinlight merged 1 commit into
developfrom
fix/mayachain-cacao-midgard-precision

Conversation

@kaladinlight

@kaladinlight kaladinlight commented Aug 25, 2026

Copy link
Copy Markdown
Member

Description

Follow-up to #53. That PR established that MAYAChain Midgard reports native CACAO in 1e10, unlike pool/external assets normalized to 1e8 (Maya docs: "CACAO is the only asset which is expressed in 1e10 format"), and corrected the affiliate feeOut amount to be stored raw.

The same assumption was still live on the other two amounts in verifyMidgardSwap. thorchainToNativePrecision shifts by (nativePrecision - 8), so a CACAO sell leg inflated verifiedSellAmountCryptoBaseUnit 100×, and a CACAO buy leg did the same to actualBuyAmountCryptoBaseUnit. The sell leg feeds volumeUsd and the bps-implied fee in calculateFeeForSwap, so affiliate volume for any CACAO-sell swap was overstated 100×.

#53 reasoned that the affiliate fee is always the chain's native asset, which happens to be true but is incidental — the docs claim is broader: CACAO is 1e10 wherever it appears. midgardToNativePrecision therefore keys on the coin's asset rather than on which field it is, skipping the shift for MAYA.CACAO only.

Testing

yarn test — 64/64 pass.

Two new cases in mayachain.test.ts, one per leg, using amounts taken verbatim from mainnet actions 8F9B6692…530D (CACAO → ETH.USDT) and DA174E41…A57 (BTC → CACAO). Both failed at exactly 100× before the change (24157000000000000 vs 241570000000000) and pass after.

Confirmed independently of #53's evidence:

  • For 8F9B6692…530D the swap's two sides only balance under the 1e10 reading — $4,616 in against $4,571 out, ~1% to fees, versus $461,641 in under the old conversion.
  • Two further mainnet actions with BTC (a known-1e8 asset) on the in-side imply CACAO prices of $0.133 and $0.103 under 1e10, tracking Midgard's own reported price, versus $0.0013 and $0.0010 under 1e8.
  • revenue-dashboard's Maya tracker divides CACAO by 1e10 in a separate codebase.

Checked for double-correction: no consumer of verifiedSellAmountCryptoBaseUnit or actualBuyAmountCryptoBaseUnit compensates for the inflation — including calculatePartnerFeeAmountUsd in public-api, which derives volume from the buy side.

Note for whoever deploys

This is forward-only and does not touch existing data. getPendingVerificationSwaps requires verificationStatus: PENDING and status IN (SUCCESS, FAILED); no row in prod currently matches, so nothing is re-verified on deploy. Stored values stay as they are, payout script output is unchanged, and dashboard figures do not move.

For context rather than as a blocker: all 270 CACAO-leg rows currently in prod belong to a single partner code under investigation for forged attribution, and 188 of them are held back by the payout script's deviation guard purely because this bug made implied and actual fees diverge 99%. If a re-verification backfill is run later, most of those stop being anomalies — re-running aggregateByPartner with corrected values gives anomalies 188 → 48 and that partner's payable total $107,614.65 → $110,159.91. That is a reason to quarantine those rows before any backfill, not before this merge.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved swap verification for MAYAChain assets.
    • Preserved native CACAO amount precision during sell and buy verification.
    • Standardized amount conversion across supported verification flows to prevent decimal shifting.

Midgard normalizes every coin to 1e8 — except MAYAChain's native CACAO,
which it reports at its own 1e10 precision ("CACAO is the only asset
which is expressed in 1e10 format",
https://docs.mayaprotocol.com/mayachain-dev-docs/concepts/memo-length-reduction).
`thorchainToNativePrecision` shifted every amount by (nativePrecision - 8),
so CACAO legs came back 100x too large.

Replaced with `midgardToNativePrecision`, which takes the coin so it can
skip the shift for MAYA.CACAO. Non-native assets are unaffected (ETH at
1e8 -> 1e18 still shifts), and THORChain is unaffected because RUNE's
native precision is already 8. `THORCHAIN_PRECISION` is renamed
`MIDGARD_PRECISION` to match what it actually describes.

This completes #53, which applied the same correction to the affiliate
fee line but left `verifiedSellAmountCryptoBaseUnit` and
`actualBuyAmountCryptoBaseUnit` converting as before.

Verified against mainnet action 8F9B6692…530D: the swap's two sides only
balance under the 1e10 reading — $4,616 in against $4,571 out (1% to
fees), versus $461,641 in under the old conversion.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Aug 25, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3e9a5a98-3ffa-47e8-beba-5ffc71254a49

📥 Commits

Reviewing files that changed from the base of the PR and between 54aec74 and e53a5b2.

📒 Files selected for processing (3)
  • apps/swap-service/src/verification/__tests__/mayachain.test.ts
  • apps/swap-service/src/verification/swap-verification.service.ts
  • apps/swap-service/src/verification/utils.ts

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The change replaces Thorchain-specific amount conversion with Midgard coin-aware conversion. It preserves native CACAO precision during sell and buy verification and adds MAYAChain tests for the behavior.

Changes

Midgard precision verification

Layer / File(s) Summary
Midgard precision conversion
apps/swap-service/src/verification/utils.ts
midgardToNativePrecision accepts MidgardCoin, uses MIDGARD_PRECISION, and applies 1e10 precision for MAYA.CACAO.
Verification integration and CACAO tests
apps/swap-service/src/verification/swap-verification.service.ts, apps/swap-service/src/verification/__tests__/mayachain.test.ts
Midgard swap verification converts complete sell and buy coin objects. Tests verify unchanged native CACAO amounts.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to e53a5

The change corrects CACAO amount normalization to prevent 100× inflation in swap verification without changing existing stored data or payout output; no actionable merge-blocking risk remains beyond normal checks.

Poem

A rabbit checks CACAO bright,
Native digits stay just right.
Midgard coins now guide the way,
Sell and buy pass tests today.
Precision hops without delay.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 3…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preventing 100× inflation of MAYAChain CACAO amounts during swap verification.
Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 3 files.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/mayachain-cacao-midgard-precision

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kaladinlight
kaladinlight merged commit 5ad19e4 into develop Aug 27, 2026
2 checks passed
@kaladinlight
kaladinlight deleted the fix/mayachain-cacao-midgard-precision branch August 27, 2026 17:45
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.

1 participant