GovernanceService class provides methods to interact with the Internet Computer’s Network Nervous System (NNS) governance canister and other governance canisters. This service enables bots to track proposals, monitor voting status, and retrieve governance-related information.
Overview
The GovernanceService integrates with the NNS governance canister (rrkah-fqaaa-aaaaa-aaaaq-cai) to provide access to proposal data, voting information, and governance metadata. This is particularly useful for bots that need to:
- Monitor NNS proposals and voting activity
- Track proposal status and outcomes
- Retrieve governance canister metadata
- List available governance functions
Class Definition
Methods
listProposals
Retrieves a list of proposals from a governance canister based on the provided filter criteria.The canister ID of the governance canister to query. Use
"rrkah-fqaaa-aaaaa-aaaaq-cai" for the NNS governance canister.Filter criteria for listing proposals. See GovernanceTypes for details.Key fields include:
limit: Maximum number of proposals to return (Nat32)before_proposal: Return proposals before this proposal IDinclude_status: Array of proposal status codes to includeinclude_reward_status: Array of reward status codes to includeexclude_topic: Array of topic IDs to excludeomit_large_fields: Whether to omit large fields for performance
Returns
#ok(ListProposalInfoResponse) containing an array of ProposalInfo objects, or #err(Text) with an error message if the request fails.getPendingProposals
Retrieves all currently pending proposals from a governance canister.The canister ID of the governance canister to query.
Returns
#ok([ProposalInfo]) containing an array of pending proposals, or #err(Text) with an error message if the request fails.getMetadata
Retrieves metadata about a governance canister, including its name and description.The canister ID of the governance canister to query. For the NNS governance canister (
rrkah-fqaaa-aaaaa-aaaaq-cai), returns hardcoded values.Returns
#ok({name: ?Text; description: ?Text}) containing the governance canister’s metadata, or #err(Text) with an error message if the request fails.For the NNS governance canister:name:?"NNS"description:?"Network Nervous System"
getGovernanceFunctions
Retrieves the list of available nervous system functions from a governance canister.The canister ID of the governance canister to query.
Returns
#ok(ListNervousSystemFunctionsResponse) containing the list of available nervous system functions and reserved IDs, or #err(Text) with an error message if the request fails.The response includes:functions: Array of NervousSystemFunction objectsreserved_ids: Array of Nat64 IDs that are reserved
Constants
NNS_GOVERNANCE_ID
The canister ID of the NNS governance canister:Error Handling
All methods return aResult type that must be handled:
#ok(value): The operation succeeded, contains the response data#err(message): The operation failed, contains an error message string
- Invalid canister ID
- Network connectivity issues
- Canister is not a governance canister
- Insufficient permissions