1
contract Marketplace {   
    uint256 transferBudget = 1 ether;
    bytes32[] ids;
    enum Position{
        Goalkeeper, Defender, Midfielder, Attacker, Coach
    }

    struct Footballer{
        bytes32 id;
        string name;
        address owner;
        Position position;
        uint price;
    }
    Footballer[] footballers;

    //function buy(uint ID, uint quantity) public payable {}

    function deposit() public payable {
        if(msg.value>0){
            transferBudget = transferBudget+msg.value;
        }
    }

    function getTransferBudget() public view returns(uint256){
        return transferBudget;
    }

    function update(bytes32 ID, uint newPrice) public {
        for(uint i=0; i<footballers.length; i++){
            if(footballers[i].id==ID && msg.sender==footballers[i].owner){
                footballers[i].price = newPrice;
                break;
            }
        }
    }

     function newProduct2() public pure returns(uint) {
        return 12;
    }

    //creates a new product and returns its ID
    function newProduct(string name, uint price, Position position) public returns(bytes32 id) {
        bytes32 hashId = keccak256(name,price,position);
        Footballer memory footballer = Footballer(hashId, name, msg.sender,position,price);
        footballers.push(footballer);
        ids.push(hashId);
        return hashId;
    }

    function getProducts() public view returns(bytes32[]) {
       return ids;
    }

    function getPrice(bytes32 ID) public view returns (uint) {
        for(uint i=0; i<footballers.length; i++){
            if(footballers[i].id==ID){
                return footballers[i].price;
            }
        }
    }
}

So here is the problem... the functions which are not views do not return any value when I run my local ethereum node but it works as expected in Javascript VM.

1
  • 2
    In general, transactions do not have return values. Remix + JavaScript VM is the one doing something strange here.
    – user19510
    Commented Mar 23, 2018 at 18:22

1 Answer 1

0

sendTransaction in JS EVM in Remix will be executed synchronously, so you'll be able to get the return value when sending a transaction. With a node this will not work since the call is processed asynchronously and there is no way to store a return value in the transaction receipt. There is only a status field which can be 0 or 1.

A pattern to communicate the result to the caller is to emit events. These can be fetched from the transaction receipt.

event FootballerCreated(bytes32 hashId);

function newProduct(string name, uint price, Position position) public {
    ...
    emit FootballerCreated (hashId);
}
0

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.