Unity ExecuteContractFunction

Hi,

I’m trying (without luck) to interact with a smart contract through Unity with ExecuteContractFunction(), but new to all this and can’t find any up to date examples on how to do it.

Could anyone provide a snippet that explain how to do it?

Thanks in advance!

//Adam

2 Likes

See if this doc helps.
https://docs.moralis.io/moralis-dapp/web3/web3#executefunction

You can check the forum if any community member has posted any read-only functions and try to run it with executefunction.

You can also follow this tutorial.

Thanks, this helps a little bit, but my problem is that I’m not experienced enough to translate from JS to C#.
For instance the ABI parameter expects a string, but is a bunch of json data. How would I format that?

Where are you at with your code?

JSON data is naturally a string so if it’s in a different format it needs to be converted.

Check this git, it has some moralis functions in C#. Hope this helps.

Thanks, I’ve been looking at this git, but the functions they’re using are deprecated. It’s the ExecuteContractFunction method you’re supposed to use from what I understand.
Unfortunatly there are no examples in C# I can find, and translating from JS is above my skill-level.
The below code is where I am now and this only generates this error:

Call to createBasicProton failed due to: Object reference not set to an instance of an object
UnityEngine.Debug:Log (object)

Any pointers would be super appriciated!

    const string ABI = @"
        [ 
            { 
                ""inputs"": [
                     {
                         ""internalType"": ""address"",
                         ""name"": ""creator"",
                         ""type"": ""address""
                     },
                     {
                         ""internalType"": ""address"",
                         ""name"": ""receiver"",
                         ""type"": ""address""
                     },
                     {
                         ""internalType"": ""string"",
                         ""name"": ""tokenMetaUri"",
                         ""type"": ""string""
                     }
                ],
                ""name"": ""createBasicProton"",
                ""outputs"": [
                    {
                         ""internalType"": ""uint256"",
                         ""name"": ""newTokenId"",
                         ""type"": ""uint256""
                    }
                ],
                ""stateMutability"": ""nonpayable"",
                ""type"": ""function""
            }
        ]";

    public async void MintBasicProton()
    {

        MoralisUser user = await MoralisInterface.GetUserAsync();

        string addr = user.authData["moralisEth"]["id"].ToString();

        string recieverAddress = addr;
        string creatorAddress = addr;
        string contractAddress = "0xe2a9b15E283456894246499Fb912CCe717f83319";
        string metaURI = @"https://firebasestorage.googleapis.com/XXXXXXXXXX";

        object[] pars = { creatorAddress, recieverAddress, metaURI };
        HexBigInteger val = new HexBigInteger("0x0");
        HexBigInteger gas = new HexBigInteger("0x0");
        HexBigInteger price = new HexBigInteger("0x0");
        try
        {
            string resp = await MoralisInterface.ExecuteContractFunction(contractAddress, ABI, "createBasicProton", pars, val, gas, price);
            Debug.Log($"$Send Transaction respo: {resp}");
        }
        catch (Exception exp)
        {
            Debug.Log($"Send transaction failed: {exp.Message}");
        }
    }

Can you try converting this JSON in to a string?
https://jsontostring.com/

This is helpful, thank you. I have a further nuance where the smart contract function is only callable by the owner (onlyAdmin) how is that done within the SDK for unity? I have viewed the vids and have not been able to find an example of this.

You will have to sign the transaction using the owner wallet address. This tutorial covers contract interactions with the new SDK.