0

I have created an smart contract that sends value from one account to another when calling the following function:

function buyEnergy(uint _sellOrderId, address _sellerAddress ) public payable /*isOwner*/{
    uint totalPrice=getTotalPriceOffer(_sellOrderId);
    uint unit = getUnitOffer(_sellOrderId);
    uint pricePerUnit = totalPrice/unit;

    //check the agent has been accepted on the exchange
    require(getExchangeRegistrationStatus()==true, "You are not a member of the exchange");
    
    //check the offer is still available
    require(getAvailabilityOffer(_sellOrderId)==true, "The sell offer is no longer available");
    
    //check the buyer is sending the required amount of money (no less or more)
    
    require(msg.value == totalPrice, "Please, send the exact amount of money");
    
    //make the payment
    _sellerAddress.transfer(msg.value);
    
    //register the buy operation in the exchange
    ee.saveBuyOrder(agentAddress,unit, pricePerUnit,totalPrice,_sellOrderId);
}

As you can see, //make the payment should transfer the money. When I tested that on both Remix and Truffle this was working fine.

But now the function is executed with no errors when called but the transfer of value does not happen (the last method its well executed because I can see the data added into the Blockchain). See how I call the function from the web:

async agentBuyEnergy(){ 


  let contractInstance = await this.getAgentInstance();

  await contractInstance.buyEnergy(this.state.inputSellOrderId,this.state.inputSellerOrderAddress ,{from:this.state.account,value:this.state.inputOfferTotalPrice},function(error,result){
    if (error){
      console.log("the error is",error);

    }
    else {

      console.log('successfull buy',result);

              
    }
  }.bind(this));
  
}

As you can see the value passed to the Solidity function is fetched from the web, but if I hardcoded a value happens the same.

Any ideas? I think that the problem is with how metamask process the value:

enter image description here

See the value set to 0. It is like specifing the value from react does the trick but not ethers are sent.

Thanks

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.