Vyper Address Data Type (Usage and Examples)

whiteboard crypto logo
Published by:
Whiteboard Crypto
on

In Vyper, a language specifically designed for Ethereum smart contracts, the address type holds an Ethereum address. Here are a few examples:

@public
def check_balance(addr: address) -> uint256:
    return addr.balance

@public
def is_contract_address(addr: address) -> bool:
    return addr.is_contract

@public
def get_codesize(addr: address) -> uint256:
    return addr.codesize

Like the addresses of houses or apartments that identify where people live, an Ethereum address identifies an entity on the Ethereum blockchain. In this article, we will explore the use of the address data type in Vyper contracts, how to interact with its members, and illustrate this with simple examples.

Overview of the Address Data Type

The address data type in Vyper can store an Ethereum address, which equates to 20 bytes or 160 bits. Address literals are written in hexadecimal notation with a leading 0x and must be checksummed, ensuring their validity.

Moreover, the address type comes with several built-in member properties:

  • balance: a uint256 value, indicating the balance of an Ethereum address.
  • codehash: a bytes32 value, representing the Keccak hash of the contract code at an address (it equals EMPTY_BYTES32 if no contract is deployed there)
  • codesize: a uint256 value, measuring the code size deployed at an address in bytes.
  • is_contract: a boolean value that indicates whether a contract is deployed at the address.
  • code: a bytes value representing the contract bytecode.

These members provide valuable information about the Ethereum address or contract associated with it, helping developers construct more efficient and secure contracts.

Practical Use of the Address Data Type in Vyper

Vyper contracts often need to interact with Ethereum addresses. For example, one might need to check the balance of an Ethereum address, verify whether an address hosts a contract, or evaluate the contract’s code size at an address. The address data type and its member properties in Vyper make these interactions straightforward.

Example 1: Checking Balance of an Address

In this example, we have a function check_balance, that checks the balance of a particular Ethereum address.

@public
def check_balance(addr: address) -> uint256:
    # Return the balance of a specific Ethereum address.

    return addr.balance

In this Vyper contract, the function check_balance accepts an Ethereum address as a parameter. It then uses the balance member property to return the balance of the provided address.

Example 2: Verifying if an Address is a Contract

Sometimes, a contract needs to verify if another address is a contract address. The is_contract member allows such a check.

@public
def is_contract_address(addr: address) -> bool:
    # Check if a provided Ethereum address has a contract deployed.

    return addr.is_contract

In this example, the is_contract_address function accepts an address parameter and returns True if there is a contract deployed at that address, otherwise, it returns False.

Example 3: Retrieving the Code Size Deployed at an Address

In rare circumstances, developers might need to check the size of the code deployed at a given address. Vyper’s codesize member makes this operation very easy.

@public
def get_codesize(addr: address) -> uint256:
    # Return the size of the code at a certain Ethereum address.

    return addr.codesize

In this contract, the function get_codesize takes an Ethereum address as a parameter and uses the codesize member to return the size of the code deployed at that address.

Best Practices and Common Issues

When working with the address data type in Vyper, there are a few important things to remember. First, operations such as SELFDESTRUCT and CREATE2 allow for bytecodes at an address to be removed and replaced. Therefore, it’s important not to make assumptions that values of address members, like codehash and codesize, will not change in the future.

Secondly, when trying to access the code member of an address, remember to use slice to explicitly extract a section of contract bytecode. This is important as the operation will throw an error if you try to access beyond the bounds of the bytecode.

Lastly, remember to verify the checksum of address literals to ensure they are valid Ethereum addresses.

The address datatype in Vyper is a powerful tool in the hands of Ethereum smart contract developers. It enables efficient interactions with Ethereum addresses, allowing checks on balance, contract status, and more. By understanding how to use the address type effectively, you can develop safer and more robust smart contracts in the Vyper language.

whiteboard crypto logo

WhiteboardCrypto is the #1 online resource for crypto education that explains topics of the cryptocurrency world using analogies, stories, and examples so that anyone can easily understand them. Growing to over 870,000 Youtube subscribers, the content has been shared around the world, played in public conferences and universities, and even in Congress.