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 = 'BXRQ5E-YACWBP-CXKGIL-I6XWCH-DRFLTB-KUK34I-YJQ';
const recipientAddress = Address.createFromRawAddress(recipientRawAddress);
// replace with token id
const tokenIdHex = '46BE9BC0626F9B1A';
// replace with token divisibility
const divisibility = 6;
const tokenId = new TokenId(tokenIdHex);
// replace with node endpoint
const nodeUrl = 'NODE_URL';
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 token
    filter((_) => _.tokens.length === 1 && _.tokens[0].id.equals(tokenId)),
    // Transform absolute amount to relative amount.
    map((_) => _.tokens[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',
        tokenId.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 = 'BXRQ5E-YACWBP-CXKGIL-I6XWCH-DRFLTB-KUK34I-YJQ';
const recipientAddress = bitxor_sdk_1.Address.createFromRawAddress(
    recipientRawAddress,
);
// replace with token id
const tokenIdHex = '46BE9BC0626F9B1A';
// replace with token divisibility
const divisibility = 6;
const tokenId = new bitxor_sdk_1.TokenId(tokenIdHex);
// replace with node endpoint
const nodeUrl = 'NODE_URL';
const repositoryFactory = new bitxor_sdk_1.RepositoryFactoryHttp(nodeUrl);
const transactionHttp = repositoryFactory.createTransactionRepository();
const searchCriteria = {
    group: bitxor_sdk_1.TransactionGroup.Confirmed,
    signerPublicKey,
    recipientAddress,
    pageSize: 100,
    pageNumber: 1,
    type: [bitxor_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 token
        operators_1.filter(
            (_) => _.tokens.length === 1 && _.tokens[0].id.equals(tokenId),
        ),
        // Transform absolute amount to relative amount.
        operators_1.map(
            (_) => _.tokens[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',
            tokenId.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 = "BXRQ5E-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.getTokens())).filter(token ->
                    networkCurrency.getTokenId().map(tokenId -> token.getId().equals(tokenId))
                        .orElse(false) || networkCurrency.getNamespaceId()
                        .map(tokenId -> token.getId().equals(tokenId)).orElse(false)).reduce(
                    BigInteger.ZERO, (acc, token) -> acc.add(token.getAmount())).toFuture()
                .get();

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

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

const tokenId = new TokenId('10293DE77C684F71');
const divisibility = 2;