ApiException: Error calling RunContractFunction: {"code":141,"error":"position is required"}

I cannot use executeFunction to pass parameterized data into the smart contract. I have been working for almost two days now. The following are the code snippets:

leaderBoardData(integer) function

        //❌Leaderboard
        public static async UniTask<string> leaderBoardData(int i)
        {
            //Define function name
            //public const string LEADERBOARD_CALL = "readStructFromMapping";
            string functionName = LeaderBoardContractData.LEADERBOARD_CALL;
            //Define contract request
            //Tried this:
            // object[] parameters = { i.ToString()};
            //
            //Also this:
            // object[] parameters = new object[1];
            // parameters[0] = i;
            //
            //Then this:
            object[] parameters = { i };
            string results = await OnRunContractFunction(functionName, parameters);
            return results;
        }

OnRunContractFunction(function, parameters) function

static async UniTask<string> OnRunContractFunction(string functionName, object[] args)
        {
            //Defining contract data
            string address = LeaderBoardContractData.Address;
            ChainList chain = LeaderBoardContractData.requiredChain;
            object[] abi = LeaderBoardContractData.GetAbiObject();
            //Ensure WalletConnect
            if (WalletConnect.Instance == null)
            {
                throw new Exception("Method failed. WalletConnect.Instance must not be null. Add the WalletConnect.prefab to your scene.");
            }
            //Setup Web3
            await Moralis.SetupWeb3();
            RunContractDto runContractDto = new RunContractDto()
            {
                Abi = abi,
                Params = args
            };
            MoralisClient moralisClient = Moralis.GetClient();
            string result = await moralisClient.Web3Api.Native.RunContractFunction(address, functionName, runContractDto, chain);
            return result;
        }

readStructMapping function object

        //readStructFromMapping
        object[] readStructFromMappingInputParams = new object[1];
        readStructFromMappingInputParams[0] = new { internalType = "uint8", name = "position", type = "uint8" };

        object[] readStructFromMappingOutputParams = new object[2];
        readStructFromMappingOutputParams[0] = new { internalType = "address", name = "refUser", type = "address" };
        readStructFromMappingOutputParams[1] = new { internalType = "uint256", name = "refScore", type = "uint256" };

        newAbi[2] = new { inputs = readStructFromMappingInputParams, outputs = readStructFromMappingOutputParams, name = LEADERBOARD_CALL, stateMutability = "view", type = "function" };

readStructMapping ABI

{
        "inputs": [
            {
                "internalType": "uint8",
                "name": "position",
                "type": "uint8"
            }
        ],
        "name": "readStructFromMapping",
        "outputs": [
            {
                "internalType": "address",
                "name": "refUser",
                "type": "address"
            },
            {
                "internalType": "uint256",
                "name": "refScore",
                "type": "uint256"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    }

maybe this value for position was not provided

Sorry! What do you mean by saying was not provided. Because I am integrating the blockchain into unity game using moralis for my client. Client only provided me with methods to call and execute transactions plus contract address and contract abi.

Not only that function also, I have another function which returns the same error.

{
        "inputs": [
            {
                "internalType": "uint256",
                "name": "index",
                "type": "uint256"
            }
        ],
        "name": "getPrizes",
        "outputs": [
            {
                "internalType": "uint8",
                "name": "prize",
                "type": "uint8"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    }

I mean, from that error that you posted in the thread title, it says that position is required like when it will give that error and that parameter would not be provided

1 Like

Which function is this error coming from? In your parameters for calling the smart contract function you need to pass in a parameter of position.

The error is coming from readStructMapping function in a smart contract here(ApiException: Error calling RunContractFunction: {"code":141,"error":"position is required"}), above smart contract code. On previous code base I had posted you can see that I have passed in the parameter position.

Which version of the Moralis Unity SDK are you using?

Can you see if there’s anything missing from the documentation’s RunContractFunction example e.g. your use of newAbi[2] vs abi[0] (and you don’t seem to be referencing newAbi anywhere else).