Skip to content

Commit

Permalink
Merge pull request #88 from dojoengine/controller-class
Browse files Browse the repository at this point in the history
feat(controller): add a new controller class
  • Loading branch information
Larkooo authored Jan 30, 2025
2 parents e307839 + c9699c7 commit 748fbfc
Show file tree
Hide file tree
Showing 19 changed files with 42,164 additions and 35,009 deletions.
Binary file modified Assets/Dojo/Plugins/Linux/libdojo_c.so
Binary file not shown.
4 changes: 2 additions & 2 deletions Assets/Dojo/Plugins/Windows/libdojo_c.dll
Git LFS file not shown
4 changes: 2 additions & 2 deletions Assets/Dojo/Plugins/iOS/libdojo_c.a
Git LFS file not shown
Binary file modified Assets/Dojo/Plugins/macOS/libdojo_c.bundle
Binary file not shown.
161 changes: 161 additions & 0 deletions Assets/Dojo/Runtime/Controller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
using System;
using System.Threading.Tasks;
using bottlenoselabs.C2CS.Runtime;
using dojo_bindings;
using UnityEngine;
using Dojo.Starknet;
using System.Linq;

namespace Dojo
{
public struct Policy
{
public FieldElement target;
public string method;
public string description;

public Policy(FieldElement target, string method, string description)
{
this.target = target;
this.method = method;
this.description = description;
}

public dojo.Policy ToNative()
{
return new dojo.Policy { target = target.Inner, method = method, description = description };
}
}

public unsafe class Controller
{
private dojo.Controller* controller;
public FieldElement Address => new FieldElement(dojo.controller_address(controller));
public FieldElement ChainId => new FieldElement(dojo.controller_chain_id(controller));
public string Username => CString.ToString(dojo.controller_username(controller));

private Controller(dojo.Controller* controller)
{
this.controller = controller;
}

public static Controller GetAccount(Policy[] policies, FieldElement chainId)
{
var nativePolicies = policies.Select(p => p.ToNative()).ToArray();
dojo.Policy* policiesPtr = null;
if (nativePolicies.Length > 0)
{
fixed (dojo.Policy* ptr = &nativePolicies[0])
{
policiesPtr = ptr;
}
}

var result = dojo.controller_account(policiesPtr, (UIntPtr)policies.Length, chainId.Inner);
if (result.tag == dojo.ResultController_Tag.ErrController)
{
Debug.LogWarning(result.err.message);
return null;
}

return new Controller(result._ok);
}

public static Task<Controller> Connect(string rpcUrl, Policy[] policies)
{
var nativePolicies = policies.Select(p => p.ToNative()).ToArray();
var connectionTask = new TaskCompletionSource<Controller>();
CString crpcUrl = CString.FromString(rpcUrl);

dojo.Policy* policiesPtr = null;
if (nativePolicies.Length > 0)
{
fixed (dojo.Policy* ptr = &nativePolicies[0])
{
policiesPtr = ptr;
}
}

var onConnect = new dojo.FnPtr_ControllerPtr_Void((controllerPtr) =>
{
var controller = new Controller(controllerPtr);
connectionTask.SetResult(controller);
});

dojo.controller_connect(crpcUrl, policiesPtr, (UIntPtr)policies.Length, onConnect);

return connectionTask.Task;
}

public static bool Clear(Policy[] policies, FieldElement chainId)
{
var nativePolicies = policies.Select(p => p.ToNative()).ToArray();
dojo.Policy* policiesPtr = null;
if (nativePolicies.Length > 0)
{
fixed (dojo.Policy* ptr = &nativePolicies[0])
{
policiesPtr = ptr;
}
}

var result = dojo.controller_clear(policiesPtr, (UIntPtr)policies.Length, chainId.Inner);
if (result.tag == dojo.Resultbool_Tag.Errbool)
{
throw new Exception(result.err.message);
}

return result.ok;
}

public FieldElement ExecuteRaw(dojo.Call[] calls)
{
dojo.Call* callsPtr;
fixed (dojo.Call* ptr = &calls[0])
{
callsPtr = ptr;
}

var result = dojo.controller_execute_raw(controller, callsPtr, (UIntPtr)calls.Length);
if (result.tag == dojo.ResultFieldElement_Tag.ErrFieldElement)
{
throw new Exception(result.err.message);
}

return new FieldElement(result.ok);
}

public FieldElement ExecuteFromOutside(dojo.Call[] calls)
{
dojo.Call* callsPtr;
fixed (dojo.Call* ptr = &calls[0])
{
callsPtr = ptr;
}

var result = dojo.controller_execute_from_outside(controller, callsPtr, (UIntPtr)calls.Length);
if (result.tag == dojo.ResultFieldElement_Tag.ErrFieldElement)
{
throw new Exception(result.err.message);
}

return new FieldElement(result.ok);
}

public FieldElement Nonce()
{
if (controller == null)
{
throw new InvalidOperationException("Controller is not initialized");
}

var result = dojo.controller_nonce(controller);
if (result.tag == dojo.ResultFieldElement_Tag.ErrFieldElement)
{
throw new Exception(result.err.message);
}

return new FieldElement(result.ok);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Dojo/Runtime/Controller.cs.meta

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

2 changes: 1 addition & 1 deletion Assets/Dojo/Runtime/Torii/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public dojo.MemberValue ToNative()
if (String != null)
return new dojo.MemberValue { tag = dojo.MemberValue_Tag.String, @string = String };
if (Primitive.HasValue)
return new dojo.MemberValue { tag = dojo.MemberValue_Tag.Primitive, primitive = Primitive.Value.ToNative() };
return new dojo.MemberValue { tag = dojo.MemberValue_Tag.PrimitiveValue, primitive_value = Primitive.Value.ToNative() };
if (List != null)
return new dojo.MemberValue { tag = dojo.MemberValue_Tag.List, list = List.Select(l => l.ToNative()).ToArray() };

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// To disable generating this file set `isEnabledGenerateAssemblyAttributes` to `false` in the config file for generating C# code.
// <auto-generated>
// This code was generated by the following tool on 2024-12-27 13:52:58 GMT+07:00:
// This code was generated by the following tool on 2025-01-30 11:15:25 GMT+07:00:
// https://github.com/bottlenoselabs/c2cs (v0.0.0.0)
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
Expand Down
2 changes: 1 addition & 1 deletion Assets/Dojo/Runtime/bindings/client/Runtime.gen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// To disable generating this file set `isEnabledGeneratingRuntimeCode` to `false` in the config file for generating C# code.

// <auto-generated>
// This code was generated by the following tool on 2024-12-27 13:52:58 GMT+07:00:
// This code was generated by the following tool on 2025-01-30 11:15:25 GMT+07:00:
// https://github.com/bottlenoselabs/c2cs (v0.0.0.0)
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
Expand Down
Loading

0 comments on commit 748fbfc

Please sign in to comment.