Skip to content

Add command to add MPL Core Assets to Collections#107

Open
nhanphan wants to merge 2 commits intomainfrom
claude/add-asset-to-collection-EJocz
Open

Add command to add MPL Core Assets to Collections#107
nhanphan wants to merge 2 commits intomainfrom
claude/add-asset-to-collection-EJocz

Conversation

@nhanphan
Copy link
Copy Markdown
Contributor

@nhanphan nhanphan commented Apr 8, 2026

Summary

This PR adds a new CLI command that allows users to add existing MPL Core Assets to Collections. The command updates an asset's update authority to point to a collection, effectively associating the asset with that collection.

Key Changes

  • Added CollectionAdd command class in src/commands/core/collection/add.ts
  • Implements asset-to-collection association by updating the asset's update authority
  • Accepts two required arguments: collectionId and assetId
  • Includes validation to prevent adding assets that already belong to a collection
  • Provides user feedback via spinner UI and formatted output with transaction signature and explorer link

Implementation Details

  • Fetches both the asset and collection from the blockchain before performing the update
  • Uses the update function from @metaplex-foundation/mpl-core to modify the asset's update authority
  • Sets the new update authority to a Collection type with the target collection's public key
  • Returns transaction details including signature and explorer URL for verification
  • Includes proper error handling with user-friendly error messages

https://claude.ai/code/session_01QjTrbKjMEQZoKDSzdmM7vV

…ction

Uses mpl-core's updateV2 instruction to set an asset's update authority
to a collection, enabling post-creation collection assignment.

https://claude.ai/code/session_01QjTrbKjMEQZoKDSzdmM7vV
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 8, 2026

Summary by CodeRabbit

  • New Features
    • Added collection add command for managing asset collection assignments. Automatically validates asset eligibility and prevents duplicate assignments, updates authorization credentials, processes transactions with automatic confirmation, provides detailed success reporting with transaction information and explorer verification links, includes comprehensive error handling with user-facing feedback.

Walkthrough

Adds a new Oclif command collection add in src/commands/core/collection/add.ts. The command accepts collection and asset arguments, fetches both entities, validates the asset is not already a collection, then builds and sends an MPL Core update transaction to set the asset's update authority and collection reference.

Changes

Cohort / File(s) Summary
Collection Add Command
src/commands/core/collection/add.ts
New Oclif command implementation with collection and asset argument parsing, asset validation logic, MPL Core transaction building to update asset authority and collection, transaction confirmation, and explorer URL generation.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • blockiosaurus
  • MarkSackerberg
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add command to add MPL Core Assets to Collections' accurately summarizes the main change: adding a new CLI command for associating assets with collections.
Description check ✅ Passed The description is directly related to the changeset, providing a clear summary of the new command, its functionality, key changes, and implementation details.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/add-asset-to-collection-EJocz

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

@nhanphan nhanphan requested a review from tonyboylehub April 8, 2026 20:15
@nhanphan nhanphan marked this pull request as ready for review April 8, 2026 20:15
Copy link
Copy Markdown
Contributor

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/commands/core/collection/add.ts`:
- Around line 30-32: The code calls fetchCollection before checking whether the
asset is already collection-bound, causing an unnecessary RPC; change the order
in the add command so you first fetch the asset with
fetchAsset(publicKey(args.asset)) and check its collection membership (e.g.,
asset.collection or equivalent) and only call
fetchCollection(publicKey(args.collection)) if the asset is not already in a
collection; ensure you still use publicKey(args.collection) when needed and keep
existing error/validation logic but move it after the early
collection-membership check to avoid unnecessary network calls.
🪄 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: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: d8de3723-376b-4d73-aade-4349bc815e24

📥 Commits

Reviewing files that changed from the base of the PR and between 8598899 and 589fd70.

📒 Files selected for processing (1)
  • src/commands/core/collection/add.ts

Comment on lines +30 to +32
const asset = await fetchAsset(umi, publicKey(args.asset))
const collection = await fetchCollection(umi, publicKey(args.collection))

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.

🧹 Nitpick | 🔵 Trivial

Avoid unnecessary RPC on the early-reject path.

At Line 31, fetchCollection is called before checking whether the asset is already collection-bound (Line 33). If the asset is already in a collection, this does extra network work and latency for no benefit.

♻️ Proposed refactor
-      const asset = await fetchAsset(umi, publicKey(args.asset))
-      const collection = await fetchCollection(umi, publicKey(args.collection))
+      const asset = await fetchAsset(umi, publicKey(args.asset))
 
       if (asset.updateAuthority.type === 'Collection') {
         spinner.fail('Asset is already in a collection')
         this.error(`Asset ${args.asset} already belongs to collection ${asset.updateAuthority.address}`)
       }
+
+      const collection = await fetchCollection(umi, publicKey(args.collection))

Also applies to: 33-36

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/commands/core/collection/add.ts` around lines 30 - 32, The code calls
fetchCollection before checking whether the asset is already collection-bound,
causing an unnecessary RPC; change the order in the add command so you first
fetch the asset with fetchAsset(publicKey(args.asset)) and check its collection
membership (e.g., asset.collection or equivalent) and only call
fetchCollection(publicKey(args.collection)) if the asset is not already in a
collection; ensure you still use publicKey(args.collection) when needed and keep
existing error/validation logic but move it after the early
collection-membership check to avoid unnecessary network calls.

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.

3 participants