@aurigami/sdk 1.7.0 → 1.7.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.
@@ -0,0 +1 @@
1
+ export declare function queryGraphQL(query: string): Promise<any>;
@@ -11,6 +11,13 @@ export declare function getDecimal(address: Address): number;
11
11
  export declare function getSymbol(address: Address): string;
12
12
  export declare function calcLMRewardApr(rewardsValue: BigNumber, totalStakeValue: BigNumber, frequencyPerYear: number): BigNumber;
13
13
  export declare function getBlockTimestamp(provider: Provider, blockNumber: number): Promise<number>;
14
+ export declare function getBlocksTimestampViaRPC(provider: Provider, blocksNumber: number[]): Promise<number[]>;
15
+ /**
16
+ * Get timestamp of multiple blocks.
17
+ * @note the getBlockTimestamp function is too slow because of the RPC call, even with Promise.all.
18
+ * So I use the GraphQL endpoint of Aurora instead. You can check it here: https://explorer.mainnet.aurora.dev/graphiql
19
+ */
20
+ export declare function getBlocksTimestamp(provider: Provider, blockNumbers: number[]): Promise<number[]>;
14
21
  /**
15
22
  * Get the block number of the last block that has a timestamp before the given timestamp
16
23
  * @param timestamp input timestamp
@@ -1,5 +1,6 @@
1
1
  export * from './aurigami-api';
2
2
  export * from './aurora-api-helpers';
3
+ export * from './graphql-helpers';
3
4
  export * from './helpers';
4
5
  export * from './priceFetcher';
5
6
  export * from './transfer-event-query';
@@ -1137,6 +1137,37 @@ function _getQuery() {
1137
1137
  return _getQuery.apply(this, arguments);
1138
1138
  }
1139
1139
 
1140
+ var GRAPHQL_URL = 'https://explorer.mainnet.aurora.dev/graphiql';
1141
+ function queryGraphQL(_x) {
1142
+ return _queryGraphQL.apply(this, arguments);
1143
+ }
1144
+
1145
+ function _queryGraphQL() {
1146
+ _queryGraphQL = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(query) {
1147
+ var resp;
1148
+ return runtime_1.wrap(function _callee$(_context) {
1149
+ while (1) {
1150
+ switch (_context.prev = _context.next) {
1151
+ case 0:
1152
+ _context.next = 2;
1153
+ return axios.post(GRAPHQL_URL, {
1154
+ query: query
1155
+ });
1156
+
1157
+ case 2:
1158
+ resp = _context.sent;
1159
+ return _context.abrupt("return", resp.data.data);
1160
+
1161
+ case 4:
1162
+ case "end":
1163
+ return _context.stop();
1164
+ }
1165
+ }
1166
+ }, _callee);
1167
+ }));
1168
+ return _queryGraphQL.apply(this, arguments);
1169
+ }
1170
+
1140
1171
  function assert(condition, message) {
1141
1172
  if (message === void 0) {
1142
1173
  message = '';
@@ -1191,14 +1222,6 @@ function calcLMRewardApr(rewardsValue, totalStakeValue, frequencyPerYear) {
1191
1222
  function getBlockTimestamp(_x, _x2) {
1192
1223
  return _getBlockTimestamp.apply(this, arguments);
1193
1224
  }
1194
- /**
1195
- * Get the block number of the last block that has a timestamp before the given timestamp
1196
- * @param timestamp input timestamp
1197
- * @returns return the last block that its timestamp <= the give timestamp
1198
- * @note Check this API for more information: https://aurorascan.dev/apis#blocks
1199
- *
1200
- * This function is much faster than using binary search.
1201
- */
1202
1225
 
1203
1226
  function _getBlockTimestamp() {
1204
1227
  _getBlockTimestamp = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(provider, blockNumber) {
@@ -1224,47 +1247,192 @@ function _getBlockTimestamp() {
1224
1247
  return _getBlockTimestamp.apply(this, arguments);
1225
1248
  }
1226
1249
 
1227
- function getBlockBeforeTimestamp(_x3, _x4) {
1250
+ function getBlocksTimestampViaRPC(_x3, _x4) {
1251
+ return _getBlocksTimestampViaRPC.apply(this, arguments);
1252
+ }
1253
+ /**
1254
+ * Get timestamp of multiple blocks.
1255
+ * @note the getBlockTimestamp function is too slow because of the RPC call, even with Promise.all.
1256
+ * So I use the GraphQL endpoint of Aurora instead. You can check it here: https://explorer.mainnet.aurora.dev/graphiql
1257
+ */
1258
+
1259
+ function _getBlocksTimestampViaRPC() {
1260
+ _getBlocksTimestampViaRPC = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(provider, blocksNumber) {
1261
+ var CHUNK_SIZE, results, i, slice, promises;
1262
+ return runtime_1.wrap(function _callee3$(_context3) {
1263
+ while (1) {
1264
+ switch (_context3.prev = _context3.next) {
1265
+ case 0:
1266
+ // Promise all 200 blocks at a time, using too many blocks at once will cause the RPC call timeout.
1267
+ CHUNK_SIZE = 200;
1268
+ results = [];
1269
+ i = 0;
1270
+
1271
+ case 3:
1272
+ if (!(i < blocksNumber.length)) {
1273
+ _context3.next = 14;
1274
+ break;
1275
+ }
1276
+
1277
+ slice = blocksNumber.slice(i, i + CHUNK_SIZE);
1278
+ promises = slice.map( /*#__PURE__*/function () {
1279
+ var _ref = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(blockNumber) {
1280
+ return runtime_1.wrap(function _callee2$(_context2) {
1281
+ while (1) {
1282
+ switch (_context2.prev = _context2.next) {
1283
+ case 0:
1284
+ return _context2.abrupt("return", getBlockTimestamp(provider, blockNumber));
1285
+
1286
+ case 1:
1287
+ case "end":
1288
+ return _context2.stop();
1289
+ }
1290
+ }
1291
+ }, _callee2);
1292
+ }));
1293
+
1294
+ return function (_x9) {
1295
+ return _ref.apply(this, arguments);
1296
+ };
1297
+ }());
1298
+ _context3.t0 = results;
1299
+ _context3.next = 9;
1300
+ return Promise.all(promises);
1301
+
1302
+ case 9:
1303
+ _context3.t1 = _context3.sent;
1304
+ results = _context3.t0.concat.call(_context3.t0, _context3.t1);
1305
+
1306
+ case 11:
1307
+ i += CHUNK_SIZE;
1308
+ _context3.next = 3;
1309
+ break;
1310
+
1311
+ case 14:
1312
+ return _context3.abrupt("return", results);
1313
+
1314
+ case 15:
1315
+ case "end":
1316
+ return _context3.stop();
1317
+ }
1318
+ }
1319
+ }, _callee3);
1320
+ }));
1321
+ return _getBlocksTimestampViaRPC.apply(this, arguments);
1322
+ }
1323
+
1324
+ function getBlocksTimestamp(_x5, _x6) {
1325
+ return _getBlocksTimestamp.apply(this, arguments);
1326
+ }
1327
+ /**
1328
+ * Get the block number of the last block that has a timestamp before the given timestamp
1329
+ * @param timestamp input timestamp
1330
+ * @returns return the last block that its timestamp <= the give timestamp
1331
+ * @note Check this API for more information: https://aurorascan.dev/apis#blocks
1332
+ *
1333
+ * This function is much faster than using binary search.
1334
+ */
1335
+
1336
+ function _getBlocksTimestamp() {
1337
+ _getBlocksTimestamp = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(provider, blockNumbers) {
1338
+ var CHUNK_SIZE, promises, lines, i, query, results, missingBlocks, missingBlocksTimestamps;
1339
+ return runtime_1.wrap(function _callee4$(_context4) {
1340
+ while (1) {
1341
+ switch (_context4.prev = _context4.next) {
1342
+ case 0:
1343
+ CHUNK_SIZE = 100; // graphql query limit
1344
+
1345
+ promises = [];
1346
+ lines = blockNumbers.map(function (x) {
1347
+ return "b" + x + ": block(number: " + x + ") { timestamp }";
1348
+ });
1349
+
1350
+ for (i = 0; i < lines.length; i += CHUNK_SIZE) {
1351
+ query = "{ " + lines.slice(i, i + CHUNK_SIZE).join('\n') + " }";
1352
+ promises.push(queryGraphQL(query));
1353
+ } // merge all the json objects
1354
+
1355
+
1356
+ _context4.next = 6;
1357
+ return Promise.all(promises);
1358
+
1359
+ case 6:
1360
+ results = _context4.sent.reduce(function (acc, cur) {
1361
+ return _extends({}, acc, cur);
1362
+ });
1363
+ missingBlocks = blockNumbers.filter(function (blockNumber) {
1364
+ return !results["b" + blockNumber];
1365
+ });
1366
+
1367
+ if (missingBlocks.length > 0) {
1368
+ devLog("These blocks are missing from Aurora GraphQL endpoint: " + missingBlocks.join(', '));
1369
+ }
1370
+
1371
+ _context4.next = 11;
1372
+ return getBlocksTimestampViaRPC(provider, missingBlocks);
1373
+
1374
+ case 11:
1375
+ missingBlocksTimestamps = _context4.sent;
1376
+ missingBlocksTimestamps.forEach(function (timestamp, index) {
1377
+ results["b" + missingBlocks[index]] = {
1378
+ timestamp: new Date(timestamp * 1000).toISOString()
1379
+ };
1380
+ });
1381
+ return _context4.abrupt("return", blockNumbers.map(function (blockNumber) {
1382
+ return Math.trunc(new Date(results["b" + blockNumber].timestamp).getTime() / 1000);
1383
+ }));
1384
+
1385
+ case 14:
1386
+ case "end":
1387
+ return _context4.stop();
1388
+ }
1389
+ }
1390
+ }, _callee4);
1391
+ }));
1392
+ return _getBlocksTimestamp.apply(this, arguments);
1393
+ }
1394
+
1395
+ function getBlockBeforeTimestamp(_x7, _x8) {
1228
1396
  return _getBlockBeforeTimestamp.apply(this, arguments);
1229
1397
  }
1230
1398
 
1231
1399
  function _getBlockBeforeTimestamp() {
1232
- _getBlockBeforeTimestamp = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(provider, timestamp) {
1400
+ _getBlockBeforeTimestamp = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee5(provider, timestamp) {
1233
1401
  var result;
1234
- return runtime_1.wrap(function _callee2$(_context2) {
1402
+ return runtime_1.wrap(function _callee5$(_context5) {
1235
1403
  while (1) {
1236
- switch (_context2.prev = _context2.next) {
1404
+ switch (_context5.prev = _context5.next) {
1237
1405
  case 0:
1238
- _context2.next = 2;
1406
+ _context5.next = 2;
1239
1407
  return getQuery('block', 'getblocknobytime', {
1240
1408
  timestamp: timestamp,
1241
1409
  closest: 'before'
1242
1410
  });
1243
1411
 
1244
1412
  case 2:
1245
- result = _context2.sent;
1246
- _context2.t0 = parseInt(result);
1413
+ result = _context5.sent;
1414
+ _context5.t0 = parseInt(result);
1247
1415
 
1248
- if (_context2.t0) {
1249
- _context2.next = 8;
1416
+ if (_context5.t0) {
1417
+ _context5.next = 8;
1250
1418
  break;
1251
1419
  }
1252
1420
 
1253
- _context2.next = 7;
1421
+ _context5.next = 7;
1254
1422
  return provider.getBlockNumber();
1255
1423
 
1256
1424
  case 7:
1257
- _context2.t0 = _context2.sent;
1425
+ _context5.t0 = _context5.sent;
1258
1426
 
1259
1427
  case 8:
1260
- return _context2.abrupt("return", _context2.t0);
1428
+ return _context5.abrupt("return", _context5.t0);
1261
1429
 
1262
1430
  case 9:
1263
1431
  case "end":
1264
- return _context2.stop();
1432
+ return _context5.stop();
1265
1433
  }
1266
1434
  }
1267
- }, _callee2);
1435
+ }, _callee5);
1268
1436
  }));
1269
1437
  return _getBlockBeforeTimestamp.apply(this, arguments);
1270
1438
  }
@@ -2126,6 +2294,7 @@ var helpers = {
2126
2294
  getApi: getApi,
2127
2295
  getPaginatedApi: getPaginatedApi,
2128
2296
  getQuery: getQuery,
2297
+ queryGraphQL: queryGraphQL,
2129
2298
  assert: assert,
2130
2299
  formatObject: formatObject,
2131
2300
  decimalFactor: decimalFactor,
@@ -2136,6 +2305,8 @@ var helpers = {
2136
2305
  getSymbol: getSymbol,
2137
2306
  calcLMRewardApr: calcLMRewardApr,
2138
2307
  getBlockTimestamp: getBlockTimestamp,
2308
+ getBlocksTimestampViaRPC: getBlocksTimestampViaRPC,
2309
+ getBlocksTimestamp: getBlocksTimestamp,
2139
2310
  getBlockBeforeTimestamp: getBlockBeforeTimestamp,
2140
2311
  devLog: devLog,
2141
2312
  fetchPriceFromCoingeckoAPI: fetchPriceFromCoingeckoAPI,
@@ -4306,7 +4477,7 @@ var ReferralRead = /*#__PURE__*/function (_BlockchainEntityRead) {
4306
4477
  /*#__PURE__*/
4307
4478
  function () {
4308
4479
  var _getReferralsList = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(userAddr) {
4309
- var result, items;
4480
+ var result, blocks, blocksTimestamp, items;
4310
4481
  return runtime_1.wrap(function _callee4$(_context4) {
4311
4482
  while (1) {
4312
4483
  switch (_context4.prev = _context4.next) {
@@ -4318,20 +4489,28 @@ var ReferralRead = /*#__PURE__*/function (_BlockchainEntityRead) {
4318
4489
 
4319
4490
  case 2:
4320
4491
  result = _context4.sent;
4321
- items = result.items.map(function (item) {
4492
+ blocks = result.items.map(function (item) {
4493
+ return item.blockNumber;
4494
+ });
4495
+ _context4.next = 6;
4496
+ return getBlocksTimestamp(this._networkConnection.provider, blocks);
4497
+
4498
+ case 6:
4499
+ blocksTimestamp = _context4.sent;
4500
+ items = result.items.map(function (item, i) {
4322
4501
  return {
4323
4502
  user: item.user,
4324
- timestamp: item.blockNumber
4503
+ timestamp: blocksTimestamp[i]
4325
4504
  };
4326
4505
  });
4327
4506
  return _context4.abrupt("return", items);
4328
4507
 
4329
- case 5:
4508
+ case 9:
4330
4509
  case "end":
4331
4510
  return _context4.stop();
4332
4511
  }
4333
4512
  }
4334
- }, _callee4);
4513
+ }, _callee4, this);
4335
4514
  }));
4336
4515
 
4337
4516
  function getReferralsList(_x4) {
@@ -4926,6 +5105,8 @@ exports.formatObject = formatObject;
4926
5105
  exports.getApi = getApi;
4927
5106
  exports.getBlockBeforeTimestamp = getBlockBeforeTimestamp;
4928
5107
  exports.getBlockTimestamp = getBlockTimestamp;
5108
+ exports.getBlocksTimestamp = getBlocksTimestamp;
5109
+ exports.getBlocksTimestampViaRPC = getBlocksTimestampViaRPC;
4929
5110
  exports.getCurrentTimestamp = getCurrentTimestamp;
4930
5111
  exports.getDecimal = getDecimal;
4931
5112
  exports.getPaginatedApi = getPaginatedApi;
@@ -4933,6 +5114,7 @@ exports.getQuery = getQuery;
4933
5114
  exports.getSymbol = getSymbol;
4934
5115
  exports.isSameAddress = isSameAddress;
4935
5116
  exports.networkAddresses = networkAddresses;
5117
+ exports.queryGraphQL = queryGraphQL;
4936
5118
  exports.symbolRecords = symbolRecords;
4937
5119
  exports.validateAndParseAddress = validateAndParseAddress;
4938
5120
  exports.valuateToken = valuateToken;