0

I am completely new to Solidity and would appreciate any help. I have a supply chain smart contract and it contains a function that is supposed to be called by customers and after the call, the function is supposed to return a uint256 purchase order number to the customer. The code for the function is as follows:

    function initiatePurchaseOrder(string memory custName, string memory custLoc, uint volumeClass, uint materialClass) public onlyConsumer(msg.sender) returns (uint _purchaseOrder){

    require(bytes(custName).length > 0 && bytes(custLoc).length > 0 && volumeClass > 0 && materialClass > 0, "either customer, or location sent as empty string or vol/material classes sent as 0.");

    _purchaseOrder = uint256(keccak256(abi.encodePacked(custName, custLoc, volumeClass, materialClass, now)));
    emit InitiatedPurchaseOrder(_purchaseOrder);

    // update purchase order mappings
    purchaseOrderToConsumerAddressMapping[_purchaseOrder] = msg.sender;
    purchaseOrderToTimeStampMapping[_purchaseOrder] = now;
    purchaseOrderToStatusMapping[_purchaseOrder] = true;

    uint materialClassUnitPrice = materialClassToUnitPriceMapping[materialClass];
    if (materialClassUnitPrice == 0 || volumeClass == 0) {
        revert("invalid volume and material class meta info passed to smart contract. make sure they are non-zero/non-negative.");
    }

    // update more maps
    purchaseOrderToCustomerDetails[_purchaseOrder]["custName"] = custName;
    purchaseOrderToCustomerDetails[_purchaseOrder]["custLoc"] = custLoc;
    purchaseOrderToVolMatDetails[_purchaseOrder]["volumeClass"] = volumeClass;
    purchaseOrderToVolMatDetails[_purchaseOrder]["materialClass"] = materialClass;

    // emit the required event
    emit CreateQuoteForCustomer(_purchaseOrder);

   // return purchase order number to customer
    return _purchaseOrder;
}

I am compiling, deploying and testing the smart contract from Remix. I am deploying it on local Ethereum node (Ganache). I also tried it on Rinkeby. When I am calling the function above, I was expecting that it would return a uint256 purchase order. However, when I expand the transaction in Remix, the decoded output shows nothing as follows: enter image description here

From this previous post, I have come to the following conclusion and I need your help into deciding whether I am correct or not:

(i) When a function changes the state of the blockchain (like mine does), it is called as a transaction and in those cases web3 does not return values. Ami I right? If thats the case, why does Web3 do that?

(ii)Now that i need to see what the function is returning (the purchase order number generated), how am I supposed to do that? Through emitting events?

Any help would be greatly appreciated. Thank you

  • 1
    It's true that transactions don't have return values. When you make a state change, that has to be via a transaction, so you won't be able to get a return value. Yes, the typical way to get data back during a transaction is to use an event. – user19510 Sep 19 at 1:14
  • thanks a lot for the reply. any particular reason why exactly transactions dont return values? – nombreinvicto Sep 19 at 1:22
  • See github.com/ethereum/EIPs/issues/1288 for a brief discussion about it. Essentially it comes down to the amount of data that would have to be stored in the blockchain and how to charge gas for it. – user19510 Sep 19 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.