Exercises
Last updated
Last updated
In this section, we will present several exercises that will fortify your Functions query skills. Each exercise provided comes along with a solution if you get stuck.
In our first exercise, we will look for functions named buy. Much like instructions, we have many Glider functions available to us that we can use to find functions. In this case, we are querying for function names.
If we review the , we can find several functions available to us to query functions by their name.
Challenge: Update the query below to find functions named buy.
Now that we have a query to find functions named buy, the next step is to filter out functions that can only be called by the contract owner.
While there are various ways a contract might restrict a function to owner-only access, we’ll focus on removing functions that use the onlyOwner modifier, as this is the most common approach developers use to enforce the owner-only restriction.
For example, we want to exclude this Solidity function from our results:
Challenge: Update the query to filter out functions that use the onlyOwner modifier.
With our updated query, we can now identify functions named buy that can be called by anyone. Our next step is to identify buy functions that call a library function.
A Solidity library is a piece of reusable code that contains one or more functions. Libraries can be injected into multiple Solidity contracts, making them a common way to reuse code across contracts.
There’s one obstacle: How do we go from querying functions to querying a function's instructions?
Thankfully, Glider provides a solution. We can call instructions()
on a list of Glider Functions, and it will return a list of Instructions, containing every instruction from each function.
Here’s what the Glider code would look like:
With this approach, you can now query library calls by converting your list of functions into their corresponding instructions and identify library calls.
Once we have a list of instructions, we call the library_calls() function from the Glider API. This will filter the results to include only instructions that contain library calls.
Challenge: Update the query from Exercise #2 to return instructions that are library calls in the buy function.
In this final exercise, we’ll update the query to only include buy functions that have more than 5 arguments.
To achieve this, we can use the arguments()
method from the Glider API, which returns the arguments of a Solidity function. We can then use Python’s len()
function to count the number of arguments returned.
Challenge: Add a filter to your query that removes buy functions with fewer than 5 arguments.
Below we have a Glider query that finds the first function named transferAll.
Challenge: Identify the Glider function that will return a list of Solidity function arguments given a function.
If we review the Glider API documentation, we can find a to filter out functions that contain a specific modifier.
So far, we’ve focused on querying Solidity functions. However, if we review the , we’ll notice there’s no direct way to check if a Solidity function is calling a library function.
To query this, we can find library calls by first querying a function's instructions and then call Glider's against the instructions.
Remember that exec() returns an . This means you can chain a method call to the list, and that method will be applied to every item in the list automatically.
We can query for function arguments via the Glider method. This Glider function will return the functions arguments. Note that we need to also call list() on the arguments() return value. For example:
Glider functions inherit from the Callable class. Refer to the to find the answer!