@depay/widgets 6.13.0 → 6.16.0

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/esm/index.js CHANGED
@@ -2021,13 +2021,14 @@ var WalletContext = /*#__PURE__*/React.createContext();
2021
2021
  var ChangableAmountProvider = (function (props) {
2022
2022
  var configurationsMissAmounts = function configurationsMissAmounts(configurations) {
2023
2023
  return !configurations.every(function (configuration) {
2024
- return typeof configuration.amount != 'undefined';
2024
+ return typeof configuration.amount != 'undefined' || typeof configuration.fromAmount != 'undefined';
2025
2025
  });
2026
2026
  };
2027
2027
 
2028
2028
  var _useContext = useContext(ConfigurationContext),
2029
- amountConfiguration = _useContext.amount,
2030
- recover = _useContext.recover;
2029
+ amountConfiguration = _useContext.amount;
2030
+ _useContext.toAmount;
2031
+ var recover = _useContext.recover;
2031
2032
 
2032
2033
  var _useState = useState(recover == undefined ? configurationsMissAmounts(props.accept) : false),
2033
2034
  _useState2 = _slicedToArray(_useState, 2),
@@ -2316,7 +2317,7 @@ var PaymentProvider = (function (props) {
2316
2317
  var _useContext9 = useContext(PaymentTrackingContext),
2317
2318
  release = _useContext9.release,
2318
2319
  tracking = _useContext9.tracking,
2319
- initializeTracking = _useContext9.initializeTracking;
2320
+ initializePaymentTracking = _useContext9.initializeTracking;
2320
2321
 
2321
2322
  var _useContext10 = useContext(TransactionTrackingContext),
2322
2323
  foundTransaction = _useContext10.foundTransaction,
@@ -2396,10 +2397,7 @@ var PaymentProvider = (function (props) {
2396
2397
  confirmed: paymentConfirmed,
2397
2398
  failed: paymentFailed
2398
2399
  })).then(function (sentTransaction) {
2399
- if (tracking) {
2400
- initializeTracking(sentTransaction, currentBlock, payment.route);
2401
- }
2402
-
2400
+ initializePaymentTracking(sentTransaction, currentBlock, payment.route);
2403
2401
  setTransaction(sentTransaction);
2404
2402
  })["catch"](function (error) {
2405
2403
  console.log('error', error);
@@ -2768,15 +2766,20 @@ var PaymentValueProvider = (function (props) {
2768
2766
  paymentValue = _useState2[0],
2769
2767
  setPaymentValue = _useState2[1];
2770
2768
 
2769
+ var _useState3 = useState(),
2770
+ _useState4 = _slicedToArray(_useState3, 2),
2771
+ paymentValueLoss = _useState4[0],
2772
+ setPaymentValueLoss = _useState4[1];
2773
+
2771
2774
  var _useContext5 = useContext(ConfigurationContext),
2772
2775
  currency = _useContext5.currency;
2773
2776
 
2774
- var _useState3 = useState(0),
2775
- _useState4 = _slicedToArray(_useState3, 2),
2776
- reloadCount = _useState4[0],
2777
- setReloadCount = _useState4[1];
2777
+ var _useState5 = useState(0),
2778
+ _useState6 = _slicedToArray(_useState5, 2),
2779
+ reloadCount = _useState6[0],
2780
+ setReloadCount = _useState6[1];
2778
2781
 
2779
- var getToTokenLocalValue = function getToTokenLocalValue(_ref) {
2782
+ var updatePaymentValue = function updatePaymentValue(_ref) {
2780
2783
  var updatable = _ref.updatable,
2781
2784
  payment = _ref.payment;
2782
2785
 
@@ -2791,29 +2794,51 @@ var PaymentValueProvider = (function (props) {
2791
2794
  amountIn: payment.route.toAmount,
2792
2795
  fromAddress: account,
2793
2796
  toAddress: account
2794
- }), new Token({
2797
+ }), !payment.route.directTransfer ? route({
2798
+ blockchain: payment.route.blockchain,
2799
+ tokenIn: payment.route.toToken.address,
2800
+ tokenOut: payment.route.fromToken.address,
2801
+ amountIn: payment.route.toAmount,
2802
+ fromAddress: account,
2803
+ toAddress: account
2804
+ }) : Promise.resolve([]), new Token({
2795
2805
  blockchain: payment.route.blockchain,
2796
2806
  address: CONSTANTS[payment.route.blockchain].USD
2797
2807
  }).decimals()]).then(function (_ref2) {
2798
- var _ref3 = _slicedToArray(_ref2, 2),
2799
- USDExchangeRoutes = _ref3[0],
2800
- USDDecimals = _ref3[1];
2808
+ var _ref3 = _slicedToArray(_ref2, 3),
2809
+ toTokenUSDExchangeRoutes = _ref3[0],
2810
+ reverseRoutes = _ref3[1],
2811
+ USDDecimals = _ref3[2];
2801
2812
 
2802
- var USDRoute = USDExchangeRoutes[0];
2803
- var USDAmount;
2813
+ var toTokenUSDRoute = toTokenUSDExchangeRoutes[0];
2814
+ var reverseRoute = reverseRoutes[0];
2815
+
2816
+ if (reverseRoute) {
2817
+ var reverseAmountOutBN = ethers.BigNumber.from(reverseRoute.amountOut);
2818
+ var paymentAmountInBN = ethers.BigNumber.from(payment.route.fromAmount);
2819
+ var divPercent = 100 - reverseAmountOutBN.mul(ethers.BigNumber.from('100')).div(paymentAmountInBN).abs().toString();
2820
+
2821
+ if (divPercent >= 5) {
2822
+ setPaymentValueLoss(divPercent);
2823
+ } else {
2824
+ setPaymentValueLoss(null);
2825
+ }
2826
+ }
2827
+
2828
+ var toTokenUSDAmount;
2804
2829
 
2805
2830
  if (payment.route.toToken.address.toLowerCase() == CONSTANTS[payment.route.blockchain].USD.toLowerCase()) {
2806
- USDAmount = payment.route.toAmount.toString();
2807
- } else if (USDRoute == undefined) {
2831
+ toTokenUSDAmount = payment.route.toAmount.toString();
2832
+ } else if (toTokenUSDRoute == undefined) {
2808
2833
  setPaymentValue('');
2809
2834
  return;
2810
2835
  } else {
2811
- USDAmount = USDRoute.amountOut.toString();
2836
+ toTokenUSDAmount = toTokenUSDRoute.amountOut.toString();
2812
2837
  }
2813
2838
 
2814
- var USDValue = ethers.utils.formatUnits(USDAmount, USDDecimals);
2839
+ var toTokenUSDValue = ethers.utils.formatUnits(toTokenUSDAmount, USDDecimals);
2815
2840
  Currency.fromUSD({
2816
- amount: USDValue,
2841
+ amount: toTokenUSDValue,
2817
2842
  code: currency,
2818
2843
  apiKey: apiKey
2819
2844
  }).then(setPaymentValue)["catch"](setError);
@@ -2822,7 +2847,7 @@ var PaymentValueProvider = (function (props) {
2822
2847
 
2823
2848
  useEffect(function () {
2824
2849
  if (account && payment) {
2825
- getToTokenLocalValue({
2850
+ updatePaymentValue({
2826
2851
  updatable: updatable,
2827
2852
  payment: payment
2828
2853
  });
@@ -2831,7 +2856,7 @@ var PaymentValueProvider = (function (props) {
2831
2856
  useEffect(function () {
2832
2857
  var timeout = setTimeout(function () {
2833
2858
  setReloadCount(reloadCount + 1);
2834
- getToTokenLocalValue({
2859
+ updatePaymentValue({
2835
2860
  updatable: updatable
2836
2861
  });
2837
2862
  }, 15000);
@@ -2841,7 +2866,8 @@ var PaymentValueProvider = (function (props) {
2841
2866
  }, [reloadCount, updatable]);
2842
2867
  return /*#__PURE__*/React.createElement(PaymentValueContext.Provider, {
2843
2868
  value: {
2844
- paymentValue: paymentValue
2869
+ paymentValue: paymentValue,
2870
+ paymentValueLoss: paymentValueLoss
2845
2871
  }
2846
2872
  }, props.children);
2847
2873
  });
@@ -3275,7 +3301,8 @@ var Footer = (function () {
3275
3301
  approvalTransaction = _useContext4.approvalTransaction;
3276
3302
 
3277
3303
  var _useContext5 = useContext(PaymentValueContext),
3278
- paymentValue = _useContext5.paymentValue;
3304
+ paymentValue = _useContext5.paymentValue,
3305
+ paymentValueLoss = _useContext5.paymentValueLoss;
3279
3306
 
3280
3307
  var _useContext6 = useContext(NavigateStackContext);
3281
3308
  _useContext6.navigate;
@@ -3421,7 +3448,16 @@ var Footer = (function () {
3421
3448
  displayedAmount = "".concat(payment.symbol, " ").concat(payment.amount);
3422
3449
  }
3423
3450
 
3424
- if ((paymentState == 'initialized' || paymentState == 'approving') && payment.route) {
3451
+ if (paymentValueLoss) {
3452
+ return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
3453
+ className: "PaddingBottomXS"
3454
+ }, /*#__PURE__*/React.createElement("div", {
3455
+ className: "Alert"
3456
+ }, /*#__PURE__*/React.createElement("strong", null, "Payment token would lose ", paymentValueLoss, "% of it's value!"))), /*#__PURE__*/React.createElement("button", {
3457
+ className: "ButtonPrimary disabled",
3458
+ onClick: function onClick() {}
3459
+ }, "Pay ", displayedAmount));
3460
+ } else if ((paymentState == 'initialized' || paymentState == 'approving') && payment.route) {
3425
3461
  return /*#__PURE__*/React.createElement("button", {
3426
3462
  className: ["ButtonPrimary", payment.route.approvalRequired && !payment.route.directTransfer ? 'disabled' : ''].join(' '),
3427
3463
  onClick: function onClick() {
@@ -3719,11 +3755,11 @@ var PaymentTrackingProvider = (function (props) {
3719
3755
  paymentRoute = _useState6[0],
3720
3756
  setPaymentRoute = _useState6[1];
3721
3757
 
3722
- var _useState7 = useState(track && !!(track.endpoint || typeof track.method == 'function')),
3758
+ var _useState7 = useState(!!(track && (track.endpoint || typeof track.method == 'function'))),
3723
3759
  _useState8 = _slicedToArray(_useState7, 1),
3724
3760
  tracking = _useState8[0];
3725
3761
 
3726
- var _useState9 = useState(track && track.poll && !!(track.poll.endpoint || typeof track.poll.method == 'function')),
3762
+ var _useState9 = useState(!!(track && track.poll && (track.poll.endpoint || typeof track.poll.method == 'function'))),
3727
3763
  _useState10 = _slicedToArray(_useState9, 1),
3728
3764
  polling = _useState10[0];
3729
3765
 
@@ -3791,7 +3827,6 @@ var PaymentTrackingProvider = (function (props) {
3791
3827
 
3792
3828
  var retryStartTracking = function retryStartTracking(transaction, afterBlock, paymentRoute, attempt) {
3793
3829
  attempt = parseInt(attempt || 1, 10);
3794
- console.log('RETRYING PAYMENT TRACKING ATTEMPT ', attempt);
3795
3830
 
3796
3831
  if (attempt < 3) {
3797
3832
  setTimeout(function () {
@@ -3834,9 +3869,7 @@ var PaymentTrackingProvider = (function (props) {
3834
3869
  after_block: afterBlock,
3835
3870
  to_token: paymentRoute.toToken.address
3836
3871
  }).then(function (response) {
3837
- if (response.status == 200) {
3838
- console.log('PAYMENT TRACKING INITIALIZED');
3839
- } else {
3872
+ if (response.status != 200) {
3840
3873
  retryStartTracking(transaction, afterBlock, paymentRoute, attempt);
3841
3874
  }
3842
3875
  })["catch"](function (error) {
@@ -3900,7 +3933,56 @@ var PaymentTrackingProvider = (function (props) {
3900
3933
  };
3901
3934
  }, [polling, transaction, afterBlock, paymentRoute]);
3902
3935
 
3936
+ var storePayment = function storePayment(transaction, afterBlock, paymentRoute, attempt) {
3937
+ if (attempt > 3) {
3938
+ return;
3939
+ }
3940
+
3941
+ fetch('https://api.depay.fi/v2/payments', {
3942
+ method: 'POST',
3943
+ headers: {
3944
+ 'X-Api-Key': apiKey,
3945
+ 'Content-Type': 'application/json'
3946
+ },
3947
+ body: JSON.stringify({
3948
+ blockchain: transaction.blockchain,
3949
+ transaction: transaction.id,
3950
+ sender: transaction.from.toLowerCase(),
3951
+ nonce: transaction.nonce,
3952
+ receiver: paymentRoute.toAddress,
3953
+ token: paymentRoute.toToken.address,
3954
+ amount: paymentRoute.fee ? ethers.utils.formatUnits(paymentRoute.transaction.params.amounts[1], paymentRoute.toDecimals) : ethers.utils.formatUnits(paymentRoute.toAmount, paymentRoute.toDecimals),
3955
+ confirmations: 1,
3956
+ after_block: afterBlock,
3957
+ uuid: transaction.id,
3958
+ payload: {
3959
+ sender_id: transaction.from.toLowerCase(),
3960
+ sender_token_id: paymentRoute.fromToken.address,
3961
+ sender_amount: ethers.utils.formatUnits(paymentRoute.fromAmount, paymentRoute.fromDecimals)
3962
+ },
3963
+ fee_amount: paymentRoute.fee ? ethers.utils.formatUnits(paymentRoute.transaction.params.amounts[4], paymentRoute.toDecimals) : null,
3964
+ fee_receiver: paymentRoute.fee ? paymentRoute.transaction.params.addresses[1] : null
3965
+ })
3966
+ }).then(function (response) {
3967
+ if (response.status == 200 || response.status == 201) ; else {
3968
+ setTimeout(function () {
3969
+ storePayment(transaction, afterBlock, paymentRoute, attempt + 1);
3970
+ }, 3000);
3971
+ }
3972
+ })["catch"](function (error) {
3973
+ setTimeout(function () {
3974
+ storePayment(transaction, afterBlock, paymentRoute, attempt + 1);
3975
+ }, 3000);
3976
+ });
3977
+ };
3978
+
3903
3979
  var initializeTracking = function initializeTracking(transaction, afterBlock, paymentRoute) {
3980
+ storePayment(transaction, afterBlock, paymentRoute, 1);
3981
+
3982
+ if (tracking == false) {
3983
+ return;
3984
+ }
3985
+
3904
3986
  setTransaction(transaction);
3905
3987
  setAfterBlock(afterBlock);
3906
3988
  setPaymentRoute(paymentRoute);
@@ -4068,8 +4150,11 @@ var TransactionTrackingProvider = (function (props) {
4068
4150
  });
4069
4151
 
4070
4152
  var WalletProvider = (function (props) {
4071
- var _useContext = useContext(ErrorContext);
4072
- _useContext.setError;
4153
+ var _useContext = useContext(ConfigurationContext),
4154
+ recover = _useContext.recover;
4155
+
4156
+ var _useContext2 = useContext(ErrorContext);
4157
+ _useContext2.setError;
4073
4158
 
4074
4159
  var _useState = useState(),
4075
4160
  _useState2 = _slicedToArray(_useState, 2),
@@ -4098,7 +4183,7 @@ var WalletProvider = (function (props) {
4098
4183
  }
4099
4184
  };
4100
4185
 
4101
- if (walletState == 'connected') {
4186
+ if (walletState == 'connected' || recover != undefined) {
4102
4187
  return /*#__PURE__*/React.createElement(WalletContext.Provider, {
4103
4188
  value: {
4104
4189
  account: account,
@@ -4630,10 +4715,14 @@ var preflight$1 = /*#__PURE__*/function () {
4630
4715
  throw 'You need to set a supported blockchain!';
4631
4716
  }
4632
4717
 
4633
- if (typeof configuration.token === 'undefined') {
4718
+ if (typeof configuration.token === 'undefined' && typeof configuration.fromToken === 'undefined' && typeof configuration.fromAmount === 'undefined' && typeof configuration.toToken === 'undefined') {
4634
4719
  throw 'You need to set the token you want to receive as payment!';
4635
4720
  }
4636
4721
 
4722
+ if (typeof configuration.token === 'undefined' && typeof configuration.fromToken !== 'undefined' && typeof configuration.fromAmount === 'undefined' && typeof configuration.toToken === 'undefined') {
4723
+ throw 'You need to set the fromToken, fromAmount and toToken!';
4724
+ }
4725
+
4637
4726
  if (typeof configuration.receiver === 'undefined') {
4638
4727
  throw 'You need to set the receiver address that you want to receive the payment!';
4639
4728
  }