0

// SPDX-License-Identifier: MIT // solium-disable linebreak-style pragma solidity >= 0.4.22 < 0.9.0;

import "./Ideator.sol"; import "./Investor.sol";

contract ContractOwner { Ideator ideatorObj; Investor investorObj;

 struct idea_info {
    string code;
    string title;
    string description;
    address ideaOwner;
    address [] investors;
    uint amount;
}

idea_info [] ideas_arr; // contains multiple ideas

constructor() {
    ideatorObj = new Ideator( address(this) );
    investorObj = new Investor( address(this) );
}

function saveIdea(string calldata _code, string calldata _title, string calldata _description, address _account) public {
    idea_info memory idea;
    idea.code = _code;
    idea.title = _title;
    idea.description = _description;
    idea.ideaOwner = _account;
    idea.amount = 0;
    ideas_arr.push(idea);
}

 function getIdeasByIdeatorAddress(address _account) public view returns(idea_info memory tempIdea) {
     for(uint8 i=0; i<ideas_arr.length; i++) {
         if( ideas_arr[i].ideaOwner == _account ) {
             return ideas_arr[i];
         }
     }
}

function getIdeas() public view returns(idea_info [] memory) {
    return ideas_arr;
}

function getIdeaByCode(string calldata _code) public view returns(idea_info memory tempIdea) {
    for(uint8 i=0; i<ideas_arr.length; i++) {
        if( keccak256(bytes(ideas_arr[i].code)) == keccak256(bytes(_code))) {
            return ideas_arr[i];
        }
    }
}

// call from investor contract
function receiveIdeaDonations(string calldata _code, uint _amount, address _account) public {
    for(uint8 i=0; i<ideas_arr.length; i++) {
        if( keccak256(bytes(ideas_arr[i].code)) == keccak256(bytes(_code)) ) {
            ideas_arr[i].investors.push(_account);
            ideas_arr[i].amount += _amount;
        }
    }
}

// Get All Idea Codes
function getIdeaCodes() public view returns(string [] memory codes) {
    codes = new string[](ideas_arr.length);
    for(uint8 i=0; i<ideas_arr.length; i++) {
        codes[i] = ideas_arr[i].code;
    }
    return codes;
}

function getIdeaOnInvestor(address _account) public view returns(idea_info memory investorIdea) {
    for(uint8 i=0; i<ideas_arr.length; i++) {
        for(uint8 j=0; j<ideas_arr[i].investors.length; j++) {
            if( ideas_arr[i].investors[j] == _account ) {
                return ideas_arr[i];
            }
        }
    }
}

function getIdeatorContractAddress() public view returns(address ideatorAddr) {
    ideatorAddr = address(ideatorObj);
}

function getInvestorContractAddress() public view returns(address investorAddr) {
    investorAddr = address(investorObj);
}

}

when i call receiveIdeaDonations() from javascript it gives the following results

amount: "0" code: "t0xxv" description: "test desc" ideaOwner: "0x54c78347202247b7EC5a20b1feDf5AE0A9c3B64e" investors: [] title: "test"

above amount is zero and investors array is empty but when testing on remix or command line it works fine

1 Answer 1

0

When triggering solidity methods you don't always get the returned values back to your JS environment.

For call methods (e.g. view) - functions that do not change state, you can reliably expect return values. Whereas for write methods - those that change state, your JS library will not expect them by default. This is due to the unknown time of mining the transaction.

The go-to solution here is to use events and check them with something like getTransactionReceipt. See here more info on this exact issue.


Your receiveIdeaDonations changes state, so return values won't be returned immediately. Note: Because remix runs a special VM when you're testing contracts, it may work there, but in production it won't.

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.