5/5
## Resetting Dynamic Array State Variables in Vyper Smart contracts often need to manage lists of data where the number of items isn't known when the contract is written. A common example is tracking participants, like addresses that have interacted with a function. Vyper provides the `DynArray` type for this purpose. This lesson explains how to declare, use, and importantly, reset (clear) a dynamic array state variable in Vyper, using a "buy me a coffee" style contract as a practical example. In our example scenario, a contract allows users to send funds (like buying coffee) and needs to keep track of the addresses of everyone who contributes (the "funders"). After the contract owner withdraws the collected funds, we want to clear this list to start tracking funders for the next cycle. ### Understanding and Declaring Dynamic Arrays While fixed-size arrays are suitable when you know the exact number of elements beforehand, dynamic arrays are necessary when the list size needs to grow during the contract's lifetime. In Vyper, a dynamic array (`DynArray`) allows appending elements up to a predefined *maximum* capacity. To store the funder addresses, we declare a state variable like this: ```vyper # State variable to store funder addresses funders: public(DynArray[address, 1000]) ``` Let's break down this declaration: * `funders`: This is the name we give to our state variable. * `public`: This visibility modifier automatically creates a getter function, allowing external accounts or contracts to read the contents of the array. * `DynArray[address, 1000]`: This defines the type: * `DynArray`: Specifies it's a dynamic array. * `address`: Defines the data type of the elements that will be stored in the array (in this case, Ethereum addresses). * `1000`: This is a crucial part of Vyper's `DynArray` declaration – **you must specify a maximum size**. This array can hold *up to* 1000 addresses. It starts empty (length 0) but cannot grow beyond this limit. If an operation attempts to add an element beyond this capacity, the transaction will likely revert. ### Adding Elements to the Array In our "buy me a coffee" contract, a `fund()` function would receive payments. Inside this function, after validating the transaction (e.g., checking the amount sent), we add the sender's address to our `funders` list using the `.append()` method: ```vyper # Inside the fund() function... # Add the sender to the list of funders self.funders.append(msg.sender) ``` * `self.funders`: Refers to the contract's `funders` state variable. * `.append()`: This built-in method adds the provided element to the end of the dynamic array, increasing its length by one (provided it's under the maximum capacity). * `msg.sender`: A global variable in Vyper representing the address that initiated the current function call. ### Resetting the Dynamic Array The core task is to clear the `funders` array, typically done after a specific event, like the owner withdrawing funds in our example `withdraw()` function. Resetting the array means emptying it of all its current elements and setting its length back to zero. Vyper provides a straightforward syntax for this: assigning an empty array literal (`[]`) to the state variable. ```vyper # Inside the withdraw() function, after sending funds... # Reset the funders list for the next cycle self.funders = [] ``` * `self.funders`: Again, targets the state variable we want to modify. * `= []`: This assignment is the key. By assigning an empty array literal, you effectively tell Vyper to replace the current contents of `self.funders` with a new, empty dynamic array. This resets its length to 0 and clears all previously stored elements. This operation efficiently clears the list, making the contract ready to track a new set of funders without needing to manually remove each element individually. The choice of *when* to reset the array (e.g., within the `withdraw` function) depends entirely on the specific logic and requirements of your smart contract. In summary, Vyper's `DynArray` offers flexibility for managing lists of unknown size up to a defined maximum. You can populate them using `.append()` and efficiently clear them by assigning an empty array literal (`[]`), a common pattern for resetting state in cyclical contract operations.
A clear walkthrough to Resetting Dynamic Array State Variables in Vyper - Understand how to declare Vyper's `DynArray` with a max size, add elements using `.append()`, and efficiently clear the array using `[]`. See how this applies to resetting a funder list in a smart contract.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with Moccasin.
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on June 11, 2025
Duration: 2h 08min
Duration: 2h 32min
Duration: 24min
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with Moccasin.
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on June 11, 2025