Addresses & Fixed Bytes
Two value types are special to blockchains: address, which identifies an account, and the fixed-size byte arrays bytes1 … bytes32, which hold hashes, identifiers and raw binary data.
The address type
An address is 20 bytes, written as 40 hex digits with a 0x prefix. Addresses in source code must use the checksummed form (EIP-55), where the mix of upper- and lower-case letters encodes a checksum, so typos are caught by the compiler.
| Expression | Meaning |
|---|---|
msg.sender |
The address that called the current function |
address(this) |
The current contract's own address |
address(0) |
The zero address, often used as "no address" |
addr.balance |
Ether held by addr, in wei |
addr.code.length |
Size of the code at addr (0 for wallets) |
payable(addr) |
Converts to address payable so ether can be sent |
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
constructor() payable {} // the playground sends 100 ETH to a payable constructor
function main() external view {
address me = address(this);
address caller = msg.sender;
address nobody = address(0);
console.log("this contract :", me);
console.log("caller :", caller);
console.log("my balance :", me.balance);
console.log("caller has code?", caller.code.length > 0);
console.log("I have code? ", me.code.length > 0);
console.log("is zero? ", nobody == address(0));
}
}this contract : 0x5FbDB2315678afecb367f032d93F642f64180aa3
caller : 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
my balance : 100000000000000000000
caller has code? false
I have code? true
is zero? true
The caller is the playground's externally owned account, so it has no code. Main is a contract, so it has. Balances are always in wei: 100000000000000000000 wei is 100 ether.
address vs address payable
Only an address payable has the transfer and send members for sending ether. This distinction makes you think twice before sending money to an address. Convert with payable(addr); msg.sender is a plain address and must be converted too.
address owner = msg.sender;
payable(owner).transfer(1 ether); // OK
owner.transfer(1 ether); // compile error: needs address payableYou will send ether for real in the Sending Ether lesson.
Comparing and converting addresses
Addresses support ==, !=, <, > and can be converted to and from uint160 and bytes20, which are also 20 bytes wide:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
function main() external pure {
address alice = 0x70997970C51812dc3A010C7d01b50e0d17dc79C8;
address bob = address(0x1234);
console.log("bob :", bob);
console.log("as number :", uint160(bob));
console.log("alice > bob?", alice > bob);
console.logBytes20(bytes20(alice));
}
}bob : 0x0000000000000000000000000000000000001234
as number : 4660
alice > bob? true
0x70997970c51812dc3a010c7d01b50e0d17dc79c8
Fixed-size byte arrays
bytes1 up to bytes32 hold exactly N bytes. bytes32 is everywhere in Ethereum because it matches the EVM word size: keccak256 hashes, storage keys and compact identifiers are all bytes32.
You can index individual bytes (read-only), read .length, compare, and use bitwise operators:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
function main() external pure {
bytes4 tag = 0xCAFEBABE;
console.log("length:", tag.length);
console.logBytes1(tag[0]);
console.logBytes1(tag[3]);
bytes2 left = bytes2(tag); // keeps the leftmost bytes
console.logBytes2(left);
bytes32 hash = keccak256("hello"); // a 32-byte fingerprint
console.logBytes32(hash);
}
}length: 4
0xca
0xbe
0xcafe
0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8
Note: Converting
bytesNto a shorterbytesMkeeps the left bytes, while converting an integer to a smaller integer keeps the right (low) bytes. Mixing the two directions up is a common source of bugs.
Short strings in bytes32
Short text identifiers (up to 32 ASCII characters) can be stored in a bytes32 instead of a string. This is cheaper, because it is a value type that fits in one storage slot:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
bytes32 public constant ROLE_ADMIN = "ADMIN";
function main() external pure {
bytes32 role = "ADMIN";
console.log("same role?", role == ROLE_ADMIN);
console.logBytes32(role); // ASCII codes, padded with zeros on the right
console.log("as string:", string(abi.encodePacked(bytes5(role))));
}
}same role? true
0x41444d494e000000000000000000000000000000000000000000000000000000
as string: ADMIN
For text of unknown length, use string, covered in the Strings & Bytes lesson.
Test yourself
What does address(this).balance return?
- The contract's balance in ether
- The contract's balance in wei
- The caller's balance
- The number of tokens the contract holds
Which type is needed to call .transfer(amount) on an address?
addressaddress payablebytes20uint160
What does bytes2(bytes4(0xCAFEBABE)) give?
0xCAFE0xBABE0x0000- It does not compile