# How to Build a Fuel Indexer ###### tags: `tutorials` Let's build an indexer for our Sway Store marketplace. ## Setup ### Dependencies If you don't already have it installed, you can install postgres with homebrew using the command below: ```shell brew install postgres ``` Make sure you are using the nightly version of Rust ```shell rustup install nightly ``` To make sure you have all of the dependencies installed, run the command below. ```shell forc index check ``` Everything except the fuel indexer service should be checked. (You can have docker unchecked too since we won't use docker in this tutorial) ![](https://i.imgur.com/MTZ4Rko.png) ### Create a new project Use `forc index` to create a new project. ```shell forc index new marketplace-index --namespace marketplace ``` ## Understanding the Template Let's walk through the code that was generated for us. ### Schema Open the project up in your IDE, and open the `marketplace_index.schema.graphql` file in the `schema` folder. There are two query options available in the `QueryRoot`: `block` and `tx` (transaction). ### Manifest ### WASM Module Open the `lib.rs` file in the `src` folder. There should be a function called `marketplace_index_handler` that takes the parameter `block_data`. The parameters of a function plus the contract ID and blockstart in the manifest determine when the handler function is run. In this template there is no contract ID or block start defined, so all transactions will be considered. This means that all blocks production transactions will be handled with this function, because only those transactions have an object that matches the type `BlockData`. The first thing this function does is use the `Logger` module to log `Processing a block. (>'.')>`. Next, the `block_id` is extracted from the `block_data` param and converted to a `u64`. Then, a new `Block` object is created using this block ID and the block height directly from the `block_data` object. Once the block is defined in the same format as in the schema, you can use the `save` method to save the instance to the index database. Next, the function loops through each transaction in the transactions array for the block. For each transaction, the function: - logs `Handling a transaction (>'.')>` - creates a new instance of a `Tx` based on the schema - the transaction instance is saved to the indexer DB ## Defining our Schema For our marketplace, let's create an index that will allow us to see: - the total number of sales - the most popular products - the items purchased by a certain user - the items listed by a certain user ### Directives There are three directives available for the schema: `@indexed`: add to a field that you want to be ordered `@unique`: prevent duplicate values `@join`: use a certain field as a foreign key ## Updating the Manifest ## Building our WASM Module ## Deploying Locally ### Postgres DB Make sure you don't have any other postgres instances running. If you used homebrew to install, you can do this with: ```shell brew services stop postgresql ``` Set up a database locally with ```shell forc index postgres create postgres --persistent ``` Then copy the hello world example ### ENV Variables If you're on mac, set ENV variables in your terminal Note: these are temporary and will disappear when you close the terminal. ```shell export AR=/opt/homebrew/opt/llvm/bin/llvm-ar export CC=/opt/homebrew/opt/llvm/bin/clang ```