Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CallContract (and helper methods) for calling contracts via Metamask #79

Merged
merged 2 commits into from
Sep 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion 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 All @@ -38,6 +50,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 Expand Up @@ -113,4 +147,4 @@ public static int Network()
}

}
#endif
#endif