Learning Contracts
Last updated
Last updated
A Solidity contract is a piece of executable code consisting of state variables, functions, modifiers, and more. Contracts form the backbone of Solidity applications, defining the logic and rules that run on the the EVM.
Glider makes it easy to identify contracts using Gliderโs Contracts class. Letโs see how this works by running the query below:
After running the query, you should see the first 10 contract results found by Glider displayed in the Output panel.
Let's take a look at our foundational contracts query and break down the query code.
We are interested in finding "Market" contracts that are eligible to receive ERC-721 tokens. To find these contracts, we will search for contracts that have the name "Market" in them and contain the "onERC721Received" function.
The query is below:
Now let's start by breaking down each query into sections below.
Next, we use the .with_name_regex() method provided by Glider. This method searches for contracts whose names match a given regular expression (regex).
Since we are looking for contracts that contain the term "Market", we pass "Market" as our regex pattern:
This ensures that any contract with "Market" in its name - such as "NFTMarket", "TokenMarketplace", or "MarketExchange" will be included in the results.
Now that weโve queried for contracts with "Market" in their names, our next step is to ensure that our results only include contracts designed to receive ERC-721 tokens.
Contracts that expect to receive ERC-721 tokens must implement the onERC721Received function. If a contract does not define this function, it is not intending to receive ERC-721 tokens.
Letโs take a closer look at how the .filter() function helps us refine our query results:
The .filter() method takes a lambda function, which is applied to every contract in the query results.
The lambda function first queries all functions in the contract:
Calling .functions() retrieves all defined functions in the contract and calling .exec() executes the query, returning a list of function objects.
We then access the function names:
This extracts the names of all functions defined in the contract.
Finally, calling "onERC721Received"in
tells Glider to check if "onERC721Received" is inside the list of function names.
If the function name exists in the list, the expression returns True. If the function does not exist, the expression returns False.
Since .filter() only keeps results where the lambda function evaluates to True. This ensures that:
Contracts defining onERC721Received remain in the results.
Contracts without onERC721Received are filtered out.
This guarantees that our query only includes contracts capable of receiving ERC-721 tokens.
Once weโve completed our query and filtered out unwanted results, we assign the final results to a variable named contracts and return it in the query() function:
This ensures our results will be displayed in the Glider IDE Output panel.
Now that weโve covered the query, letโs run it in Glider IDE.
Paste the following query into the Glider IDE, click โRunโ, and wait for the results:
Once the query completes, Glider returns a list of contracts and their addresses. Each contract in the results:
Contains "Market" in its name, as specified in our query.
Defines the onERC721Received function, confirming it is designed to receive ERC-721 tokens.
This ensures we have successfully filtered contracts that both match the "Market" naming criteria and are capable of handling ERC-721 token transfers.
To find smart contracts, we first create an instance of the :
We add a .filter() method to our to exclude contracts that lack the onERC721Received function. The filter uses a lambda function to apply this criterion: