Forwarding ether to another contract

Just like with .gas(), gas units can be forwarded to a called contract. Similarly, the ether value can be forwarded or sent to the called contract. This is done using the .value() method call. As shown in the following code, the current contract initiates the methodToCall function call on otherContract and forwards 1 ether to it. As methodToCall is going to receive 1 ether, it must be a payable function, as follows:

otherContract.call.value(1 ether)("methodToCall", "param1");

Notice that in the preceding code, to transfer the ether amount to otherContract, the current contract must have ether balance.

The .gas() option is available on both the call and delegatecall methods, while the .value() option is not supported for delegatecall, as follows:

contract GasExample {
constructor () public {
address otherContract = 0xC4FE5518f0168DA7BbafE375Cd84d30f64CDa491;
/* gas adjustments */
require(otherContract.call.gas(1000000)(
"methodName", "param1"));
require(otherContract.delegatecall.gas(1000000)(

"methodName", "param1"));

/* Wei forwarding using value() */
require(otherContract.call.value(1 ether)(
"methodName", "param1"));
//.value() not supported on delegatecall, Compilation error
//require(otherContract.delegatecall.value(1 ether)(
// "methodName", "param1"));

/* Using gas() and value() together */
require(otherContract.call.gas(1000000).value(1 ether)(
"methodName", "param1"));
//This is also valid
require(otherContract.call.value(1 ether).gas(1000000)(

"methodName", "param1"));
}
}
Solidity version 0.4.25 also has the callcode method supported for the address data type, but it was deprecated. However, in Solidity version 0.5.0, callcode has been completely removed and is not supported anymore. Due to this reason, we have not covered callcode in this chapter. 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.11.20