@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.
@@ -5,7 +5,7 @@ import _, { sortBy } from 'lodash-es';
5
5
  import * as math from 'mathjs';
6
6
  import { divide as divide$1, evaluate, format, floor, bignumber, sum, round as round$1 } from 'mathjs';
7
7
  import Big from 'big.js';
8
- import React, { useState, useEffect, createContext, useContext, useMemo, useRef } from 'react';
8
+ import React, { useState, useEffect, useMemo, useCallback, createContext, useContext, useRef } from 'react';
9
9
  import { FiChevronDown } from '@react-icons/all-files/fi/FiChevronDown';
10
10
  import { FiChevronLeft } from '@react-icons/all-files/fi/FiChevronLeft';
11
11
  import { FiChevronUp } from '@react-icons/all-files/fi/FiChevronUp';
@@ -524,18 +524,6 @@ function _extends() {
524
524
  };
525
525
  return _extends.apply(this, arguments);
526
526
  }
527
- function _objectWithoutPropertiesLoose(source, excluded) {
528
- if (source == null) return {};
529
- var target = {};
530
- var sourceKeys = Object.keys(source);
531
- var key, i;
532
- for (i = 0; i < sourceKeys.length; i++) {
533
- key = sourceKeys[i];
534
- if (excluded.indexOf(key) >= 0) continue;
535
- target[key] = source[key];
536
- }
537
- return target;
538
- }
539
527
  function _unsupportedIterableToArray(o, minLen) {
540
528
  if (!o) return;
541
529
  if (typeof o === "string") return _arrayLikeToArray(o, minLen);
@@ -4688,24 +4676,36 @@ var getRefPools = /*#__PURE__*/function () {
4688
4676
  }();
4689
4677
  var fetchAllPools = /*#__PURE__*/function () {
4690
4678
  var _ref9 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(perPage) {
4691
- var totalPools, pages, pools;
4679
+ var _pools, totalPools, pages, pools;
4692
4680
  return _regeneratorRuntime().wrap(function _callee7$(_context7) {
4693
4681
  while (1) {
4694
4682
  switch (_context7.prev = _context7.next) {
4695
4683
  case 0:
4684
+ _context7.prev = 0;
4685
+ _context7.next = 3;
4686
+ return fetch(config.indexerUrl + "/fetchAllPools").then(function (res) {
4687
+ return res.json();
4688
+ });
4689
+ case 3:
4690
+ _pools = _context7.sent;
4691
+ return _context7.abrupt("return", _pools);
4692
+ case 7:
4693
+ _context7.prev = 7;
4694
+ _context7.t0 = _context7["catch"](0);
4695
+ case 9:
4696
4696
  if (perPage) {
4697
4697
  DEFAULT_PAGE_LIMIT = Math.min(perPage, 500);
4698
4698
  }
4699
- _context7.next = 3;
4699
+ _context7.next = 12;
4700
4700
  return getTotalPools();
4701
- case 3:
4701
+ case 12:
4702
4702
  totalPools = _context7.sent;
4703
4703
  pages = Math.ceil(totalPools / DEFAULT_PAGE_LIMIT);
4704
- _context7.next = 7;
4704
+ _context7.next = 16;
4705
4705
  return Promise.all([].concat(Array(pages)).fill(0).map(function (_, i) {
4706
4706
  return getRefPools(i + 1);
4707
4707
  }));
4708
- case 7:
4708
+ case 16:
4709
4709
  pools = _context7.sent.flat();
4710
4710
  return _context7.abrupt("return", {
4711
4711
  simplePools: pools.filter(function (p) {
@@ -4718,19 +4718,18 @@ var fetchAllPools = /*#__PURE__*/function () {
4718
4718
  return p.pool_kind && p.pool_kind === 'RATED_SWAP';
4719
4719
  })
4720
4720
  });
4721
- case 9:
4721
+ case 18:
4722
4722
  case "end":
4723
4723
  return _context7.stop();
4724
4724
  }
4725
4725
  }
4726
- }, _callee7);
4726
+ }, _callee7, null, [[0, 7]]);
4727
4727
  }));
4728
4728
  return function fetchAllPools(_x8) {
4729
4729
  return _ref9.apply(this, arguments);
4730
4730
  };
4731
4731
  }();
4732
4732
 
4733
- var _excluded = ["slippageTolerance", "refreshTrigger", "onSwap", "AccountId", "poolFetchingState", "referralId"];
4734
4733
  var ThemeContext = /*#__PURE__*/createContext(defaultTheme);
4735
4734
  var ThemeContextProvider = function ThemeContextProvider(_ref) {
4736
4735
  var customTheme = _ref.customTheme,
@@ -4909,7 +4908,7 @@ var useSwap = function useSwap(params) {
4909
4908
  AccountId = params.AccountId,
4910
4909
  poolFetchingState = params.poolFetchingState,
4911
4910
  referralId = params.referralId,
4912
- swapParams = _objectWithoutPropertiesLoose(params, _excluded);
4911
+ tokens = params.tokens;
4913
4912
  var tokenIn = params.tokenIn,
4914
4913
  tokenOut = params.tokenOut,
4915
4914
  amountIn = params.amountIn;
@@ -4931,6 +4930,15 @@ var useSwap = function useSwap(params) {
4931
4930
  var _useState14 = useState(false),
4932
4931
  forceEstimate = _useState14[0],
4933
4932
  setForceEstimate = _useState14[1];
4933
+ var _useTokenBalances = useTokenBalances(tokens, AccountId),
4934
+ balances = _useTokenBalances.balances,
4935
+ updateTokenBalance = _useTokenBalances.updateTokenBalance;
4936
+ var tokenInBalance = useMemo(function () {
4937
+ return balances[tokenIn == null ? void 0 : tokenIn.id];
4938
+ }, [tokenIn, balances]);
4939
+ var tokenOutBalance = useMemo(function () {
4940
+ return balances[tokenOut == null ? void 0 : tokenOut.id] || '';
4941
+ }, [tokenOut, balances]);
4934
4942
  var minAmountOut = amountOut ? percentLess(slippageTolerance, amountOut) : '';
4935
4943
  var fee = !params.tokenOut || !params.tokenIn ? 0 : getAvgFee(estimates, params.tokenOut.id, toNonDivisibleNumber(params.tokenIn.decimals, params.amountIn));
4936
4944
  var rate = calculateExchangeRate(ONLY_ZEROS.test(params.amountIn) ? '1' : params.amountIn, amountOut || '1');
@@ -4972,8 +4980,16 @@ var useSwap = function useSwap(params) {
4972
4980
  _minAmountOut = percentLess(slippageTolerance, scientificNotationToString(bigEstimate.toString()));
4973
4981
  transactionsRef.push(nearWithdrawTransaction(_minAmountOut));
4974
4982
  }
4975
- onSwap(transactionsRef);
4976
- case 8:
4983
+ _context3.next = 9;
4984
+ return onSwap(transactionsRef);
4985
+ case 9:
4986
+ setTimeout(function () {
4987
+ var tokensToUpdate = [];
4988
+ if (tokenIn) tokensToUpdate.push(tokenIn.id);
4989
+ if (tokenOut) tokensToUpdate.push(tokenOut.id);
4990
+ if (tokensToUpdate.length > 0 && AccountId) updateTokenBalance(tokensToUpdate, AccountId);
4991
+ }, 3000);
4992
+ case 10:
4977
4993
  case "end":
4978
4994
  return _context3.stop();
4979
4995
  }
@@ -5035,6 +5051,9 @@ var useSwap = function useSwap(params) {
5035
5051
  return {
5036
5052
  amountOut: amountOut,
5037
5053
  minAmountOut: minAmountOut,
5054
+ balances: balances,
5055
+ tokenInBalance: tokenInBalance,
5056
+ tokenOutBalance: tokenOutBalance,
5038
5057
  fee: fee,
5039
5058
  rate: rate,
5040
5059
  estimates: estimates,
@@ -5062,30 +5081,108 @@ var TokenPriceContextProvider = function TokenPriceContextProvider(_ref7) {
5062
5081
  value: tokenPriceList
5063
5082
  }, children);
5064
5083
  };
5065
- var useTokenBalnces = function useTokenBalnces(tokens, AccountId) {
5084
+ var useTokenBalances = function useTokenBalances(tokens, AccountId) {
5066
5085
  var _useState16 = useState({}),
5067
5086
  balances = _useState16[0],
5068
5087
  setBalances = _useState16[1];
5088
+ var updateTokenBalance = useCallback( /*#__PURE__*/function () {
5089
+ var _ref8 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(tokenIds, AccountId) {
5090
+ var updatedTokensBalancesMap;
5091
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
5092
+ while (1) {
5093
+ switch (_context5.prev = _context5.next) {
5094
+ case 0:
5095
+ updatedTokensBalancesMap = {};
5096
+ _context5.next = 3;
5097
+ return Promise.all(tokenIds.map( /*#__PURE__*/function () {
5098
+ var _ref9 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(tokenId) {
5099
+ var _tokens$find;
5100
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
5101
+ while (1) {
5102
+ switch (_context4.prev = _context4.next) {
5103
+ case 0:
5104
+ _context4.t0 = toReadableNumber;
5105
+ _context4.t1 = ((_tokens$find = tokens.find(function (t) {
5106
+ return t.id === tokenId;
5107
+ })) == null ? void 0 : _tokens$find.decimals) || 0;
5108
+ _context4.next = 4;
5109
+ return ftGetBalance(tokenId === WRAP_NEAR_CONTRACT_ID ? 'NEAR' : tokenId, AccountId);
5110
+ case 4:
5111
+ _context4.t2 = _context4.sent;
5112
+ updatedTokensBalancesMap[tokenId] = (0, _context4.t0)(_context4.t1, _context4.t2);
5113
+ case 6:
5114
+ case "end":
5115
+ return _context4.stop();
5116
+ }
5117
+ }
5118
+ }, _callee4);
5119
+ }));
5120
+ return function (_x3) {
5121
+ return _ref9.apply(this, arguments);
5122
+ };
5123
+ }()));
5124
+ case 3:
5125
+ setBalances(function (prevState) {
5126
+ return _extends({}, prevState, updatedTokensBalancesMap);
5127
+ });
5128
+ case 4:
5129
+ case "end":
5130
+ return _context5.stop();
5131
+ }
5132
+ }
5133
+ }, _callee5);
5134
+ }));
5135
+ return function (_x, _x2) {
5136
+ return _ref8.apply(this, arguments);
5137
+ };
5138
+ }(), [tokens]);
5069
5139
  useEffect(function () {
5070
- var validTokens = tokens.filter(function (t) {
5071
- return !!(t != null && t.id);
5072
- });
5073
- var ids = validTokens.map(function (token) {
5074
- return token.id;
5075
- });
5076
- Promise.all(ids.map(function (id) {
5077
- return ftGetBalance(id === WRAP_NEAR_CONTRACT_ID ? 'NEAR' : id, AccountId);
5078
- })).then(function (balances) {
5079
- var balancesMap = validTokens.reduce(function (acc, token, index) {
5080
- var _extends2;
5081
- return _extends({}, acc, (_extends2 = {}, _extends2[token.id] = toReadableNumber(token.decimals, balances[index]), _extends2));
5082
- }, {});
5083
- setBalances(balancesMap);
5084
- });
5085
- }, [tokens.map(function (t) {
5140
+ // Initializes token balances
5141
+ // Called in 1 minute intervals
5142
+ var initTokenBalances = /*#__PURE__*/function () {
5143
+ var _ref10 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
5144
+ var validTokens, ids;
5145
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
5146
+ while (1) {
5147
+ switch (_context6.prev = _context6.next) {
5148
+ case 0:
5149
+ if (AccountId) {
5150
+ _context6.next = 3;
5151
+ break;
5152
+ }
5153
+ setBalances({});
5154
+ return _context6.abrupt("return");
5155
+ case 3:
5156
+ validTokens = tokens.filter(function (t) {
5157
+ return !!(t != null && t.id);
5158
+ });
5159
+ ids = validTokens.map(function (token) {
5160
+ return token.id;
5161
+ });
5162
+ updateTokenBalance(ids, AccountId);
5163
+ case 6:
5164
+ case "end":
5165
+ return _context6.stop();
5166
+ }
5167
+ }
5168
+ }, _callee6);
5169
+ }));
5170
+ return function initTokenBalances() {
5171
+ return _ref10.apply(this, arguments);
5172
+ };
5173
+ }();
5174
+ initTokenBalances();
5175
+ var interval = setInterval(initTokenBalances, 60000);
5176
+ return function () {
5177
+ return clearInterval(interval);
5178
+ };
5179
+ }, [AccountId, tokens.map(function (t) {
5086
5180
  return t == null ? void 0 : t.id;
5087
5181
  }).join('-')]);
5088
- return balances;
5182
+ return {
5183
+ balances: balances,
5184
+ updateTokenBalance: updateTokenBalance
5185
+ };
5089
5186
  };
5090
5187
 
5091
5188
  function styleInject(css, ref) {
@@ -6395,15 +6492,9 @@ var SwapWidget = function SwapWidget(props) {
6395
6492
  var _useState2 = useState(),
6396
6493
  tokenOut = _useState2[0],
6397
6494
  setTokenOut = _useState2[1];
6398
- var _useState3 = useState(''),
6399
- tokenInBalance = _useState3[0],
6400
- setTokenInBalance = _useState3[1];
6401
- var _useState4 = useState(''),
6402
- tokenOutBalance = _useState4[0],
6403
- setTokenOutBalance = _useState4[1];
6404
- var _useState5 = useState(false),
6405
- notOpen = _useState5[0],
6406
- setNotOpen = _useState5[1];
6495
+ var _useState3 = useState(false),
6496
+ notOpen = _useState3[0],
6497
+ setNotOpen = _useState3[1];
6407
6498
  useEffect(function () {
6408
6499
  if (!transactionState) return;
6409
6500
  if (transactionState && transactionState.state !== null) {
@@ -6411,18 +6502,18 @@ var SwapWidget = function SwapWidget(props) {
6411
6502
  }
6412
6503
  transactionState == null ? void 0 : transactionState.setState((transactionState == null ? void 0 : transactionState.state) || null);
6413
6504
  }, [transactionState]);
6414
- var _useState6 = useState('swap'),
6415
- widgetRoute = _useState6[0],
6416
- setWidgetRoute = _useState6[1];
6417
- var _useState7 = useState('1'),
6418
- amountIn = _useState7[0],
6419
- setAmountIn = _useState7[1];
6420
- var _useState8 = useState(false),
6421
- showSlip = _useState8[0],
6422
- setShowSlip = _useState8[1];
6423
- var _useState9 = useState(0.5),
6424
- slippageTolerance = _useState9[0],
6425
- setSlippageTolerance = _useState9[1];
6505
+ var _useState4 = useState('swap'),
6506
+ widgetRoute = _useState4[0],
6507
+ setWidgetRoute = _useState4[1];
6508
+ var _useState5 = useState('1'),
6509
+ amountIn = _useState5[0],
6510
+ setAmountIn = _useState5[1];
6511
+ var _useState6 = useState(false),
6512
+ showSlip = _useState6[0],
6513
+ setShowSlip = _useState6[1];
6514
+ var _useState7 = useState(0.5),
6515
+ slippageTolerance = _useState7[0],
6516
+ setSlippageTolerance = _useState7[1];
6426
6517
  var _useTokensIndexer = useTokensIndexer({
6427
6518
  defaultTokenList: defaultTokenList,
6428
6519
  AccountId: AccountId
@@ -6433,10 +6524,9 @@ var SwapWidget = function SwapWidget(props) {
6433
6524
  useAllTokens({
6434
6525
  reload: true
6435
6526
  });
6436
- var balances = useTokenBalnces(tokens, AccountId);
6437
- var _useState10 = useState(false),
6438
- refreshTrigger = _useState10[0],
6439
- setRreshTrigger = _useState10[1];
6527
+ var _useState8 = useState(false),
6528
+ refreshTrigger = _useState8[0],
6529
+ setRreshTrigger = _useState8[1];
6440
6530
  var tokenPriceList = useTokenPriceList();
6441
6531
  var _useRefPools = useRefPools(refreshTrigger),
6442
6532
  allPools = _useRefPools.allPools,
@@ -6478,28 +6568,6 @@ var SwapWidget = function SwapWidget(props) {
6478
6568
  handleSetTokenOut(REF_META_DATA);
6479
6569
  }
6480
6570
  }, [tokens, tokenLoading]);
6481
- useEffect(function () {
6482
- if (!tokenIn) return;
6483
- var wrapedId = tokenIn.id === WRAP_NEAR_CONTRACT_ID ? 'NEAR' : tokenIn.id;
6484
- if (balances[wrapedId]) {
6485
- setTokenInBalance(balances[wrapedId]);
6486
- return;
6487
- }
6488
- ftGetBalance(wrapedId, AccountId).then(function (available) {
6489
- setTokenInBalance(toReadableNumber(tokenIn.decimals, available));
6490
- });
6491
- }, [tokenIn, AccountId, balances]);
6492
- useEffect(function () {
6493
- if (!tokenOut) return;
6494
- var wrapedId = tokenOut.id === WRAP_NEAR_CONTRACT_ID ? 'NEAR' : tokenOut.id;
6495
- if (balances[wrapedId]) {
6496
- setTokenOutBalance(balances[wrapedId]);
6497
- return;
6498
- }
6499
- ftGetBalance(wrapedId, AccountId).then(function (available) {
6500
- setTokenOutBalance(toReadableNumber(tokenOut.decimals, available));
6501
- });
6502
- }, [tokenOut, AccountId, balances]);
6503
6571
  var _useSwap = useSwap({
6504
6572
  tokenIn: tokenIn,
6505
6573
  tokenOut: tokenOut,
@@ -6521,10 +6589,14 @@ var SwapWidget = function SwapWidget(props) {
6521
6589
  AccountId: AccountId,
6522
6590
  refreshTrigger: refreshTrigger,
6523
6591
  poolFetchingState: poolFetchingState,
6524
- referralId: referralId
6592
+ referralId: referralId,
6593
+ tokens: tokens
6525
6594
  }),
6526
6595
  amountOut = _useSwap.amountOut,
6527
6596
  minAmountOut = _useSwap.minAmountOut,
6597
+ balances = _useSwap.balances,
6598
+ tokenInBalance = _useSwap.tokenInBalance,
6599
+ tokenOutBalance = _useSwap.tokenOutBalance,
6528
6600
  rate = _useSwap.rate,
6529
6601
  fee = _useSwap.fee,
6530
6602
  estimates = _useSwap.estimates,