@aurigami/sdk 1.7.0 → 1.7.1

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';
@@ -26,6 +26,7 @@ var abis$9 = _interopDefault(require('@aurigami/contracts/artifacts/contracts/PU
26
26
  var abis$a = _interopDefault(require('@aurigami/contracts/artifacts/contracts/ReferralDirectory.sol/ReferralDirectory.json'));
27
27
  var abstractSigner = require('@ethersproject/abstract-signer');
28
28
  var axios = _interopDefault(require('axios'));
29
+ var fetch = _interopDefault(require('node-fetch'));
29
30
 
30
31
  var dummyAddress = '0xDEADbeEfEEeEEEeEEEeEEeeeeeEeEEeeeeEEEEeE';
31
32
  var ETHAddress = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';
@@ -1137,6 +1138,47 @@ function _getQuery() {
1137
1138
  return _getQuery.apply(this, arguments);
1138
1139
  }
1139
1140
 
1141
+ var GRAPHQL_URL = 'https://explorer.mainnet.aurora.dev/graphiql';
1142
+ function queryGraphQL(_x) {
1143
+ return _queryGraphQL.apply(this, arguments);
1144
+ }
1145
+
1146
+ function _queryGraphQL() {
1147
+ _queryGraphQL = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(query) {
1148
+ var resp;
1149
+ return runtime_1.wrap(function _callee$(_context) {
1150
+ while (1) {
1151
+ switch (_context.prev = _context.next) {
1152
+ case 0:
1153
+ _context.next = 2;
1154
+ return fetch(GRAPHQL_URL, {
1155
+ headers: {
1156
+ 'Content-Type': 'application/json'
1157
+ },
1158
+ method: 'POST',
1159
+ body: JSON.stringify({
1160
+ query: query
1161
+ })
1162
+ });
1163
+
1164
+ case 2:
1165
+ resp = _context.sent;
1166
+ _context.next = 5;
1167
+ return resp.json();
1168
+
1169
+ case 5:
1170
+ return _context.abrupt("return", _context.sent.data);
1171
+
1172
+ case 6:
1173
+ case "end":
1174
+ return _context.stop();
1175
+ }
1176
+ }
1177
+ }, _callee);
1178
+ }));
1179
+ return _queryGraphQL.apply(this, arguments);
1180
+ }
1181
+
1140
1182
  function assert(condition, message) {
1141
1183
  if (message === void 0) {
1142
1184
  message = '';
@@ -1191,14 +1233,6 @@ function calcLMRewardApr(rewardsValue, totalStakeValue, frequencyPerYear) {
1191
1233
  function getBlockTimestamp(_x, _x2) {
1192
1234
  return _getBlockTimestamp.apply(this, arguments);
1193
1235
  }
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
1236
 
1203
1237
  function _getBlockTimestamp() {
1204
1238
  _getBlockTimestamp = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(provider, blockNumber) {
@@ -1224,47 +1258,192 @@ function _getBlockTimestamp() {
1224
1258
  return _getBlockTimestamp.apply(this, arguments);
1225
1259
  }
1226
1260
 
1227
- function getBlockBeforeTimestamp(_x3, _x4) {
1261
+ function getBlocksTimestampViaRPC(_x3, _x4) {
1262
+ return _getBlocksTimestampViaRPC.apply(this, arguments);
1263
+ }
1264
+ /**
1265
+ * Get timestamp of multiple blocks.
1266
+ * @note the getBlockTimestamp function is too slow because of the RPC call, even with Promise.all.
1267
+ * So I use the GraphQL endpoint of Aurora instead. You can check it here: https://explorer.mainnet.aurora.dev/graphiql
1268
+ */
1269
+
1270
+ function _getBlocksTimestampViaRPC() {
1271
+ _getBlocksTimestampViaRPC = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(provider, blocksNumber) {
1272
+ var CHUNK_SIZE, results, i, slice, promises;
1273
+ return runtime_1.wrap(function _callee3$(_context3) {
1274
+ while (1) {
1275
+ switch (_context3.prev = _context3.next) {
1276
+ case 0:
1277
+ // Promise all 200 blocks at a time, using too many blocks at once will cause the RPC call timeout.
1278
+ CHUNK_SIZE = 200;
1279
+ results = [];
1280
+ i = 0;
1281
+
1282
+ case 3:
1283
+ if (!(i < blocksNumber.length)) {
1284
+ _context3.next = 14;
1285
+ break;
1286
+ }
1287
+
1288
+ slice = blocksNumber.slice(i, i + CHUNK_SIZE);
1289
+ promises = slice.map( /*#__PURE__*/function () {
1290
+ var _ref = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(blockNumber) {
1291
+ return runtime_1.wrap(function _callee2$(_context2) {
1292
+ while (1) {
1293
+ switch (_context2.prev = _context2.next) {
1294
+ case 0:
1295
+ return _context2.abrupt("return", getBlockTimestamp(provider, blockNumber));
1296
+
1297
+ case 1:
1298
+ case "end":
1299
+ return _context2.stop();
1300
+ }
1301
+ }
1302
+ }, _callee2);
1303
+ }));
1304
+
1305
+ return function (_x9) {
1306
+ return _ref.apply(this, arguments);
1307
+ };
1308
+ }());
1309
+ _context3.t0 = results;
1310
+ _context3.next = 9;
1311
+ return Promise.all(promises);
1312
+
1313
+ case 9:
1314
+ _context3.t1 = _context3.sent;
1315
+ results = _context3.t0.concat.call(_context3.t0, _context3.t1);
1316
+
1317
+ case 11:
1318
+ i += CHUNK_SIZE;
1319
+ _context3.next = 3;
1320
+ break;
1321
+
1322
+ case 14:
1323
+ return _context3.abrupt("return", results);
1324
+
1325
+ case 15:
1326
+ case "end":
1327
+ return _context3.stop();
1328
+ }
1329
+ }
1330
+ }, _callee3);
1331
+ }));
1332
+ return _getBlocksTimestampViaRPC.apply(this, arguments);
1333
+ }
1334
+
1335
+ function getBlocksTimestamp(_x5, _x6) {
1336
+ return _getBlocksTimestamp.apply(this, arguments);
1337
+ }
1338
+ /**
1339
+ * Get the block number of the last block that has a timestamp before the given timestamp
1340
+ * @param timestamp input timestamp
1341
+ * @returns return the last block that its timestamp <= the give timestamp
1342
+ * @note Check this API for more information: https://aurorascan.dev/apis#blocks
1343
+ *
1344
+ * This function is much faster than using binary search.
1345
+ */
1346
+
1347
+ function _getBlocksTimestamp() {
1348
+ _getBlocksTimestamp = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(provider, blockNumbers) {
1349
+ var CHUNK_SIZE, promises, lines, i, query, results, missingBlocks, missingBlocksTimestamps;
1350
+ return runtime_1.wrap(function _callee4$(_context4) {
1351
+ while (1) {
1352
+ switch (_context4.prev = _context4.next) {
1353
+ case 0:
1354
+ CHUNK_SIZE = 100; // graphql query limit
1355
+
1356
+ promises = [];
1357
+ lines = blockNumbers.map(function (x) {
1358
+ return "b" + x + ": block(number: " + x + ") { timestamp }";
1359
+ });
1360
+
1361
+ for (i = 0; i < lines.length; i += CHUNK_SIZE) {
1362
+ query = "{ " + lines.slice(i, i + CHUNK_SIZE).join('\n') + " }";
1363
+ promises.push(queryGraphQL(query));
1364
+ } // merge all the json objects
1365
+
1366
+
1367
+ _context4.next = 6;
1368
+ return Promise.all(promises);
1369
+
1370
+ case 6:
1371
+ results = _context4.sent.reduce(function (acc, cur) {
1372
+ return _extends({}, acc, cur);
1373
+ });
1374
+ missingBlocks = blockNumbers.filter(function (blockNumber) {
1375
+ return !results["b" + blockNumber];
1376
+ });
1377
+
1378
+ if (missingBlocks.length > 0) {
1379
+ devLog("These blocks are missing from Aurora GraphQL endpoint: " + missingBlocks.join(', '));
1380
+ }
1381
+
1382
+ _context4.next = 11;
1383
+ return getBlocksTimestampViaRPC(provider, missingBlocks);
1384
+
1385
+ case 11:
1386
+ missingBlocksTimestamps = _context4.sent;
1387
+ missingBlocksTimestamps.forEach(function (timestamp, index) {
1388
+ results["b" + missingBlocks[index]] = {
1389
+ timestamp: new Date(timestamp * 1000).toISOString()
1390
+ };
1391
+ });
1392
+ return _context4.abrupt("return", blockNumbers.map(function (blockNumber) {
1393
+ return Math.trunc(new Date(results["b" + blockNumber].timestamp).getTime() / 1000);
1394
+ }));
1395
+
1396
+ case 14:
1397
+ case "end":
1398
+ return _context4.stop();
1399
+ }
1400
+ }
1401
+ }, _callee4);
1402
+ }));
1403
+ return _getBlocksTimestamp.apply(this, arguments);
1404
+ }
1405
+
1406
+ function getBlockBeforeTimestamp(_x7, _x8) {
1228
1407
  return _getBlockBeforeTimestamp.apply(this, arguments);
1229
1408
  }
1230
1409
 
1231
1410
  function _getBlockBeforeTimestamp() {
1232
- _getBlockBeforeTimestamp = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(provider, timestamp) {
1411
+ _getBlockBeforeTimestamp = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee5(provider, timestamp) {
1233
1412
  var result;
1234
- return runtime_1.wrap(function _callee2$(_context2) {
1413
+ return runtime_1.wrap(function _callee5$(_context5) {
1235
1414
  while (1) {
1236
- switch (_context2.prev = _context2.next) {
1415
+ switch (_context5.prev = _context5.next) {
1237
1416
  case 0:
1238
- _context2.next = 2;
1417
+ _context5.next = 2;
1239
1418
  return getQuery('block', 'getblocknobytime', {
1240
1419
  timestamp: timestamp,
1241
1420
  closest: 'before'
1242
1421
  });
1243
1422
 
1244
1423
  case 2:
1245
- result = _context2.sent;
1246
- _context2.t0 = parseInt(result);
1424
+ result = _context5.sent;
1425
+ _context5.t0 = parseInt(result);
1247
1426
 
1248
- if (_context2.t0) {
1249
- _context2.next = 8;
1427
+ if (_context5.t0) {
1428
+ _context5.next = 8;
1250
1429
  break;
1251
1430
  }
1252
1431
 
1253
- _context2.next = 7;
1432
+ _context5.next = 7;
1254
1433
  return provider.getBlockNumber();
1255
1434
 
1256
1435
  case 7:
1257
- _context2.t0 = _context2.sent;
1436
+ _context5.t0 = _context5.sent;
1258
1437
 
1259
1438
  case 8:
1260
- return _context2.abrupt("return", _context2.t0);
1439
+ return _context5.abrupt("return", _context5.t0);
1261
1440
 
1262
1441
  case 9:
1263
1442
  case "end":
1264
- return _context2.stop();
1443
+ return _context5.stop();
1265
1444
  }
1266
1445
  }
1267
- }, _callee2);
1446
+ }, _callee5);
1268
1447
  }));
1269
1448
  return _getBlockBeforeTimestamp.apply(this, arguments);
1270
1449
  }
@@ -2126,6 +2305,7 @@ var helpers = {
2126
2305
  getApi: getApi,
2127
2306
  getPaginatedApi: getPaginatedApi,
2128
2307
  getQuery: getQuery,
2308
+ queryGraphQL: queryGraphQL,
2129
2309
  assert: assert,
2130
2310
  formatObject: formatObject,
2131
2311
  decimalFactor: decimalFactor,
@@ -2136,6 +2316,8 @@ var helpers = {
2136
2316
  getSymbol: getSymbol,
2137
2317
  calcLMRewardApr: calcLMRewardApr,
2138
2318
  getBlockTimestamp: getBlockTimestamp,
2319
+ getBlocksTimestampViaRPC: getBlocksTimestampViaRPC,
2320
+ getBlocksTimestamp: getBlocksTimestamp,
2139
2321
  getBlockBeforeTimestamp: getBlockBeforeTimestamp,
2140
2322
  devLog: devLog,
2141
2323
  fetchPriceFromCoingeckoAPI: fetchPriceFromCoingeckoAPI,
@@ -4306,7 +4488,7 @@ var ReferralRead = /*#__PURE__*/function (_BlockchainEntityRead) {
4306
4488
  /*#__PURE__*/
4307
4489
  function () {
4308
4490
  var _getReferralsList = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(userAddr) {
4309
- var result, items;
4491
+ var result, blocks, blocksTimestamp, items;
4310
4492
  return runtime_1.wrap(function _callee4$(_context4) {
4311
4493
  while (1) {
4312
4494
  switch (_context4.prev = _context4.next) {
@@ -4318,20 +4500,28 @@ var ReferralRead = /*#__PURE__*/function (_BlockchainEntityRead) {
4318
4500
 
4319
4501
  case 2:
4320
4502
  result = _context4.sent;
4321
- items = result.items.map(function (item) {
4503
+ blocks = result.items.map(function (item) {
4504
+ return item.blockNumber;
4505
+ });
4506
+ _context4.next = 6;
4507
+ return getBlocksTimestamp(this._networkConnection.provider, blocks);
4508
+
4509
+ case 6:
4510
+ blocksTimestamp = _context4.sent;
4511
+ items = result.items.map(function (item, i) {
4322
4512
  return {
4323
4513
  user: item.user,
4324
- timestamp: item.blockNumber
4514
+ timestamp: blocksTimestamp[i]
4325
4515
  };
4326
4516
  });
4327
4517
  return _context4.abrupt("return", items);
4328
4518
 
4329
- case 5:
4519
+ case 9:
4330
4520
  case "end":
4331
4521
  return _context4.stop();
4332
4522
  }
4333
4523
  }
4334
- }, _callee4);
4524
+ }, _callee4, this);
4335
4525
  }));
4336
4526
 
4337
4527
  function getReferralsList(_x4) {
@@ -4926,6 +5116,8 @@ exports.formatObject = formatObject;
4926
5116
  exports.getApi = getApi;
4927
5117
  exports.getBlockBeforeTimestamp = getBlockBeforeTimestamp;
4928
5118
  exports.getBlockTimestamp = getBlockTimestamp;
5119
+ exports.getBlocksTimestamp = getBlocksTimestamp;
5120
+ exports.getBlocksTimestampViaRPC = getBlocksTimestampViaRPC;
4929
5121
  exports.getCurrentTimestamp = getCurrentTimestamp;
4930
5122
  exports.getDecimal = getDecimal;
4931
5123
  exports.getPaginatedApi = getPaginatedApi;
@@ -4933,6 +5125,7 @@ exports.getQuery = getQuery;
4933
5125
  exports.getSymbol = getSymbol;
4934
5126
  exports.isSameAddress = isSameAddress;
4935
5127
  exports.networkAddresses = networkAddresses;
5128
+ exports.queryGraphQL = queryGraphQL;
4936
5129
  exports.symbolRecords = symbolRecords;
4937
5130
  exports.validateAndParseAddress = validateAndParseAddress;
4938
5131
  exports.valuateToken = valuateToken;