How to track asset balances across the whole chain. Do I need to use some indexer service for this?

I have a list of asset Ids and their contract Id and want to track who owns how much of these assets? I was looking into envio indexer where you can track events. The issue is when I was going through the code for asset transfer function:

pub fn transfer_to_address(to: Address, asset_id: AssetId, amount: u64) {
    // maintain a manual index as we only have `while` loops in sway atm:
    let mut index = 0;

    // If an output of type `OutputVariable` is found, check if its `amount` is
    // zero. As one cannot transfer zero coins to an output without a panic, a
    // variable output with a value of zero is by definition unused.
    let number_of_outputs = output_count();
    while index < number_of_outputs {
        if let Output::Variable = output_type(index) {
            if output_amount(index) == 0 {
                asm(r1: to.value, r2: index, r3: amount, r4: asset_id) {
                    tro r1 r2 r3 r4;
                };
                return;
            }
        }
        index += 1;
    }

    revert(FAILED_TRANSFER_TO_ADDRESS_SIGNAL);
}

There is no event emitted in this so how do I track this? Do I need to track every transaction of UTXO input/outputs?
Also, there could be assets whose contract id we don’t have what to do in that case?

I believe you’d need to track UTXO inputs and outputs for each txn, when you dont have events emitted. all the txns need to be monitored for the required asset Id and keep a record of balances for each address

to truly track account balances, you need to index all transactions, including:

  • All inputs
  • All outputs
  • All “script” transfers (Mint,Burn, Call & Transfer receipts)

Of course, a normal fuel node makes all this data available over the RPC, but for indexers you need to track this

Can’t I track balances just by monitoring inputs and outputs? Even for TRANSFER and TRANSFER_OUT receipts, we can derive the necessary balance data from the inputs and outputs. Is this possible, or am I missing something?

I will have a loook at it and get back to you

Hey, inputs/outputs can let you track all UTXO balances, but you won’t track the balances in smart contracts

ok and if i want to track balances accross smart contracts and normal addresses what are the things i should be looking for like input outputs and what else?

I believe for that you’d need to track all script transfers (Mint, Burn, Call & Transfer receipts)