Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to find and remove entity by id? #1501

Closed
chlenc opened this issue Dec 6, 2023 · 5 comments
Closed

How to find and remove entity by id? #1501

chlenc opened this issue Dec 6, 2023 · 5 comments

Comments

@chlenc
Copy link

chlenc commented Dec 6, 2023

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
Copy link
Author

chlenc commented Dec 6, 2023

Found solution here

this code works for me

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

But there is no .delete method

@chlenc
Copy link
Author

chlenc commented Dec 6, 2023

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
Copy link
Author

chlenc commented Dec 6, 2023

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
Copy link
Contributor

ra0x3 commented Dec 11, 2023

@chlenc

@chlenc
Copy link
Author

chlenc commented Dec 19, 2023

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.

@chlenc chlenc closed this as completed Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants