0

General purpose of code:

I am trying to create a helper function that returns true if an element exists in a dynamic array in solidity, and false if an element does not exist at a certain index location. The purpose of this function is to help the findLastIndexLocation function determine the length of a dynamic array.

Problem I am having:

When I call the index location of an element that does not exist (because we reached the end of the dynamic array), the call is Thrown and there is an Error message.

Concise question for you:

I would like to include logic in the if statement of the checkIndex function to detect this throw and return false.

Can you help me with that?

// the goal of the code below is to detect the last index location in a solidity array
// Note: from my understanding, solidity cannot return arrays

// this variable exists to catch the last index location of a certain array
let lastIndexLocation = null;

// this is a "helper" function for the findLastIndexLocation function below
let checkIndex = async (account, index) => {
    if(await contractInstance.methods.notary(account, index)
        .call((error, result) => { 
            // issue is in this code block
            // if result.timestamp or result.digitalFingerprint is undefined, a Throw takes place.
            if(result.timestamp && result.digitalFingerprint) {
                return true
            } else {
                return false
        })
    ){
        return true
    } else { 
        return false
    {
};


let findLastIndexLocation = async (account) => {
    index = 0;

    // start with if statement to check if an array even exists
    if ( await checkIndex(account, index) != true) {
        return 'empty array: no entries where recorded'; 
    } else {
        while(await checkIndex(account, index)) {
            if(await checkIndex(account, index)) {
                index++;
            } else {
                lastIndexLocation = index - 1;
                return lastIndexLocation;
            }
        } 
    }
};
  • 1
    To begin with, checkIndex should be async. Then, you should await before calling contractInstance.notary. Finally, you should await before calling checkIndex. Please start with that and report back if there are any other issues. – goodvibration Aug 24 at 15:06
  • You did not change anything in your question. Am I supposed to guess what you did in your actual code??? You're obviously missing an await somewhere! – goodvibration Aug 26 at 4:56
  • I added async and await. I also used web3@v1.2.1 this time. It still throws an error in the if statement when it reaches the end of the dynamic array. I also made the changes in the actual code. How do I return false when this happens? – Julian Martinez Aug 27 at 23:23
0

Try this:

async function checkIndex(account, index) {
    try {
        const result = await contractInstance.methods.notary(account, index).call();
        return result.timestamp && result.digitalFingerprint;
    }
    catch (error) {
        return false;
    }
}
  • Thanks! The catch() function was what I was missing. – Julian Martinez Aug 29 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.