@ref-finance/ref-sdk 1.3.8 → 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);
@@ -4742,7 +4730,6 @@ var fetchAllPools = /*#__PURE__*/function () {
4742
4730
  };
4743
4731
  }();
4744
4732
 
4745
- var _excluded = ["slippageTolerance", "refreshTrigger", "onSwap", "AccountId", "poolFetchingState", "referralId"];
4746
4733
  var ThemeContext = /*#__PURE__*/createContext(defaultTheme);
4747
4734
  var ThemeContextProvider = function ThemeContextProvider(_ref) {
4748
4735
  var customTheme = _ref.customTheme,
@@ -4921,7 +4908,7 @@ var useSwap = function useSwap(params) {
4921
4908
  AccountId = params.AccountId,
4922
4909
  poolFetchingState = params.poolFetchingState,
4923
4910
  referralId = params.referralId,
4924
- swapParams = _objectWithoutPropertiesLoose(params, _excluded);
4911
+ tokens = params.tokens;
4925
4912
  var tokenIn = params.tokenIn,
4926
4913
  tokenOut = params.tokenOut,
4927
4914
  amountIn = params.amountIn;
@@ -4943,6 +4930,15 @@ var useSwap = function useSwap(params) {
4943
4930
  var _useState14 = useState(false),
4944
4931
  forceEstimate = _useState14[0],
4945
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]);
4946
4942
  var minAmountOut = amountOut ? percentLess(slippageTolerance, amountOut) : '';
4947
4943
  var fee = !params.tokenOut || !params.tokenIn ? 0 : getAvgFee(estimates, params.tokenOut.id, toNonDivisibleNumber(params.tokenIn.decimals, params.amountIn));
4948
4944
  var rate = calculateExchangeRate(ONLY_ZEROS.test(params.amountIn) ? '1' : params.amountIn, amountOut || '1');
@@ -4984,8 +4980,16 @@ var useSwap = function useSwap(params) {
4984
4980
  _minAmountOut = percentLess(slippageTolerance, scientificNotationToString(bigEstimate.toString()));
4985
4981
  transactionsRef.push(nearWithdrawTransaction(_minAmountOut));
4986
4982
  }
4987
- onSwap(transactionsRef);
4988
- 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:
4989
4993
  case "end":
4990
4994
  return _context3.stop();
4991
4995
  }
@@ -5047,6 +5051,9 @@ var useSwap = function useSwap(params) {
5047
5051
  return {
5048
5052
  amountOut: amountOut,
5049
5053
  minAmountOut: minAmountOut,
5054
+ balances: balances,
5055
+ tokenInBalance: tokenInBalance,
5056
+ tokenOutBalance: tokenOutBalance,
5050
5057
  fee: fee,
5051
5058
  rate: rate,
5052
5059
  estimates: estimates,
@@ -5074,30 +5081,108 @@ var TokenPriceContextProvider = function TokenPriceContextProvider(_ref7) {
5074
5081
  value: tokenPriceList
5075
5082
  }, children);
5076
5083
  };
5077
- var useTokenBalnces = function useTokenBalnces(tokens, AccountId) {
5084
+ var useTokenBalances = function useTokenBalances(tokens, AccountId) {
5078
5085
  var _useState16 = useState({}),
5079
5086
  balances = _useState16[0],
5080
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]);
5081
5139
  useEffect(function () {
5082
- var validTokens = tokens.filter(function (t) {
5083
- return !!(t != null && t.id);
5084
- });
5085
- var ids = validTokens.map(function (token) {
5086
- return token.id;
5087
- });
5088
- Promise.all(ids.map(function (id) {
5089
- return ftGetBalance(id === WRAP_NEAR_CONTRACT_ID ? 'NEAR' : id, AccountId);
5090
- })).then(function (balances) {
5091
- var balancesMap = validTokens.reduce(function (acc, token, index) {
5092
- var _extends2;
5093
- return _extends({}, acc, (_extends2 = {}, _extends2[token.id] = toReadableNumber(token.decimals, balances[index]), _extends2));
5094
- }, {});
5095
- setBalances(balancesMap);
5096
- });
5097
- }, [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) {
5098
5180
  return t == null ? void 0 : t.id;
5099
5181
  }).join('-')]);
5100
- return balances;
5182
+ return {
5183
+ balances: balances,
5184
+ updateTokenBalance: updateTokenBalance
5185
+ };
5101
5186
  };
5102
5187
 
5103
5188
  function styleInject(css, ref) {
@@ -6407,15 +6492,9 @@ var SwapWidget = function SwapWidget(props) {
6407
6492
  var _useState2 = useState(),
6408
6493
  tokenOut = _useState2[0],
6409
6494
  setTokenOut = _useState2[1];
6410
- var _useState3 = useState(''),
6411
- tokenInBalance = _useState3[0],
6412
- setTokenInBalance = _useState3[1];
6413
- var _useState4 = useState(''),
6414
- tokenOutBalance = _useState4[0],
6415
- setTokenOutBalance = _useState4[1];
6416
- var _useState5 = useState(false),
6417
- notOpen = _useState5[0],
6418
- setNotOpen = _useState5[1];
6495
+ var _useState3 = useState(false),
6496
+ notOpen = _useState3[0],
6497
+ setNotOpen = _useState3[1];
6419
6498
  useEffect(function () {
6420
6499
  if (!transactionState) return;
6421
6500
  if (transactionState && transactionState.state !== null) {
@@ -6423,18 +6502,18 @@ var SwapWidget = function SwapWidget(props) {
6423
6502
  }
6424
6503
  transactionState == null ? void 0 : transactionState.setState((transactionState == null ? void 0 : transactionState.state) || null);
6425
6504
  }, [transactionState]);
6426
- var _useState6 = useState('swap'),
6427
- widgetRoute = _useState6[0],
6428
- setWidgetRoute = _useState6[1];
6429
- var _useState7 = useState('1'),
6430
- amountIn = _useState7[0],
6431
- setAmountIn = _useState7[1];
6432
- var _useState8 = useState(false),
6433
- showSlip = _useState8[0],
6434
- setShowSlip = _useState8[1];
6435
- var _useState9 = useState(0.5),
6436
- slippageTolerance = _useState9[0],
6437
- 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];
6438
6517
  var _useTokensIndexer = useTokensIndexer({
6439
6518
  defaultTokenList: defaultTokenList,
6440
6519
  AccountId: AccountId
@@ -6445,10 +6524,9 @@ var SwapWidget = function SwapWidget(props) {
6445
6524
  useAllTokens({
6446
6525
  reload: true
6447
6526
  });
6448
- var balances = useTokenBalnces(tokens, AccountId);
6449
- var _useState10 = useState(false),
6450
- refreshTrigger = _useState10[0],
6451
- setRreshTrigger = _useState10[1];
6527
+ var _useState8 = useState(false),
6528
+ refreshTrigger = _useState8[0],
6529
+ setRreshTrigger = _useState8[1];
6452
6530
  var tokenPriceList = useTokenPriceList();
6453
6531
  var _useRefPools = useRefPools(refreshTrigger),
6454
6532
  allPools = _useRefPools.allPools,
@@ -6490,28 +6568,6 @@ var SwapWidget = function SwapWidget(props) {
6490
6568
  handleSetTokenOut(REF_META_DATA);
6491
6569
  }
6492
6570
  }, [tokens, tokenLoading]);
6493
- useEffect(function () {
6494
- if (!tokenIn) return;
6495
- var wrapedId = tokenIn.id === WRAP_NEAR_CONTRACT_ID ? 'NEAR' : tokenIn.id;
6496
- if (balances[wrapedId]) {
6497
- setTokenInBalance(balances[wrapedId]);
6498
- return;
6499
- }
6500
- ftGetBalance(wrapedId, AccountId).then(function (available) {
6501
- setTokenInBalance(toReadableNumber(tokenIn.decimals, available));
6502
- });
6503
- }, [tokenIn, AccountId, balances]);
6504
- useEffect(function () {
6505
- if (!tokenOut) return;
6506
- var wrapedId = tokenOut.id === WRAP_NEAR_CONTRACT_ID ? 'NEAR' : tokenOut.id;
6507
- if (balances[wrapedId]) {
6508
- setTokenOutBalance(balances[wrapedId]);
6509
- return;
6510
- }
6511
- ftGetBalance(wrapedId, AccountId).then(function (available) {
6512
- setTokenOutBalance(toReadableNumber(tokenOut.decimals, available));
6513
- });
6514
- }, [tokenOut, AccountId, balances]);
6515
6571
  var _useSwap = useSwap({
6516
6572
  tokenIn: tokenIn,
6517
6573
  tokenOut: tokenOut,
@@ -6533,10 +6589,14 @@ var SwapWidget = function SwapWidget(props) {
6533
6589
  AccountId: AccountId,
6534
6590
  refreshTrigger: refreshTrigger,
6535
6591
  poolFetchingState: poolFetchingState,
6536
- referralId: referralId
6592
+ referralId: referralId,
6593
+ tokens: tokens
6537
6594
  }),
6538
6595
  amountOut = _useSwap.amountOut,
6539
6596
  minAmountOut = _useSwap.minAmountOut,
6597
+ balances = _useSwap.balances,
6598
+ tokenInBalance = _useSwap.tokenInBalance,
6599
+ tokenOutBalance = _useSwap.tokenOutBalance,
6540
6600
  rate = _useSwap.rate,
6541
6601
  fee = _useSwap.fee,
6542
6602
  estimates = _useSwap.estimates,