Here's my solidity code. And I want all the objects to be returned stored in mapping array contracts in the getContract method by giving address as a parameter to the getContract function.

pragma solidity ^0.5.0;

contract company {

    struct contractDetails {
        string contractId;
        string companyA;
        string companyB;
        address A;
        address B;
        uint256 timestamp;
        string signHash;
        bool isSigned;

    mapping (string => contractDetails) contracts;
    mapping (string => string) hashMap;

    event contractSigned (
        string contractId,
        string companyA,
        string companyB,
        address A,
        address B,
        uint256 timestamp,
        string signHash,
        bool isSigned

    function createContract(string memory _contractId, string memory _companyA, string memory _companyB, address _B) public {
        contracts[_contractId] = contractDetails(_contractId, _companyA, _companyB, msg.sender, _B, now, '', false);

    function signContract(string memory _contractId) public {
        contractDetails storage signDetails = contracts[_contractId];
        signDetails.isSigned = true;
        signDetails.timestamp = now;
        emit contractSigned(_contractId, signDetails.companyA, signDetails.companyB, signDetails.A, signDetails.B, signDetails.timestamp, signDetails.signHash, signDetails.isSigned);

    function verifySign(string memory _contractId) public view returns(bool) {
        contractDetails storage verify = contracts[_contractId];

    function getContract(address _address) view public returns(/* Array containing all objects */) {
      /* Code Here */

First, you should clarify what is the _address address that you pass to the getContract() function.

Anyway, there is no good way of viewing all the mapping's elements. But there are some options:

  1. Use dynamically sized array. It is easier to show all the elements, however, the elemend can be accessed only by uint id. It is suitable for you if _contractId is actually a number. Also, there will appear 'holes' in the array in case you want to remove contracts.

  2. Use mapping + array. It is a popular solution in smart-contracts, however not perfect. You can place your structure members in a mapping to easily get them by string key, and also put them in the array to easily show them all. This solution is not gas-efficient and is prone to mistakes when adding/removing elements. In your case, you can store just _contractId's in the array.

Overall, showing all the elements is rather costly operation and may not fit the block size. So, in case the array is used it is better to add the functionality to show the elements of the array partially.

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.