Getting the amount of assets sent to an account

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

Prerequisites

Method #01: Using the SDK

// replace with signer public key
const signerPublicKey =
  'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
// replace with recipient address
const recipientRawAddress = 'TB6Q5E-YACWBP-CXKGIL-I6XWCH-DRFLTB-KUK34I-YJQ';
const recipientAddress = Address.createFromRawAddress(recipientRawAddress);
// replace with mosaic id
const mosaicIdHex = '46BE9BC0626F9B1A';
// replace with mosaic divisibility
const divisibility = 6;
const mosaicId = new MosaicId(mosaicIdHex);
// replace with node endpoint
const nodeUrl = 'http://ngl-dual-101.testnet.symboldev.network:3000';
const repositoryFactory = new RepositoryFactoryHttp(nodeUrl);
const transactionHttp = repositoryFactory.createTransactionRepository();

const searchCriteria = {
  group: TransactionGroup.Confirmed,
  signerPublicKey,
  recipientAddress,
  pageSize: 100,
  pageNumber: 1,
  type: [TransactionType.TRANSFER],
};

transactionHttp
  .search(searchCriteria)
  .pipe(
    map((_) => _.data),
    // Process each transaction individually.
    mergeMap((_) => _),
    // Map transaction as transfer transaction.
    map((_) => _ as TransferTransaction),
    // Filter transactions containing a given mosaic
    filter((_) => _.mosaics.length === 1 && _.mosaics[0].id.equals(mosaicId)),
    // Transform absolute amount to relative amount.
    map((_) => _.mosaics[0].amount.compact() / Math.pow(10, divisibility)),
    // Add all amounts into an array.
    toArray(),
    // Sum all the amounts.
    map((_) => _.reduce((a, b) => a + b, 0)),
  )
  .subscribe(
    (total) =>
      console.log(
        'Total',
        mosaicId.toHex(),
        'sent to account',
        recipientAddress.pretty(),
        'is:',
        total,
      ),
    (err) => console.error(err),
  );
// replace with signer public key
const signerPublicKey = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
// replace with recipient address
const recipientRawAddress = 'TB6Q5E-YACWBP-CXKGIL-I6XWCH-DRFLTB-KUK34I-YJQ';
const recipientAddress = symbol_sdk_1.Address.createFromRawAddress(recipientRawAddress);
// replace with mosaic id
const mosaicIdHex = '46BE9BC0626F9B1A';
// replace with mosaic divisibility
const divisibility = 6;
const mosaicId = new symbol_sdk_1.MosaicId(mosaicIdHex);
// replace with node endpoint
const nodeUrl = 'http://ngl-dual-101.testnet.symboldev.network:3000';
const repositoryFactory = new symbol_sdk_1.RepositoryFactoryHttp(nodeUrl);
const transactionHttp = repositoryFactory.createTransactionRepository();
const searchCriteria = {
    group: symbol_sdk_1.TransactionGroup.Confirmed,
    signerPublicKey,
    recipientAddress,
    pageSize: 100,
    pageNumber: 1,
    type: [symbol_sdk_1.TransactionType.TRANSFER],
};
transactionHttp
    .search(searchCriteria)
    .pipe(operators_1.map((_) => _.data), 
// Process each transaction individually.
operators_1.mergeMap((_) => _), 
// Map transaction as transfer transaction.
operators_1.map((_) => _), 
// Filter transactions containing a given mosaic
operators_1.filter((_) => _.mosaics.length === 1 && _.mosaics[0].id.equals(mosaicId)), 
// Transform absolute amount to relative amount.
operators_1.map((_) => _.mosaics[0].amount.compact() / Math.pow(10, divisibility)), 
// Add all amounts into an array.
operators_1.toArray(), 
// Sum all the amounts.
operators_1.map((_) => _.reduce((a, b) => a + b, 0)))
    .subscribe((total) => console.log('Total', mosaicId.toHex(), 'sent to account', recipientAddress.pretty(), 'is:', total), (err) => console.error(err));
            // replace with signer public key
            PublicKey signerPublicKey = PublicKey.fromHexString(
                "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
            // replace with recipient address
            String recipientRawAddress = "TB6Q5E-YACWBP-CXKGIL-I6XWCH-DRFLTB-KUK34I-YJQ";
            Address recipientAddress = Address.createFromRawAddress(recipientRawAddress);
            NetworkCurrency networkCurrency = repositoryFactory.getNetworkCurrency().toFuture()
                .get();

            final TransactionRepository transactionRepository = repositoryFactory
                .createTransactionRepository();

            TransactionPaginationStreamer streamer = new TransactionPaginationStreamer(
                transactionRepository);

            Observable<Transaction> transactions = streamer
                .search(new TransactionSearchCriteria(TransactionGroup.CONFIRMED).transactionTypes(
                    Collections.singletonList(TransactionType.TRANSFER))
                    .recipientAddress(recipientAddress)
                    .signerPublicKey(signerPublicKey));

            BigInteger total = transactions.map(t -> (TransferTransaction) t)
                .flatMap(t -> Observable.fromIterable(t.getMosaics())).filter(mosaic ->
                    networkCurrency.getMosaicId().map(mosaicId -> mosaic.getId().equals(mosaicId))
                        .orElse(false) || networkCurrency.getNamespaceId()
                        .map(mosaicId -> mosaic.getId().equals(mosaicId)).orElse(false)).reduce(
                    BigInteger.ZERO, (acc, mosaic) -> acc.add(mosaic.getAmount())).toFuture()
                .get();

            System.out.println("Total " + networkCurrency.getUnresolvedMosaicId().getIdAsHex() +
                " sent to account " + recipientAddress.pretty() +
                " is:" + total);

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('10293DE77C684F71');
const divisibility = 2;

Did you find what you were looking for? Give us your feedback.