Getting the amount of XEM sent to an account

Check the amount of XEM you have sent to any account.

Prerequisites

Let’s get into some code

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);

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.equals(address)), // Filter transactions from to account
        filter((_) => _.mosaics.length === 1 && _.mosaics[0].id.equals(XEM.MOSAIC_ID)), // Filter xem transactions
        map((_) => _.mosaics[0].amount.compact() / Math.pow(10, XEM.DIVISIBILITY)), // Map only amount in xem
        toArray(), // Add all mosaics amounts into one array
        map((_) => _.reduce((a, b) => a + b, 0))
    )
    .subscribe(
        total => console.log('Total xem send to account', address.pretty(), 'is:', total),
        err => console.error(err)
    );
        // Replace with public key
        final String originPublicKey = "";

        // Replace with recipient address
        final String recipientAddress = "SB2RPH-EMTFMB-KELX2Y-Q3MZTD-RV7DQG-UZEADV-CYKC";

        // Replace with public key
        final PublicAccount originAccount = PublicAccount.createFromPublicKey(originPublicKey, NetworkType.MIJIN_TEST);

        // Replace with address
        final Address address = Address.createFromRawAddress(recipientAddress);

        final AccountHttp accountHttp = new AccountHttp("http://localhost:3000");

        final BigInteger total = accountHttp.outgoingTransactions(originAccount)
                .flatMapIterable(tx -> tx) // Transform transaction array to single transactions to process them
                .filter(tx -> tx.getType().equals(TransactionType.TRANSFER)) // Filter transfer transactions
                .map(tx -> (TransferTransaction) tx) // Map transaction as transfer transaction
                .filter(tx -> tx.getRecipient().equals(address)) // Filter transactions from to account
                .filter(tx -> tx.getMosaics().size() == 1 && tx.getMosaics().get(0).getId().equals(XEM.MOSAICID)) // Filter xem transactions
                .map(tx -> tx.getMosaics().get(0).getAmount().divide(BigDecimal.valueOf(Math.pow(10, XEM.DIVISIBILITY)).toBigInteger())) // Map only amount in xem
                .toList() // Add all mosaics amounts into one array
                .map(amounts -> amounts.stream().reduce(BigInteger.ZERO, BigInteger::add))
                .toFuture()
                .get();

        System.out.println("Total xem send to account " + address.pretty() + " is: " + total.toString());

The amount of XEM sent is displayed in your terminal.

What’s next?

Repeat the example by changing NEM filter for another mosaic.