0

I had been using remix IDE for a long time but recently switched to using Truffle. Everything else was fine until I had to build a function for payment. In remix IDE, there is a box provided, labelled "value" that takes values in wei and is used to transfer ether from one to another address. I don't understand how can I do the same using msg.value when there is no way to get that value for the function. Below is the code I am using:

function payment(address payable _ID) external payable {

        _ID.transfer(msg.value);
        
}

I just want to know, when using truffle, how can I pass required amount in msg.value. If it isn't possible, is there any other way to transfer an amount that can be assigned dynamically in place of msg.value? like this (Solidity 0.5):

function payment(address payable _ID, uint _amount) external payable {

        _ID.transfer(_amount);
}
2
  • Just transfer the amount directly to the contract address Jul 5, 2021 at 10:09
  • AvocadoChocolate I don't understand what you said, could you show me a code for reference please? Jul 5, 2021 at 15:45

1 Answer 1

4

In the contract, you just treat msg.value like any other uint256. It's "given" by the transaction. But how did it get there?

The first thing to understand is that it's not simply a message the user signed. It's actual ETH that got sent with the message. So, it's treated differently.

Truffle = Javascript. You use the transaction object that contains metadata about the transaction. You can describe value, gas limit and gas price there. You put that object after the values the function wants.

await myContractInstance.myFunction(arg1, arg2, { value: <amountInWei> })

Hope it helps

1
  • Thank you so much for the awesome answer. Yes, it totally works. This is what I had been looking for @RobHitchens Jul 8, 2021 at 8:23

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.