Created
August 11, 2020 07:11
-
-
Save vieyang/3adaff2f3cf11b07b7c4110a297c4094 to your computer and use it in GitHub Desktop.
Send value and data to an unknown address inside the contract
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function external_call0(address destination, uint value, uint dataLength, bytes data) internal returns (bool) { | |
return (destination.call.value(tx.value)(tx.data)) | |
} | |
function external_call1(address destination, uint value, uint dataLength, bytes data) internal returns (bool) { | |
bool result; | |
assembly { | |
let x := mload(0x40) // "Allocate" memory for output (0x40 is where "free memory" pointer is stored by convention) | |
let d := add(data, 32) // First 32 bytes are the padded length of data, so exclude that | |
result := call( | |
sub(gas, 34710), // 34710 is the value that solidity is currently emitting | |
// It includes callGas (700) + callVeryLow (3, to pay for SUB) + callValueTransferGas (9000) + | |
// callNewAccountGas (25000, in case the destination address does not exist and needs creating) | |
destination, | |
value, | |
d, | |
dataLength, // Size of the input (in bytes) - this is what fixes the padding problem | |
x, | |
0 // Output is ignored, therefore the output size is zero | |
) | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Which is the best way to send value and data to an unknown address inside the contract?