Skip to content

Accept ICP funding in create_canister / top_up_canister via the CMC - #39

Merged
aterga merged 2 commits into
mainfrom
claude/mcp-spec-compliance-9202ka
Jul 6, 2026
Merged

aterga merged 2 commits into
mainfrom
claude/mcp-spec-compliance-9202ka

Conversation

@aterga

@aterga aterga commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

What & why

Makes create_canister / top_up_canister genuinely fund from ICP, not just size a cycles withdrawal.

Before, the icp argument only estimated a cycles amount at the CMC's current rate and then spent cycles from the cycles ledger — so a caller who held ICP (but no cycles) still had to convert out-of-band first. Now icp actually spends ICP:

  1. transfer ICP from your ICP-ledger account (management principal, default subaccount) to the CMC's memo-tagged deposit account, then
  2. notify_create_canister / notify_top_up to mint cycles into the canister.

Which accounts must hold what (both keyed to your management principal — the one cycles_balance reports)

Argument Account that must be funded
cycles your cycles-ledger balance (um5iw-…)
icp your ICP-ledger account (ryjl3-…, default subaccount)

cycles takes precedence when both are given. This is now documented in the tool descriptions, the arg field docs, and the README funding section.

Best-effort semantics (no retries)

Single attempt. If the ICP transfer lands but the CMC notify fails, the ICP is held by the CMC and the error message carries the ICP-ledger block index to recover with. The call is not idempotent, so callers must not blindly re-run it (that transfers ICP again).

Verified mechanics

Checked against the canonical CMC / ICP-ledger definitions before writing money-moving code:

  • memos CREATE = 0x41455243 ('CREA'), TOP_UP = 0x50555054 ('TPUP');
  • CMC deposit subaccount = [len][principal bytes][zero-pad];
  • AccountIdentifier = CRC32 ‖ SHA-224("\x0Aaccount-id" ‖ owner ‖ subaccount) (adds the small crc32fast crate — already transitive; ICP transfer via the legacy transfer method with the u64 memo the CMC checks);
  • notify_* result/arg shapes and NotifyError variants.

Removes the now-unused ICP→XDR rate query.

Tests

Encoding/structure-level unit tests (all 57 green): CMC subaccount layout, AccountIdentifier CRC-prefix self-consistency, TransferArgs / NotifyTopUpArg / NotifyCreateCanisterArg encoding, and NotifyError / TransferError reply round-trips.

⚠️ The on-chain money paths cannot be exercised in CI (no mainnet ICP), so they're covered indirectly by the encoding/structure tests above and want a mainnet smoke test before merge (create + top-up with a small ICP amount, and one deliberate notify-failure to confirm the recovery message).

🤖 Generated with Claude Code

https://claude.ai/code/session_014T9N8USDfNK5yzznPGg7Ym


Generated by Claude Code

Previously the `icp` argument only *sized* a cycles-ledger withdrawal at the
CMC's current rate — it never spent ICP, so a caller holding ICP (but no cycles)
still had to convert out-of-band first. Now `icp` genuinely funds from ICP:

- transfer ICP from your ICP-ledger account (management principal, default
  subaccount) to the CMC's memo-tagged deposit account, then
- notify_create_canister / notify_top_up to mint cycles into the canister.

Both funding accounts are keyed to the same management principal that
cycles_balance reports: `cycles` draws from its cycles-ledger balance, `icp` from
its ICP-ledger account. `cycles` takes precedence when both are given. The tool
descriptions and arg field docs now state exactly which account each path spends,
and the README funding section is updated to match.

Best-effort, single attempt (no retries): if the ICP transfer lands but the CMC
notify fails, the ICP is held by the CMC and the error carries the ICP-ledger
block index to recover with — the call is not idempotent, so callers must not
blindly re-run it.

Mechanics verified against the canonical CMC / ICP-ledger definitions: memos
CREATE=0x41455243 / TOP_UP=0x50555054, the length-prefixed CMC deposit
subaccount, and AccountIdentifier = CRC32 ‖ SHA-224("\x0Aaccount-id" ‖ owner ‖
subaccount) (adds the small crc32fast crate, already transitive). Removes the now
-unused ICP→XDR rate query. Adds unit tests for the subaccount layout, the
AccountIdentifier checksum structure, arg encoding, and reply-enum round-trips.

Note: the on-chain money paths cannot be exercised in CI (no mainnet ICP), so
they are covered by encoding/structure tests and want mainnet validation before
merge.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_014T9N8USDfNK5yzznPGg7Ym

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR changes create_canister and top_up_canister so the icp argument actually spends ICP (via an ICP-ledger transfer to the CMC deposit account + CMC notify), instead of only estimating cycles and spending from the cycles ledger. It also updates user-facing documentation/tool descriptions and adds encoding/structure tests around the new CMC/ICP-ledger wire types.

Changes:

  • Implement ICP funding flow: ICP-ledger transfer → CMC notify_create_canister / notify_top_up, returning actionable recovery info on partial failures.
  • Refactor funding selection logic (cycles vs icp) and update outputs/messages to reflect the chosen funding path.
  • Add CRC32-based AccountIdentifier computation (new crc32fast dependency) plus unit tests for subaccount/account-id layout and Candid encoding/decoding of new args/results.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/management.rs Adds ICP-ledger transfer + CMC notify flow for icp funding, removes rate-query conversion path, and introduces new helper/types + tests.
src/main.rs Updates tool descriptions to document ICP-via-CMC funding semantics, precedence, and non-idempotent behavior.
README.md Updates funding documentation to clearly distinguish cycles-ledger vs ICP-ledger funding requirements and semantics.
Cargo.toml Adds crc32fast dependency for AccountIdentifier CRC32 prefixing.
Cargo.lock Locks in crc32fast as a dependency.

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

Comment thread src/management.rs Resolved
Comment thread src/management.rs Resolved
Comment thread src/management.rs Resolved
Comment thread src/management.rs Resolved
- Surface TransferError::BadFee explicitly (expected vs sent fee) instead of
  collapsing it into a debug string, so ICP-ledger fee drift is actionable.
- notify_failed_hint no longer duplicates the method name: update_call already
  prefixes the error with "<method> failed: …", so the wrapper says "the
  follow-up CMC notify did not complete: {e}".
- Recovery guidance no longer implies block_index alone suffices — re-notifying
  needs the same arguments the call used (controller/canister_id/settings), so
  the Processing / Other / transport-failure messages say so.

Text-only changes; the transfer/notify logic is unchanged.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_014T9N8USDfNK5yzznPGg7Ym
@aterga
aterga marked this pull request as ready for review July 6, 2026 20:52
Copilot AI review requested due to automatic review settings July 6, 2026 20:52
@aterga
aterga merged commit 5718046 into main Jul 6, 2026
1 check passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread src/management.rs
Comment on lines +730 to +735
format!(
"ICP transfer succeeded (ICP-ledger block {block}) but the follow-up CMC notify did not \
complete: {e}. Your ICP is held by the CMC — recover it by re-calling {method} for block \
{block} with the SAME arguments this call used (block_index alone is not enough). Do NOT \
re-run this tool; that would transfer ICP again."
)
Comment thread src/management.rs
Comment on lines +750 to +754
NotifyError::Processing => format!(
"the CMC is still processing the deposit. Finish by re-calling {method} for block \
{block} shortly, with the SAME arguments this call used (block_index alone is not \
enough). Do NOT re-run this tool; that would transfer ICP again."
),
Comment thread src/management.rs
Comment on lines +762 to +769
NotifyError::Other {
error_code,
error_message,
} => format!(
"the CMC returned an error ({error_code}): {error_message}. If your ICP left your \
account, recover by re-calling {method} for block {block} with the SAME arguments this \
call used (block_index alone is not enough)."
),
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