In this video, we continue building our decentralized application by creating the Candid file (.did) for the smart contract we completed in lib.rs. This step ensures our front-end knows how to communicate with the back-end logic on the Internet Computer.
The Candid file defines:
These declarations bridge communication between the Rust-based backend and the Motoko or frontend code.
type Proposal = record {
description: text;
approve: nat32;
reject: nat32;
pass: nat32;
is_active: bool;
voted: vec principal;
owner: principal;
};
type CreateProposal = record {
description: text;
is_active: bool;
};
To mimic Rust’s Result type:
type Result = variant {
ok: null;
err: VoteError;
};
type VoteError = variant {
AlreadyVoted;
ProposalNotActive;
NoSuchProposal;
AccessRejected;
};
type Choice = variant {
Approve;
Reject;
Pass;
};
We then define the public functions that can be called from the frontend:
service : {
get_proposal: (nat64) -> opt Proposal query;
get_proposal_count: () -> nat64 query;
create_proposal: (nat64, CreateProposal) -> opt Proposal;
edit_proposal: (nat64, CreateProposal) -> Result;
end_proposal: (nat64) -> Result;
vote: (nat64, Choice) -> Result;
};
Your Candid file acts as the contract’s public interface. With the types and function signatures correctly defined, your frontend can now interact seamlessly with the backend. In the next video, we’ll move on to testing the smart contract.
Swap insights and ask questions about “Build on Internet Computer with ICP Rust CDK”.
Ask a question or share your thoughts about this lesson.