Collection of Internet Computer Protocol (ICP) libraries for .NET/Blazor/Unity
Agent - Library to communicate to and from the Internet Computer
Candid - Library of Candid Encoding, Models and Helpers
Client Generator - Client source code generator for ICP canisters
PocketIC - PocketIC Server client and runner for automating tests for canisters
- Download latest
agent.unitypackagefrom: https://github.com/edjCase/ICP.NET/releases - Import
agent.unitypackageinto your unity project - If using WebGL, follow the additional WebGL instructions in the Agent Docs
- If generating a client (see below), place the generated files into the scripts folder:
Assets/scripts/MyClient - Start coding 💻
You can specify all the models and api calls yourself, but this is a tool to automatically generate a client and models based on the canister or .did file
Prerequisite: Have .Net 6 installed (https://dotnet.microsoft.com/en-us/download/dotnet)
Navigate to directory of .Net project
cd {path/to/project}Add Agent nuget package to project
dotnet add package EdjCase.ICP.AgentInstall ClientGenerator
dotnet tool install -g EdjCase.ICP.ClientGeneratorThis will allow a client to be automatically be generated for a canister. See ClientGenerator README for more details and advanced config
Initialize ClientGenerator config (first run only)
candid-client-generator initThis will create a TOML config file in the directory that can be changed for more advanced options
Update created config file
candid-client.tomlIf using a canister id:
namespace = "ProjectGovernance"# Base namespace to useoutput-directory = "./Clients"# Output directory [[clients]] name = "Governance"# Label of client to usetype = "canister"# Indicates to make client from a canister idcanister-id = "rrkah-fqaaa-aaaaa-aaaaq-cai"# Canister id to make client for
If using a service definition file (.did)
namespace = "ProjectGovernance"# Base namespace to useoutput-directory = "./Clients"# Output directory [[clients]] name = "Governance"# Label of client to usetype = "file"# Indicates to make client from a service definition filefile-path = "Governance.did"# File to use
For all configuration options see ClientGenerator README for more details
Generate Client
candid-client-generator genWill output C# file to the output directory specified in the config
Use client in code
varagent=newHttpAgent();PrincipalcanisterId=Principal.FromText("rrkah-fqaaa-aaaaa-aaaaq-cai");varclient=newGovernanceApiClient(agent,canisterId);OptionalValue<Sample.Shared.Governance.Models.ProposalInfo>proposalInfo=awaitclient.GetProposalInfo(110174); ...
SHIP IT! 🚀
The big change here was around variant classes and their attributes. Before the option types were defined by the attribute on each enum member, but in 4.x.x it changed to using method return types and having not type information in attributes. Also the VariantAttribute now gets the enum type from the Tag property vs the attribute
[Variant(typeof(MyVariantTag))] // Required to flag as variant and define options with enum
public class MyVariant
{
[VariantTagProperty] // Flag for tag/enum property, not required if name is `Tag`
public MyVariantTag Tag { get; set; }
[VariantValueProperty] // Flag for value property, not required if name is `Value`
public object? Value { get; set; }
}
public enum MyVariantTag
{
[CandidName("o1")] // Used to override name for candid
Option1,
[CandidName("o2")]
[VariantType(typeof(string))] // Used to specify if the option has a value associated
Option2
}
[Variant] // Required to flag as variant
public class MyVariant
{
[VariantTagProperty] // Flag for tag/enum property, not required if name is `Tag`
public MyVariantTag Tag { get; set; }
[VariantValueProperty] // Flag for value property, not required if name is `Value`
public object? Value { get; set; }
// This method is used to specify if the option has a type/value associated
[VariantOption("o2")] // Specify the candid tag if different than 'As{CandidTag}' like 'Option2' here
public string AsOption2()
{
return (string)this.Value!;
}
}
public enum MyVariantTag
{
[CandidName("o1")] // Used to override name for candid
Option1,
[CandidName("o2")]
Option2
}