Published
Apr 17, 2023
3y ago
Total Downloads
57.0K
57,035
Install
dotnet add package EdjCase.ICP.Candid --version 3.0.0-beta.1
| Product | Versions |
|---|---|
| .NET Standard |
README
Candid - Library of Candid Encoding, Models and Helpers
- Nuget:
EdjCase.ICP.Candid
Using Native Candid Types
CandidArgis the set of candid typed value parameters that is sent to a IC canister for a requestCandidTypedValueis the combo of aCandidValueand its correspondingCandidTypeCandidValueis a raw candid valueCandidTypeis the type definition of a candid type
Create Type
Some Examples:
// Nat (positive integer)
CandidType natType = CandidType.Nat(); // shorthand for `new CandidPrimitiveType(PrimitiveType.Nat)`
// Text (string)
CandidType textType = CandidType.Text(); // shorthand for `new CandidPrimitiveType(PrimitiveType.Text)`
// Opt (optional/nullable) Principal (identifier)
CandidType optionalPrincipalType = CandidType.Opt(CandidType.Principal());
// Record (dictionary/object)
CandidType recordType = new CandidRecordType(new Dictionary<CandidTag, CandidType>
{
{ CandidTag.FromName("name"), CandidType.Text() },
{ CandidTag.FromName("age"), CandidType.Nat() }
});
// Variant (union)
CandidType variantType = new CandidVariantType(new Dictionary<CandidTag, CandidType>
{
{ CandidTag.FromName("option1"), CandidType.Bool() },
{ CandidTag.FromName("option2"), CandidType.Null() }
});
// Vec (list/array) of Float32
CandidType float32Vec = new CandidVectorType(CandidType.Float32());
Create Value
Some Examples:
// Nat (positive integer)
CandidValue natType = CandidValue.Nat(4);
// Text (string)
CandidValue textType = CandidValue.Text("SomeText");
// Opt (optional/nullable) Principal (identifier)
CandidValue optionalPrincipalType = new CandidOptional(CandidValue.Principal(Principal.Anonymous()));
// Record (dictionary/object)
CandidValue recordType = new CandidRecord(new Dictionary<CandidTag, CandidValue>
{
{ CandidTag.FromName("name"), CandidValue.Text("Name1") },
{ CandidTag.FromName("age"), CandidValue.Nat(21) }
});
// Variant (union)
CandidValue variantType = new CandidVariant("option1", CandidValue.Bool(true));
// Vec (list/array) of Float32
CandidValue float32Vec = new CandidVector(new CandidValue[]
{
CandidValue.Float32(1.1f),
CandidValue.Float32(2.2f),
CandidValue.Float32(3.3f)
});
Parse From bytes
When candid is encoded, its in the form of a CandidArg. Individual values and types are not usually encoded
CandidArg arg = CandidByteParser.Parse(rawCandidBytes);
OR
CandidArg arg = CandidArg.FromBytes(rawCandidBytes);
Reading Values (without custom types)
// Assume arg is `(Nat, record { title : Text; length : Nat; })`
CandidArg arg = ...;
CandidTypedValue firstTypedValue = arg.Values[0];
UnboundedUInt natValue = firstTypedValue.Value.AsNat();
CandidTypedValue secondTypedValue = arg.Values[1];
CandidRecord recordValue = secondTypedValue.Value.AsRecord();
string title = recordValue["title"].AsText();
UnboundedUInt length = recordValue["length"].AsNat();
Using Self Defined Types
Self defined types are helpful for predefining classes to the candid schema of an endpoint. This can be done manually (as shown below) or done automattically with the ClientGenerator
From Candid To Self Defined Type
Single argument:
CandidArg arg = ...;
MyObj1 obj = arg.ToObjects<MyObj1>();
Multi arguments:
CandidArg arg = ...;
(MyObj1 obj, MyObj2 obj2) = arg.ToObjects<MyObj1, MyObj2>();
To Candid From Self Defined Type
// Serialze
MyObj obj = new MyObj
{
Title = "Title 1",
IsGoodTitle = false
};
CandidTypedValue value = CandidTypedValue.FromObject(obj);
Defining Variant Types
[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
}
Or if variant options have no type, just an Enum can be used
public enum MyVariant
{
[CandidName("o1")]
Option1,
[CandidName("o2")]
Option2
}
Defining Record Types
public class MyRecord
{
[CandidName("title")] // Used to override name for candid
public string Title { get; set; }
[CandidName("is_good_title")]
public bool IsGoodTitle { get; set; }
}
NOTE: [CandidName(...)] is not needed if the property name is exactly the same as the candid name
// Equivalent to above
public class MyRecord
{
public string title { get; set; }
public bool is_good_title { get; set; }
}
Defining Other Types
(C# type) -> (Candid type)
UnboundedUInt -> Nat
byte -> Nat8
ushort -> Nat16
uint -> Nat32
ulong -> Nat64
UnboundedInt -> Int
sbyte -> Int8
short -> Int16
int -> Int32
long -> Int64
string -> Text
float -> Float32
double -> Float64
bool -> Bool
Principal -> Principal
List<T> -> Vec T
T[] -> Vec T
CandidFunc -> Func
OptionalValue<T> -> Opt T
EmptyValue -> Empty
ReservedValue -> Reserved
NullValue -> Null
Changes from Previous Version
No structural changes between v2.3.9 and v3.0.0-beta.1.
Adoption Assessment
Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.
This version is marked prerelease. Validate its API and upgrade path before production use.No SPDX license expression is published. Review the license text and your organisation's policy before redistribution.Compatibility metadata lists 1 target framework; confirm the exact target used by your application.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.