Repository navigation
Accept ICP funding in create_canister / top_up_canister via the CMC - #39
Merged
Merged
Conversation
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
There was a problem hiding this comment.
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→ CMCnotify_create_canister/notify_top_up, returning actionable recovery info on partial failures. - Refactor funding selection logic (
cyclesvsicp) and update outputs/messages to reflect the chosen funding path. - Add CRC32-based AccountIdentifier computation (new
crc32fastdependency) 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.
- 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
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 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 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)." | ||
| ), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
Makes
create_canister/top_up_canistergenuinely fund from ICP, not just size a cycles withdrawal.Before, the
icpargument 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. Nowicpactually spends ICP:notify_create_canister/notify_top_upto mint cycles into the canister.Which accounts must hold what (both keyed to your management principal — the one
cycles_balancereports)cyclesum5iw-…)icpryjl3-…, default subaccount)cyclestakes 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:
CREATE = 0x41455243('CREA'),TOP_UP = 0x50555054('TPUP');[len][principal bytes][zero-pad];AccountIdentifier = CRC32 ‖ SHA-224("\x0Aaccount-id" ‖ owner ‖ subaccount)(adds the smallcrc32fastcrate — already transitive; ICP transfer via the legacytransfermethod with the u64 memo the CMC checks);notify_*result/arg shapes andNotifyErrorvariants.Removes the now-unused ICP→XDR rate query.
Tests
Encoding/structure-level unit tests (all 57 green): CMC subaccount layout,
AccountIdentifierCRC-prefix self-consistency,TransferArgs/NotifyTopUpArg/NotifyCreateCanisterArgencoding, andNotifyError/TransferErrorreply round-trips.🤖 Generated with Claude Code
https://claude.ai/code/session_014T9N8USDfNK5yzznPGg7Ym
Generated by Claude Code