Skip to content

Commit

Permalink
Merge pull request #79 from blevinstein/main
Browse files Browse the repository at this point in the history
Add CallContract (and helper methods) for calling contracts via Metamask
  • Loading branch information
KBryan authored Sep 4, 2022
2 parents ef5ebaa + 087712d commit 80a96ae
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Web3Unity/Scripts/Library/WebGL/Web3GL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ public class Web3GL
[DllImport("__Internal")]
private static extern void SetContractResponse(string value);

[DllImport("__Internal")]
private static extern void CallContractJs(string method, string abi, string contract, string args);

[DllImport("__Internal")]
private static extern void ResetCallContractResponse();

[DllImport("__Internal")]
private static extern string CallContractResponse();

[DllImport("__Internal")]
private static extern string CallContractError();

[DllImport("__Internal")]
private static extern void SendTransactionJs(string to, string value, string gasLimit, string gasPrice);

Expand Down Expand Up @@ -54,6 +66,28 @@ public class Web3GL
[DllImport("__Internal")]
private static extern int GetNetwork();

async public static Task<string> CallContract(string _method, string _abi, string _contract, string _args, float _waitSeconds = 0.1f)
{
ResetCallContractResponse();
CallContractJs(_method, _abi, _contract, _args);
string response = CallContractResponse();
string error = CallContractError();
while (response == "" && error == "")
{
await new WaitForSeconds(_waitSeconds);
response = CallContractResponse();
error = CallContractError();
}
ResetCallContractResponse();
if (error.Length > 0)
{
throw new Exception(error);
} else
{
return response;
}
}

// this function will create a metamask tx for user to confirm.
async public static Task<string> SendContract(string _method, string _abi, string _contract, string _args, string _value, string _gasLimit = "", string _gasPrice = "")
{
Expand Down

0 comments on commit 80a96ae

Please sign in to comment.