What is the difference between using nameFunction.value(n)(parameters of nameFunction) and address.transfer(10)?

I know that in the case of address.transfer, I'll send the amount to the fallback function so that the contract has that amount in it. But in the first case, where is the amount sent?

And, as last thing, is nameFunction.value susceptible of reentrancy attack as address.call.value()()?

I am a little bit confused about this. Thank you.


Calling address.transfer(10) will send the ether (10 wei) to the target address. If said address is a contract and it has a fallback payable function implemented, it will be triggered in the target contract.

On the other hand nameFunction.value(n)(parameters of nameFunction) will call the selected function, send the parameters and the ether specified by value along with it. For it to work said function also has to be marked as payable.

So, you would use address.transfer(10) when you just want to send ether to another contract (or EOA), if that contract happens to implement something on it's fallback function, it's up to it (limited by the 2300 gas stipend).

You would use nameFunction.value(n)(parameters of nameFunction) when you know you want to execute said function on this other contract which requires ether to be sent to work.

Say contract B has this function:

function doSomethingWithEther(string _param) public payable{
 balances[msg.sender] = msg.value;

For contract A to execute this function and provide the ether said function needs it would do:

contractB.doSomethingWithEther.value(1 ether)("This is a string param");

nameFunction.value(n)(parameters of nameFunction) is indeed susceptible to re-entrancy attacks. It could execute a function from your contract that messes with it's internal balance allowing it to drain the funds before the actual ether transfer is done. (This can be prevented by following the Checks-Effects-Interactions pattern).

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.