MissReyyo
MissReyyo

Reputation: 11

I'm having trouble connecting solidty to Unity

I have a contract that I can interact with through the JavaScript interface using Ganache. I want to integrate it into Unity and run it. For this, I used the Nethereum library. However, my contract and C# code cannot communicate, or there seems to be an issue with the library. I couldn't figure it out.

using UnityEngine;
using Nethereum.Web3;
using Nethereum.Web3.Accounts;
using Nethereum.Contracts;
using System.Threading.Tasks;
using System.Numerics;
using System.Collections.Generic;
using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.Contracts.CQS;
using Nethereum.Util;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.Contracts.Extensions;
using Nethereum.RPC.Eth.DTOs;
public class AuthSymbolInteraction : MonoBehaviour
{
    public string contractAddress = "0xA295faf8F5c5894DA2e782A4A5a7900B037389ac";
    public string contractABI = @"[
        {
            ""inputs"": [
                {
                    ""internalType"": ""uint256"",
                    ""name"": ""userId"",
                    ""type"": ""uint256""
                },
                {
                    ""internalType"": ""uint256"",
                    ""name"": ""symbolId"",
                    ""type"": ""uint256""
                }
            ],
            ""name"": ""setSymbol"",
            ""outputs"": [],
            ""stateMutability"": ""nonpayable"",
            ""type"": ""function""
        },
        {
            ""inputs"": [
                {
                    ""internalType"": ""uint256"",
                    ""name"": ""userId"",
                    ""type"": ""uint256""
                },
                {
                    ""internalType"": ""uint256"",
                    ""name"": ""symbolId"",
                    ""type"": ""uint256""
                }
            ],
            ""name"": ""checkSymbol"",
            ""outputs"": [
                {
                    ""internalType"": ""bool"",
                    ""name"": """",
                    ""type"": ""bool""
                }
            ],
            ""stateMutability"": ""view"",
            ""type"": ""function""
        },
        {
            ""inputs"": [
                {
                    ""internalType"": ""uint256"",
                    ""name"": ""userId"",
                    ""type"": ""uint256""
                }
            ],
            ""name"": ""userExists"",
            ""outputs"": [
                {
                    ""internalType"": ""bool"",
                    ""name"": """",
                    ""type"": ""bool""
                }
            ],
            ""stateMutability"": ""view"",
            ""type"": ""function""
        },
        {
            ""inputs"": [
                {
                    ""internalType"": ""uint256"",
                    ""name"": """",
                    ""type"": ""uint256""
                }
            ],
            ""name"": ""users"",
            ""outputs"": [
                {
                    ""internalType"": ""uint256"",
                    ""name"": ""userId"",
                    ""type"": ""uint256""
                },
                {
                    ""internalType"": ""uint256"",
                    ""name"": ""symbolId"",
                    ""type"": ""uint256""
                },
                {
                    ""internalType"": ""bool"",
                    ""name"": ""exists"",
                    ""type"": ""bool""
                }
            ],
            ""stateMutability"": ""view"",
            ""type"": ""function""
        }
    ]";

    public uint userId = 3;
    public uint symbolId = 3;
     private async void Start()
    {
        var url = "HTTP://127.0.0.1:7545";
        var privateKey = "my private key"; 
        var account = new Account(privateKey);
        var web3 = new Web3(account, url);
        var contract = web3.Eth.GetContract(contractABI, contractAddress);
        var setSymbolFunction = contract.GetFunction("setSymbol");
        var checkSymbolFunction = contract.GetFunction("checkSymbol");
        var userExistsFunction = contract.GetFunction("userExists");

        var setSymbolReceipt = await setSymbolFunction.SendTransactionAndWaitForReceiptAsync(web3.TransactionManager.Account.Address, null, null, userId.ToString(), symbolId.ToString());


        Debug.Log("setSymbol Transaction Hash: " + setSymbolReceipt.TransactionHash);

        
        var checkSymbolResult = await checkSymbolFunction.CallAsync<bool>(userId, symbolId);


        Debug.Log("Symbol Exists: " + checkSymbolResult);

        
        var userExistsResult = await userExistsFunction.CallAsync<bool>(userId);

        Debug.Log("User Exists: " + userExistsResult);
    }
}

error:

Exception: Function not found:setSymbol
Nethereum.Contracts.ContractBuilder.GetFunctionAbi (System.String name) (at <6bb0d176930b4bd4876aa91ecbf3c219>:0)
Nethereum.Contracts.ContractBuilder.GetFunctionBuilder (System.String name) (at <6bb0d176930b4bd4876aa91ecbf3c219>:0)
Nethereum.Contracts.Contract.GetFunctionBuilder (System.String name) (at <6bb0d176930b4bd4876aa91ecbf3c219>:0)
Nethereum.Contracts.Contract.GetFunction (System.String name) (at <6bb0d176930b4bd4876aa91ecbf3c219>:0)
AuthSymbolInteraction.Start () (at Assets/Scripts/AuthSymbolInteraction.cs:119)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) (at <8f02bdf82edc4309b57af878cc7723ff>:0)
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at /home/bokken/build/output/unity/unity/Runtime/Export/Scripting/UnitySynchronizationContext.cs:153)
UnityEngine.UnitySynchronizationContext.Exec () (at /home/bokken/build/output/unity/unity/Runtime/Export/Scripting/UnitySynchronizationContext.cs:83)
UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at /home/bokken/build/output/unity/unity/Runtime/Export/Scripting/UnitySynchronizationContext.cs:107)

Upvotes: 1

Views: 77

Answers (1)

MissReyyo
MissReyyo

Reputation: 11

. I clicked right button and I choose "Solidity:Compile and Code generate CSharp contract definition" . I added this codes.

 [Function("setSymbol")]
public class SetSymbolFunction : FunctionMessage
{
    [Parameter("uint256", "userId", 1)]
    public BigInteger UserId { get; set; }

    [Parameter("uint256", "symbolId", 2)]
    public BigInteger SymbolId { get; set; }
}

[Function("checkSymbol", "bool")]

public class CheckSymbolFunction : FunctionMessage
{
    [Parameter("uint256", "userId", 1)]
    public BigInteger UserId { get; set; }

    [Parameter("uint256", "symbolId", 2)]
    public BigInteger SymbolId { get; set; }
}

[Function("userExists", "bool")]

public class UserExistsFunction : FunctionMessage
{
    [Parameter("uint256", "userId", 1)]
    public BigInteger UserId { get; set; }
}

Upvotes: 0

Related Questions