@ref-finance/ref-sdk 1.3.7 → 1.3.9

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.
@@ -531,18 +531,6 @@ function _extends() {
531
531
  };
532
532
  return _extends.apply(this, arguments);
533
533
  }
534
- function _objectWithoutPropertiesLoose(source, excluded) {
535
- if (source == null) return {};
536
- var target = {};
537
- var sourceKeys = Object.keys(source);
538
- var key, i;
539
- for (i = 0; i < sourceKeys.length; i++) {
540
- key = sourceKeys[i];
541
- if (excluded.indexOf(key) >= 0) continue;
542
- target[key] = source[key];
543
- }
544
- return target;
545
- }
546
534
  function _unsupportedIterableToArray(o, minLen) {
547
535
  if (!o) return;
548
536
  if (typeof o === "string") return _arrayLikeToArray(o, minLen);
@@ -4694,24 +4682,36 @@ var getRefPools = /*#__PURE__*/function () {
4694
4682
  }();
4695
4683
  var fetchAllPools = /*#__PURE__*/function () {
4696
4684
  var _ref9 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(perPage) {
4697
- var totalPools, pages, pools;
4685
+ var _pools, totalPools, pages, pools;
4698
4686
  return _regeneratorRuntime().wrap(function _callee7$(_context7) {
4699
4687
  while (1) {
4700
4688
  switch (_context7.prev = _context7.next) {
4701
4689
  case 0:
4690
+ _context7.prev = 0;
4691
+ _context7.next = 3;
4692
+ return fetch(exports.config.indexerUrl + "/fetchAllPools").then(function (res) {
4693
+ return res.json();
4694
+ });
4695
+ case 3:
4696
+ _pools = _context7.sent;
4697
+ return _context7.abrupt("return", _pools);
4698
+ case 7:
4699
+ _context7.prev = 7;
4700
+ _context7.t0 = _context7["catch"](0);
4701
+ case 9:
4702
4702
  if (perPage) {
4703
4703
  DEFAULT_PAGE_LIMIT = Math.min(perPage, 500);
4704
4704
  }
4705
- _context7.next = 3;
4705
+ _context7.next = 12;
4706
4706
  return getTotalPools();
4707
- case 3:
4707
+ case 12:
4708
4708
  totalPools = _context7.sent;
4709
4709
  pages = Math.ceil(totalPools / DEFAULT_PAGE_LIMIT);
4710
- _context7.next = 7;
4710
+ _context7.next = 16;
4711
4711
  return Promise.all([].concat(Array(pages)).fill(0).map(function (_, i) {
4712
4712
  return getRefPools(i + 1);
4713
4713
  }));
4714
- case 7:
4714
+ case 16:
4715
4715
  pools = _context7.sent.flat();
4716
4716
  return _context7.abrupt("return", {
4717
4717
  simplePools: pools.filter(function (p) {
@@ -4724,19 +4724,18 @@ var fetchAllPools = /*#__PURE__*/function () {
4724
4724
  return p.pool_kind && p.pool_kind === 'RATED_SWAP';
4725
4725
  })
4726
4726
  });
4727
- case 9:
4727
+ case 18:
4728
4728
  case "end":
4729
4729
  return _context7.stop();
4730
4730
  }
4731
4731
  }
4732
- }, _callee7);
4732
+ }, _callee7, null, [[0, 7]]);
4733
4733
  }));
4734
4734
  return function fetchAllPools(_x8) {
4735
4735
  return _ref9.apply(this, arguments);
4736
4736
  };
4737
4737
  }();
4738
4738
 
4739
- var _excluded = ["slippageTolerance", "refreshTrigger", "onSwap", "AccountId", "poolFetchingState", "referralId"];
4740
4739
  var ThemeContext = /*#__PURE__*/React.createContext(defaultTheme);
4741
4740
  var ThemeContextProvider = function ThemeContextProvider(_ref) {
4742
4741
  var customTheme = _ref.customTheme,
@@ -4915,7 +4914,7 @@ var useSwap = function useSwap(params) {
4915
4914
  AccountId = params.AccountId,
4916
4915
  poolFetchingState = params.poolFetchingState,
4917
4916
  referralId = params.referralId,
4918
- swapParams = _objectWithoutPropertiesLoose(params, _excluded);
4917
+ tokens = params.tokens;
4919
4918
  var tokenIn = params.tokenIn,
4920
4919
  tokenOut = params.tokenOut,
4921
4920
  amountIn = params.amountIn;
@@ -4937,6 +4936,15 @@ var useSwap = function useSwap(params) {
4937
4936
  var _useState14 = React.useState(false),
4938
4937
  forceEstimate = _useState14[0],
4939
4938
  setForceEstimate = _useState14[1];
4939
+ var _useTokenBalances = useTokenBalances(tokens, AccountId),
4940
+ balances = _useTokenBalances.balances,
4941
+ updateTokenBalance = _useTokenBalances.updateTokenBalance;
4942
+ var tokenInBalance = React.useMemo(function () {
4943
+ return balances[tokenIn == null ? void 0 : tokenIn.id];
4944
+ }, [tokenIn, balances]);
4945
+ var tokenOutBalance = React.useMemo(function () {
4946
+ return balances[tokenOut == null ? void 0 : tokenOut.id] || '';
4947
+ }, [tokenOut, balances]);
4940
4948
  var minAmountOut = amountOut ? percentLess(slippageTolerance, amountOut) : '';
4941
4949
  var fee = !params.tokenOut || !params.tokenIn ? 0 : getAvgFee(estimates, params.tokenOut.id, toNonDivisibleNumber(params.tokenIn.decimals, params.amountIn));
4942
4950
  var rate = calculateExchangeRate(ONLY_ZEROS.test(params.amountIn) ? '1' : params.amountIn, amountOut || '1');
@@ -4978,8 +4986,16 @@ var useSwap = function useSwap(params) {
4978
4986
  _minAmountOut = percentLess(slippageTolerance, scientificNotationToString(bigEstimate.toString()));
4979
4987
  transactionsRef.push(nearWithdrawTransaction(_minAmountOut));
4980
4988
  }
4981
- onSwap(transactionsRef);
4982
- case 8:
4989
+ _context3.next = 9;
4990
+ return onSwap(transactionsRef);
4991
+ case 9:
4992
+ setTimeout(function () {
4993
+ var tokensToUpdate = [];
4994
+ if (tokenIn) tokensToUpdate.push(tokenIn.id);
4995
+ if (tokenOut) tokensToUpdate.push(tokenOut.id);
4996
+ if (tokensToUpdate.length > 0 && AccountId) updateTokenBalance(tokensToUpdate, AccountId);
4997
+ }, 3000);
4998
+ case 10:
4983
4999
  case "end":
4984
5000
  return _context3.stop();
4985
5001
  }
@@ -5041,6 +5057,9 @@ var useSwap = function useSwap(params) {
5041
5057
  return {
5042
5058
  amountOut: amountOut,
5043
5059
  minAmountOut: minAmountOut,
5060
+ balances: balances,
5061
+ tokenInBalance: tokenInBalance,
5062
+ tokenOutBalance: tokenOutBalance,
5044
5063
  fee: fee,
5045
5064
  rate: rate,
5046
5065
  estimates: estimates,
@@ -5068,30 +5087,108 @@ var TokenPriceContextProvider = function TokenPriceContextProvider(_ref7) {
5068
5087
  value: tokenPriceList
5069
5088
  }, children);
5070
5089
  };
5071
- var useTokenBalnces = function useTokenBalnces(tokens, AccountId) {
5090
+ var useTokenBalances = function useTokenBalances(tokens, AccountId) {
5072
5091
  var _useState16 = React.useState({}),
5073
5092
  balances = _useState16[0],
5074
5093
  setBalances = _useState16[1];
5094
+ var updateTokenBalance = React.useCallback( /*#__PURE__*/function () {
5095
+ var _ref8 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(tokenIds, AccountId) {
5096
+ var updatedTokensBalancesMap;
5097
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
5098
+ while (1) {
5099
+ switch (_context5.prev = _context5.next) {
5100
+ case 0:
5101
+ updatedTokensBalancesMap = {};
5102
+ _context5.next = 3;
5103
+ return Promise.all(tokenIds.map( /*#__PURE__*/function () {
5104
+ var _ref9 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(tokenId) {
5105
+ var _tokens$find;
5106
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
5107
+ while (1) {
5108
+ switch (_context4.prev = _context4.next) {
5109
+ case 0:
5110
+ _context4.t0 = toReadableNumber;
5111
+ _context4.t1 = ((_tokens$find = tokens.find(function (t) {
5112
+ return t.id === tokenId;
5113
+ })) == null ? void 0 : _tokens$find.decimals) || 0;
5114
+ _context4.next = 4;
5115
+ return ftGetBalance(tokenId === exports.WRAP_NEAR_CONTRACT_ID ? 'NEAR' : tokenId, AccountId);
5116
+ case 4:
5117
+ _context4.t2 = _context4.sent;
5118
+ updatedTokensBalancesMap[tokenId] = (0, _context4.t0)(_context4.t1, _context4.t2);
5119
+ case 6:
5120
+ case "end":
5121
+ return _context4.stop();
5122
+ }
5123
+ }
5124
+ }, _callee4);
5125
+ }));
5126
+ return function (_x3) {
5127
+ return _ref9.apply(this, arguments);
5128
+ };
5129
+ }()));
5130
+ case 3:
5131
+ setBalances(function (prevState) {
5132
+ return _extends({}, prevState, updatedTokensBalancesMap);
5133
+ });
5134
+ case 4:
5135
+ case "end":
5136
+ return _context5.stop();
5137
+ }
5138
+ }
5139
+ }, _callee5);
5140
+ }));
5141
+ return function (_x, _x2) {
5142
+ return _ref8.apply(this, arguments);
5143
+ };
5144
+ }(), [tokens]);
5075
5145
  React.useEffect(function () {
5076
- var validTokens = tokens.filter(function (t) {
5077
- return !!(t != null && t.id);
5078
- });
5079
- var ids = validTokens.map(function (token) {
5080
- return token.id;
5081
- });
5082
- Promise.all(ids.map(function (id) {
5083
- return ftGetBalance(id === exports.WRAP_NEAR_CONTRACT_ID ? 'NEAR' : id, AccountId);
5084
- })).then(function (balances) {
5085
- var balancesMap = validTokens.reduce(function (acc, token, index) {
5086
- var _extends2;
5087
- return _extends({}, acc, (_extends2 = {}, _extends2[token.id] = toReadableNumber(token.decimals, balances[index]), _extends2));
5088
- }, {});
5089
- setBalances(balancesMap);
5090
- });
5091
- }, [tokens.map(function (t) {
5146
+ // Initializes token balances
5147
+ // Called in 1 minute intervals
5148
+ var initTokenBalances = /*#__PURE__*/function () {
5149
+ var _ref10 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
5150
+ var validTokens, ids;
5151
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
5152
+ while (1) {
5153
+ switch (_context6.prev = _context6.next) {
5154
+ case 0:
5155
+ if (AccountId) {
5156
+ _context6.next = 3;
5157
+ break;
5158
+ }
5159
+ setBalances({});
5160
+ return _context6.abrupt("return");
5161
+ case 3:
5162
+ validTokens = tokens.filter(function (t) {
5163
+ return !!(t != null && t.id);
5164
+ });
5165
+ ids = validTokens.map(function (token) {
5166
+ return token.id;
5167
+ });
5168
+ updateTokenBalance(ids, AccountId);
5169
+ case 6:
5170
+ case "end":
5171
+ return _context6.stop();
5172
+ }
5173
+ }
5174
+ }, _callee6);
5175
+ }));
5176
+ return function initTokenBalances() {
5177
+ return _ref10.apply(this, arguments);
5178
+ };
5179
+ }();
5180
+ initTokenBalances();
5181
+ var interval = setInterval(initTokenBalances, 60000);
5182
+ return function () {
5183
+ return clearInterval(interval);
5184
+ };
5185
+ }, [AccountId, tokens.map(function (t) {
5092
5186
  return t == null ? void 0 : t.id;
5093
5187
  }).join('-')]);
5094
- return balances;
5188
+ return {
5189
+ balances: balances,
5190
+ updateTokenBalance: updateTokenBalance
5191
+ };
5095
5192
  };
5096
5193
 
5097
5194
  var getPriceImpact$1 = function getPriceImpact(value, tokenIn, tokenInAmount) {
@@ -6371,15 +6468,9 @@ var SwapWidget = function SwapWidget(props) {
6371
6468
  var _useState2 = React.useState(),
6372
6469
  tokenOut = _useState2[0],
6373
6470
  setTokenOut = _useState2[1];
6374
- var _useState3 = React.useState(''),
6375
- tokenInBalance = _useState3[0],
6376
- setTokenInBalance = _useState3[1];
6377
- var _useState4 = React.useState(''),
6378
- tokenOutBalance = _useState4[0],
6379
- setTokenOutBalance = _useState4[1];
6380
- var _useState5 = React.useState(false),
6381
- notOpen = _useState5[0],
6382
- setNotOpen = _useState5[1];
6471
+ var _useState3 = React.useState(false),
6472
+ notOpen = _useState3[0],
6473
+ setNotOpen = _useState3[1];
6383
6474
  React.useEffect(function () {
6384
6475
  if (!transactionState) return;
6385
6476
  if (transactionState && transactionState.state !== null) {
@@ -6387,18 +6478,18 @@ var SwapWidget = function SwapWidget(props) {
6387
6478
  }
6388
6479
  transactionState == null ? void 0 : transactionState.setState((transactionState == null ? void 0 : transactionState.state) || null);
6389
6480
  }, [transactionState]);
6390
- var _useState6 = React.useState('swap'),
6391
- widgetRoute = _useState6[0],
6392
- setWidgetRoute = _useState6[1];
6393
- var _useState7 = React.useState('1'),
6394
- amountIn = _useState7[0],
6395
- setAmountIn = _useState7[1];
6396
- var _useState8 = React.useState(false),
6397
- showSlip = _useState8[0],
6398
- setShowSlip = _useState8[1];
6399
- var _useState9 = React.useState(0.5),
6400
- slippageTolerance = _useState9[0],
6401
- setSlippageTolerance = _useState9[1];
6481
+ var _useState4 = React.useState('swap'),
6482
+ widgetRoute = _useState4[0],
6483
+ setWidgetRoute = _useState4[1];
6484
+ var _useState5 = React.useState('1'),
6485
+ amountIn = _useState5[0],
6486
+ setAmountIn = _useState5[1];
6487
+ var _useState6 = React.useState(false),
6488
+ showSlip = _useState6[0],
6489
+ setShowSlip = _useState6[1];
6490
+ var _useState7 = React.useState(0.5),
6491
+ slippageTolerance = _useState7[0],
6492
+ setSlippageTolerance = _useState7[1];
6402
6493
  var _useTokensIndexer = useTokensIndexer({
6403
6494
  defaultTokenList: defaultTokenList,
6404
6495
  AccountId: AccountId
@@ -6409,10 +6500,9 @@ var SwapWidget = function SwapWidget(props) {
6409
6500
  useAllTokens({
6410
6501
  reload: true
6411
6502
  });
6412
- var balances = useTokenBalnces(tokens, AccountId);
6413
- var _useState10 = React.useState(false),
6414
- refreshTrigger = _useState10[0],
6415
- setRreshTrigger = _useState10[1];
6503
+ var _useState8 = React.useState(false),
6504
+ refreshTrigger = _useState8[0],
6505
+ setRreshTrigger = _useState8[1];
6416
6506
  var tokenPriceList = useTokenPriceList();
6417
6507
  var _useRefPools = useRefPools(refreshTrigger),
6418
6508
  allPools = _useRefPools.allPools,
@@ -6454,28 +6544,6 @@ var SwapWidget = function SwapWidget(props) {
6454
6544
  handleSetTokenOut(exports.REF_META_DATA);
6455
6545
  }
6456
6546
  }, [tokens, tokenLoading]);
6457
- React.useEffect(function () {
6458
- if (!tokenIn) return;
6459
- var wrapedId = tokenIn.id === exports.WRAP_NEAR_CONTRACT_ID ? 'NEAR' : tokenIn.id;
6460
- if (balances[wrapedId]) {
6461
- setTokenInBalance(balances[wrapedId]);
6462
- return;
6463
- }
6464
- ftGetBalance(wrapedId, AccountId).then(function (available) {
6465
- setTokenInBalance(toReadableNumber(tokenIn.decimals, available));
6466
- });
6467
- }, [tokenIn, AccountId, balances]);
6468
- React.useEffect(function () {
6469
- if (!tokenOut) return;
6470
- var wrapedId = tokenOut.id === exports.WRAP_NEAR_CONTRACT_ID ? 'NEAR' : tokenOut.id;
6471
- if (balances[wrapedId]) {
6472
- setTokenOutBalance(balances[wrapedId]);
6473
- return;
6474
- }
6475
- ftGetBalance(wrapedId, AccountId).then(function (available) {
6476
- setTokenOutBalance(toReadableNumber(tokenOut.decimals, available));
6477
- });
6478
- }, [tokenOut, AccountId, balances]);
6479
6547
  var _useSwap = useSwap({
6480
6548
  tokenIn: tokenIn,
6481
6549
  tokenOut: tokenOut,
@@ -6497,10 +6565,14 @@ var SwapWidget = function SwapWidget(props) {
6497
6565
  AccountId: AccountId,
6498
6566
  refreshTrigger: refreshTrigger,
6499
6567
  poolFetchingState: poolFetchingState,
6500
- referralId: referralId
6568
+ referralId: referralId,
6569
+ tokens: tokens
6501
6570
  }),
6502
6571
  amountOut = _useSwap.amountOut,
6503
6572
  minAmountOut = _useSwap.minAmountOut,
6573
+ balances = _useSwap.balances,
6574
+ tokenInBalance = _useSwap.tokenInBalance,
6575
+ tokenOutBalance = _useSwap.tokenOutBalance,
6504
6576
  rate = _useSwap.rate,
6505
6577
  fee = _useSwap.fee,
6506
6578
  estimates = _useSwap.estimates,