@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.
package/dist/sdk.esm.js CHANGED
@@ -1132,6 +1132,37 @@ function _getQuery() {
1132
1132
  return _getQuery.apply(this, arguments);
1133
1133
  }
1134
1134
 
1135
+ var GRAPHQL_URL = 'https://explorer.mainnet.aurora.dev/graphiql';
1136
+ function queryGraphQL(_x) {
1137
+ return _queryGraphQL.apply(this, arguments);
1138
+ }
1139
+
1140
+ function _queryGraphQL() {
1141
+ _queryGraphQL = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(query) {
1142
+ var resp;
1143
+ return runtime_1.wrap(function _callee$(_context) {
1144
+ while (1) {
1145
+ switch (_context.prev = _context.next) {
1146
+ case 0:
1147
+ _context.next = 2;
1148
+ return axios.post(GRAPHQL_URL, {
1149
+ query: query
1150
+ });
1151
+
1152
+ case 2:
1153
+ resp = _context.sent;
1154
+ return _context.abrupt("return", resp.data.data);
1155
+
1156
+ case 4:
1157
+ case "end":
1158
+ return _context.stop();
1159
+ }
1160
+ }
1161
+ }, _callee);
1162
+ }));
1163
+ return _queryGraphQL.apply(this, arguments);
1164
+ }
1165
+
1135
1166
  function assert(condition, message) {
1136
1167
  if (message === void 0) {
1137
1168
  message = '';
@@ -1186,14 +1217,6 @@ function calcLMRewardApr(rewardsValue, totalStakeValue, frequencyPerYear) {
1186
1217
  function getBlockTimestamp(_x, _x2) {
1187
1218
  return _getBlockTimestamp.apply(this, arguments);
1188
1219
  }
1189
- /**
1190
- * Get the block number of the last block that has a timestamp before the given timestamp
1191
- * @param timestamp input timestamp
1192
- * @returns return the last block that its timestamp <= the give timestamp
1193
- * @note Check this API for more information: https://aurorascan.dev/apis#blocks
1194
- *
1195
- * This function is much faster than using binary search.
1196
- */
1197
1220
 
1198
1221
  function _getBlockTimestamp() {
1199
1222
  _getBlockTimestamp = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(provider, blockNumber) {
@@ -1219,47 +1242,192 @@ function _getBlockTimestamp() {
1219
1242
  return _getBlockTimestamp.apply(this, arguments);
1220
1243
  }
1221
1244
 
1222
- function getBlockBeforeTimestamp(_x3, _x4) {
1245
+ function getBlocksTimestampViaRPC(_x3, _x4) {
1246
+ return _getBlocksTimestampViaRPC.apply(this, arguments);
1247
+ }
1248
+ /**
1249
+ * Get timestamp of multiple blocks.
1250
+ * @note the getBlockTimestamp function is too slow because of the RPC call, even with Promise.all.
1251
+ * So I use the GraphQL endpoint of Aurora instead. You can check it here: https://explorer.mainnet.aurora.dev/graphiql
1252
+ */
1253
+
1254
+ function _getBlocksTimestampViaRPC() {
1255
+ _getBlocksTimestampViaRPC = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(provider, blocksNumber) {
1256
+ var CHUNK_SIZE, results, i, slice, promises;
1257
+ return runtime_1.wrap(function _callee3$(_context3) {
1258
+ while (1) {
1259
+ switch (_context3.prev = _context3.next) {
1260
+ case 0:
1261
+ // Promise all 200 blocks at a time, using too many blocks at once will cause the RPC call timeout.
1262
+ CHUNK_SIZE = 200;
1263
+ results = [];
1264
+ i = 0;
1265
+
1266
+ case 3:
1267
+ if (!(i < blocksNumber.length)) {
1268
+ _context3.next = 14;
1269
+ break;
1270
+ }
1271
+
1272
+ slice = blocksNumber.slice(i, i + CHUNK_SIZE);
1273
+ promises = slice.map( /*#__PURE__*/function () {
1274
+ var _ref = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(blockNumber) {
1275
+ return runtime_1.wrap(function _callee2$(_context2) {
1276
+ while (1) {
1277
+ switch (_context2.prev = _context2.next) {
1278
+ case 0:
1279
+ return _context2.abrupt("return", getBlockTimestamp(provider, blockNumber));
1280
+
1281
+ case 1:
1282
+ case "end":
1283
+ return _context2.stop();
1284
+ }
1285
+ }
1286
+ }, _callee2);
1287
+ }));
1288
+
1289
+ return function (_x9) {
1290
+ return _ref.apply(this, arguments);
1291
+ };
1292
+ }());
1293
+ _context3.t0 = results;
1294
+ _context3.next = 9;
1295
+ return Promise.all(promises);
1296
+
1297
+ case 9:
1298
+ _context3.t1 = _context3.sent;
1299
+ results = _context3.t0.concat.call(_context3.t0, _context3.t1);
1300
+
1301
+ case 11:
1302
+ i += CHUNK_SIZE;
1303
+ _context3.next = 3;
1304
+ break;
1305
+
1306
+ case 14:
1307
+ return _context3.abrupt("return", results);
1308
+
1309
+ case 15:
1310
+ case "end":
1311
+ return _context3.stop();
1312
+ }
1313
+ }
1314
+ }, _callee3);
1315
+ }));
1316
+ return _getBlocksTimestampViaRPC.apply(this, arguments);
1317
+ }
1318
+
1319
+ function getBlocksTimestamp(_x5, _x6) {
1320
+ return _getBlocksTimestamp.apply(this, arguments);
1321
+ }
1322
+ /**
1323
+ * Get the block number of the last block that has a timestamp before the given timestamp
1324
+ * @param timestamp input timestamp
1325
+ * @returns return the last block that its timestamp <= the give timestamp
1326
+ * @note Check this API for more information: https://aurorascan.dev/apis#blocks
1327
+ *
1328
+ * This function is much faster than using binary search.
1329
+ */
1330
+
1331
+ function _getBlocksTimestamp() {
1332
+ _getBlocksTimestamp = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(provider, blockNumbers) {
1333
+ var CHUNK_SIZE, promises, lines, i, query, results, missingBlocks, missingBlocksTimestamps;
1334
+ return runtime_1.wrap(function _callee4$(_context4) {
1335
+ while (1) {
1336
+ switch (_context4.prev = _context4.next) {
1337
+ case 0:
1338
+ CHUNK_SIZE = 100; // graphql query limit
1339
+
1340
+ promises = [];
1341
+ lines = blockNumbers.map(function (x) {
1342
+ return "b" + x + ": block(number: " + x + ") { timestamp }";
1343
+ });
1344
+
1345
+ for (i = 0; i < lines.length; i += CHUNK_SIZE) {
1346
+ query = "{ " + lines.slice(i, i + CHUNK_SIZE).join('\n') + " }";
1347
+ promises.push(queryGraphQL(query));
1348
+ } // merge all the json objects
1349
+
1350
+
1351
+ _context4.next = 6;
1352
+ return Promise.all(promises);
1353
+
1354
+ case 6:
1355
+ results = _context4.sent.reduce(function (acc, cur) {
1356
+ return _extends({}, acc, cur);
1357
+ });
1358
+ missingBlocks = blockNumbers.filter(function (blockNumber) {
1359
+ return !results["b" + blockNumber];
1360
+ });
1361
+
1362
+ if (missingBlocks.length > 0) {
1363
+ devLog("These blocks are missing from Aurora GraphQL endpoint: " + missingBlocks.join(', '));
1364
+ }
1365
+
1366
+ _context4.next = 11;
1367
+ return getBlocksTimestampViaRPC(provider, missingBlocks);
1368
+
1369
+ case 11:
1370
+ missingBlocksTimestamps = _context4.sent;
1371
+ missingBlocksTimestamps.forEach(function (timestamp, index) {
1372
+ results["b" + missingBlocks[index]] = {
1373
+ timestamp: new Date(timestamp * 1000).toISOString()
1374
+ };
1375
+ });
1376
+ return _context4.abrupt("return", blockNumbers.map(function (blockNumber) {
1377
+ return Math.trunc(new Date(results["b" + blockNumber].timestamp).getTime() / 1000);
1378
+ }));
1379
+
1380
+ case 14:
1381
+ case "end":
1382
+ return _context4.stop();
1383
+ }
1384
+ }
1385
+ }, _callee4);
1386
+ }));
1387
+ return _getBlocksTimestamp.apply(this, arguments);
1388
+ }
1389
+
1390
+ function getBlockBeforeTimestamp(_x7, _x8) {
1223
1391
  return _getBlockBeforeTimestamp.apply(this, arguments);
1224
1392
  }
1225
1393
 
1226
1394
  function _getBlockBeforeTimestamp() {
1227
- _getBlockBeforeTimestamp = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(provider, timestamp) {
1395
+ _getBlockBeforeTimestamp = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee5(provider, timestamp) {
1228
1396
  var result;
1229
- return runtime_1.wrap(function _callee2$(_context2) {
1397
+ return runtime_1.wrap(function _callee5$(_context5) {
1230
1398
  while (1) {
1231
- switch (_context2.prev = _context2.next) {
1399
+ switch (_context5.prev = _context5.next) {
1232
1400
  case 0:
1233
- _context2.next = 2;
1401
+ _context5.next = 2;
1234
1402
  return getQuery('block', 'getblocknobytime', {
1235
1403
  timestamp: timestamp,
1236
1404
  closest: 'before'
1237
1405
  });
1238
1406
 
1239
1407
  case 2:
1240
- result = _context2.sent;
1241
- _context2.t0 = parseInt(result);
1408
+ result = _context5.sent;
1409
+ _context5.t0 = parseInt(result);
1242
1410
 
1243
- if (_context2.t0) {
1244
- _context2.next = 8;
1411
+ if (_context5.t0) {
1412
+ _context5.next = 8;
1245
1413
  break;
1246
1414
  }
1247
1415
 
1248
- _context2.next = 7;
1416
+ _context5.next = 7;
1249
1417
  return provider.getBlockNumber();
1250
1418
 
1251
1419
  case 7:
1252
- _context2.t0 = _context2.sent;
1420
+ _context5.t0 = _context5.sent;
1253
1421
 
1254
1422
  case 8:
1255
- return _context2.abrupt("return", _context2.t0);
1423
+ return _context5.abrupt("return", _context5.t0);
1256
1424
 
1257
1425
  case 9:
1258
1426
  case "end":
1259
- return _context2.stop();
1427
+ return _context5.stop();
1260
1428
  }
1261
1429
  }
1262
- }, _callee2);
1430
+ }, _callee5);
1263
1431
  }));
1264
1432
  return _getBlockBeforeTimestamp.apply(this, arguments);
1265
1433
  }
@@ -2121,6 +2289,7 @@ var helpers = {
2121
2289
  getApi: getApi,
2122
2290
  getPaginatedApi: getPaginatedApi,
2123
2291
  getQuery: getQuery,
2292
+ queryGraphQL: queryGraphQL,
2124
2293
  assert: assert,
2125
2294
  formatObject: formatObject,
2126
2295
  decimalFactor: decimalFactor,
@@ -2131,6 +2300,8 @@ var helpers = {
2131
2300
  getSymbol: getSymbol,
2132
2301
  calcLMRewardApr: calcLMRewardApr,
2133
2302
  getBlockTimestamp: getBlockTimestamp,
2303
+ getBlocksTimestampViaRPC: getBlocksTimestampViaRPC,
2304
+ getBlocksTimestamp: getBlocksTimestamp,
2134
2305
  getBlockBeforeTimestamp: getBlockBeforeTimestamp,
2135
2306
  devLog: devLog,
2136
2307
  fetchPriceFromCoingeckoAPI: fetchPriceFromCoingeckoAPI,
@@ -4305,7 +4476,7 @@ var ReferralRead = /*#__PURE__*/function (_BlockchainEntityRead) {
4305
4476
  /*#__PURE__*/
4306
4477
  function () {
4307
4478
  var _getReferralsList = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4(userAddr) {
4308
- var result, items;
4479
+ var result, blocks, blocksTimestamp, items;
4309
4480
  return runtime_1.wrap(function _callee4$(_context4) {
4310
4481
  while (1) {
4311
4482
  switch (_context4.prev = _context4.next) {
@@ -4317,20 +4488,28 @@ var ReferralRead = /*#__PURE__*/function (_BlockchainEntityRead) {
4317
4488
 
4318
4489
  case 2:
4319
4490
  result = _context4.sent;
4320
- items = result.items.map(function (item) {
4491
+ blocks = result.items.map(function (item) {
4492
+ return item.blockNumber;
4493
+ });
4494
+ _context4.next = 6;
4495
+ return getBlocksTimestamp(this._networkConnection.provider, blocks);
4496
+
4497
+ case 6:
4498
+ blocksTimestamp = _context4.sent;
4499
+ items = result.items.map(function (item, i) {
4321
4500
  return {
4322
4501
  user: item.user,
4323
- timestamp: item.blockNumber
4502
+ timestamp: blocksTimestamp[i]
4324
4503
  };
4325
4504
  });
4326
4505
  return _context4.abrupt("return", items);
4327
4506
 
4328
- case 5:
4507
+ case 9:
4329
4508
  case "end":
4330
4509
  return _context4.stop();
4331
4510
  }
4332
4511
  }
4333
- }, _callee4);
4512
+ }, _callee4, this);
4334
4513
  }));
4335
4514
 
4336
4515
  function getReferralsList(_x4) {
@@ -4844,5 +5023,5 @@ BigNumber.config({
4844
5023
  DECIMAL_PLACES: 50
4845
5024
  });
4846
5025
 
4847
- export { AIRDROP_END_TIMESTAMP, AIRDROP_KEEP_THRESHOLD, AIRDROP_START_TIMESTAMP, ALL_STAKING_POOLS, AURORA_CHAINID, Airdrop, AirdropRead, AirdropReadWrite, AuriEnv, BORROW_LIMIT_BUFFER, BORROW_NEAR_REWARDS_PER_WEEK, BlockchainEntity, BlockchainEntityRead, BlockchainEntityReadWrite, ComptrollerRewardType, DECIMAL_PRECISION, DEPOSIT_NEAR_REWARDS_PER_WEEK, ETHAddress, HARDCODE_PRICE_TOKENS, INF, MarketAction, MiscRead, MoneyMarket, MoneyMarketRead, MoneyMarketReadWrite, ONE_DAY, ONE_WEEK, ONE_YEAR, PLYToken, PLYWNEARToken, PLYWNEAR_POOL_ID, PLYWNEAR_REWARD_TOKENS, PRICE_WHITELISTED, Papermill, PapermillRead, PapermillReadWrite, REWARD_CLAIM_START, Referral, ReferralRead, ReferralReadWrite, SDK, SdkRead, SdkReadWrite, Staking, StakingRead, StakingReadWrite, Token, TokenAmount, TransferEventQuery, assert, calcLMRewardApr, calcValuation, decimalFactor, decimalRecords, devLog, dummyAddress, dummyMarketAddress, dummyPly, dummyPlyAurora, dummyToken, dummyTokenAmount, dummyTokenAmount2, dummyTokenAmount3, dummyUserMarketDetails, dummyZeroTokenAmount, fetchLPPositions, fetchLPPrice, fetchPLYPrice, fetchPULPPrice, fetchPrice, fetchPriceFromCoingecko, fetchPriceFromCoingeckoAPI, fetchPriceFromOracle, fetchStNEARPrice, fetchToken0PriceByLP, fetchUnderlyingTokensPrices, fetchValuation, formatObject, getApi, getBlockBeforeTimestamp, getBlockTimestamp, getCurrentTimestamp, getDecimal, getPaginatedApi, getQuery, getSymbol, isSameAddress, networkAddresses, symbolRecords, validateAndParseAddress, valuateToken };
5026
+ export { AIRDROP_END_TIMESTAMP, AIRDROP_KEEP_THRESHOLD, AIRDROP_START_TIMESTAMP, ALL_STAKING_POOLS, AURORA_CHAINID, Airdrop, AirdropRead, AirdropReadWrite, AuriEnv, BORROW_LIMIT_BUFFER, BORROW_NEAR_REWARDS_PER_WEEK, BlockchainEntity, BlockchainEntityRead, BlockchainEntityReadWrite, ComptrollerRewardType, DECIMAL_PRECISION, DEPOSIT_NEAR_REWARDS_PER_WEEK, ETHAddress, HARDCODE_PRICE_TOKENS, INF, MarketAction, MiscRead, MoneyMarket, MoneyMarketRead, MoneyMarketReadWrite, ONE_DAY, ONE_WEEK, ONE_YEAR, PLYToken, PLYWNEARToken, PLYWNEAR_POOL_ID, PLYWNEAR_REWARD_TOKENS, PRICE_WHITELISTED, Papermill, PapermillRead, PapermillReadWrite, REWARD_CLAIM_START, Referral, ReferralRead, ReferralReadWrite, SDK, SdkRead, SdkReadWrite, Staking, StakingRead, StakingReadWrite, Token, TokenAmount, TransferEventQuery, assert, calcLMRewardApr, calcValuation, decimalFactor, decimalRecords, devLog, dummyAddress, dummyMarketAddress, dummyPly, dummyPlyAurora, dummyToken, dummyTokenAmount, dummyTokenAmount2, dummyTokenAmount3, dummyUserMarketDetails, dummyZeroTokenAmount, fetchLPPositions, fetchLPPrice, fetchPLYPrice, fetchPULPPrice, fetchPrice, fetchPriceFromCoingecko, fetchPriceFromCoingeckoAPI, fetchPriceFromOracle, fetchStNEARPrice, fetchToken0PriceByLP, fetchUnderlyingTokensPrices, fetchValuation, formatObject, getApi, getBlockBeforeTimestamp, getBlockTimestamp, getBlocksTimestamp, getBlocksTimestampViaRPC, getCurrentTimestamp, getDecimal, getPaginatedApi, getQuery, getSymbol, isSameAddress, networkAddresses, queryGraphQL, symbolRecords, validateAndParseAddress, valuateToken };
4848
5027
  //# sourceMappingURL=sdk.esm.js.map