0

I have deployed the following smart contract deployed on a Geth private blockchain:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract CRUD {
    
    struct Data {
        uint id;
        bytes32 contentHash;
        string description;
    }
    
    // Variables de estado 
    Data[] data;

    uint nextId;
    
    // Operaciones CRUD: CREATE, READ, UPDATE y DELETE:

    // Operación CREATE
    function createData(string memory _description) public{
        bytes32 contentHash = createHash(_description);
        data.push(Data(nextId, contentHash, _description));
        nextId++;
    }

    // Operación READ por búsqueda del id
    function readData(uint _id) public view returns (uint, bytes32,string memory) {
        uint index = findIndex(_id);
        return (data[index].id, data[index].contentHash, data[index].description);
    }

    // Operación READ por búsqueda del hash de la entrada de datos
    function readDataByHash(bytes32 _hash) public view returns (uint, bytes32,string memory) {
        uint index = findByHash(_hash);
        return (data[index].id, data[index].contentHash, data[index].description);
    }

    //Operacion READ todos las entradas de datos
    function readAllData() public view returns (Data[] memory){
        return data;
    }

    //Operacion COUNT de todas las entradas de datos
    function countAllData() public view returns (uint){
        return data.length;
    }


    // Operación UPDATE
    function updateData(uint _id, string memory _description) public returns (uint, bytes32, string memory){
        uint index =  findIndex(_id);
        bytes32 contentHash = createHash(_description);
        data[index].contentHash= contentHash;
        data[index].description = _description;
        return (data[index].id, data[index].contentHash, data[index].description);
    }
    
    // Operación DELETE
    function deleteData(uint _id) public{
        uint index = findIndex(_id);
        delete data[index];
    }

    // Creación del hash
    function createHash(string memory _description) internal pure returns (bytes32){
        return keccak256(abi.encodePacked(_description));
    }

    // Búsqueda del id
    function findIndex(uint _id) internal view returns (uint) {
        for (uint i = 0; i < data.length; i++) {
            if (data[i].id == _id) {                
                return i;
            }
        }
        revert("Data not found");
    }

    // Busqueda por el hash de la entrada de datos
    function findByHash(bytes32 _contentHash) internal view returns (uint) {
        for (uint i = 0; i < data.length; i++) {
            if (data[i].contentHash == _contentHash) {                
                return i;
            }
        }
        revert("Data not found");
    }
    
}

I have a Node.js express API with the contract instance, and when I call countAllData in an endpoint, it keeps returning the following error:

Error: Returned error: execution reverted
    at Object.ErrorResponse (/home/ubu/Escritorio/node_modules/web3-core-helpers/lib/errors.js:28:19)
    at /home/ubu/Escritorio/node_modules/web3-core-requestmanager/lib/index.js:300:36
    at XMLHttpRequest.request.onreadystatechange (/home/ubu/Escritorio/node_modules/web3-providers-http/lib/index.js:98:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/ubu/Escritorio/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (/home/ubu/Escritorio/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpResponseEnd (/home/ubu/Escritorio/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
    at IncomingMessage.<anonymous> (/home/ubu/Escritorio/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)
    at IncomingMessage.emit (node:events:539:35)
    at endReadableNT (node:internal/streams/readable:1345:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  data: null
}

The Node.js contract instance and endpoint making the call:

const address = "0x65fd1316f6634809f9dca80fc966e3e5ee0c345e";
    const abi = [
        {
            "inputs": [],
            "name": "countAllData",
            "outputs": [
                {
                    "internalType": "uint256",
                    "name": "",
                    "type": "uint256"
                }
            ],
            "stateMutability": "view",
            "type": "function"
        },
        {
            "inputs": [
                {
                    "internalType": "string",
                    "name": "_description",
                    "type": "string"
                }
            ],
            "name": "createData",
            "outputs": [],
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "inputs": [
                {
                    "internalType": "uint256",
                    "name": "_id",
                    "type": "uint256"
                }
            ],
            "name": "deleteData",
            "outputs": [],
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "inputs": [],
            "name": "readAllData",
            "outputs": [
                {
                    "components": [
                        {
                            "internalType": "uint256",
                            "name": "id",
                            "type": "uint256"
                        },
                        {
                            "internalType": "bytes32",
                            "name": "contentHash",
                            "type": "bytes32"
                        },
                        {
                            "internalType": "string",
                            "name": "description",
                            "type": "string"
                        }
                    ],
                    "internalType": "struct CRUD.Data[]",
                    "name": "",
                    "type": "tuple[]"
                }
            ],
            "stateMutability": "view",
            "type": "function"
        },
        {
            "inputs": [
                {
                    "internalType": "uint256",
                    "name": "_id",
                    "type": "uint256"
                }
            ],
            "name": "readData",
            "outputs": [
                {
                    "internalType": "uint256",
                    "name": "",
                    "type": "uint256"
                },
                {
                    "internalType": "bytes32",
                    "name": "",
                    "type": "bytes32"
                },
                {
                    "internalType": "string",
                    "name": "",
                    "type": "string"
                }
            ],
            "stateMutability": "view",
            "type": "function"
        },
        {
            "inputs": [
                {
                    "internalType": "bytes32",
                    "name": "_hash",
                    "type": "bytes32"
                }
            ],
            "name": "readDataByHash",
            "outputs": [
                {
                    "internalType": "uint256",
                    "name": "",
                    "type": "uint256"
                },
                {
                    "internalType": "bytes32",
                    "name": "",
                    "type": "bytes32"
                },
                {
                    "internalType": "string",
                    "name": "",
                    "type": "string"
                }
            ],
            "stateMutability": "view",
            "type": "function"
        },
        {
            "inputs": [
                {
                    "internalType": "uint256",
                    "name": "_id",
                    "type": "uint256"
                },
                {
                    "internalType": "string",
                    "name": "_description"
const web3 = new Web3('http://3.215.70.98:8101');
    const address = "0x65fd1316f6634809f9dca80fc966e3e5ee0c345e";
    const abi = [
        {
            "inputs": [],
            "name": "countAllData",
            "outputs": [
                {
                    "internalType": "uint256",
                    "name": "",
                    "type": "uint256"
                }
            ],
            "stateMutability": "view",
            "type": "function"
        },
        {
            "inputs": [
                {
                    "internalType": "string",
                    "name": "_description",
                    "type": "string"
                }
            ],
            "name": "createData",
            "outputs": [],
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "inputs": [
                {
                    "internalType": "uint256",
                    "name": "_id",
                    "type": "uint256"
                }
            ],
            "name": "deleteData",
            "outputs": [],
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "inputs": [],
            "name": "readAllData",
            "outputs": [
                {
                    "components": [
                        {
                            "internalType": "uint256",
                            "name": "id",
                            "type": "uint256"
                        },
                        {
                            "internalType": "bytes32",
                            "name": "contentHash",
                            "type": "bytes32"
                        },
                        {
                            "internalType": "string",
                            "name": "description",
                            "type": "string"
                        }
                    ],
                    "internalType": "struct CRUD.Data[]",
                    "name": "",
                    "type": "tuple[]"
                }
            ],
            "stateMutability": "view",
            "type": "function"
        },
        {
            "inputs": [
                {
                    "internalType": "uint256",
                    "name": "_id",
                    "type": "uint256"
                }
            ],
            "name": "readData",
            "outputs": [
                {
                    "internalType": "uint256",
                    "name": "",
                    "type": "uint256"
                },
                {
                    "internalType": "bytes32",
                    "name": "",
                    "type": "bytes32"
                },
                {
                    "internalType": "string",
                    "name": "",
                    "type": "string"
                }
            ],
            "stateMutability": "view",
            "type": "function"
        },
        {
            "inputs": [
                {
                    "internalType": "bytes32",
                    "name": "_hash",
                    "type": "bytes32"
                }
            ],
            "name": "readDataByHash",
            "outputs": [
                {
                    "internalType": "uint256",
                    "name": "",
                    "type": "uint256"
                },
                {
                    "internalType": "bytes32",
                    "name": "",
                    "type": "bytes32"
                },
                {
                    "internalType": "string",
                    "name": "",
                    "type": "string"
                }
            ],
            "stateMutability": "view",
            "type": "function"
        },
        {
            "inputs": [
                {
                    "internalType": "uint256",
                    "name": "_id",
                    "type": "uint256"
                },
                {
                    "internalType": "uint256",
                    "name": "",
                    "type": "uint256"
                },
                {
                    "internalType": "bytes32",
                    "name": "",
                    "type": "bytes32"
                },
                {
                    "internalType": "string",
                    "name": "",
                    "type": "string"
                }
            ],
            "stateMutability": "view",
            "type": "function"
        },
        {
            "inputs": [
                {
                    "internalType": "uint256",
                    "name": "_id",
                    "type": "uint256"
                },
                {
                    "internalType": "string",
                    "name": "_description",
                    "type": "string"
                }
            ],
            "name": "updateData",
            "outputs": [
                {
                    "internalType": "uint256",
                    "name": "",
                    "type": "uint256"
                },
                {
                    "internalType": "bytes32",
                    "name": "",
                    "type": "bytes32"
                },
                {
                    "internalType": "string",
                    "name": "",
                    "type": "string"
                }
            ],
            "stateMutability": "nonpayable",
            "type": "function"
        }
    ];

    const contract = new web3.eth.Contract(
       abi,
        address
    );

app.get('/getCount', async (req, res, next) => {
        console.log("El API KEY incluido en la cabecera de la petición es correcto");
        const id = req.params.id;
        const receipt = await contract.methods.countAllData().call()
        .then((receipt) => {
            res.status(200).json(receipt);
            console.log(receipt);
        }).catch( err => {
            console.log(err);
        });

    next();
    });

Does someone knows why it keeps returning this error? Thanks

1
  • Can you call any other function? What solidity version are you using? Can you share the genesis file of the private chain? From the error message it seems the call fails, so either the contract address is wrong, the abi is wrong, of the EVM is faulting.
    – Ismael
    Jul 30 at 4:27

0

Your Answer

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

Browse other questions tagged or ask your own question.