🟠2. Medium - Interview Questions
Last updated
Last updated
send
Returns a bool indicating success (true) or failure (false).
Only forwards a fixed 2300 gas stipend, which is often enough for basic operations but might not suffice for complex logic in the receiving contract.
If the transfer fails, it does not revert the transaction. You need to handle the failure by checking the return value.
transfer
Automatically reverts the transaction if the transfer fails, so no additional error handling is needed.
Also forwards a fixed 2300 gas stipend, preventing the receiving contract from executing too much code.
Generally considered safer because it reverts on failure, meaning you don’t have to manually check for success.
Due to the fixed gas limits, they are not flexible enough to work with smart contract wallets which may require more gas than is available from transfer
or send
.
When a function in the proxy contract has the same 4-byte identifier as a function in the implementation contract. This will cause a collision and mean that the function in the implementation contract can never be called.
abi.encode
Encodes data in ABI (Application Binary Interface) encoding format.
Adds padding to make each data item fit into a 32-byte slot, following the ABI encoding standards.
Each argument is encoded as a standalone item, with proper padding and alignment, which makes it more readable and less error-prone when used in conjunction with other Solidity functions.
Example:
Input: bytes memory encodedData = abi.encode("Hello", uint256(123));
Output (encodedData): 0x0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000007b000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000
(padded to 32 bytes).
abi.encodePacked
Encodes data in a compact, non-standard format.
Does not add padding, so each data item is placed directly after the previous one without any alignment.
Generally used for more compact encoding or for concatenating data, such as when creating a unique hash (e.g., in keccak256 hashing).
Be careful with potential collisions:
Concatenating variable-length types (like string and bytes) without padding can result in ambiguous results. For example, encoding two strings with abi.encodePacked("abc", "def") and abi.encodePacked("ab", "cdef") would produce the same output.
Example:
Input: bytes memory packedData = abi.encodePacked("Hello", uint256(123));
Output (packedData): 0x48656c6c6f000000000000000000000000000000000000000000000000000000000000007b000000000000000000000000000000000000000000000000000000
(no padding).
Cold reads happen when a storage slot is read for the first time during a transaction call. Cold reads are more expensive in terms of gas because the storage slot is being loaded into memory from disk.
Warm reads are for each subsequent call and have a lower gas cost than the initial cold read. The gas cost is lower for warm reads because the storage slot is already loaded into memory, eliminating the need for the more expensive disk access.
This distinction was introduced with EIP-2929 (Ethereum Berlin Hardfork) to make gas costs better reflect the computational effort required for storage access.
0x40