FuelLabs / fuel-indexer

*Archived* 🗃 The Fuel indexer is a standalone service that can be used to index various components of the Fuel blockchain.
140 stars 67 forks source link

How to find and remove entity by id? #1501

Closed chlenc closed 11 months ago

chlenc commented 11 months ago

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 11 months ago

Found solution here https://github.com/FuelLabs/fuel-indexer/blob/fefbf2c1f8addb952bb78cb95f28ee2cee041d4e/packages/fuel-indexer-tests/indexers/fuel-indexer-test/src/lib.rs#L597

this code works for me

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

But there is no .delete method

chlenc commented 11 months ago

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 11 months ago

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 11 months ago

@chlenc

chlenc commented 11 months ago

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.