Skip to content

Instantly share code, notes, and snippets.

@vieyang
Created August 11, 2020 07:11
Show Gist options
  • Save vieyang/3adaff2f3cf11b07b7c4110a297c4094 to your computer and use it in GitHub Desktop.
Save vieyang/3adaff2f3cf11b07b7c4110a297c4094 to your computer and use it in GitHub Desktop.
Send value and data to an unknown address inside the contract
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;
}
@vieyang
Copy link
Author

vieyang commented Aug 11, 2020

Which is the best way to send value and data to an unknown address inside the contract?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment