Skip to content

refactor: introduce adapter layer for Djed transaction handling#98

Open
P-ragati wants to merge 5 commits into
DjedAlliance:mainfrom
P-ragati:djed-adapter-refactor
Open

refactor: introduce adapter layer for Djed transaction handling#98
P-ragati wants to merge 5 commits into
DjedAlliance:mainfrom
P-ragati:djed-adapter-refactor

Conversation

@P-ragati
Copy link
Copy Markdown

@P-ragati P-ragati commented Mar 17, 2026

Summary

This PR refactors Djed contract interaction by introducing an adapter layer to centralize transaction-building logic.

Previously, transaction methods like buyStableCoins and sellStableCoins were called directly from multiple locations. This change introduces a createDjedAdapter abstraction that standardizes how these interactions are handled.

Changes Made

  • Added djed-sdk/src/adapters/djedAdapter.js

  • Implemented createDjedAdapter(djedContract):

    • Wraps contract methods
    • Injects FEE_UI_UNSCALED internally
  • Refactored:

    • buyScTx
    • sellScTx
  • Removed direct dependency on contract method calls in transaction builders

Why this change?

  • Improves separation of concerns
  • Makes the codebase more modular and maintainable
  • Enables easier support for multiple Djed variants (Osiris, Shu, Tefnut, etc.)
  • Avoids duplication of fee-related logic across the codebase

Impact

  • No change in external behavior
  • Internal structure is cleaner and more extensible

Notes

  • This is a foundational refactor for future protocol adapter support
  • Happy to extend this pattern to other contract interactions if needed

Checklist

  • Code follows project conventions
  • Refactor does not break existing functionality
  • Focused, single-purpose PR

Summary by CodeRabbit

  • Refactor

    • Introduced an adapter layer to standardize stablecoin buy/sell interactions and ensure required methods exist.
    • Added internal transaction helpers to prepare and build buy/sell stablecoin transactions with consistent fee handling.
    • Improved validation and clearer error handling for the contract interface to reduce runtime failures.
  • Chores

    • Updated ignore rules to exclude local/npm metadata files from version control.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 17, 2026

Warning

Rate limit exceeded

@P-ragati has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 19 minutes and 51 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 44de3c2e-8c25-41b5-a183-3151fae0e6f7

📥 Commits

Reviewing files that changed from the base of the PR and between f24b61d and 0108b79.

📒 Files selected for processing (2)
  • djed-sdk/src/adapters/djedAdapter.js
  • djed-sdk/src/djed/djed.js
📝 Walkthrough

Walkthrough

Adds a new Djed adapter factory that validates a contract and exposes buy/sell methods; djed.js now uses the adapter and buildTx to create ABI-encoded transaction helper functions for buying and selling stablecoins.

Changes

Cohort / File(s) Summary
New Adapter Module
src/adapters/djedAdapter.js
Adds createDjedAdapter(djedContract) which verifies djedContract?.methods and required methods, throws on missing pieces, and returns an adapter exposing buyStableCoins(receiver, UI) and sellStableCoins(amount, account, UI) that inject FEE_UI_UNSCALED.
Integration in Main Module
src/djed/djed.js
Imports createDjedAdapter and buildTx; adds internal helpers buyScTx and sellScTx that instantiate the adapter, ABI-encode adapter calls (.encodeABI()), and build transaction objects targeting DJED_ADDRESS with appropriate sender/value/data.
Repo config
.gitignore
Adds ignore entries djed-sdk/.npmrc and package-lock.json to exclude these files from VCS.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant DjedModule
    participant Adapter
    participant Contract
    participant Builder as buildTx

    Caller->>DjedModule: request buyScTx(receiver, UI)
    DjedModule->>Adapter: createDjedAdapter(djedContract)
    Adapter->>Contract: methods.buyStableCoins(receiver, FEE_UI_UNSCALED, UI)
    Contract-->>Adapter: method call object (callable)
    DjedModule->>DjedModule: .encodeABI() → tx.data
    DjedModule->>Builder: buildTx(sender, DJED_ADDRESS, value, data)
    Builder-->>Caller: transaction object
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I nudged a contract, snug and neat,
Fee tucked in like a tiny treat,
Encode the hop, buildTx in hand,
Stablecoins ready — watch them land,
A rabbit's patch of code, so fleet.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 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 main change: introducing an adapter layer for Djed transaction handling. It directly reflects the primary purpose of the PR—adding the djedAdapter module and refactoring transaction builders to use it.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

Tip

CodeRabbit can use OpenGrep to find security vulnerabilities and bugs across 17+ programming languages.

OpenGrep is compatible with Semgrep configurations. Add an opengrep.yml or semgrep.yml configuration file to your project to enable OpenGrep analysis.

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.

🧹 Nitpick comments (1)
djed-sdk/src/adapters/djedAdapter.js (1)

4-6: Tighten adapter validation to fail fast on missing contract methods.

Line 4 currently validates only methods; if buyStableCoins/sellStableCoins are missing, failure shifts to a later TypeError. Consider validating required method functions here for a clearer error boundary.

Proposed diff
 export const createDjedAdapter = (djedContract) => {
-  if (!djedContract || !djedContract.methods) {
-    throw new Error("Djed contract instance is required");
+  const methods = djedContract?.methods;
+  if (
+    !methods ||
+    typeof methods.buyStableCoins !== "function" ||
+    typeof methods.sellStableCoins !== "function"
+  ) {
+    throw new Error("Invalid Djed contract instance: required methods are missing");
   }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@djed-sdk/src/adapters/djedAdapter.js` around lines 4 - 6, The adapter's
initial validation only checks djedContract and djedContract.methods but doesn't
ensure required RPC methods exist, so add a fail-fast check in the same
validation block (where djedContract and djedContract.methods are validated) to
assert that djedContract.methods.buyStableCoins and
djedContract.methods.sellStableCoins are functions; if any are missing throw a
clear Error that names the missing methods (e.g., "Djed contract is missing
required methods: buyStableCoins, sellStableCoins") so callers get an immediate,
descriptive failure.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@djed-sdk/src/adapters/djedAdapter.js`:
- Around line 4-6: The adapter's initial validation only checks djedContract and
djedContract.methods but doesn't ensure required RPC methods exist, so add a
fail-fast check in the same validation block (where djedContract and
djedContract.methods are validated) to assert that
djedContract.methods.buyStableCoins and djedContract.methods.sellStableCoins are
functions; if any are missing throw a clear Error that names the missing methods
(e.g., "Djed contract is missing required methods: buyStableCoins,
sellStableCoins") so callers get an immediate, descriptive failure.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 20622e7b-985d-457d-977c-bf0e9bb2479c

📥 Commits

Reviewing files that changed from the base of the PR and between 509e7d3 and f6b5323.

📒 Files selected for processing (2)
  • djed-sdk/src/adapters/djedAdapter.js
  • djed-sdk/src/djed/djed.js

Copilot AI review requested due to automatic review settings March 22, 2026 16:02
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors Djed contract interaction by introducing a createDjedAdapter layer to centralize transaction-building logic and standardize fee injection.

Changes:

  • Added a Djed adapter that validates the contract interface and injects FEE_UI_UNSCALED into buy/sell calls.
  • Updated Djed transaction-building helpers to use the adapter and a shared buildTx helper.
  • Added .gitignore entries for SDK .npmrc and package-lock.json.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.

File Description
djed-sdk/src/djed/djed.js Uses the new adapter to build buy/sell stablecoin tx calldata and wraps it via buildTx.
djed-sdk/src/adapters/djedAdapter.js Introduces adapter that validates contract methods and injects a standardized fee argument.
.gitignore Adds ignore rules for local npm config and lockfile.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread djed-sdk/src/adapters/djedAdapter.js Outdated
Comment thread djed-sdk/src/djed/djed.js Outdated
- Avoid recreating adapter on each call
- Rename UI to ui for consistency and clarity
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