Skip to content

feat: join and fund an agent wallet in one call - #169

Merged
MicBun merged 4 commits into
mainfrom
feat/maa-join-and-fund
Jul 22, 2026
Merged

feat: join and fund an agent wallet in one call#169
MicBun merged 4 commits into
mainfrom
feat/maa-join-and-fund

Conversation

@MicBun

@MicBun MicBun commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Adds joinAndFundAgentAddress, so someone who already holds TRUF or USDC on TN can join a rule and fund the derived agent wallet in one signed transaction, instead of a join followed by a separate transfer. Either both legs commit or neither does, so activation can no longer strand a joined-but-unfunded wallet.

resolves: trufnetwork/truf-network#1399

A kwil transaction runs exactly one action, so join + transfer can only be atomic inside a single on-chain action. That action shipped in the node (maa_join_and_fund, merged in trufnetwork/node#1409); this exposes it as one SDK call.

What is here

  • src/contracts-api/maaActions.tsjoinAndFundAgentAddress({ ruleId, bridge, amount }). Mirrors joinAgentAddress: it resolves the rule on-chain and derives the wallet locally, so the caller learns the address before broadcast, then submits one execute. The $amount is pinned to Numeric(78, 0) so a plain JS string reaches the node as NUMERIC, not TEXT (the same pin createAgentRule uses for $fee_flat). Returns the derived address and the submission tx hash.
  • src/types/maa.tsMAAJoinAndFundInput; src/internal.ts — export.
  • examples/maa_lifecycle_example/README.md — a note on the atomic path next to the two-step flow.

Notes for review

  • Amount contract: positive integer base units, the same rule as the bridged-token transfer. The caller must already hold the funds on TN; bringing tokens in from L1 is a separate bridge deposit, so it isn't part of this call.
  • Validation runs before the network read: a bad rule id, empty bridge, or non-positive amount rejects before getRule and before broadcast.
  • Deployment dependency: the method calls the on-chain maa_join_and_fund action (node migration 054). It errors on a live call until that migration is deployed to the target network.

Testing

npm run test:unit green (326 tests, 8 new): the NUMERIC pin, local address derivation, raw-bytes rule id, and every guard (bad rule length, empty bridge, non-positive or non-integer amount, unknown rule, missing tx hash). Typecheck and build clean. Live end-to-end verification follows once the node action is deployed to testnet.

Summary by CodeRabbit

  • New Features
    • Added joinAndFundAgentAddress for atomically joining and funding a Modular Agent Address in a single on-chain transaction.
    • Includes input validation for ruleId, bridge, and funding amount, and returns the transaction hash plus the derived agent wallet address.
  • Tests
    • Added unit tests covering request construction, validation edge cases, and missing-response failure modes.
  • Documentation
    • Added an end-to-end example (README, .env.example, and runnable script) demonstrating an atomic activation smoke test workflow.

@MicBun MicBun self-assigned this Jul 22, 2026
@holdex

holdex Bot commented Jul 22, 2026

Copy link
Copy Markdown

Time Submission Status

Member # Time Running Total Status Last Update
MicBun 1 4h 4h Jul 22, 2026, 11:33 AM
MicBun 2 5h 5h Jul 22, 2026, 11:34 AM
MicBun 5h ✅ Submitted

Submit or update total time with:

@holdex pr submit-time 2h

Add time on top of previous submission with:

@holdex pr add-time 1h30m

See available commands to help comply with our Guidelines.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@MicBun, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 49 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: af057b08-420a-4624-a5f9-211e776cb11e

📥 Commits

Reviewing files that changed from the base of the PR and between 464e280 and 8923189.

📒 Files selected for processing (1)
  • examples/maa_join_and_fund_example/package.json
📝 Walkthrough

Walkthrough

Adds a typed joinAndFundAgentAddress SDK method that validates inputs, derives the MAA address, submits an atomic maa_join_and_fund action, returns the transaction and address, and adds tests, documentation, and an end-to-end smoke test.

Changes

Atomic MAA join and funding

Layer / File(s) Summary
MAA join-and-fund API
src/types/maa.ts, src/contracts-api/maaActions.ts, src/internal.ts
Defines and exports MAAJoinAndFundInput, then implements validation, rule lookup, address derivation, atomic execution, and result handling.
Join-and-fund validation tests
src/contracts-api/maaActions.test.ts
Covers successful execution, typed numeric parameters, derived addresses, invalid inputs, unknown rules, and missing transaction hashes.
Atomic activation smoke test
examples/maa_join_and_fund_example/*
Adds configuration, package setup, documentation, and a runnable flow that creates a rule, performs atomic joining and funding, verifies state and balances, and withdraws escrow.
Lifecycle flow documentation
examples/maa_lifecycle_example/README.md
Documents the atomic flow, required parameters, funding prerequisite, and node support requirement.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Owner
  participant MAAAction
  participant Node
  Owner->>MAAAction: joinAndFundAgentAddress
  MAAAction->>Node: resolve rule
  MAAAction->>Node: execute maa_join_and_fund
  Node-->>MAAAction: return transaction hash
  MAAAction-->>Owner: return derived address and tx hash
Loading

Possibly related PRs

Suggested labels: enhancement

🚥 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 clearly matches the main change: adding a single-call join-and-fund flow for an agent wallet.
Linked Issues check ✅ Passed The PR implements atomic MAA join-and-fund support in JS SDK, which directly addresses issue #1399.
Out of Scope Changes check ✅ Passed The added tests, type export, docs, and runnable example all support the atomic join-and-fund feature and appear in scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/maa-join-and-fund

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@src/contracts-api/maaActions.ts`:
- Around line 149-150: The amount validation in maaActions.ts must require a
string, positive base-10 integer with at most 78 digits before any network call
or getRule operation; update the validation near the existing amount check
accordingly. In src/types/maa.ts lines 55-56, document the NUMERIC(78,0)
78-significant-digit maximum; in src/contracts-api/maaActions.test.ts lines
225-234, add pre-network rejection cases for a numeric amount and a 79-digit
string; and in examples/maa_lifecycle_example/README.md lines 48-57, state that
amount is a positive base-10 string fitting NUMERIC(78,0).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7f5c948c-d974-4586-ad98-f67bcfbb922a

📥 Commits

Reviewing files that changed from the base of the PR and between c4ef7ea and 9bb5742.

📒 Files selected for processing (5)
  • examples/maa_lifecycle_example/README.md
  • src/contracts-api/maaActions.test.ts
  • src/contracts-api/maaActions.ts
  • src/internal.ts
  • src/types/maa.ts

Comment thread src/contracts-api/maaActions.ts Outdated
@MicBun

MicBun commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

@holdex pr submit-time 5h

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 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.

Inline comments:
In `@examples/maa_join_and_fund_example/.env.example`:
- Around line 28-30: Reorder the MAA_FEE_BPS and MAA_FUND_AMOUNT declarations in
the environment example so MAA_FEE_BPS appears before MAA_FUND_AMOUNT,
preserving both values and the existing comment.

In `@examples/maa_join_and_fund_example/package.json`:
- Around line 10-15: Update the `@trufnetwork/kwil-js` dependency in the example
package’s dependencies to match the root SDK version, changing the pinned
version from 0.9.12 to 0.9.14 while leaving the other dependencies unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a287c04c-2623-42cd-9d79-9b52db41827e

📥 Commits

Reviewing files that changed from the base of the PR and between 9bb5742 and 464e280.

📒 Files selected for processing (8)
  • examples/maa_join_and_fund_example/.env.example
  • examples/maa_join_and_fund_example/README.md
  • examples/maa_join_and_fund_example/main.ts
  • examples/maa_join_and_fund_example/package.json
  • examples/maa_lifecycle_example/README.md
  • src/contracts-api/maaActions.test.ts
  • src/contracts-api/maaActions.ts
  • src/types/maa.ts
🚧 Files skipped from review as they are similar to previous changes (4)
  • src/types/maa.ts
  • src/contracts-api/maaActions.ts
  • examples/maa_lifecycle_example/README.md
  • src/contracts-api/maaActions.test.ts

Comment on lines +28 to +30
MAA_FUND_AMOUNT=10000000000000000000
# Owner-withdraw commission paid to the agent, in basis points (250 = 2.5%).
MAA_FEE_BPS=250

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Reorder keys per dotenv-linter.

MAA_FEE_BPS should be declared before MAA_FUND_AMOUNT to satisfy the UnorderedKey check.

🔧 Proposed fix
-# Amount to fund the wallet with, in base units (a positive base-10 integer that fits
-# NUMERIC(78,0)). Default is 10 tokens. The owner must already hold at least this much.
-MAA_FUND_AMOUNT=10000000000000000000
 # Owner-withdraw commission paid to the agent, in basis points (250 = 2.5%).
 MAA_FEE_BPS=250
+# Amount to fund the wallet with, in base units (a positive base-10 integer that fits
+# NUMERIC(78,0)). Default is 10 tokens. The owner must already hold at least this much.
+MAA_FUND_AMOUNT=10000000000000000000
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
MAA_FUND_AMOUNT=10000000000000000000
# Owner-withdraw commission paid to the agent, in basis points (250 = 2.5%).
MAA_FEE_BPS=250
# Owner-withdraw commission paid to the agent, in basis points (250 = 2.5%).
MAA_FEE_BPS=250
# Amount to fund the wallet with, in base units (a positive base-10 integer that fits
# NUMERIC(78,0)). Default is 10 tokens. The owner must already hold at least this much.
MAA_FUND_AMOUNT=10000000000000000000
🧰 Tools
🪛 dotenv-linter (4.0.0)

[warning] 30-30: [UnorderedKey] The MAA_FEE_BPS key should go before the MAA_FUND_AMOUNT key

(UnorderedKey)

🤖 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 `@examples/maa_join_and_fund_example/.env.example` around lines 28 - 30,
Reorder the MAA_FEE_BPS and MAA_FUND_AMOUNT declarations in the environment
example so MAA_FEE_BPS appears before MAA_FUND_AMOUNT, preserving both values
and the existing comment.

Source: Linters/SAST tools

Comment thread examples/maa_join_and_fund_example/package.json
@MicBun
MicBun merged commit f3ddcb9 into main Jul 22, 2026
4 checks passed
@MicBun
MicBun deleted the feat/maa-join-and-fund branch July 22, 2026 13:31
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