WebGLSendContractExample.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. #if UNITY_WEBGL
  6. public class WebGLSendContractExample : MonoBehaviour
  7. {
  8. async public void OnSendContract()
  9. {
  10. // smart contract method to call
  11. string method = "addTotal";
  12. // abi in json format
  13. string abi = "[ { \"inputs\": [ { \"internalType\": \"uint8\", \"name\": \"_myArg\", \"type\": \"uint8\" } ], \"name\": \"addTotal\", \"outputs\": [], \"stateMutability\": \"nonpayable\", \"type\": \"function\" }, { \"inputs\": [], \"name\": \"myTotal\", \"outputs\": [ { \"internalType\": \"uint256\", \"name\": \"\", \"type\": \"uint256\" } ], \"stateMutability\": \"view\", \"type\": \"function\" } ]";
  14. // address of contract
  15. string contract = "0x7286Cf0F6E80014ea75Dbc25F545A3be90F4904F";
  16. // array of arguments for contract
  17. string args = "[\"1\"]";
  18. // value in wei
  19. string value = "0";
  20. // gas limit OPTIONAL
  21. string gasLimit = "";
  22. // gas price OPTIONAL
  23. string gasPrice = "";
  24. // connects to user's browser wallet (metamask) to update contract state
  25. try
  26. {
  27. string response = await Web3GL.SendContract(method, abi, contract, args, value, gasLimit, gasPrice);
  28. Debug.Log(response);
  29. }
  30. catch (Exception e)
  31. {
  32. Debug.LogException(e, this);
  33. }
  34. }
  35. }
  36. #endif