ℹ️ If you have any questions you can ask them in our telegram chat for developers: https://t.me/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
fuelup update
fuelup toolchain install beta-3
fuelup default beta-3
- 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
.env
file at the root of thecounter
folder.
Example: .env.example
-
Change the Rust SDK version in the
Cargo.toml
to 0.41.1 to allow usage of beta-3. YourCargo.toml
should look like this: Cargo.toml -
The code for the contract
src/main.sw
can 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
.
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
- Create a new indexer instance near the
counter
folder:
forc index new counter_indexer --namespace composabilitylabs
cd counter_indexer
-
First, copy the ABI file from
counter/out/debug/counter-abi.json
intocounter_indexer/
. -
Edit the
counter_indexer/schema/counter_indexer.schema.graphql
file to match the following schema: counter_indexer.schema.graphql -
Create a
counter_indexer/src/lib.rs
file like this: lib.rs -
Create the
counter_indexer/counter_indexer.manifest.yaml
manifest as shown here: counter_indexer.manifest.yaml -
If you want to avoid waiting for a long sync, you can change the
start_block
at 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