How to Index Data on the Fuel Network using Fuel Indexer
Repository: GitHub - compolabs/fuel-indexer-tutorial
If you have any questions you can ask them in our telegram chat for developers:
Telegram: View @compolabs_devs.
Welcome to an advanced tutorial by Composability Labs on how to index events on the blockchain fuel.network using the fuel indexer.
You can find the source code for this tutorial here: fuel-indexer-tutorial
Before going through this tutorial, make sure you have the required dependencies installed
Counter Contract Test Project
- Create a test counter project:
forc new counter
cd counter
cargo generate --init fuellabs/sway templates/sway-test-rs --name counter
- Add your beta-3 account’s private key with some ETH balance (to pay fees) into a
.envfile at the root of thecounterfolder.
Example: .env.example
-
Change the Rust SDK version in the
Cargo.tomlto 0.41.1 to allow usage of beta-3. YourCargo.tomlshould look like this: Cargo.toml -
The code for the contract
src/main.swcan be found here: main.sw -
The test code can be found in
tests/harness.rs: harness.rs. This test contains both local node and testnet tests, with the latter used to generate events. -
Build the contract using
forc build.
Before building of contract, please ensure that you are using the beta-3 version of the toolchain. You can switch your toolchain to beta-3 using the following commands:
fuelup update
fuelup toolchain install beta-3
fuelup default beta-3
to come back to latest you can type fuelup default latest
Done! The code is ready. Now let’s deploy our contracts. Alternatively, you can use the already deployed contract address on beta-3:
- 0x.. Address:
0xd968d8c91c8f7a4b09e86fb5080a1cb9bcd8b016f2764751e1c900d2be20b0cc - fuel.. Address:
fuel1m95d3jgu3aaykz0gd76sszsuhx7d3vqk7fmyw50peyqd903qkrxq67czu2
Indexer Instance Test Project
- Create a new indexer instance near the
counterfolder:
forc index new counter_indexer --namespace composabilitylabs
cd counter_indexer
-
First, copy the ABI file from
counter/out/debug/counter-abi.jsonintocounter_indexer/. -
Edit the
counter_indexer/schema/counter_indexer.schema.graphqlfile to match the following schema: counter_indexer.schema.graphql -
Create a
counter_indexer/src/lib.rsfile like this: lib.rs -
Create the
counter_indexer/counter_indexer.manifest.yamlmanifest as shown here: counter_indexer.manifest.yaml -
If you want to avoid waiting for a long sync, you can change the
start_blockat the end from the fuel explorer. -
Run the database in a Docker container:
docker run -d -p 5432:5432 --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword postgres
- Run the fuel-indexer:
fuel-indexer run --run-migrations --fuel-node-host beta-3.fuel.network --fuel-node-port 80 --postgres-host 127.0.0.1 --postgres-port 5432 --postgres-password mysecretpassword --postgres-user postgres
- Deploy the indexer:
forc index deploy
Done! Now you will see some logs like INFO fuel_indexer::ffi: 55: Increment 6 -> 7 in your fuel-indexer shell.
To redeploy the indexer code, you can use the following commands:
forc index remove
forc index deploy
Additionally, the GraphQL playground is available at the URL: http://localhost:29987/api/playground/composabilitylabs/counter_indexer