0

Here's my code:

contract company {

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

    mapping (address => contractDetails) contractAddr;
    uint public counter = 0;

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

function getAllContracts(address _address) public view returns(contractDetails[] memory) {
        contractDetails[] memory y = new contractDetails[];
        for(uint i = 0; i < counter; i++) {
            y[i] = contractAddrArr[i];
        }
        return(y);
    }
}

I'm expecting the result as below:

address1 => [struct1, struct2, struct3......]

A function should return an array of structs by passing a user's address as a parameter.

1

Currently, it is impossible to return struct in solidity. Though you can use experimental ABIEncoderV2.

pragma solidity 0.5.9;
pragma experimental ABIEncoderV2;
contract company {

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

mapping (address => contractDetails) contractAddr;
uint public counter = 0;

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

function getAllContracts(address _address) public view returns(contractDetails[] memory) {
    contractDetails[] memory y;
    for(uint i = 0; i < counter; i++) {
        y[i] = contractAddr[_address];
    }
    return(y);
}
}

But, in live production, experimental feature should not be used. Link

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.