1
pragma solidity ^0.4.18;

contract TransManager {
  //state variables

  // contract owner
  address private creator;

  uint transCounter;

  // File Meta metaData
  struct metaData{
    uint id;
    address owner;
    string docType;
    string fileHash;
  }

  //Array to store tranasactions
  mapping (address => metaData[])  fileLogs;


  //Send a trnsactionId

  function regTransaction(string _docType, string _fileHash) public {
    transCounter++;
    var fileLog = metaData(transCounter, msg.sender, _docType, _fileHash );

    fileLogs[msg.sender].push(fileLog);

  }

  function getTransactions() public returns (uint []){
    var temp = fileLogs[msg.sender];


    uint[] memory ids = new uint[](transCounter);


    for (uint i=0; i<= transCounter; i++){
        ids[i]=temp[i].id;
    }

    return ids;
  }



  // constructor
    function TransManager() {
        creator = msg.sender;
    }

  // kills contract and sends remaining funds back to creator
   function kill() {
       if (msg.sender == creator) {
           selfdestruct(creator);
       }
   }

}

getTransactions - is always returning array of zeros. But I am expecting to return the indexed number.

truffle console --network ganache
truffle(ganache)> TransManager.deployed().then(function(instance){app=instance;})
undefined
truffle(ganache)> app.regTransaction("EDI","Hash",{from: web3.eth.accounts[1]})
{ tx: '0x46785333bce5f682c586ccf995682203a78d26bfba54957bb0bd5593d6cc4542',
  receipt: 
   { transactionHash: '0x46785333bce5f682c586ccf995682203a78d26bfba54957bb0bd5593d6cc4542',
     transactionIndex: 0,
     blockHash: '0x26bf163083f35bade4aca17c2d05706846b4dd8f555efa7117cdea0b64da51ca',
     blockNumber: 5,
     gasUsed: 146345,
     cumulativeGasUsed: 146345,
     contractAddress: null,
     logs: [],
     status: 1 },
  logs: [] }
truffle(ganache)> app.getTransactions()

Error: VM Exception while processing transaction: invalid opcode
    at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:509:1)
    at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:354:1)
    at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:64:1)
    at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/httpprovider.js:128:1)
    at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-provider/wrapper.js:134:1
    at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/requestmanager.js:86:1
    at Object.InvalidResponse (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/errors.js:38:1)
1
  • Doesn't getTransactions always overflow? It seems to allocate space for only transCounter elements but then write transCounter + 1 elements (due to the i <= transCounter condition).
    – user19510
    Commented Mar 15, 2018 at 22:16

1 Answer 1

0

I think the only issue with your code is the terminal condition of the for loop. i <= transCounter should be i < transCounter. (Of course, transCounter is global, so this is only the right count as long as there's only one account using the contract, but that's an orthogonal issue.)

Here's a simplified, cleaned up version of your code that seems to work properly when testing in Remix:

pragma solidity ^0.4.21;

contract TransManager {
    uint transCounter;

    struct MetaData{
        uint id;
        address owner;
        string docType;
        string fileHash;
    }

    mapping (address => MetaData[])  fileLogs;

    function regTransaction(string _docType, string _fileHash) public {
        transCounter++;
        MetaData memory fileLog = MetaData(transCounter, msg.sender, _docType, _fileHash );

        fileLogs[msg.sender].push(fileLog);
    }

    function getTransactions() public view returns (uint[]) {
        MetaData[] storage temp = fileLogs[msg.sender];

        uint[] memory ids = new uint[](transCounter);

        for (uint i = 0; i < transCounter; i++){
            ids[i]=temp[i].id;
        }

        return ids;
    }
}
7
  • Thanks smarx. It is working in Remix when i invoke the function from truffle console i am getting below error . truffle(ganache)> app.getTransactions() Error: VM Exception while processing transaction: invalid opcode at Object.InvalidResponse (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:41484:16) at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:329530:36 Commented Mar 15, 2018 at 22:51
  • If you can't debug it yourself, edit your question to include the code you're having trouble with.
    – user19510
    Commented Mar 15, 2018 at 23:07
  • It is the same code above which is working in Remix but not in truffle console. Commented Mar 16, 2018 at 2:50
  • I was referring to the Truffle code.
    – user19510
    Commented Mar 16, 2018 at 3:15
  • 1
    Edit your question, not my answer. :-)
    – user19510
    Commented Mar 16, 2018 at 3:43

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.