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