Getting the amount of assets sent to an account

Check the number of asset units you have sent to an account.

Prerequisites

Getting into some code

In this example, we are going to check how many assets of a certain type have we sent to an account.

const accountHttp = new AccountHttp('http://localhost:3000');

const originPublicKey = '7D08373CFFE4154E129E04F0827E5F3D6907587E348757B0F87D2F839BF88246';
const originAccount = PublicAccount.createFromPublicKey(originPublicKey, NetworkType.MIJIN_TEST);

const recipientAddress = 'SDG4WG-FS7EQJ-KFQKXM-4IUCQG-PXUW5H-DJVIJB-OXJG';
const address = Address.createFromRawAddress(recipientAddress);
const mosaicId = new MosaicId('7cdf3b117a3c40cc'); // Replace with mosaicId you want to check
const divisibility = 6; // Replace with mosaic divisibility

accountHttp
    .outgoingTransactions(originAccount)
    .pipe(
        mergeMap((_) => _), // Transform transaction array to single transactions to process them
        filter((_) => _.type === TransactionType.TRANSFER), // Filter transfer transactions
        map((_) => _ as TransferTransaction), // Map transaction as transfer transaction
        filter((_) => _.recipient instanceof Address &&_.recipient.equals(address)), // Filter transactions from to account
        filter((_) => _.mosaics.length === 1 && _.mosaics[0].id.equals(mosaicId)), // Filter mosaicId transactions
        map((_) => _.mosaics[0].amount.compact() / Math.pow(10, divisibility)), // Map relative amount
        toArray(), // Add all mosaics amounts into one array
        map((_) => _.reduce((a, b) => a + b, 0))
    )
    .subscribe(
        total => console.log('Total '+ mosaicId.toHex() +' sent to account', address.pretty(), 'is:', total),
        err => console.error(err)
    );

If you want to check another mosaic different than the native currency, change mosaicId and divisibility for the target mosaic properties.

const mosaicId = new MosaicId([3562970624, 976072438]);
const divisibility = 6;