I am trying to work with SymbolSdk.
I try to get the balance of a list of accounts for a given “mosaic”.
I want the code to query the blockchain and resolve promises when the balance has been properly read for every user.
Then I want to join all the promises and display the output of every query at the end.
I got a code sample from symbol documentation showing how to trigger code when the information is received from the blockchain.
However it keeps throwing UnhandledPromiseRejectionWarning
and I cannot find a way to catch these errors.
I guess they come from somewhere in accountHttp.getAccountInfo(userAddress)
function, but I could not dive any further."
import * as SymbolSdk from 'symbol-sdk';
import { merge, of, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
const nodeUrl = 'http://localhost:3000';
export async function userBalance(userAddress: SymbolSdk.Address, mosaicId:
SymbolSdk.MosaicId): Promise<SymbolSdk.UInt64> {
const repositoryFactory = new SymbolSdk.RepositoryFactoryHttp(nodeUrl);
const accountHttp = repositoryFactory.createAccountRepository();
return new Promise<SymbolSdk.UInt64>(async (resolve, reject) => {
accountHttp.getAccountInfo(userAddress)
.pipe(catchError((err) => {
const next = of(err)
reject(err);
return next;
}))
.subscribe((transactions) => {
for (const mosaic of transactions.mosaics) {
// console.log(userAddress.plain(), mosaic.id.toHex(), mosaic.amount.toString());
if (mosaicId.equals(mosaic.id)) {
resolve(mosaic.amount);
}
}
resolve(new SymbolSdk.UInt64([0, 0]));
},
(err) => reject(err))
});
}
if (require.main === module) {
const promiseList = [];
const users = ["TCC4A2H6VX4MVBYHBG33JRCL7SI4Z7OKSKUQRNXS", "TBHE2SZCMVGQTDDVSMM6G7KE33IWCXA726WXEV3H", "TBKFTBO4X5EIEHE4LDWVFALJGQU3HCVTAHN2NXCH",
"TCWUJ4ENXWC4QYJ4N7ESVUHTBXL6IJ4TTPNFME3F", "TC2X5PCGSOEGX6TPZYTZQZYJBOP2QOPDC4VQ5GCK"];
const rawMosaicId = "0874BB5FEAAA53C4";
users.forEach(async (user) => {
const promise = new Promise<[string, SymbolSdk.UInt64, SymbolSdk.MosaicId]>((resolve, reject) => {
try {
const address = SymbolSdk.Address.createFromRawAddress(user);
const mosaicId = new SymbolSdk.MosaicId(rawMosaicId);
userBalance(address, mosaicId).then((amount) => {
resolve([user, amount, mosaicId]);
}).catch(reject);
}
catch (err) {
reject(err);
}
});
promiseList.push(promise);
});
Promise.allSettled(promiseList).then((values) => {
console.log("Chain over");
values.forEach((settled) => {
if (settled.status === "fulfilled")
{
const [user, amount, mosaicId] = settled.value;
console.log(`User ${user}, amount of ${mosaicId.toHex()}: ${amount.toString()}`);
} else {
console.log(`Failed to get balance, reason: ${settled.reason}`);
}
});
}).catch(console.error);
}
Here is an example of the output I get:
(node:30500) UnhandledPromiseRejectionWarning: HttpError: HTTP request failed
at Request._callback (/home/my_user/my_projects/my_symbol_project/node_modules/symbol-openapi-typescript-node-client/dist/api/nodeRoutesApi.js:168:40)
at Request.self.callback (/home/my_user/my_projects/my_symbol_project/node_modules/request/request.js:185:22)
at Request.emit (events.js:315:20)
at Request.<anonymous> (/home/my_user/my_projects/my_symbol_project/node_modules/request/request.js:1154:10)
at Request.emit (events.js:315:20)
at IncomingMessage.<anonymous> (/home/my_user/my_projects/my_symbol_project/node_modules/request/request.js:1076:12)
at Object.onceWrapper (events.js:421:28)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1225:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:30500) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
This error originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch(). To terminate
the node process on unhandled promise rejection, use the CLI flag `--unhandled-
rejections=strict` (see
https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:30500) [DEP0018] DeprecationWarning: Unhandled promise rejections are
deprecated. In the future, promise rejections that are not handled will terminate the
Node.js process with a non-zero exit code.
(node:30500) UnhandledPromiseRejectionWarning: HttpError: HTTP request failed
at Request._callback (/home/my_user/my_projects/my_symbol_project/node_modules/symbol-openapi-typescript-node-client/dist/api/nodeRoutesApi.js:168:40)
at Request.self.callback (/home/my_user/my_projects/my_symbol_project/node_modules/request/request.js:185:22)
at Request.emit (events.js:315:20)
at Request.<anonymous> (/home/my_user/my_projects/my_symbol_project/node_modules/request/request.js:1154:10)
at Request.emit (events.js:315:20)
at IncomingMessage.<anonymous> (/home/my_user/my_projects/my_symbol_project/node_modules/request/request.js:1076:12)
at Object.onceWrapper (events.js:421:28)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1225:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:30500) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
Chain over
User TCC4A2H6VX4MVBYHBG33JRCL7SI4Z7OKSKUQRNXS, amount of
0874BB5FEAAA53C4: 0
User TBHE2SZCMVGQTDDVSMM6G7KE33IWCXA726WXEV3H, amount of
0874BB5FEAAA53C4: 169489
User TBKFTBO4X5EIEHE4LDWVFALJGQU3HCVTAHN2NXCH, amount of
0874BB5FEAAA53C4: 177213
User TCWUJ4ENXWC4QYJ4N7ESVUHTBXL6IJ4TTPNFME3F, amount of
0874BB5FEAAA53C4: 180560
User TC2X5PCGSOEGX6TPZYTZQZYJBOP2QOPDC4VQ5GCK, amount of
0874BB5FEAAA53C4: 6472738