FuelLabs/fuel-indexer

How to find and remove entity by id?

chlenc opened this issue · 5 comments

chlenc commented

How to find and remove entity by id?

Hi frens, in my indexer I have AccountBalance data, and to avoid excessive loops on the sway side we decided to remove zero AccountBalances, after removing AccountBalance on sway I gonna get Option::None on the indexer and want to find and remove that data by id let id = uid([event.trader.to_string(), event.token.to_string()].concat());

Can you help me to do that?

 fn account_balance_change_event_handler(event: AccountBalanceChangeEvent, block: BlockData) {
        let height = block.height;

        let id = uid([event.trader.to_string(), event.token.to_string()].concat());
        let entity = if event.account_balance.is_none() {
            //fixme remove PositionEntity with that id
            let entity = PositionEntity {
                id,
                trader: event.trader,
                token: event.token,
                taker_position_size: "0".to_string(),
                taker_open_notional: "0".to_string(),
                last_tw_premium_growth_global: "0".to_string(),
            };
            entity.save()
        } else {
            let position = event.account_balance.unwrap();
            let entity = PositionEntity {
                id,
                trader: event.trader,
                token: event.token,
                taker_position_size: position.taker_position_size.as_i64().to_string(),
                taker_open_notional: position.taker_open_notional.as_i64().to_string(),
                last_tw_premium_growth_global: position
                    .last_tw_premium_growth_global
                    .as_i64()
                    .to_string(),
            };
            entity.save()
        };
        info!(
            "⚡️ Account balance (height: {height}): AccountBalanceChangeEvent: \n{:#?}",
            entity
        );
    }

My toolchain

Default host: aarch64-apple-darwin
fuelup home: /Users/alexey/.fuelup

installed toolchains
--------------------
latest-aarch64-apple-darwin (default)
beta-4-aarch64-apple-darwin
my-custom-toolchain

active toolchain
-----------------
latest-aarch64-apple-darwin (default)
  forc : 0.46.1
    - forc-client
      - forc-deploy : 0.46.1
      - forc-run : 0.46.1
    - forc-doc : 0.46.1
    - forc-explore : 0.28.1
    - forc-fmt : 0.46.1
    - forc-index : 0.24.1
    - forc-lsp : 0.46.1
    - forc-tx : 0.46.1
    - forc-wallet : 0.3.0
  fuel-core : 0.20.5
  fuel-core-keygen : Error getting version string
  fuel-indexer : 0.24.1

fuels versions
---------------
forc : 0.45
forc-wallet : 0.45
chlenc commented

Found solution here

this code works for me

     let entity = OrderEntity::find(OrderEntity::id().eq(id));

But there is no .delete method

chlenc commented

We generate functions for each field in each entity.
Entity::field_name() -> Field<u32, Entity>
And Field has impl gt, eq, and so on.
There’s also asc() and desc() and limit() for use with find_many

chlenc commented

I just faced a problem
when I try to find an entity by id
let entity = OrderEntity::find(OrderEntity::id().eq(id));
indexer fails with error from screenshot during deployment
and print this message in fuel-indexer service

Failed to get WASM module toolchain version: Error while importing "env"."ff_find_many": incompatible import type. Expected Function(FunctionType { params: [I64, I32, I32], results: [I32] }) but received Function(FunctionType { params: [I64, I64, I32, I32], results: [I32] })
image
ra0x3 commented

@chlenc

  • Your find issue should have been resolved in #1505, and released in v0.24.3
  • We currently don't support deleting entities but I've added an issue for this #1511
  • If this works for you, I'll probably close this issue by EOD :)
chlenc commented

For now I made active: bool instead of deleting, I but thanks for supporting this, I'll use it in future versions of the spark indexer.