0

I'm currently working (as a begineer) at a project involving ethereum blockchain and solidity.

In my contract I have a mapping with addresses as key and the value of an array of a FileDetails structure. Anyway when I try to add elements to the array and to the mapping it doesn't get updated even if the method is correctly called (see the transaction details below). I am currently using a React application with the truffle suite. (Ganache for running a test blockchain).

I have the following contract:

contract Files {
    struct FileDetails {
        string fileHash; // The IPFS file hash
        string fileName; // The file name
        string transactionDate; // The date in which file infos was stored
        string fileExtension; // The type of file (its extension)
    }

    mapping(address => FileDetails[]) filesList;

    function addFile(string memory fileHash, string memory fileName, string memory fileType, string memory date) public {
        FileDetails memory fileDetail = FileDetails({
            fileHash: fileHash,
            fileName: fileName,
            fileExtension: fileType,
            transactionDate: date
        });
        filesList[msg.sender].push(fileDetail);
    }
}

Now I'm calling the function addFile(...) from javascript with the following:

await contract.methods.addFile("aaaa", "bbbb", "cccc", "dddd").send({ from: accounts[0] });

As you can see I put into parameters random strings just for testing.

Now, when I call it I actually get something: enter image description here

The contract is called and even the function with parameters is correctly called in the transaction.

Beside that, I don't get any updates in the mapping structure that I can see in the contract from Ganache (0 items): enter image description here

I don't know if it can be useful, but when I try to make a transaction to the contract to give it some funds, but I get the following error:

VM Exception while processing transaction: out of gas

Do you know what could be the problem? Why even if the method is correctly called it doesn't update the mapping structure? Thank you in advance.

EDIT

Here there is a transaction receipt form the javscript console log: enter image description here

I also tried adding the following function to the solidity contract:

function getFilesCount() public view returns (uint256) {
    return filesList[msg.sender].length;
}

But when I call it from javascript I get always 0.

5
  • Can you retrieve the transaction receipt? There's an error reported in ganache repo github.com/trufflesuite/ganache/issues/2164. It might be that ganache doesn't display mappings yet.
    – Ismael
    Feb 24, 2021 at 0:37
  • Thank you @Ismael for your answer, I've added a screenshot of the receipt, can it be useful? Feb 24, 2021 at 12:42
  • @Ismael as you can see in the "update" section of the question I've tried adding a function to retrieve the amount of elements in the selected item's array of the mapping but I still get 0, so I don't think the problem is Ganache Feb 24, 2021 at 12:56
  • The receipt's status is true so the transaction was successful. How are you making the call to getFilesCount? The code is fine and it should work, so either ganache has a bug, used a wrong contract address, deployed a different contract, call to getFilesCount isn't correct.
    – Ismael
    Feb 24, 2021 at 14:16
  • @Ismael thank you for replying. As 3DLures stated in the answer below it can be a Ganache bug or something like that. I'll try creating a brand new environment/workspace with ganache and truffle with react. Thank you again! Feb 24, 2021 at 19:32

1 Answer 1

0

Trying the Solidity code on my local environment running go-ethereum, it worked just fine. It may sound dumb but after you altered the smart contract code and re-deployed it, did you add files to the storage? Because it's a new smart contract, new address, new storage. Myself, I got rid of Ganache because it was acting different that the production network (go-ethereum)

4
  • Thank you for your contribution! Yes, I tried adding files to the storage but nothing changed. I'll try creating a completely new environment and workspace with Ganache and try it again. Thank you for trying my contract in your network, I was getting crazy. Feb 24, 2021 at 19:27
  • Have you tried also the getFiles() method? I gave up to Ganache and switched to the Ropsten testnet but nothing changed... Feb 24, 2021 at 22:39
  • Yes, I did call getFilesCount() and the count was correct. Are you using exactly the same code you posted above?
    – 3DLures
    Feb 25, 2021 at 7:25
  • Yes, I am. It looks very strange, now I'll try restructuring the entire app, maybe something is going wrong behind the scenes with react and web3 js. Feb 25, 2021 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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