Getting the amount of assets sent to an account

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


Getting into some code

In this code 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 rawAddress = process.env.ADDRESS as string;
const originAddress = Address.createFromRawAddress(rawAddress);

const recipientRawAddress = process.env.ADDRESS as string;
const recipientAddress = Address.createFromRawAddress(recipientRawAddress);

const mosaicIdHexa = process.env.MOSAIC_ID_HEXA as string;
const divisibility = 6;
const mosaicId = new MosaicId(mosaicIdHexa);

        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((_) => _.recipientAddress instanceof Address &&_.recipientAddress.equals(recipientAddress)), // 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))
        total => console.log('Total '+ mosaicId.toHex() +' sent to account', recipientAddress.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;