Skip to content

Commit

Permalink
chore: public readonly property for CandidFunc (#130)
Browse files Browse the repository at this point in the history
* chore: CandidFunc public property

* chore: remove new properties

* fix: typo
  • Loading branch information
witter-deland authored Oct 21, 2024
1 parent 3875c3f commit d17263f
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 47 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Collection of Internet Computer Protocol (ICP) libraries for .NET/Blazor/Unity
- 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
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
```
Expand Down Expand Up @@ -141,4 +141,4 @@ public enum MyVariantTag
- [IC Http Interface Spec](https://smartcontracts.org/docs/current/references/ic-interface-spec)
- [Candid Spec](https://github.com/dfinity/candid/blob/master/spec/Candid.md)
- [Candid Decoder](https://fxa77-fiaaa-aaaae-aaana-cai.raw.ic0.app/explain)
- [Candid UI Tester](https://a4gq6-oaaaa-aaaab-qaa4q-cai.raw.ic0.app)
- [Candid UI Tester](https://a4gq6-oaaaa-aaaab-qaa4q-cai.raw.ic0.app)
14 changes: 7 additions & 7 deletions src/Agent/API.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/Agent/Agents/IAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace EdjCase.ICP.Agent.Agents
{
/// <summary>
/// An agent is used to communicate with the Internet Computer with certain protocols that
/// An agent is used to communicate with the Internet Computer with certain protocols that
/// are specific to an `IAgent` implementation
/// </summary>
public interface IAgent
Expand All @@ -24,7 +24,7 @@ public interface IAgent
/// <param name="canisterId">Canister to read state for</param>
/// <param name="paths">The state paths to get information for. Other state data will be pruned if not specified</param>
/// <param name="cancellationToken">Optional. Token to cancel request</param>
/// <returns>A response that contains the certificate of the current cansiter state</returns>
/// <returns>A response that contains the certificate of the current canister state</returns>
Task<ReadStateResponse> ReadStateAsync(Principal canisterId, List<StatePath> paths, CancellationToken? cancellationToken = null);

/// <summary>
Expand All @@ -37,12 +37,12 @@ public interface IAgent
Task<RequestStatus?> GetRequestStatusAsync(Principal canisterId, RequestId id, CancellationToken? cancellationToken = null);

/// <summary>
/// Sends a call request to a specified canister method and gets back an id of the
/// Sends a call request to a specified canister method and gets back an id of the
/// request that is being processed. This call does NOT wait for the request to be complete.
/// Either check the status with `GetRequestStatusAsync` or use the `CallAndWaitAsync` method
/// </summary>
/// <param name="canisterId">Canister to read state for</param>
/// <param name="method">The name of the method to call on the cansiter</param>
/// <param name="method">The name of the method to call on the canister</param>
/// <param name="arg">The candid arg to send with the request</param>
/// <param name="effectiveCanisterId">Optional. Specifies the relevant canister id if calling the root canister</param>
/// <param name="cancellationToken">Optional. Token to cancel request</param>
Expand All @@ -60,7 +60,7 @@ public interface IAgent
/// Sends a query request to a specified canister method
/// </summary>
/// <param name="canisterId">Canister to read state for</param>
/// <param name="method">The name of the method to call on the cansiter</param>
/// <param name="method">The name of the method to call on the canister</param>
/// <param name="arg">The candid arg to send with the request</param>
/// <param name="cancellationToken">Optional. Token to cancel request</param>
/// <returns>The response data of the query call</returns>
Expand All @@ -86,7 +86,7 @@ public static class IAgentExtensions
/// </summary>
/// <param name="agent">The agent to use for the call</param>
/// <param name="canisterId">Canister to read state for</param>
/// <param name="method">The name of the method to call on the cansiter</param>
/// <param name="method">The name of the method to call on the canister</param>
/// <param name="arg">The candid arg to send with the request</param>
/// <param name="effectiveCanisterId">Optional. Specifies the relevant canister id if calling the root canister</param>
/// <param name="cancellationToken">Optional. Token to cancel request</param>
Expand Down
4 changes: 2 additions & 2 deletions src/Agent/Responses/QueryResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public enum QueryResponseType
/// </summary>
Replied,
/// <summary>
/// When the cansiter request has errors to query request
/// When the canister request has errors to query request
/// </summary>
Rejected
}
Expand Down Expand Up @@ -229,4 +229,4 @@ internal QueryRejectInfo(RejectCode code, string? message, string? errorCode)
this.ErrorCode = errorCode;
}
}
}
}
6 changes: 3 additions & 3 deletions src/Candid/API.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/Candid/Models/Principal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public string ToHex()
///
/// The method expects a sub-account byte array of exactly 32 bytes in length. If the provided array does not meet this
/// requirement, an <see cref="ArgumentException"/> is thrown.
///
///
/// The account identifier format follows the specification:
/// account_identifier(principal, subaccount_identifier) = CRC32(h) || h
/// where h = sha224("\x0Aaccount-id" || principal || subaccount_identifier).
Expand All @@ -119,7 +119,7 @@ public byte[] ToLedgerAccount(byte[]? subAccount)
if (subAccount == null)
{
// Empty byte array of 32 bytes for no subaccount
subAccount = new byte[32];
subAccount = new byte[32];
}
// Ensure the subAccount is of expected length (32 bytes)
if (subAccount.Length != 32)
Expand Down Expand Up @@ -147,9 +147,9 @@ public byte[] ToLedgerAccount(byte[]? subAccount)
}

/// <summary>
/// Helper method to create the principal for the Internet Computer management cansiter "aaaaa-aa"
/// Helper method to create the principal for the Internet Computer management canister "aaaaa-aa"
/// </summary>
/// <returns>Principal for the management cansiter</returns>
/// <returns>Principal for the management canister</returns>
public static Principal ManagementCanisterId()
{
return new Principal(PrincipalType.Opaque, new byte[0]);
Expand Down Expand Up @@ -287,4 +287,4 @@ public override int GetHashCode()
return !v1.Equals(v2);
}
}
}
}
26 changes: 15 additions & 11 deletions src/Candid/Models/Values/CandidFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using EdjCase.ICP.Candid.Utilities;
using System;
using System.Buffers;
using System.Linq;
using System.Text;

namespace EdjCase.ICP.Candid.Models.Values
{
Expand All @@ -21,33 +19,36 @@ public class CandidFunc : CandidValue
/// </summary>
public bool IsOpaqueReference { get; }

private readonly (CandidService Service, string Method)? serviceInfo;

public (CandidService Service, string Method)? ServiceInfo { get; }

Check warning on line 23 in src/Candid/Models/Values/CandidFunc.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'CandidFunc.ServiceInfo'

Check warning on line 23 in src/Candid/Models/Values/CandidFunc.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'CandidFunc.ServiceInfo'

Check warning on line 23 in src/Candid/Models/Values/CandidFunc.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'CandidFunc.ServiceInfo'

Check warning on line 23 in src/Candid/Models/Values/CandidFunc.cs

View workflow job for this annotation

GitHub Actions / Publish

Missing XML comment for publicly visible type or member 'CandidFunc.ServiceInfo'

/// <param name="service">The candid service definition the function lives in</param>
/// <param name="name">The name of the function</param>
public CandidFunc(CandidService service, string name)
{
this.IsOpaqueReference = false;
this.serviceInfo = (service, name);
this.ServiceInfo = (service, name);
}


private CandidFunc()
{
this.IsOpaqueReference = true;
this.serviceInfo = null;
this.ServiceInfo = null;
}

/// <inheritdoc />
internal override void EncodeValue(CandidType type, Func<CandidId, CandidCompoundType> getReferencedType, IBufferWriter<byte> destination)
internal override void EncodeValue(CandidType type, Func<CandidId, CandidCompoundType> getReferencedType,
IBufferWriter<byte> destination)
{
if (this.IsOpaqueReference)
{
destination.WriteOne<byte>(0);
return;
}

CandidFuncType t = DereferenceType<CandidFuncType>(type, getReferencedType);
(CandidService service, string method) = this.serviceInfo!.Value;
(CandidService service, string method) = this.ServiceInfo!.Value;
destination.WriteOne<byte>(1); // Encode bit to indicate it is not opaque
service.EncodeValue(t, getReferencedType, destination); // Encode value
destination.WriteUtf8LebAndValue(method); // Encode method name
Expand All @@ -56,7 +57,7 @@ internal override void EncodeValue(CandidType type, Func<CandidId, CandidCompoun
/// <inheritdoc />
public override int GetHashCode()
{
return HashCode.Combine(this.IsOpaqueReference, this.serviceInfo);
return HashCode.Combine(this.IsOpaqueReference, this.ServiceInfo);
}

/// <inheritdoc />
Expand All @@ -68,13 +69,16 @@ public override bool Equals(CandidValue? other)
{
return false;
}

if (this.IsOpaqueReference)
{
// TODO can we tell if they are equal?
return false;
}
return this.serviceInfo == f.serviceInfo;

return this.ServiceInfo == f.ServiceInfo;
}

return false;
}

Expand All @@ -85,7 +89,8 @@ public override string ToString()
{
return "(Opaque Reference)";
}
(CandidService service, string method) = this.serviceInfo!.Value;

(CandidService service, string method) = this.ServiceInfo!.Value;
return $"(Method: {method}, Service: {service})";
}

Expand All @@ -99,5 +104,4 @@ public static CandidFunc OpaqueReference()
return new CandidFunc();
}
}

}
4 changes: 2 additions & 2 deletions src/ClientGenerator/API.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d17263f

Please sign in to comment.