refactor(motoko/icp_transfer): call the ICP ledger via canister: import + --actor-id-alias - #1455
Open
marc0olo wants to merge 1 commit into
Open
refactor(motoko/icp_transfer): call the ICP ledger via canister: import + --actor-id-alias#1455marc0olo wants to merge 1 commit into
marc0olo wants to merge 1 commit into
Conversation
…rt + --actor-id-alias
Replace the hand-written ICP ledger actor type (and the hardcoded
`actor("ryjl3-…")` reference) with a typed `canister:icp_ledger` import bound
by moc's `--actor-id-alias`. All ledger types now come from the ledger's
official Candid interface — nothing is hand-declared.
Why `--actor-id-alias` (and not `--actor-env-alias` or generated bindings):
the ICP ledger lives at the *same, fixed, well-known* principal
(ryjl3-tyaaa-aaaaa-aaaba-cai) on both mainnet and the local development
network. When the target id is fixed and universal, binding it at compile time
is the simplest correct choice — no per-environment env-var injection needed
(that's what `--actor-env-alias` is for), and no runtime `actor(id)`
construction (that's for targets chosen at runtime).
- candid/icp_ledger.did — the ledger's official interface, from the
ledger-suite-icp-2025-08-29 release (`ledger.did`).
- mops.toml — `--actor-id-alias icp_ledger ryjl3-tyaaa-aaaaa-aaaba-cai candid/icp_ledger.did`.
- backend/app.mo — `import IcpLedger "canister:icp_ledger"`; types are now
`IcpLedger.Tokens` / `IcpLedger.TransferArgs` / …; removed the hand-written
Tokens/SubAccount/BlockIndex/AccountIdentifier/LedgerTransferArgs/TransferError/
TransferResult and the `actor("ryjl3-…")` line.
Verified end-to-end: `icp deploy && bash test.sh` — all 3 tests pass (real ICP
transfers via the imported ledger, with correct balance deltas).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR refactors the Motoko icp_transfer example to call the ICP ledger via a typed canister:icp_ledger import, with the ledger principal bound at compile time using moc --actor-id-alias. This removes hand-written ledger types and the hardcoded actor("ryjl3-...") reference, aligning the example with the repository’s typed inter-canister call patterns.
Changes:
- Adds the official ICP ledger Candid interface (
candid/icp_ledger.did) and uses it as the single source of truth for ledger request/response types. - Updates
mops.tomlto bindcanister:icp_ledgerto the fixed ledger principal via--actor-id-alias. - Refactors the backend to use
IcpLedger.*types andIcpLedger.transfer(...), removing all locally-declared ledger types/actor wiring, and documents the mechanism in the README.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| motoko/icp_transfer/README.md | Documents the canister:icp_ledger + --actor-id-alias approach and how to refresh the ledger .did. |
| motoko/icp_transfer/mops.toml | Adds --actor-id-alias icp_ledger ryjl3-tyaaa-aaaaa-aaaba-cai candid/icp_ledger.did to moc args. |
| motoko/icp_transfer/candid/icp_ledger.did | Introduces the official ICP ledger Candid interface used for typed imports. |
| motoko/icp_transfer/backend/app.mo | Replaces hand-written ledger types and hardcoded actor with typed canister:icp_ledger import + official Candid types. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Replaces
icp_transfer's hand-written ICP ledger actor type (and the hardcodedactor("ryjl3-…")reference) with a typedcanister:icp_ledgerimport bound by moc's--actor-id-alias. All ledger types now come from the ledger's official Candid interface — nothing is hand-declared.This completes the inter-canister-call taxonomy across the Motoko examples: each moc mechanism matched to the situation it's actually for.
Why
--actor-id-aliashereThe ICP ledger lives at the same, fixed, well-known principal (
ryjl3-tyaaa-aaaaa-aaaba-cai) on both mainnet and the local development network (the managed local network provisions it at the canonical id — that's whytest.shcan useicp token transfer/balance). When the target id is fixed and universal, binding it at compile time is the simplest correct choice.--actor-id-alias--actor-env-aliasactor(id)Using
--actor-env-aliashere would mean addingicp.yamlenv injection to carry an id that never varies — moving parts for no benefit. Generated bindings would be for the runtime/many-targets case.--actor-id-aliaskeeps the constant id in one place (themops.tomlflag) and needs no runtime resolution — while still dropping every hand-written ledger type.Changes
candid/icp_ledger.did— the ledger's official interface, taken from theledger-suite-icp-2025-08-29release (ledger.did).mops.toml—--actor-id-alias icp_ledger ryjl3-tyaaa-aaaaa-aaaba-cai candid/icp_ledger.did(plus the repeated global flags).backend/app.mo—import IcpLedger "canister:icp_ledger"; types are nowIcpLedger.Tokens/IcpLedger.TransferArgs/ etc.; removed the hand-writtenTokens/SubAccount/BlockIndex/AccountIdentifier/LedgerTransferArgs/TransferError/TransferResultand theactor("ryjl3-…")line..did.icp_transferis backend-only (no committedbackend.did), so there's no frontend-bindings impact.Testing
icp deploy && bash test.sh— all 3 tests pass:toAccountIdHexmatchesicp identity account-id, and bothtransferToPrincipalandtransferToAccountIdperform real ICP transfers through the imported ledger with correct balance deltas (−1 ICP sender, +99_990_000 e8s recipient). Build is clean (no warnings).🤖 Generated with Claude Code