@gnolang/tm2-js-client 1.0.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.
Files changed (77) hide show
  1. package/LICENSE.md +201 -0
  2. package/README.md +78 -0
  3. package/bin/index.d.ts +4 -0
  4. package/bin/index.js +20 -0
  5. package/bin/proto/google/protobuf/any.d.ts +151 -0
  6. package/bin/proto/google/protobuf/any.js +124 -0
  7. package/bin/proto/index.d.ts +2 -0
  8. package/bin/proto/index.js +20 -0
  9. package/bin/proto/tm2/tx.d.ts +540 -0
  10. package/bin/proto/tm2/tx.js +365 -0
  11. package/bin/provider/endpoints.d.ts +28 -0
  12. package/bin/provider/endpoints.js +36 -0
  13. package/bin/provider/index.d.ts +6 -0
  14. package/bin/provider/index.js +22 -0
  15. package/bin/provider/jsonrpc/index.d.ts +1 -0
  16. package/bin/provider/jsonrpc/index.js +17 -0
  17. package/bin/provider/jsonrpc/jsonrpc.d.ts +27 -0
  18. package/bin/provider/jsonrpc/jsonrpc.js +219 -0
  19. package/bin/provider/jsonrpc/jsonrpc.test.d.ts +1 -0
  20. package/bin/provider/jsonrpc/jsonrpc.test.js +374 -0
  21. package/bin/provider/provider.d.ts +86 -0
  22. package/bin/provider/provider.js +2 -0
  23. package/bin/provider/types/abci.d.ts +39 -0
  24. package/bin/provider/types/abci.js +4 -0
  25. package/bin/provider/types/common.d.ts +154 -0
  26. package/bin/provider/types/common.js +2 -0
  27. package/bin/provider/types/index.d.ts +3 -0
  28. package/bin/provider/types/index.js +19 -0
  29. package/bin/provider/types/jsonrpc.d.ts +26 -0
  30. package/bin/provider/types/jsonrpc.js +2 -0
  31. package/bin/provider/utility/index.d.ts +2 -0
  32. package/bin/provider/utility/index.js +18 -0
  33. package/bin/provider/utility/provider.utility.d.ts +28 -0
  34. package/bin/provider/utility/provider.utility.js +194 -0
  35. package/bin/provider/utility/requests.utility.d.ts +33 -0
  36. package/bin/provider/utility/requests.utility.js +74 -0
  37. package/bin/provider/websocket/index.d.ts +1 -0
  38. package/bin/provider/websocket/index.js +17 -0
  39. package/bin/provider/websocket/ws.d.ts +55 -0
  40. package/bin/provider/websocket/ws.js +303 -0
  41. package/bin/provider/websocket/ws.test.d.ts +1 -0
  42. package/bin/provider/websocket/ws.test.js +535 -0
  43. package/bin/services/index.d.ts +2 -0
  44. package/bin/services/index.js +18 -0
  45. package/bin/services/rest/restService.d.ts +4 -0
  46. package/bin/services/rest/restService.js +72 -0
  47. package/bin/services/rest/restService.types.d.ts +6 -0
  48. package/bin/services/rest/restService.types.js +2 -0
  49. package/bin/wallet/index.d.ts +5 -0
  50. package/bin/wallet/index.js +21 -0
  51. package/bin/wallet/key/index.d.ts +1 -0
  52. package/bin/wallet/key/index.js +17 -0
  53. package/bin/wallet/key/key.d.ts +21 -0
  54. package/bin/wallet/key/key.js +102 -0
  55. package/bin/wallet/key/key.test.d.ts +1 -0
  56. package/bin/wallet/key/key.test.js +121 -0
  57. package/bin/wallet/ledger/index.d.ts +1 -0
  58. package/bin/wallet/ledger/index.js +17 -0
  59. package/bin/wallet/ledger/ledger.d.ts +22 -0
  60. package/bin/wallet/ledger/ledger.js +109 -0
  61. package/bin/wallet/signer.d.ts +29 -0
  62. package/bin/wallet/signer.js +2 -0
  63. package/bin/wallet/types/index.d.ts +2 -0
  64. package/bin/wallet/types/index.js +18 -0
  65. package/bin/wallet/types/sign.d.ts +17 -0
  66. package/bin/wallet/types/sign.js +4 -0
  67. package/bin/wallet/types/wallet.d.ts +5 -0
  68. package/bin/wallet/types/wallet.js +2 -0
  69. package/bin/wallet/utility/index.d.ts +1 -0
  70. package/bin/wallet/utility/index.js +17 -0
  71. package/bin/wallet/utility/utility.d.ts +30 -0
  72. package/bin/wallet/utility/utility.js +105 -0
  73. package/bin/wallet/wallet.d.ts +94 -0
  74. package/bin/wallet/wallet.js +340 -0
  75. package/bin/wallet/wallet.test.d.ts +1 -0
  76. package/bin/wallet/wallet.test.js +345 -0
  77. package/package.json +64 -0
@@ -0,0 +1,219 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.JSONRPCProvider = void 0;
40
+ var services_1 = require("../../services");
41
+ var utility_1 = require("../utility");
42
+ var endpoints_1 = require("../endpoints");
43
+ /**
44
+ * Provider based on JSON-RPC HTTP requests
45
+ */
46
+ var JSONRPCProvider = /** @class */ (function () {
47
+ /**
48
+ * Creates a new instance of the JSON-RPC Provider
49
+ * @param {string} baseURL the JSON-RPC URL of the node
50
+ */
51
+ function JSONRPCProvider(baseURL) {
52
+ this.baseURL = baseURL;
53
+ }
54
+ JSONRPCProvider.prototype.estimateGas = function (tx) {
55
+ return Promise.reject('not supported');
56
+ };
57
+ JSONRPCProvider.prototype.getBalance = function (address, denomination, height) {
58
+ return __awaiter(this, void 0, void 0, function () {
59
+ var abciResponse;
60
+ return __generator(this, function (_a) {
61
+ switch (_a.label) {
62
+ case 0: return [4 /*yield*/, services_1.RestService.post(this.baseURL, {
63
+ request: (0, utility_1.newRequest)(endpoints_1.ABCIEndpoint.ABCI_QUERY, [
64
+ "bank/balances/".concat(address),
65
+ '',
66
+ '0',
67
+ false,
68
+ ]),
69
+ })];
70
+ case 1:
71
+ abciResponse = _a.sent();
72
+ return [2 /*return*/, (0, utility_1.extractBalanceFromResponse)(abciResponse.response.ResponseBase.Data, denomination ? denomination : 'ugnot')];
73
+ }
74
+ });
75
+ });
76
+ };
77
+ JSONRPCProvider.prototype.getBlock = function (height) {
78
+ return __awaiter(this, void 0, void 0, function () {
79
+ return __generator(this, function (_a) {
80
+ switch (_a.label) {
81
+ case 0: return [4 /*yield*/, services_1.RestService.post(this.baseURL, {
82
+ request: (0, utility_1.newRequest)(endpoints_1.BlockEndpoint.BLOCK, [height.toString()]),
83
+ })];
84
+ case 1: return [2 /*return*/, _a.sent()];
85
+ }
86
+ });
87
+ });
88
+ };
89
+ JSONRPCProvider.prototype.getBlockResult = function (height) {
90
+ return __awaiter(this, void 0, void 0, function () {
91
+ return __generator(this, function (_a) {
92
+ switch (_a.label) {
93
+ case 0: return [4 /*yield*/, services_1.RestService.post(this.baseURL, {
94
+ request: (0, utility_1.newRequest)(endpoints_1.BlockEndpoint.BLOCK_RESULTS, [height.toString()]),
95
+ })];
96
+ case 1: return [2 /*return*/, _a.sent()];
97
+ }
98
+ });
99
+ });
100
+ };
101
+ JSONRPCProvider.prototype.getBlockNumber = function () {
102
+ return __awaiter(this, void 0, void 0, function () {
103
+ var status;
104
+ return __generator(this, function (_a) {
105
+ switch (_a.label) {
106
+ case 0: return [4 /*yield*/, this.getStatus()];
107
+ case 1:
108
+ status = _a.sent();
109
+ return [2 /*return*/, parseInt(status.sync_info.latest_block_height)];
110
+ }
111
+ });
112
+ });
113
+ };
114
+ JSONRPCProvider.prototype.getConsensusParams = function (height) {
115
+ return __awaiter(this, void 0, void 0, function () {
116
+ return __generator(this, function (_a) {
117
+ switch (_a.label) {
118
+ case 0: return [4 /*yield*/, services_1.RestService.post(this.baseURL, {
119
+ request: (0, utility_1.newRequest)(endpoints_1.ConsensusEndpoint.CONSENSUS_PARAMS, [
120
+ height.toString(),
121
+ ]),
122
+ })];
123
+ case 1: return [2 /*return*/, _a.sent()];
124
+ }
125
+ });
126
+ });
127
+ };
128
+ JSONRPCProvider.prototype.getGasPrice = function () {
129
+ return Promise.reject('not supported');
130
+ };
131
+ JSONRPCProvider.prototype.getNetwork = function () {
132
+ return __awaiter(this, void 0, void 0, function () {
133
+ return __generator(this, function (_a) {
134
+ switch (_a.label) {
135
+ case 0: return [4 /*yield*/, services_1.RestService.post(this.baseURL, {
136
+ request: (0, utility_1.newRequest)(endpoints_1.ConsensusEndpoint.NET_INFO),
137
+ })];
138
+ case 1: return [2 /*return*/, _a.sent()];
139
+ }
140
+ });
141
+ });
142
+ };
143
+ JSONRPCProvider.prototype.getAccountSequence = function (address, height) {
144
+ return __awaiter(this, void 0, void 0, function () {
145
+ var abciResponse;
146
+ return __generator(this, function (_a) {
147
+ switch (_a.label) {
148
+ case 0: return [4 /*yield*/, services_1.RestService.post(this.baseURL, {
149
+ request: (0, utility_1.newRequest)(endpoints_1.ABCIEndpoint.ABCI_QUERY, [
150
+ "auth/accounts/".concat(address),
151
+ '',
152
+ '0',
153
+ false,
154
+ ]),
155
+ })];
156
+ case 1:
157
+ abciResponse = _a.sent();
158
+ return [2 /*return*/, (0, utility_1.extractSequenceFromResponse)(abciResponse.response.ResponseBase.Data)];
159
+ }
160
+ });
161
+ });
162
+ };
163
+ JSONRPCProvider.prototype.getAccountNumber = function (address, height) {
164
+ return __awaiter(this, void 0, void 0, function () {
165
+ var abciResponse;
166
+ return __generator(this, function (_a) {
167
+ switch (_a.label) {
168
+ case 0: return [4 /*yield*/, services_1.RestService.post(this.baseURL, {
169
+ request: (0, utility_1.newRequest)(endpoints_1.ABCIEndpoint.ABCI_QUERY, [
170
+ "auth/accounts/".concat(address),
171
+ '',
172
+ '0',
173
+ false,
174
+ ]),
175
+ })];
176
+ case 1:
177
+ abciResponse = _a.sent();
178
+ return [2 /*return*/, (0, utility_1.extractAccountNumberFromResponse)(abciResponse.response.ResponseBase.Data)];
179
+ }
180
+ });
181
+ });
182
+ };
183
+ JSONRPCProvider.prototype.getStatus = function () {
184
+ return __awaiter(this, void 0, void 0, function () {
185
+ return __generator(this, function (_a) {
186
+ switch (_a.label) {
187
+ case 0: return [4 /*yield*/, services_1.RestService.post(this.baseURL, {
188
+ request: (0, utility_1.newRequest)(endpoints_1.CommonEndpoint.STATUS),
189
+ })];
190
+ case 1: return [2 /*return*/, _a.sent()];
191
+ }
192
+ });
193
+ });
194
+ };
195
+ JSONRPCProvider.prototype.sendTransaction = function (tx) {
196
+ return __awaiter(this, void 0, void 0, function () {
197
+ var response;
198
+ return __generator(this, function (_a) {
199
+ switch (_a.label) {
200
+ case 0: return [4 /*yield*/, services_1.RestService.post(this.baseURL, {
201
+ request: (0, utility_1.newRequest)(endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC, [tx]),
202
+ })];
203
+ case 1:
204
+ response = _a.sent();
205
+ return [2 /*return*/, response.hash];
206
+ }
207
+ });
208
+ });
209
+ };
210
+ JSONRPCProvider.prototype.waitForTransaction = function (hash, fromHeight, timeout) {
211
+ return __awaiter(this, void 0, void 0, function () {
212
+ return __generator(this, function (_a) {
213
+ return [2 /*return*/, (0, utility_1.waitForTransaction)(this, hash, fromHeight, timeout)];
214
+ });
215
+ });
216
+ };
217
+ return JSONRPCProvider;
218
+ }());
219
+ exports.JSONRPCProvider = JSONRPCProvider;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,374 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ var axios_1 = __importDefault(require("axios"));
43
+ var jsonrpc_1 = require("./jsonrpc");
44
+ var utility_1 = require("../utility");
45
+ var jest_mock_extended_1 = require("jest-mock-extended");
46
+ var proto_1 = require("../../proto");
47
+ var crypto_1 = require("@cosmjs/crypto");
48
+ var endpoints_1 = require("../endpoints");
49
+ jest.mock('axios');
50
+ var mockedAxios = axios_1.default;
51
+ var mockURL = '127.0.0.1:26657';
52
+ describe('JSON-RPC Provider', function () {
53
+ test('getNetwork', function () { return __awaiter(void 0, void 0, void 0, function () {
54
+ var mockInfo, provider, info;
55
+ return __generator(this, function (_a) {
56
+ switch (_a.label) {
57
+ case 0:
58
+ mockInfo = (0, jest_mock_extended_1.mock)();
59
+ mockInfo.listening = false;
60
+ mockedAxios.post.mockResolvedValue({
61
+ data: (0, utility_1.newResponse)(mockInfo),
62
+ });
63
+ provider = new jsonrpc_1.JSONRPCProvider(mockURL);
64
+ return [4 /*yield*/, provider.getNetwork()];
65
+ case 1:
66
+ info = _a.sent();
67
+ expect(axios_1.default.post).toHaveBeenCalled();
68
+ expect(info).toEqual(mockInfo);
69
+ return [2 /*return*/];
70
+ }
71
+ });
72
+ }); });
73
+ test('getBlock', function () { return __awaiter(void 0, void 0, void 0, function () {
74
+ var mockInfo, provider, info;
75
+ return __generator(this, function (_a) {
76
+ switch (_a.label) {
77
+ case 0:
78
+ mockInfo = (0, jest_mock_extended_1.mock)();
79
+ mockedAxios.post.mockResolvedValue({
80
+ data: (0, utility_1.newResponse)(mockInfo),
81
+ });
82
+ provider = new jsonrpc_1.JSONRPCProvider(mockURL);
83
+ return [4 /*yield*/, provider.getBlock(0)];
84
+ case 1:
85
+ info = _a.sent();
86
+ expect(axios_1.default.post).toHaveBeenCalled();
87
+ expect(info).toEqual(mockInfo);
88
+ return [2 /*return*/];
89
+ }
90
+ });
91
+ }); });
92
+ test('getBlockResult', function () { return __awaiter(void 0, void 0, void 0, function () {
93
+ var mockResult, provider, result;
94
+ return __generator(this, function (_a) {
95
+ switch (_a.label) {
96
+ case 0:
97
+ mockResult = (0, jest_mock_extended_1.mock)();
98
+ mockedAxios.post.mockResolvedValue({
99
+ data: (0, utility_1.newResponse)(mockResult),
100
+ });
101
+ provider = new jsonrpc_1.JSONRPCProvider(mockURL);
102
+ return [4 /*yield*/, provider.getBlockResult(0)];
103
+ case 1:
104
+ result = _a.sent();
105
+ expect(axios_1.default.post).toHaveBeenCalled();
106
+ expect(result).toEqual(mockResult);
107
+ return [2 /*return*/];
108
+ }
109
+ });
110
+ }); });
111
+ test('sendTransaction', function () { return __awaiter(void 0, void 0, void 0, function () {
112
+ var mockResult, provider, hash;
113
+ return __generator(this, function (_a) {
114
+ switch (_a.label) {
115
+ case 0:
116
+ mockResult = (0, jest_mock_extended_1.mock)();
117
+ mockResult.hash = 'hash123';
118
+ mockedAxios.post.mockResolvedValue({
119
+ data: (0, utility_1.newResponse)(mockResult),
120
+ });
121
+ provider = new jsonrpc_1.JSONRPCProvider(mockURL);
122
+ return [4 /*yield*/, provider.sendTransaction('encoded tx')];
123
+ case 1:
124
+ hash = _a.sent();
125
+ expect(axios_1.default.post).toHaveBeenCalled();
126
+ expect(hash).toEqual(mockResult.hash);
127
+ return [2 /*return*/];
128
+ }
129
+ });
130
+ }); });
131
+ test('waitForTransaction', function () { return __awaiter(void 0, void 0, void 0, function () {
132
+ var emptyBlock, tx, encodedTx, txHash, filledBlock, latestBlock, startBlock, mockStatus, responseMap, provider, receivedTx;
133
+ return __generator(this, function (_a) {
134
+ switch (_a.label) {
135
+ case 0:
136
+ emptyBlock = (0, jest_mock_extended_1.mock)();
137
+ emptyBlock.block.data = {
138
+ txs: [],
139
+ };
140
+ tx = {
141
+ messages: [],
142
+ signatures: [],
143
+ memo: 'tx memo',
144
+ };
145
+ encodedTx = proto_1.Tx.encode(tx).finish();
146
+ txHash = (0, crypto_1.sha256)(encodedTx);
147
+ filledBlock = (0, jest_mock_extended_1.mock)();
148
+ filledBlock.block.data = {
149
+ txs: [(0, utility_1.uint8ArrayToBase64)(encodedTx)],
150
+ };
151
+ latestBlock = 5;
152
+ startBlock = latestBlock - 2;
153
+ mockStatus = (0, jest_mock_extended_1.mock)();
154
+ mockStatus.sync_info.latest_block_height = "".concat(latestBlock);
155
+ responseMap = new Map([
156
+ [latestBlock, filledBlock],
157
+ [latestBlock - 1, emptyBlock],
158
+ [startBlock, emptyBlock],
159
+ ]);
160
+ mockedAxios.post.mockImplementation(function (url, params, config) {
161
+ var request = params;
162
+ if (request.method == endpoints_1.CommonEndpoint.STATUS) {
163
+ return Promise.resolve({
164
+ data: (0, utility_1.newResponse)(mockStatus),
165
+ });
166
+ }
167
+ if (!request.params) {
168
+ return Promise.reject('invalid params');
169
+ }
170
+ var blockNum = +request.params[0];
171
+ var info = responseMap.get(blockNum);
172
+ return Promise.resolve({
173
+ data: (0, utility_1.newResponse)(info),
174
+ });
175
+ });
176
+ provider = new jsonrpc_1.JSONRPCProvider(mockURL);
177
+ return [4 /*yield*/, provider.waitForTransaction((0, utility_1.uint8ArrayToBase64)(txHash), startBlock)];
178
+ case 1:
179
+ receivedTx = _a.sent();
180
+ expect(axios_1.default.post).toHaveBeenCalled();
181
+ expect(receivedTx).toEqual(tx);
182
+ return [2 /*return*/];
183
+ }
184
+ });
185
+ }); });
186
+ test('getConsensusParams', function () { return __awaiter(void 0, void 0, void 0, function () {
187
+ var mockParams, provider, params;
188
+ return __generator(this, function (_a) {
189
+ switch (_a.label) {
190
+ case 0:
191
+ mockParams = (0, jest_mock_extended_1.mock)();
192
+ mockParams.block_height = '1';
193
+ mockedAxios.post.mockResolvedValue({
194
+ data: (0, utility_1.newResponse)(mockParams),
195
+ });
196
+ provider = new jsonrpc_1.JSONRPCProvider(mockURL);
197
+ return [4 /*yield*/, provider.getConsensusParams(1)];
198
+ case 1:
199
+ params = _a.sent();
200
+ expect(axios_1.default.post).toHaveBeenCalled();
201
+ expect(params).toEqual(mockParams);
202
+ return [2 /*return*/];
203
+ }
204
+ });
205
+ }); });
206
+ test('getStatus', function () { return __awaiter(void 0, void 0, void 0, function () {
207
+ var mockStatus, provider, status;
208
+ return __generator(this, function (_a) {
209
+ switch (_a.label) {
210
+ case 0:
211
+ mockStatus = (0, jest_mock_extended_1.mock)();
212
+ mockStatus.validator_info.address = 'address';
213
+ mockedAxios.post.mockResolvedValue({
214
+ data: (0, utility_1.newResponse)(mockStatus),
215
+ });
216
+ provider = new jsonrpc_1.JSONRPCProvider(mockURL);
217
+ return [4 /*yield*/, provider.getStatus()];
218
+ case 1:
219
+ status = _a.sent();
220
+ expect(axios_1.default.post).toHaveBeenCalled();
221
+ expect(status).toEqual(mockStatus);
222
+ return [2 /*return*/];
223
+ }
224
+ });
225
+ }); });
226
+ test('getBlockNumber', function () { return __awaiter(void 0, void 0, void 0, function () {
227
+ var expectedBlockNumber, mockStatus, provider, blockNumber;
228
+ return __generator(this, function (_a) {
229
+ switch (_a.label) {
230
+ case 0:
231
+ expectedBlockNumber = 10;
232
+ mockStatus = (0, jest_mock_extended_1.mock)();
233
+ mockStatus.sync_info.latest_block_height = "".concat(expectedBlockNumber);
234
+ mockedAxios.post.mockResolvedValue({
235
+ data: (0, utility_1.newResponse)(mockStatus),
236
+ });
237
+ provider = new jsonrpc_1.JSONRPCProvider(mockURL);
238
+ return [4 /*yield*/, provider.getBlockNumber()];
239
+ case 1:
240
+ blockNumber = _a.sent();
241
+ expect(axios_1.default.post).toHaveBeenCalled();
242
+ expect(blockNumber).toEqual(expectedBlockNumber);
243
+ return [2 /*return*/];
244
+ }
245
+ });
246
+ }); });
247
+ describe('getBalance', function () {
248
+ var denomination = 'atom';
249
+ test.each([
250
+ ['"5gnot,100atom"', 100],
251
+ ['"5universe"', 0],
252
+ ['""', 0], // account doesn't exist
253
+ ])('case %#', function (existing, expected) { return __awaiter(void 0, void 0, void 0, function () {
254
+ var mockABCIResponse, provider, balance;
255
+ return __generator(this, function (_a) {
256
+ switch (_a.label) {
257
+ case 0:
258
+ mockABCIResponse = (0, jest_mock_extended_1.mock)();
259
+ mockABCIResponse.response.ResponseBase = {
260
+ Log: '',
261
+ Info: '',
262
+ Data: (0, utility_1.stringToBase64)(existing),
263
+ Error: null,
264
+ Events: null,
265
+ };
266
+ mockedAxios.post.mockResolvedValue({
267
+ data: (0, utility_1.newResponse)(mockABCIResponse),
268
+ });
269
+ provider = new jsonrpc_1.JSONRPCProvider(mockURL);
270
+ return [4 /*yield*/, provider.getBalance('address', denomination)];
271
+ case 1:
272
+ balance = _a.sent();
273
+ expect(axios_1.default.post).toHaveBeenCalled();
274
+ expect(balance).toBe(expected);
275
+ return [2 /*return*/];
276
+ }
277
+ });
278
+ }); });
279
+ });
280
+ describe('getSequence', function () {
281
+ var validAccount = {
282
+ BaseAccount: {
283
+ address: 'random address',
284
+ coins: '',
285
+ public_key: null,
286
+ account_number: '0',
287
+ sequence: '10',
288
+ },
289
+ };
290
+ test.each([
291
+ [
292
+ JSON.stringify(validAccount),
293
+ parseInt(validAccount.BaseAccount.sequence, 10),
294
+ ],
295
+ ['null', 0], // account doesn't exist
296
+ ])('case %#', function (response, expected) { return __awaiter(void 0, void 0, void 0, function () {
297
+ var mockABCIResponse, provider, sequence;
298
+ return __generator(this, function (_a) {
299
+ switch (_a.label) {
300
+ case 0:
301
+ mockABCIResponse = (0, jest_mock_extended_1.mock)();
302
+ mockABCIResponse.response.ResponseBase = {
303
+ Log: '',
304
+ Info: '',
305
+ Data: (0, utility_1.stringToBase64)(response),
306
+ Error: null,
307
+ Events: null,
308
+ };
309
+ mockedAxios.post.mockResolvedValue({
310
+ data: (0, utility_1.newResponse)(mockABCIResponse),
311
+ });
312
+ provider = new jsonrpc_1.JSONRPCProvider(mockURL);
313
+ return [4 /*yield*/, provider.getAccountSequence('address')];
314
+ case 1:
315
+ sequence = _a.sent();
316
+ expect(axios_1.default.post).toHaveBeenCalled();
317
+ expect(sequence).toBe(expected);
318
+ return [2 /*return*/];
319
+ }
320
+ });
321
+ }); });
322
+ });
323
+ describe('getAccountNumber', function () {
324
+ var validAccount = {
325
+ BaseAccount: {
326
+ address: 'random address',
327
+ coins: '',
328
+ public_key: null,
329
+ account_number: '10',
330
+ sequence: '0',
331
+ },
332
+ };
333
+ test.each([
334
+ [
335
+ JSON.stringify(validAccount),
336
+ parseInt(validAccount.BaseAccount.account_number, 10),
337
+ ],
338
+ ['null', 0], // account doesn't exist
339
+ ])('case %#', function (response, expected) { return __awaiter(void 0, void 0, void 0, function () {
340
+ var mockABCIResponse, provider, accountNumber, e_1;
341
+ return __generator(this, function (_a) {
342
+ switch (_a.label) {
343
+ case 0:
344
+ mockABCIResponse = (0, jest_mock_extended_1.mock)();
345
+ mockABCIResponse.response.ResponseBase = {
346
+ Log: '',
347
+ Info: '',
348
+ Data: (0, utility_1.stringToBase64)(response),
349
+ Error: null,
350
+ Events: null,
351
+ };
352
+ mockedAxios.post.mockResolvedValue({
353
+ data: (0, utility_1.newResponse)(mockABCIResponse),
354
+ });
355
+ _a.label = 1;
356
+ case 1:
357
+ _a.trys.push([1, 3, , 4]);
358
+ provider = new jsonrpc_1.JSONRPCProvider(mockURL);
359
+ return [4 /*yield*/, provider.getAccountNumber('address')];
360
+ case 2:
361
+ accountNumber = _a.sent();
362
+ expect(axios_1.default.post).toHaveBeenCalled();
363
+ expect(accountNumber).toBe(expected);
364
+ return [3 /*break*/, 4];
365
+ case 3:
366
+ e_1 = _a.sent();
367
+ expect(e_1.message).toContain('account is not initialized');
368
+ return [3 /*break*/, 4];
369
+ case 4: return [2 /*return*/];
370
+ }
371
+ });
372
+ }); });
373
+ });
374
+ });
@@ -0,0 +1,86 @@
1
+ import { BlockInfo, BlockResult, ConsensusParams, NetworkInfo, Status } from './types';
2
+ import { Tx } from '../proto';
3
+ /**
4
+ * Read-only abstraction for accessing blockchain data
5
+ */
6
+ export interface Provider {
7
+ /**
8
+ * Fetches the denomination balance of the account
9
+ * @param {string} address the bech32 address of the account
10
+ * @param {string} [denomination=ugnot] the balance denomination
11
+ * @param {number} [height=0] the height for querying.
12
+ * If omitted, the latest height is used
13
+ */
14
+ getBalance(address: string, denomination?: string, height?: number): Promise<number>;
15
+ /**
16
+ * Fetches the account sequence
17
+ * @param {string} address the bech32 address of the account
18
+ * @param {number} [height=0] the height for querying.
19
+ * If omitted, the latest height is used.
20
+ */
21
+ getAccountSequence(address: string, height?: number): Promise<number>;
22
+ /**
23
+ * Fetches the account number. Errors out if the account
24
+ * is not initialized
25
+ * @param {string} address the bech32 address of the account
26
+ * @param {number} [height=0] the height for querying.
27
+ * If omitted, the latest height is used
28
+ */
29
+ getAccountNumber(address: string, height?: number): Promise<number>;
30
+ /**
31
+ * Fetches the block at the specific height, if any
32
+ * @param {number} height the height for querying
33
+ */
34
+ getBlock(height: number): Promise<BlockInfo>;
35
+ /**
36
+ * Fetches the block at the specific height, if any
37
+ * @param {number} height the height for querying
38
+ */
39
+ getBlockResult(height: number): Promise<BlockResult>;
40
+ /**
41
+ * Fetches the latest block number from the chain
42
+ */
43
+ getBlockNumber(): Promise<number>;
44
+ /**
45
+ * Fetches the network information
46
+ */
47
+ getNetwork(): Promise<NetworkInfo>;
48
+ /**
49
+ * Fetches the consensus params for the specific block height
50
+ * @param {number} height the height for querying
51
+ */
52
+ getConsensusParams(height: number): Promise<ConsensusParams>;
53
+ /**
54
+ * Fetches the current node status
55
+ */
56
+ getStatus(): Promise<Status>;
57
+ /**
58
+ * Fetches the current (recommended) average gas price
59
+ */
60
+ getGasPrice(): Promise<number>;
61
+ /**
62
+ * Estimates the gas limit for the transaction
63
+ * @param {Tx} tx the transaction that needs estimating
64
+ */
65
+ estimateGas(tx: Tx): Promise<number>;
66
+ /**
67
+ * Sends the transaction to the node for committing.
68
+ * The transaction needs to be signed beforehand.
69
+ * Returns the transaction hash
70
+ * @param {string} tx the base64-encoded signed transaction
71
+ */
72
+ sendTransaction(tx: string): Promise<string>;
73
+ /**
74
+ * Waits for the transaction to be committed on the chain.
75
+ * NOTE: This method will not take in the fromHeight parameter once
76
+ * proper transaction indexing is added - the implementation should
77
+ * simply try to fetch the transaction first to see if it's included in a block
78
+ * before starting to wait for it; Until then, this method should be used
79
+ * in the sequence:
80
+ * get latest block -> send transaction -> waitForTransaction(block before send)
81
+ * @param {string} hash The transaction hash
82
+ * @param {number} [fromHeight=latest] The block height used to begin the search
83
+ * @param {number} [timeout=15000] Optional wait timeout in MS
84
+ */
85
+ waitForTransaction(hash: string, fromHeight?: number, timeout?: number): Promise<Tx>;
86
+ }