@gnolang/tm2-js-client 1.3.1 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import { Tx } from '../../proto';
2
2
  import { Provider } from '../provider';
3
- import { BlockInfo, BlockResult, BroadcastTransactionMap, ConsensusParams, NetworkInfo, Status, TxResult } from '../types';
3
+ import { ABCIAccount, BlockInfo, BlockResult, BroadcastTransactionMap, ConsensusParams, NetworkInfo, Status, TxResult } from '../types';
4
4
  /**
5
5
  * Provider based on JSON-RPC HTTP requests
6
6
  */
@@ -21,6 +21,7 @@ export declare class JSONRPCProvider implements Provider {
21
21
  getNetwork(): Promise<NetworkInfo>;
22
22
  getAccountSequence(address: string, height?: number): Promise<number>;
23
23
  getAccountNumber(address: string, height?: number): Promise<number>;
24
+ getAccount(address: string, height?: number): Promise<ABCIAccount>;
24
25
  getStatus(): Promise<Status>;
25
26
  getTransaction(hash: string): Promise<TxResult>;
26
27
  sendTransaction<K extends keyof BroadcastTransactionMap>(tx: string, endpoint: K): Promise<BroadcastTransactionMap[K]['result']>;
@@ -203,6 +203,26 @@ var JSONRPCProvider = /** @class */ (function () {
203
203
  });
204
204
  });
205
205
  };
206
+ JSONRPCProvider.prototype.getAccount = function (address, height) {
207
+ return __awaiter(this, void 0, void 0, function () {
208
+ var abciResponse;
209
+ return __generator(this, function (_a) {
210
+ switch (_a.label) {
211
+ case 0: return [4 /*yield*/, services_1.RestService.post(this.baseURL, {
212
+ request: (0, utility_1.newRequest)(endpoints_1.ABCIEndpoint.ABCI_QUERY, [
213
+ "auth/accounts/".concat(address),
214
+ '',
215
+ '0', // Height; not supported > 0 for now
216
+ false,
217
+ ]),
218
+ })];
219
+ case 1:
220
+ abciResponse = _a.sent();
221
+ return [2 /*return*/, (0, utility_1.extractAccountFromResponse)(abciResponse.response.ResponseBase.Data)];
222
+ }
223
+ });
224
+ });
225
+ };
206
226
  JSONRPCProvider.prototype.getStatus = function () {
207
227
  return __awaiter(this, void 0, void 0, function () {
208
228
  return __generator(this, function (_a) {
@@ -437,4 +437,52 @@ describe('JSON-RPC Provider', function () {
437
437
  });
438
438
  }); });
439
439
  });
440
+ describe('getAccount', function () {
441
+ var validAccount = {
442
+ BaseAccount: {
443
+ address: 'random address',
444
+ coins: '',
445
+ public_key: { '@type': 'pktype', value: 'pk' },
446
+ account_number: '10',
447
+ sequence: '42',
448
+ },
449
+ };
450
+ test.each([
451
+ [JSON.stringify(validAccount), validAccount], // account exists
452
+ ['null', null], // account doesn't exist
453
+ ])('case %#', function (response, expected) { return __awaiter(void 0, void 0, void 0, function () {
454
+ var mockABCIResponse, provider, account, e_3;
455
+ return __generator(this, function (_a) {
456
+ switch (_a.label) {
457
+ case 0:
458
+ mockABCIResponse = (0, jest_mock_extended_1.mock)();
459
+ mockABCIResponse.response.ResponseBase = {
460
+ Log: '',
461
+ Info: '',
462
+ Data: (0, utility_1.stringToBase64)(response),
463
+ Error: null,
464
+ Events: null,
465
+ };
466
+ mockedAxios.post.mockResolvedValue({
467
+ data: (0, utility_1.newResponse)(mockABCIResponse),
468
+ });
469
+ _a.label = 1;
470
+ case 1:
471
+ _a.trys.push([1, 3, , 4]);
472
+ provider = new jsonrpc_1.JSONRPCProvider(mockURL);
473
+ return [4 /*yield*/, provider.getAccount('address')];
474
+ case 2:
475
+ account = _a.sent();
476
+ expect(axios_1.default.post).toHaveBeenCalled();
477
+ expect(account).toStrictEqual(expected);
478
+ return [3 /*break*/, 4];
479
+ case 3:
480
+ e_3 = _a.sent();
481
+ expect(e_3.message).toContain('account is not initialized');
482
+ return [3 /*break*/, 4];
483
+ case 4: return [2 /*return*/];
484
+ }
485
+ });
486
+ }); });
487
+ });
440
488
  });
@@ -1,4 +1,4 @@
1
- import { BlockInfo, BlockResult, BroadcastAsGeneric, BroadcastTransactionMap, ConsensusParams, NetworkInfo, Status } from './types';
1
+ import { ABCIAccount, BlockInfo, BlockResult, BroadcastAsGeneric, BroadcastTransactionMap, ConsensusParams, NetworkInfo, Status } from './types';
2
2
  import { Tx } from '../proto';
3
3
  /**
4
4
  * Read-only abstraction for accessing blockchain data
@@ -17,6 +17,7 @@ export interface Provider {
17
17
  * @param {string} address the bech32 address of the account
18
18
  * @param {number} [height=0] the height for querying.
19
19
  * If omitted, the latest height is used.
20
+ * @deprecated use {@link getAccount} instead
20
21
  */
21
22
  getAccountSequence(address: string, height?: number): Promise<number>;
22
23
  /**
@@ -25,8 +26,17 @@ export interface Provider {
25
26
  * @param {string} address the bech32 address of the account
26
27
  * @param {number} [height=0] the height for querying.
27
28
  * If omitted, the latest height is used
29
+ * @deprecated use {@link getAccount} instead
28
30
  */
29
31
  getAccountNumber(address: string, height?: number): Promise<number>;
32
+ /**
33
+ * Fetches the account. Errors out if the account
34
+ * is not initialized
35
+ * @param {string} address the bech32 address of the account
36
+ * @param {number} [height=0] the height for querying.
37
+ * If omitted, the latest height is used
38
+ */
39
+ getAccount(address: string, height?: number): Promise<ABCIAccount>;
30
40
  /**
31
41
  * Fetches the block at the specific height, if any
32
42
  * @param {number} height the height for querying
@@ -1,7 +1,7 @@
1
1
  import { Tx } from '../../proto';
2
2
  import { ResponseDeliverTx } from '../../proto/tm2/abci';
3
3
  import { Provider } from '../provider';
4
- import { ABCIResponse } from '../types';
4
+ import { ABCIAccount, ABCIResponse } from '../types';
5
5
  /**
6
6
  * Extracts the specific balance denomination from the ABCI response
7
7
  * @param {string | null} abciData the base64-encoded ABCI data
@@ -18,6 +18,11 @@ export declare const extractSequenceFromResponse: (abciData: string | null) => n
18
18
  * @param {string | null} abciData the base64-encoded ABCI data
19
19
  */
20
20
  export declare const extractAccountNumberFromResponse: (abciData: string | null) => number;
21
+ /**
22
+ * Extracts the account from the ABCI response
23
+ * @param {string | null} abciData the base64-encoded ABCI data
24
+ */
25
+ export declare const extractAccountFromResponse: (abciData: string | null) => ABCIAccount;
21
26
  /**
22
27
  * Extracts the simulate transaction response from the ABCI response value
23
28
  * @param {string | null} abciData the base64-encoded ResponseDeliverTx proto message
@@ -36,7 +36,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.waitForTransaction = exports.extractSimulateFromResponse = exports.extractAccountNumberFromResponse = exports.extractSequenceFromResponse = exports.extractBalanceFromResponse = void 0;
39
+ exports.waitForTransaction = exports.extractSimulateFromResponse = exports.extractAccountFromResponse = exports.extractAccountNumberFromResponse = exports.extractSequenceFromResponse = exports.extractBalanceFromResponse = void 0;
40
40
  var crypto_1 = require("@cosmjs/crypto");
41
41
  var proto_1 = require("../../proto");
42
42
  var abci_1 = require("../../proto/tm2/abci");
@@ -115,6 +115,25 @@ var extractAccountNumberFromResponse = function (abciData) {
115
115
  }
116
116
  };
117
117
  exports.extractAccountNumberFromResponse = extractAccountNumberFromResponse;
118
+ /**
119
+ * Extracts the account from the ABCI response
120
+ * @param {string | null} abciData the base64-encoded ABCI data
121
+ */
122
+ var extractAccountFromResponse = function (abciData) {
123
+ // Make sure the response is initialized
124
+ if (!abciData) {
125
+ throw new Error('account is not initialized');
126
+ }
127
+ try {
128
+ // Parse the account
129
+ var account = (0, requests_utility_1.parseABCI)(abciData);
130
+ return account;
131
+ }
132
+ catch (e) {
133
+ throw new Error('account is not initialized');
134
+ }
135
+ };
136
+ exports.extractAccountFromResponse = extractAccountFromResponse;
118
137
  /**
119
138
  * Extracts the simulate transaction response from the ABCI response value
120
139
  * @param {string | null} abciData the base64-encoded ResponseDeliverTx proto message
@@ -1,6 +1,6 @@
1
1
  import { Tx } from '../../proto';
2
2
  import { Provider } from '../provider';
3
- import { BlockInfo, BlockResult, BroadcastTransactionMap, ConsensusParams, NetworkInfo, RPCRequest, RPCResponse, Status, TxResult } from '../types';
3
+ import { ABCIAccount, BlockInfo, BlockResult, BroadcastTransactionMap, ConsensusParams, NetworkInfo, RPCRequest, RPCResponse, Status, TxResult } from '../types';
4
4
  /**
5
5
  * Provider based on WS JSON-RPC HTTP requests
6
6
  */
@@ -48,6 +48,7 @@ export declare class WSProvider implements Provider {
48
48
  getNetwork(): Promise<NetworkInfo>;
49
49
  getAccountSequence(address: string, height?: number): Promise<number>;
50
50
  getAccountNumber(address: string, height?: number): Promise<number>;
51
+ getAccount(address: string, height?: number): Promise<ABCIAccount>;
51
52
  getStatus(): Promise<Status>;
52
53
  getTransaction(hash: string): Promise<TxResult>;
53
54
  sendTransaction<K extends keyof BroadcastTransactionMap>(tx: string, endpoint: K): Promise<BroadcastTransactionMap[K]['result']>;
@@ -296,6 +296,25 @@ var WSProvider = /** @class */ (function () {
296
296
  });
297
297
  });
298
298
  };
299
+ WSProvider.prototype.getAccount = function (address, height) {
300
+ return __awaiter(this, void 0, void 0, function () {
301
+ var response, abciResponse;
302
+ return __generator(this, function (_a) {
303
+ switch (_a.label) {
304
+ case 0: return [4 /*yield*/, this.sendRequest((0, utility_1.newRequest)(endpoints_1.ABCIEndpoint.ABCI_QUERY, [
305
+ "auth/accounts/".concat(address),
306
+ '',
307
+ '0', // Height; not supported > 0 for now
308
+ false,
309
+ ]))];
310
+ case 1:
311
+ response = _a.sent();
312
+ abciResponse = this.parseResponse(response);
313
+ return [2 /*return*/, (0, utility_1.extractAccountFromResponse)(abciResponse.response.ResponseBase.Data)];
314
+ }
315
+ });
316
+ });
317
+ };
299
318
  WSProvider.prototype.getStatus = function () {
300
319
  return __awaiter(this, void 0, void 0, function () {
301
320
  var response;
@@ -336,6 +336,47 @@ describe('WS Provider', function () {
336
336
  });
337
337
  }); });
338
338
  });
339
+ describe('getAccount', function () {
340
+ var validAccount = {
341
+ BaseAccount: {
342
+ address: 'random address',
343
+ coins: '',
344
+ public_key: null,
345
+ account_number: '10',
346
+ sequence: '0',
347
+ },
348
+ };
349
+ test.each([
350
+ [JSON.stringify(validAccount), validAccount], // account exists
351
+ ['null', null], // account doesn't exist
352
+ ])('case %#', function (response, expected) { return __awaiter(void 0, void 0, void 0, function () {
353
+ var mockResponse, account, e_2;
354
+ return __generator(this, function (_a) {
355
+ switch (_a.label) {
356
+ case 0:
357
+ mockResponse = mockABCIResponse(response);
358
+ // Set the response
359
+ return [4 /*yield*/, setHandler(mockResponse)];
360
+ case 1:
361
+ // Set the response
362
+ _a.sent();
363
+ _a.label = 2;
364
+ case 2:
365
+ _a.trys.push([2, 4, , 5]);
366
+ return [4 /*yield*/, wsProvider.getAccount('address')];
367
+ case 3:
368
+ account = _a.sent();
369
+ expect(account).toStrictEqual(expected);
370
+ return [3 /*break*/, 5];
371
+ case 4:
372
+ e_2 = _a.sent();
373
+ expect(e_2.message).toContain('account is not initialized');
374
+ return [3 /*break*/, 5];
375
+ case 5: return [2 /*return*/];
376
+ }
377
+ });
378
+ }); });
379
+ });
339
380
  describe('getBalance', function () {
340
381
  var denomination = 'atom';
341
382
  test.each([
@@ -405,7 +446,7 @@ describe('WS Provider', function () {
405
446
  [validResult, validResult.hash, '', ''], // no error
406
447
  [invalidResult, invalidResult.hash, messages_1.UnauthorizedErrorMessage, mockLog], // error out
407
448
  ])('case %#', function (response, expectedHash, expectedErr, expectedLog) { return __awaiter(void 0, void 0, void 0, function () {
408
- var tx, e_2;
449
+ var tx, e_3;
409
450
  return __generator(this, function (_a) {
410
451
  switch (_a.label) {
411
452
  case 0: return [4 /*yield*/, setHandler(response)];
@@ -423,9 +464,9 @@ describe('WS Provider', function () {
423
464
  }
424
465
  return [3 /*break*/, 5];
425
466
  case 4:
426
- e_2 = _a.sent();
427
- expect(e_2.message).toBe(expectedErr);
428
- expect(e_2.log).toBe(expectedLog);
467
+ e_3 = _a.sent();
468
+ expect(e_3.message).toBe(expectedErr);
469
+ expect(e_3.log).toBe(expectedLog);
429
470
  return [3 /*break*/, 5];
430
471
  case 5: return [2 /*return*/];
431
472
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gnolang/tm2-js-client",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "description": "Tendermint2 JS / TS Client",
5
5
  "main": "./bin/index.js",
6
6
  "repository": {