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.
A function selector clash in a proxy contract occurs when a function in the proxy contract has the same function selector as a function in the implementation contract. Since the proxy handles calls before they reach the implementation, if the proxy defines a function with the same selector as one in the implementation, the proxy function will always take precedence, preventing the implementation function from ever being called.
When a signature is reused multiple times, this could occur on the same chain or across different chains. Signature replay attacks can be mitigated by specifying a nonce to stop a signature from being used twice on the same chain, as well as a chain identifier so that the signature is only valid on the chain it was intended to be used on.
0x40
Only external
is a valid modifier for interfaces.
A constructor is run the first time a contract is deployed, but as an implementation contract stores its state on the proxy contract, any actions that occur in the constructor of the implementation contract would act in the context of that implementation contract.
This would lead to unexpected and unintended consequences, so this is why a initializer is used for upgradeable contracts instead of the constructor.
They implement a callback mechanism that has the potential to allow reentrancy attacks.
1. Determinism and Precision: Solidity operates in a decentralized environment where all computations must produce identical results across all nodes. Floating-point arithmetic is prone to rounding errors and imprecision due to the way computers represent real numbers, which could lead to inconsistencies in transaction outcomes.
2. Gas Costs: Implementing floating-point operations would increase the computational complexity and gas costs, as these operations require additional processing for handling precision, rounding, and edge cases. This inefficiency makes them impractical for the EVM.
For signed integers, it will be the year 2038.
For unsigned 32-bit integers, it will be the year 2106.