Exploring Solidity Objects: Address - Part 1
Dive into the world of Solidity in pursuit of leveling up! (Catching gotchas before gotchas get us)
If you are a smart contract developer, I believe you might have used (or at least) came across the address
object in Solidity. Common use cases of this object includes reading address balance, and sending tokens. In this post, we will dive deeper into the different members of address
by combining visual illustrations while referencing Solidity’s cheatsheet.
Balance
The balance
member provides access to read balance of address in Wei (1 Ether = 10^18 Wei).
The example above for retrieveBalance
function is an example of a gotcha that catches new Solidity devs off-guard. Since we deployed the contract, it is not unusual to assume that address(this).balance
would return the balance of our address. In reality, it is the balance of the smart contract itself that is read.
Code
The code
member allows us to query the deployed contract’s bytecode information.
retrieveCode
function queries the EVM bytecode and in this example, the output looks something like this “0x60806040…4300081a0033”. This is useful for contract verification purposes. Here is an example of WETH’s deployed contract in Etherscan. Scroll below to see the Deployed Bytecode’s output. retrieveCodehash
simply provides the Keccak-256 hash of the output. It provides a more efficient way to verify the deployed contract as it is cheaper in terms of gas costs.
Transfer, Send & Call
These members are used in sending Ether. It is worth noting that Solidity by Example has put together a guide covering these members. The section below covers the output/responses when using these members.
transfer
and send
members are not recommended for sending assets. If you plan to use them, do take note on the expected responses. In case of failed transactions, transfer
reverts with an error while send
returns a boolean value of “false” (but the transaction will be successful). The difference here is that in the event of a failure using transfer
member, the blockchain state will not change, while send
updates the state regardless.
Note: both members will still consume gas regardless of a success or failed transaction.
call
member is recommended due to its flexibility for sending Ether. It also allows you to pass arguments if interacting with custom function(s) that require any parameters.
(Attaching the full contract below if you are interested with tinkering with the contract yourself. Feel free to use this code in Remix)
This concludes part 1. In the next part, we will cover other members of the address
object such as staticcall
and delegatecall
.
Thank you very much for reading. Feel free to reach out to me on my socials if you would like to engage with me on anything and everything related to building on Ethereum! I love learning with the community!