@gnolang/tm2-js-client 1.0.5 → 1.1.0

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,5 @@
1
1
  import { Provider } from '../provider';
2
- import { BlockInfo, BlockResult, ConsensusParams, NetworkInfo, Status } from '../types';
3
- import { TransactionEndpoint } from '../endpoints';
2
+ import { BlockInfo, BlockResult, BroadcastTransactionMap, ConsensusParams, NetworkInfo, Status } from '../types';
4
3
  import { Tx } from '../../proto';
5
4
  /**
6
5
  * Provider based on JSON-RPC HTTP requests
@@ -23,7 +22,7 @@ export declare class JSONRPCProvider implements Provider {
23
22
  getAccountSequence(address: string, height?: number): Promise<number>;
24
23
  getAccountNumber(address: string, height?: number): Promise<number>;
25
24
  getStatus(): Promise<Status>;
26
- sendTransaction(tx: string, endpoint?: TransactionEndpoint.BROADCAST_TX_SYNC | TransactionEndpoint.BROADCAST_TX_COMMIT): Promise<string>;
25
+ sendTransaction<K extends keyof BroadcastTransactionMap>(tx: string, endpoint: K): Promise<BroadcastTransactionMap[K]['result']>;
27
26
  private broadcastTxSync;
28
27
  private broadcastTxCommit;
29
28
  waitForTransaction(hash: string, fromHeight?: number, timeout?: number): Promise<Tx>;
@@ -195,18 +195,19 @@ var JSONRPCProvider = /** @class */ (function () {
195
195
  };
196
196
  JSONRPCProvider.prototype.sendTransaction = function (tx, endpoint) {
197
197
  return __awaiter(this, void 0, void 0, function () {
198
- var queryEndpoint, request;
198
+ var request;
199
199
  return __generator(this, function (_a) {
200
- queryEndpoint = endpoint
201
- ? endpoint
202
- : endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC;
203
- request = (0, utility_1.newRequest)(queryEndpoint, [tx]);
204
- if (queryEndpoint == endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC) {
205
- return [2 /*return*/, this.broadcastTxSync(request)];
200
+ request = (0, utility_1.newRequest)(endpoint, [tx]);
201
+ switch (endpoint) {
202
+ case endpoints_1.TransactionEndpoint.BROADCAST_TX_COMMIT:
203
+ // The endpoint is a commit broadcast
204
+ // (it waits for the transaction to be committed) to the chain before returning
205
+ return [2 /*return*/, this.broadcastTxCommit(request)];
206
+ case endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC:
207
+ default:
208
+ return [2 /*return*/, this.broadcastTxSync(request)];
206
209
  }
207
- // The endpoint is a commit broadcast
208
- // (it waits for the transaction to be committed) to the chain before returning
209
- return [2 /*return*/, this.broadcastTxCommit(request)];
210
+ return [2 /*return*/];
210
211
  });
211
212
  });
212
213
  };
@@ -227,7 +228,7 @@ var JSONRPCProvider = /** @class */ (function () {
227
228
  log = response.Log;
228
229
  throw (0, errors_utility_1.constructRequestError)(errType, log);
229
230
  }
230
- return [2 /*return*/, response.hash];
231
+ return [2 /*return*/, response];
231
232
  }
232
233
  });
233
234
  });
@@ -255,7 +256,7 @@ var JSONRPCProvider = /** @class */ (function () {
255
256
  log = deliver_tx.ResponseBase.Log;
256
257
  throw (0, errors_utility_1.constructRequestError)(errType, log);
257
258
  }
258
- return [2 /*return*/, response.hash];
259
+ return [2 /*return*/, response];
259
260
  }
260
261
  });
261
262
  });
@@ -130,7 +130,7 @@ describe('JSON-RPC Provider', function () {
130
130
  [validResult, validResult.hash, '', ''],
131
131
  [invalidResult, invalidResult.hash, messages_1.UnauthorizedErrorMessage, mockLog], // error out
132
132
  ])('case %#', function (response, expectedHash, expectedErr, expectedLog) { return __awaiter(void 0, void 0, void 0, function () {
133
- var provider, hash, e_1;
133
+ var provider, tx, e_1;
134
134
  return __generator(this, function (_a) {
135
135
  switch (_a.label) {
136
136
  case 0:
@@ -141,11 +141,11 @@ describe('JSON-RPC Provider', function () {
141
141
  case 1:
142
142
  _a.trys.push([1, 3, , 4]);
143
143
  provider = new jsonrpc_1.JSONRPCProvider(mockURL);
144
- return [4 /*yield*/, provider.sendTransaction('encoded tx')];
144
+ return [4 /*yield*/, provider.sendTransaction('encoded tx', endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC)];
145
145
  case 2:
146
- hash = _a.sent();
146
+ tx = _a.sent();
147
147
  expect(axios_1.default.post).toHaveBeenCalled();
148
- expect(hash).toEqual(expectedHash);
148
+ expect(tx.hash).toEqual(expectedHash);
149
149
  if (expectedErr != '') {
150
150
  fail('expected error');
151
151
  }
@@ -1,6 +1,5 @@
1
- import { BlockInfo, BlockResult, ConsensusParams, NetworkInfo, Status } from './types';
1
+ import { BlockInfo, BlockResult, BroadcastAsGeneric, BroadcastTransactionMap, ConsensusParams, NetworkInfo, Status } from './types';
2
2
  import { Tx } from '../proto';
3
- import { TransactionEndpoint } from './endpoints';
4
3
  /**
5
4
  * Read-only abstraction for accessing blockchain data
6
5
  */
@@ -65,14 +64,14 @@ export interface Provider {
65
64
  */
66
65
  estimateGas(tx: Tx): Promise<number>;
67
66
  /**
68
- * Sends the transaction to the node for committing.
67
+ * Sends the transaction to the node. If the type of endpoint
68
+ * is a broadcast commit, waits for the transaction to be committed to the chain.
69
69
  * The transaction needs to be signed beforehand.
70
- * Returns the transaction hash
70
+ * Returns the transaction broadcast result.
71
71
  * @param {string} tx the base64-encoded signed transaction
72
- * @param {TransactionEndpoint} endpoint the transaction broadcast type (sync / commit).
73
- * Defaults to broadcast_sync
72
+ * @param {BroadcastType} endpoint the transaction broadcast type (sync / commit)
74
73
  */
75
- sendTransaction(tx: string, endpoint?: TransactionEndpoint.BROADCAST_TX_SYNC | TransactionEndpoint.BROADCAST_TX_COMMIT): Promise<string>;
74
+ sendTransaction<K extends keyof BroadcastTransactionMap>(tx: string, endpoint: K): Promise<BroadcastAsGeneric<K>['result']>;
76
75
  /**
77
76
  * Waits for the transaction to be committed on the chain.
78
77
  * NOTE: This method will not take in the fromHeight parameter once
@@ -1,4 +1,5 @@
1
1
  import { ABCIResponseBase } from './abci';
2
+ import { TransactionEndpoint } from '../endpoints';
2
3
  export interface NetworkInfo {
3
4
  listening: boolean;
4
5
  listeners: string[];
@@ -157,4 +158,20 @@ export interface BroadcastTxCommitResult {
157
158
  hash: string;
158
159
  height: string;
159
160
  }
161
+ export type BroadcastType = TransactionEndpoint.BROADCAST_TX_SYNC | TransactionEndpoint.BROADCAST_TX_COMMIT;
162
+ export type BroadcastTransactionSync = {
163
+ endpoint: TransactionEndpoint.BROADCAST_TX_SYNC;
164
+ result: BroadcastTxSyncResult;
165
+ };
166
+ export type BroadcastTransactionCommit = {
167
+ endpoint: TransactionEndpoint.BROADCAST_TX_COMMIT;
168
+ result: BroadcastTxCommitResult;
169
+ };
170
+ export type BroadcastTransactionMap = {
171
+ [TransactionEndpoint.BROADCAST_TX_COMMIT]: BroadcastTransactionCommit;
172
+ [TransactionEndpoint.BROADCAST_TX_SYNC]: BroadcastTransactionSync;
173
+ };
174
+ export type BroadcastAsGeneric<K extends keyof BroadcastTransactionMap = keyof BroadcastTransactionMap> = {
175
+ [P in K]: BroadcastTransactionMap[P];
176
+ }[K];
160
177
  export {};
@@ -1,2 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ var endpoints_1 = require("../endpoints");
@@ -1,7 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { Provider } from '../provider';
3
- import { BlockInfo, BlockResult, ConsensusParams, NetworkInfo, RPCRequest, RPCResponse, Status } from '../types';
4
- import { TransactionEndpoint } from '../endpoints';
3
+ import { BlockInfo, BlockResult, BroadcastTransactionMap, ConsensusParams, NetworkInfo, RPCRequest, RPCResponse, Status } from '../types';
5
4
  import { Tx } from '../../proto';
6
5
  /**
7
6
  * Provider based on WS JSON-RPC HTTP requests
@@ -51,7 +50,7 @@ export declare class WSProvider implements Provider {
51
50
  getAccountSequence(address: string, height?: number): Promise<number>;
52
51
  getAccountNumber(address: string, height?: number): Promise<number>;
53
52
  getStatus(): Promise<Status>;
54
- sendTransaction(tx: string, endpoint?: TransactionEndpoint.BROADCAST_TX_SYNC | TransactionEndpoint.BROADCAST_TX_COMMIT): Promise<string>;
53
+ sendTransaction<K extends keyof BroadcastTransactionMap>(tx: string, endpoint: K): Promise<BroadcastTransactionMap[K]['result']>;
55
54
  private broadcastTxSync;
56
55
  private broadcastTxCommit;
57
56
  waitForTransaction(hash: string, fromHeight?: number, timeout?: number): Promise<Tx>;
@@ -285,18 +285,19 @@ var WSProvider = /** @class */ (function () {
285
285
  };
286
286
  WSProvider.prototype.sendTransaction = function (tx, endpoint) {
287
287
  return __awaiter(this, void 0, void 0, function () {
288
- var queryEndpoint, request;
288
+ var request;
289
289
  return __generator(this, function (_a) {
290
- queryEndpoint = endpoint
291
- ? endpoint
292
- : endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC;
293
- request = (0, utility_1.newRequest)(queryEndpoint, [tx]);
294
- if (queryEndpoint == endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC) {
295
- return [2 /*return*/, this.broadcastTxSync(request)];
290
+ request = (0, utility_1.newRequest)(endpoint, [tx]);
291
+ switch (endpoint) {
292
+ case endpoints_1.TransactionEndpoint.BROADCAST_TX_COMMIT:
293
+ // The endpoint is a commit broadcast
294
+ // (it waits for the transaction to be committed) to the chain before returning
295
+ return [2 /*return*/, this.broadcastTxCommit(request)];
296
+ case endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC:
297
+ default:
298
+ return [2 /*return*/, this.broadcastTxSync(request)];
296
299
  }
297
- // The endpoint is a commit broadcast
298
- // (it waits for the transaction to be committed) to the chain before returning
299
- return [2 /*return*/, this.broadcastTxCommit(request)];
300
+ return [2 /*return*/];
300
301
  });
301
302
  });
302
303
  };
@@ -316,7 +317,7 @@ var WSProvider = /** @class */ (function () {
316
317
  log = broadcastResponse.Log;
317
318
  throw (0, errors_utility_1.constructRequestError)(errType, log);
318
319
  }
319
- return [2 /*return*/, broadcastResponse.hash];
320
+ return [2 /*return*/, broadcastResponse];
320
321
  }
321
322
  });
322
323
  });
@@ -343,7 +344,7 @@ var WSProvider = /** @class */ (function () {
343
344
  log = deliver_tx.ResponseBase.Log;
344
345
  throw (0, errors_utility_1.constructRequestError)(errType, log);
345
346
  }
346
- return [2 /*return*/, broadcastResponse.hash];
347
+ return [2 /*return*/, broadcastResponse];
347
348
  }
348
349
  });
349
350
  });
@@ -358,7 +358,7 @@ describe('WS Provider', function () {
358
358
  [validResult, validResult.hash, '', ''],
359
359
  [invalidResult, invalidResult.hash, messages_1.UnauthorizedErrorMessage, mockLog], // error out
360
360
  ])('case %#', function (response, expectedHash, expectedErr, expectedLog) { return __awaiter(void 0, void 0, void 0, function () {
361
- var hash, e_2;
361
+ var tx, e_2;
362
362
  return __generator(this, function (_a) {
363
363
  switch (_a.label) {
364
364
  case 0: return [4 /*yield*/, setHandler(response)];
@@ -367,10 +367,10 @@ describe('WS Provider', function () {
367
367
  _a.label = 2;
368
368
  case 2:
369
369
  _a.trys.push([2, 4, , 5]);
370
- return [4 /*yield*/, wsProvider.sendTransaction('encoded tx')];
370
+ return [4 /*yield*/, wsProvider.sendTransaction('encoded tx', endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC)];
371
371
  case 3:
372
- hash = _a.sent();
373
- expect(hash).toEqual(expectedHash);
372
+ tx = _a.sent();
373
+ expect(tx.hash).toEqual(expectedHash);
374
374
  if (expectedErr != '') {
375
375
  fail('expected error');
376
376
  }
@@ -1,4 +1,4 @@
1
- import { Provider } from '../provider';
1
+ import { BroadcastTransactionMap, Provider } from '../provider';
2
2
  import { Signer } from './signer';
3
3
  import { LedgerConnector } from '@cosmjs/ledger-amino';
4
4
  import { Any, Tx } from '../proto';
@@ -81,12 +81,14 @@ export declare class Wallet {
81
81
  */
82
82
  signTransaction: (tx: Tx, decodeTxMessages: (messages: Any[]) => any[]) => Promise<Tx>;
83
83
  /**
84
- * Encodes and sends the transaction.
84
+ * Encodes and sends the transaction. If the type of endpoint
85
+ * is a broadcast commit, waits for the transaction to be committed to the chain.
85
86
  * The transaction needs to be signed beforehand.
86
87
  * Returns the transaction hash (base-64)
87
88
  * @param {Tx} tx the signed transaction
89
+ * @param {BroadcastType} endpoint the transaction broadcast type (sync / commit)
88
90
  */
89
- sendTransaction: (tx: Tx) => Promise<string>;
91
+ sendTransaction<K extends keyof BroadcastTransactionMap>(tx: Tx, endpoint: K): Promise<BroadcastTransactionMap[K]['result']>;
90
92
  /**
91
93
  * Returns the associated signer
92
94
  */
@@ -244,12 +244,22 @@ var Wallet = /** @class */ (function () {
244
244
  });
245
245
  }); };
246
246
  /**
247
- * Encodes and sends the transaction.
248
- * The transaction needs to be signed beforehand.
249
- * Returns the transaction hash (base-64)
250
- * @param {Tx} tx the signed transaction
247
+ * Returns the associated signer
251
248
  */
252
- this.sendTransaction = function (tx) { return __awaiter(_this, void 0, void 0, function () {
249
+ this.getSigner = function () {
250
+ return _this.signer;
251
+ };
252
+ }
253
+ /**
254
+ * Encodes and sends the transaction. If the type of endpoint
255
+ * is a broadcast commit, waits for the transaction to be committed to the chain.
256
+ * The transaction needs to be signed beforehand.
257
+ * Returns the transaction hash (base-64)
258
+ * @param {Tx} tx the signed transaction
259
+ * @param {BroadcastType} endpoint the transaction broadcast type (sync / commit)
260
+ */
261
+ Wallet.prototype.sendTransaction = function (tx, endpoint) {
262
+ return __awaiter(this, void 0, void 0, function () {
253
263
  var encodedTx;
254
264
  return __generator(this, function (_b) {
255
265
  if (!this.provider) {
@@ -257,16 +267,10 @@ var Wallet = /** @class */ (function () {
257
267
  }
258
268
  encodedTx = (0, provider_1.uint8ArrayToBase64)(proto_1.Tx.encode(tx).finish());
259
269
  // Send the encoded transaction
260
- return [2 /*return*/, this.provider.sendTransaction(encodedTx)];
270
+ return [2 /*return*/, this.provider.sendTransaction(encodedTx, endpoint)];
261
271
  });
262
- }); };
263
- /**
264
- * Returns the associated signer
265
- */
266
- this.getSigner = function () {
267
- return _this.signer;
268
- };
269
- }
272
+ });
273
+ };
270
274
  var _a;
271
275
  _a = Wallet;
272
276
  // Wallet initialization //
@@ -39,6 +39,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
39
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
40
  };
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
+ var provider_1 = require("../provider");
42
43
  var jest_mock_extended_1 = require("jest-mock-extended");
43
44
  var wallet_1 = require("./wallet");
44
45
  var crypto_1 = require("@cosmjs/crypto");
@@ -298,7 +299,7 @@ describe('Wallet', function () {
298
299
  });
299
300
  }); });
300
301
  test('sendTransaction', function () { return __awaiter(void 0, void 0, void 0, function () {
301
- var mockTx, mockTxHash, mockStatus, mockProvider, wallet, txHash;
302
+ var mockTx, mockTxHash, mockStatus, mockTransaction, mockProvider, wallet, tx;
302
303
  return __generator(this, function (_a) {
303
304
  switch (_a.label) {
304
305
  case 0:
@@ -325,19 +326,25 @@ describe('Wallet', function () {
325
326
  },
326
327
  network: 'testchain',
327
328
  };
329
+ mockTransaction = {
330
+ error: null,
331
+ data: null,
332
+ Log: '',
333
+ hash: mockTxHash,
334
+ };
328
335
  mockProvider = (0, jest_mock_extended_1.mock)();
329
336
  mockProvider.getStatus.mockResolvedValue(mockStatus);
330
337
  mockProvider.getAccountNumber.mockResolvedValue(10);
331
338
  mockProvider.getAccountSequence.mockResolvedValue(10);
332
- mockProvider.sendTransaction.mockResolvedValue(mockTxHash);
339
+ mockProvider.sendTransaction.mockResolvedValue(mockTransaction);
333
340
  return [4 /*yield*/, wallet_1.Wallet.createRandom()];
334
341
  case 1:
335
342
  wallet = _a.sent();
336
343
  wallet.connect(mockProvider);
337
- return [4 /*yield*/, wallet.sendTransaction(mockTx)];
344
+ return [4 /*yield*/, wallet.sendTransaction(mockTx, provider_1.TransactionEndpoint.BROADCAST_TX_SYNC)];
338
345
  case 2:
339
- txHash = _a.sent();
340
- expect(txHash).toBe(mockTxHash);
346
+ tx = _a.sent();
347
+ expect(tx.hash).toBe(mockTxHash);
341
348
  return [2 /*return*/];
342
349
  }
343
350
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gnolang/tm2-js-client",
3
- "version": "1.0.5",
3
+ "version": "1.1.0",
4
4
  "description": "Tendermint2 JS / TS Client",
5
5
  "main": "./bin/index.js",
6
6
  "repository": {