Skip to content

feat(swap-service): Mayachain swap verification#39

Merged
kaladinlight merged 1 commit into
developfrom
feat/swap-maya-verification
May 18, 2026
Merged

feat(swap-service): Mayachain swap verification#39
kaladinlight merged 1 commit into
developfrom
feat/swap-maya-verification

Conversation

@kaladinlight
Copy link
Copy Markdown
Member

@kaladinlight kaladinlight commented May 18, 2026

Description

Adds Mayachain swap verification by extracting the Thorchain Midgard verifier into a shared verifyMidgardSwap helper parameterized on Midgard URL and affiliate code (ss for Thorchain, ssmaya for Maya). The previous Maya path (node-API memo regex against /mayachain/tx/<hash>) is removed in favor of Midgard parity with Thorchain — same buy-out-by-memo-destination matching, same affiliate-out detection, same precision conversion.

Also includes minor cleanup carried in alongside: generalized error strings (Missing sell txHash, Swap action failed, No request data returned from Relay API) and renamed the test env var from VITE_MAYACHAIN_NODE_URL to VITE_MAYACHAIN_MIDGARD_URL.

Testing

  • cd apps/swap-service && npx jest src/verification/__tests__/maya.test.ts — 14 tests pass, mirroring thorchain.test.ts coverage
  • cd apps/swap-service && npx jest src/verification/__tests__/thorchain.test.ts — unchanged behavior on Thorchain side after the shared-helper extraction
  • cd apps/swap-service && npx jest src/verification/__tests__/relay.test.ts — updated error-string assertion still passes

Summary by CodeRabbit

  • Bug Fixes

    • Improved error messaging for swap verification failures across multiple scenarios.
  • Tests

    • Added comprehensive test coverage for Maya swap verification including affiliate handling and transaction validation.
    • Updated test expectations for error messages.
  • Refactor

    • Unified THORChain and Maya swap verification logic through a shared verification approach for consistency and maintainability.

Review Change Stack

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

coderabbitai Bot commented May 18, 2026

📝 Walkthrough

Walkthrough

This PR refactors THORChain and Maya swap verification to use a shared Midgard-based helper function, replacing separate legacy implementations. It includes comprehensive Maya verification test coverage with fixtures and updates test infrastructure to reflect the unified Midgard URL approach.

Changes

Maya Swap Verification via Midgard Unification

Layer / File(s) Summary
Midgard unification refactor and cleanup
apps/swap-service/src/verification/swap-verification.service.ts, apps/swap-service/src/verification/types.ts
verifyThorchain and verifyMaya now delegate to a shared verifyMidgardSwap helper that fetches Midgard actions, validates action type/status, parses memo destination, matches outbound and affiliate outbound, and computes hasAffiliate based on affiliate address matching. Removed ThorchainMayaTxResponse type and Maya-node memo parsing flow.
Test setup and message updates
apps/swap-service/src/verification/__tests__/setup.ts, apps/swap-service/src/verification/__tests__/relay.test.ts, apps/swap-service/src/verification/__tests__/thorchain.test.ts, apps/swap-service/src/verification/swap-verification.service.ts
Test environment now uses Midgard URLs for both THORChain and MAYAChain (removed node URL references). Error message strings updated across verifySwap, verifyRelay, and test assertions for consistency with the unified implementation.
Maya verification test fixtures
apps/swap-service/src/verification/__tests__/fixtures/maya/response.json, apps/swap-service/src/verification/__tests__/fixtures/maya/swap.ts
Added Midgard API response JSON fixture with successful swap metadata including affiliate info, fees, memo, and pools; added TypeScript Swap fixture object matching the response structure for testing.
Maya verification comprehensive test suite
apps/swap-service/src/verification/__tests__/maya.test.ts
Added 200+ lines of Jest test coverage for verifyMaya, including successful affiliate attribution with fee computation, txHash normalization, affiliate edge cases, failure paths (missing txHash, no actions, pending/failed actions, invalid type, missing metadata), memo-based outbound selection with destination matching, and upstream HTTP failure handling with error propagation.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 Two chains, one Midgard flow,
THORchain and Maya now unified grow,
Memo destinations, affiliate fees so clear,
With fixtures and tests to hold them dear,
The swap service shines, bright and lean!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the primary change: adding Mayachain swap verification support to the swap-service.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 feat/swap-maya-verification

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 and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
apps/swap-service/src/verification/swap-verification.service.ts (1)

376-381: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Guard Midgard amount fields before nested indexing.

action.in[0].coins[0].amount and buyOut.coins[0].amount are assumed present. If Midgard returns partial data, this throws and collapses into a generic top-level catch path instead of a deterministic verification reason.

Suggested hardening
+    const inAmount = action.in?.[0]?.coins?.[0]?.amount
+    if (!inAmount) return noAffiliateResult('PENDING', 'Missing inbound amount in Midgard action')
+
+    const buyAmount = buyOut.coins?.[0]?.amount
+    if (!buyAmount) return noAffiliateResult('PENDING', 'Missing outbound amount in Midgard action')
+
+    const affiliateFeeAmount = feeOut?.coins?.[0]?.amount
+
     return {
       verificationStatus: 'SUCCESS',
       hasAffiliate,
       affiliateBps: hasAffiliate ? parseInt(swapMetadata.affiliateFee) : undefined,
       affiliateAddress: hasAffiliate ? affiliateAddress : undefined,
-      verifiedSellAmountCryptoBaseUnit: thorchainToNativePrecision(
-        action.in[0].coins[0].amount,
-        swap.sellAsset.precision,
-      ),
-      actualBuyAmountCryptoBaseUnit: thorchainToNativePrecision(buyOut.coins[0].amount, swap.buyAsset.precision),
-      actualAffiliateFeeAmountCryptoBaseUnit: hasAffiliate ? feeOut?.coins[0].amount : undefined,
+      verifiedSellAmountCryptoBaseUnit: thorchainToNativePrecision(inAmount, swap.sellAsset.precision),
+      actualBuyAmountCryptoBaseUnit: thorchainToNativePrecision(buyAmount, swap.buyAsset.precision),
+      actualAffiliateFeeAmountCryptoBaseUnit: hasAffiliate ? affiliateFeeAmount : undefined,
     }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/swap-service/src/verification/swap-verification.service.ts` around lines
376 - 381, The code assumes nested arrays exist when calling
thorchainToNativePrecision on action.in[0].coins[0].amount and
buyOut.coins[0].amount (and reads feeOut?.coins[0].amount); add explicit guards
that check action.in && action.in[0] && action.in[0].coins &&
action.in[0].coins[0] and buyOut && buyOut.coins && buyOut.coins[0] (and
feeOut?.coins?.[0]) before indexing, and if any are missing return/throw a
deterministic verification error (or set the corresponding verified/actual
fields to undefined in the same verification-result shape) so the verification
logic produces a clear, recoverable reason instead of a generic catch; update
the places using thorchainToNativePrecision and the variables
verifiedSellAmountCryptoBaseUnit, actualBuyAmountCryptoBaseUnit, and
actualAffiliateFeeAmountCryptoBaseUnit accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@apps/swap-service/src/verification/swap-verification.service.ts`:
- Around line 376-381: The code assumes nested arrays exist when calling
thorchainToNativePrecision on action.in[0].coins[0].amount and
buyOut.coins[0].amount (and reads feeOut?.coins[0].amount); add explicit guards
that check action.in && action.in[0] && action.in[0].coins &&
action.in[0].coins[0] and buyOut && buyOut.coins && buyOut.coins[0] (and
feeOut?.coins?.[0]) before indexing, and if any are missing return/throw a
deterministic verification error (or set the corresponding verified/actual
fields to undefined in the same verification-result shape) so the verification
logic produces a clear, recoverable reason instead of a generic catch; update
the places using thorchainToNativePrecision and the variables
verifiedSellAmountCryptoBaseUnit, actualBuyAmountCryptoBaseUnit, and
actualAffiliateFeeAmountCryptoBaseUnit accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d1101ccd-7548-4ada-8bd9-55dfaf679262

📥 Commits

Reviewing files that changed from the base of the PR and between c0c026c and 5df3bcf.

📒 Files selected for processing (8)
  • apps/swap-service/src/verification/__tests__/fixtures/maya/response.json
  • apps/swap-service/src/verification/__tests__/fixtures/maya/swap.ts
  • apps/swap-service/src/verification/__tests__/maya.test.ts
  • apps/swap-service/src/verification/__tests__/relay.test.ts
  • apps/swap-service/src/verification/__tests__/setup.ts
  • apps/swap-service/src/verification/__tests__/thorchain.test.ts
  • apps/swap-service/src/verification/swap-verification.service.ts
  • apps/swap-service/src/verification/types.ts
💤 Files with no reviewable changes (1)
  • apps/swap-service/src/verification/types.ts

@kaladinlight kaladinlight merged commit 4cac300 into develop May 18, 2026
2 checks passed
@kaladinlight kaladinlight deleted the feat/swap-maya-verification branch May 18, 2026 17:40
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