Coder Social home page Coder Social logo

jsonpythonwill / icp.net Goto Github PK

View Code? Open in Web Editor NEW

This project forked from boomdao/icp.net

0.0 0.0 0.0 6.12 MB

A library for .NET/C#/Unity to natively communicate with the Internet Computer (ICP)

License: MIT License

JavaScript 3.30% C# 96.70%

icp.net's Introduction

ICP.NET

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

See each individual project README for more in depth guides

๐ŸŽฎ Unity Integration

  • Download latest agent.unitypackage from: https://github.com/edjCase/ICP.NET/releases
  • Import agent.unitypackage into 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 ๐Ÿ’ป

๐Ÿ“ก Generating a client for a canister

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 cansiter 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.Agent
    
  • Install ClientGenerator

    dotnet tool install -g EdjCase.ICP.ClientGenerator
    

    This 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 init
    

    This will create a TOML config file in the directory that can be changed for more advanced options

  • Update created config file candid-client.toml

    If using a canister id:

    namespace = "ProjectGovernance" # Base namespace to use
    output-directory = "./Clients" # Output directory
    
    [[clients]]
    name = "Governance" # Label of client to use
    type = "canister" # Indicates to make client from a canister id
    canister-id = "rrkah-fqaaa-aaaaa-aaaaq-cai" # Canister id to make client for

    If using a service definition file (.did)

    namespace = "ProjectGovernance" # Base namespace to use
    output-directory = "./Clients" # Output directory
    
    [[clients]]
    name = "Governance" # Label of client to use
    type = "file" # Indicates to make client from a service definition file
    file-path = "Governance.did" # File to use

    For all configuration options see ClientGenerator README for more details

  • Generate Client

    candid-client-generator gen
    

    Will output C# file to the output directory specified in the config

  • Use client in code

    var agent = new HttpAgent();
    Principal canisterId = Principal.FromText("rrkah-fqaaa-aaaaa-aaaaq-cai");
    var client = new GovernanceApiClient(agent, canisterId);
    OptionalValue<Sample.Shared.Governance.Models.ProposalInfo> proposalInfo = await client.GetProposalInfo(110174);
    ...
  • SHIP IT! ๐Ÿš€

Breaking change migrations

3.x.x => 4.x.x

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

Version 3

[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
}

Version 4

[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
}

Candid Related Links

icp.net's People

Contributors

gekctek avatar godcare avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.