@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/umd/index.js CHANGED
@@ -2016,13 +2016,14 @@
2016
2016
  var ChangableAmountProvider = (function (props) {
2017
2017
  var configurationsMissAmounts = function configurationsMissAmounts(configurations) {
2018
2018
  return !configurations.every(function (configuration) {
2019
- return typeof configuration.amount != 'undefined';
2019
+ return typeof configuration.amount != 'undefined' || typeof configuration.fromAmount != 'undefined';
2020
2020
  });
2021
2021
  };
2022
2022
 
2023
2023
  var _useContext = React.useContext(ConfigurationContext),
2024
- amountConfiguration = _useContext.amount,
2025
- recover = _useContext.recover;
2024
+ amountConfiguration = _useContext.amount;
2025
+ _useContext.toAmount;
2026
+ var recover = _useContext.recover;
2026
2027
 
2027
2028
  var _useState = React.useState(recover == undefined ? configurationsMissAmounts(props.accept) : false),
2028
2029
  _useState2 = _slicedToArray(_useState, 2),
@@ -2311,7 +2312,7 @@
2311
2312
  var _useContext9 = React.useContext(PaymentTrackingContext),
2312
2313
  release = _useContext9.release,
2313
2314
  tracking = _useContext9.tracking,
2314
- initializeTracking = _useContext9.initializeTracking;
2315
+ initializePaymentTracking = _useContext9.initializeTracking;
2315
2316
 
2316
2317
  var _useContext10 = React.useContext(TransactionTrackingContext),
2317
2318
  foundTransaction = _useContext10.foundTransaction,
@@ -2391,10 +2392,7 @@
2391
2392
  confirmed: paymentConfirmed,
2392
2393
  failed: paymentFailed
2393
2394
  })).then(function (sentTransaction) {
2394
- if (tracking) {
2395
- initializeTracking(sentTransaction, currentBlock, payment.route);
2396
- }
2397
-
2395
+ initializePaymentTracking(sentTransaction, currentBlock, payment.route);
2398
2396
  setTransaction(sentTransaction);
2399
2397
  })["catch"](function (error) {
2400
2398
  console.log('error', error);
@@ -2763,15 +2761,20 @@
2763
2761
  paymentValue = _useState2[0],
2764
2762
  setPaymentValue = _useState2[1];
2765
2763
 
2764
+ var _useState3 = React.useState(),
2765
+ _useState4 = _slicedToArray(_useState3, 2),
2766
+ paymentValueLoss = _useState4[0],
2767
+ setPaymentValueLoss = _useState4[1];
2768
+
2766
2769
  var _useContext5 = React.useContext(ConfigurationContext),
2767
2770
  currency = _useContext5.currency;
2768
2771
 
2769
- var _useState3 = React.useState(0),
2770
- _useState4 = _slicedToArray(_useState3, 2),
2771
- reloadCount = _useState4[0],
2772
- setReloadCount = _useState4[1];
2772
+ var _useState5 = React.useState(0),
2773
+ _useState6 = _slicedToArray(_useState5, 2),
2774
+ reloadCount = _useState6[0],
2775
+ setReloadCount = _useState6[1];
2773
2776
 
2774
- var getToTokenLocalValue = function getToTokenLocalValue(_ref) {
2777
+ var updatePaymentValue = function updatePaymentValue(_ref) {
2775
2778
  var updatable = _ref.updatable,
2776
2779
  payment = _ref.payment;
2777
2780
 
@@ -2786,29 +2789,51 @@
2786
2789
  amountIn: payment.route.toAmount,
2787
2790
  fromAddress: account,
2788
2791
  toAddress: account
2789
- }), new web3Tokens.Token({
2792
+ }), !payment.route.directTransfer ? web3Exchanges.route({
2793
+ blockchain: payment.route.blockchain,
2794
+ tokenIn: payment.route.toToken.address,
2795
+ tokenOut: payment.route.fromToken.address,
2796
+ amountIn: payment.route.toAmount,
2797
+ fromAddress: account,
2798
+ toAddress: account
2799
+ }) : Promise.resolve([]), new web3Tokens.Token({
2790
2800
  blockchain: payment.route.blockchain,
2791
2801
  address: web3Constants.CONSTANTS[payment.route.blockchain].USD
2792
2802
  }).decimals()]).then(function (_ref2) {
2793
- var _ref3 = _slicedToArray(_ref2, 2),
2794
- USDExchangeRoutes = _ref3[0],
2795
- USDDecimals = _ref3[1];
2803
+ var _ref3 = _slicedToArray(_ref2, 3),
2804
+ toTokenUSDExchangeRoutes = _ref3[0],
2805
+ reverseRoutes = _ref3[1],
2806
+ USDDecimals = _ref3[2];
2796
2807
 
2797
- var USDRoute = USDExchangeRoutes[0];
2798
- var USDAmount;
2808
+ var toTokenUSDRoute = toTokenUSDExchangeRoutes[0];
2809
+ var reverseRoute = reverseRoutes[0];
2810
+
2811
+ if (reverseRoute) {
2812
+ var reverseAmountOutBN = ethers.ethers.BigNumber.from(reverseRoute.amountOut);
2813
+ var paymentAmountInBN = ethers.ethers.BigNumber.from(payment.route.fromAmount);
2814
+ var divPercent = 100 - reverseAmountOutBN.mul(ethers.ethers.BigNumber.from('100')).div(paymentAmountInBN).abs().toString();
2815
+
2816
+ if (divPercent >= 5) {
2817
+ setPaymentValueLoss(divPercent);
2818
+ } else {
2819
+ setPaymentValueLoss(null);
2820
+ }
2821
+ }
2822
+
2823
+ var toTokenUSDAmount;
2799
2824
 
2800
2825
  if (payment.route.toToken.address.toLowerCase() == web3Constants.CONSTANTS[payment.route.blockchain].USD.toLowerCase()) {
2801
- USDAmount = payment.route.toAmount.toString();
2802
- } else if (USDRoute == undefined) {
2826
+ toTokenUSDAmount = payment.route.toAmount.toString();
2827
+ } else if (toTokenUSDRoute == undefined) {
2803
2828
  setPaymentValue('');
2804
2829
  return;
2805
2830
  } else {
2806
- USDAmount = USDRoute.amountOut.toString();
2831
+ toTokenUSDAmount = toTokenUSDRoute.amountOut.toString();
2807
2832
  }
2808
2833
 
2809
- var USDValue = ethers.ethers.utils.formatUnits(USDAmount, USDDecimals);
2834
+ var toTokenUSDValue = ethers.ethers.utils.formatUnits(toTokenUSDAmount, USDDecimals);
2810
2835
  localCurrency.Currency.fromUSD({
2811
- amount: USDValue,
2836
+ amount: toTokenUSDValue,
2812
2837
  code: currency,
2813
2838
  apiKey: apiKey
2814
2839
  }).then(setPaymentValue)["catch"](setError);
@@ -2817,7 +2842,7 @@
2817
2842
 
2818
2843
  React.useEffect(function () {
2819
2844
  if (account && payment) {
2820
- getToTokenLocalValue({
2845
+ updatePaymentValue({
2821
2846
  updatable: updatable,
2822
2847
  payment: payment
2823
2848
  });
@@ -2826,7 +2851,7 @@
2826
2851
  React.useEffect(function () {
2827
2852
  var timeout = setTimeout(function () {
2828
2853
  setReloadCount(reloadCount + 1);
2829
- getToTokenLocalValue({
2854
+ updatePaymentValue({
2830
2855
  updatable: updatable
2831
2856
  });
2832
2857
  }, 15000);
@@ -2836,7 +2861,8 @@
2836
2861
  }, [reloadCount, updatable]);
2837
2862
  return /*#__PURE__*/React__default["default"].createElement(PaymentValueContext.Provider, {
2838
2863
  value: {
2839
- paymentValue: paymentValue
2864
+ paymentValue: paymentValue,
2865
+ paymentValueLoss: paymentValueLoss
2840
2866
  }
2841
2867
  }, props.children);
2842
2868
  });
@@ -3270,7 +3296,8 @@
3270
3296
  approvalTransaction = _useContext4.approvalTransaction;
3271
3297
 
3272
3298
  var _useContext5 = React.useContext(PaymentValueContext),
3273
- paymentValue = _useContext5.paymentValue;
3299
+ paymentValue = _useContext5.paymentValue,
3300
+ paymentValueLoss = _useContext5.paymentValueLoss;
3274
3301
 
3275
3302
  var _useContext6 = React.useContext(reactDialogStack.NavigateStackContext);
3276
3303
  _useContext6.navigate;
@@ -3416,7 +3443,16 @@
3416
3443
  displayedAmount = "".concat(payment.symbol, " ").concat(payment.amount);
3417
3444
  }
3418
3445
 
3419
- if ((paymentState == 'initialized' || paymentState == 'approving') && payment.route) {
3446
+ if (paymentValueLoss) {
3447
+ return /*#__PURE__*/React__default["default"].createElement("div", null, /*#__PURE__*/React__default["default"].createElement("div", {
3448
+ className: "PaddingBottomXS"
3449
+ }, /*#__PURE__*/React__default["default"].createElement("div", {
3450
+ className: "Alert"
3451
+ }, /*#__PURE__*/React__default["default"].createElement("strong", null, "Payment token would lose ", paymentValueLoss, "% of it's value!"))), /*#__PURE__*/React__default["default"].createElement("button", {
3452
+ className: "ButtonPrimary disabled",
3453
+ onClick: function onClick() {}
3454
+ }, "Pay ", displayedAmount));
3455
+ } else if ((paymentState == 'initialized' || paymentState == 'approving') && payment.route) {
3420
3456
  return /*#__PURE__*/React__default["default"].createElement("button", {
3421
3457
  className: ["ButtonPrimary", payment.route.approvalRequired && !payment.route.directTransfer ? 'disabled' : ''].join(' '),
3422
3458
  onClick: function onClick() {
@@ -3714,11 +3750,11 @@
3714
3750
  paymentRoute = _useState6[0],
3715
3751
  setPaymentRoute = _useState6[1];
3716
3752
 
3717
- var _useState7 = React.useState(track && !!(track.endpoint || typeof track.method == 'function')),
3753
+ var _useState7 = React.useState(!!(track && (track.endpoint || typeof track.method == 'function'))),
3718
3754
  _useState8 = _slicedToArray(_useState7, 1),
3719
3755
  tracking = _useState8[0];
3720
3756
 
3721
- var _useState9 = React.useState(track && track.poll && !!(track.poll.endpoint || typeof track.poll.method == 'function')),
3757
+ var _useState9 = React.useState(!!(track && track.poll && (track.poll.endpoint || typeof track.poll.method == 'function'))),
3722
3758
  _useState10 = _slicedToArray(_useState9, 1),
3723
3759
  polling = _useState10[0];
3724
3760
 
@@ -3786,7 +3822,6 @@
3786
3822
 
3787
3823
  var retryStartTracking = function retryStartTracking(transaction, afterBlock, paymentRoute, attempt) {
3788
3824
  attempt = parseInt(attempt || 1, 10);
3789
- console.log('RETRYING PAYMENT TRACKING ATTEMPT ', attempt);
3790
3825
 
3791
3826
  if (attempt < 3) {
3792
3827
  setTimeout(function () {
@@ -3829,9 +3864,7 @@
3829
3864
  after_block: afterBlock,
3830
3865
  to_token: paymentRoute.toToken.address
3831
3866
  }).then(function (response) {
3832
- if (response.status == 200) {
3833
- console.log('PAYMENT TRACKING INITIALIZED');
3834
- } else {
3867
+ if (response.status != 200) {
3835
3868
  retryStartTracking(transaction, afterBlock, paymentRoute, attempt);
3836
3869
  }
3837
3870
  })["catch"](function (error) {
@@ -3895,7 +3928,56 @@
3895
3928
  };
3896
3929
  }, [polling, transaction, afterBlock, paymentRoute]);
3897
3930
 
3931
+ var storePayment = function storePayment(transaction, afterBlock, paymentRoute, attempt) {
3932
+ if (attempt > 3) {
3933
+ return;
3934
+ }
3935
+
3936
+ fetch('https://api.depay.fi/v2/payments', {
3937
+ method: 'POST',
3938
+ headers: {
3939
+ 'X-Api-Key': apiKey,
3940
+ 'Content-Type': 'application/json'
3941
+ },
3942
+ body: JSON.stringify({
3943
+ blockchain: transaction.blockchain,
3944
+ transaction: transaction.id,
3945
+ sender: transaction.from.toLowerCase(),
3946
+ nonce: transaction.nonce,
3947
+ receiver: paymentRoute.toAddress,
3948
+ token: paymentRoute.toToken.address,
3949
+ amount: paymentRoute.fee ? ethers.ethers.utils.formatUnits(paymentRoute.transaction.params.amounts[1], paymentRoute.toDecimals) : ethers.ethers.utils.formatUnits(paymentRoute.toAmount, paymentRoute.toDecimals),
3950
+ confirmations: 1,
3951
+ after_block: afterBlock,
3952
+ uuid: transaction.id,
3953
+ payload: {
3954
+ sender_id: transaction.from.toLowerCase(),
3955
+ sender_token_id: paymentRoute.fromToken.address,
3956
+ sender_amount: ethers.ethers.utils.formatUnits(paymentRoute.fromAmount, paymentRoute.fromDecimals)
3957
+ },
3958
+ fee_amount: paymentRoute.fee ? ethers.ethers.utils.formatUnits(paymentRoute.transaction.params.amounts[4], paymentRoute.toDecimals) : null,
3959
+ fee_receiver: paymentRoute.fee ? paymentRoute.transaction.params.addresses[1] : null
3960
+ })
3961
+ }).then(function (response) {
3962
+ if (response.status == 200 || response.status == 201) ; else {
3963
+ setTimeout(function () {
3964
+ storePayment(transaction, afterBlock, paymentRoute, attempt + 1);
3965
+ }, 3000);
3966
+ }
3967
+ })["catch"](function (error) {
3968
+ setTimeout(function () {
3969
+ storePayment(transaction, afterBlock, paymentRoute, attempt + 1);
3970
+ }, 3000);
3971
+ });
3972
+ };
3973
+
3898
3974
  var initializeTracking = function initializeTracking(transaction, afterBlock, paymentRoute) {
3975
+ storePayment(transaction, afterBlock, paymentRoute, 1);
3976
+
3977
+ if (tracking == false) {
3978
+ return;
3979
+ }
3980
+
3899
3981
  setTransaction(transaction);
3900
3982
  setAfterBlock(afterBlock);
3901
3983
  setPaymentRoute(paymentRoute);
@@ -4063,8 +4145,11 @@
4063
4145
  });
4064
4146
 
4065
4147
  var WalletProvider = (function (props) {
4066
- var _useContext = React.useContext(ErrorContext);
4067
- _useContext.setError;
4148
+ var _useContext = React.useContext(ConfigurationContext),
4149
+ recover = _useContext.recover;
4150
+
4151
+ var _useContext2 = React.useContext(ErrorContext);
4152
+ _useContext2.setError;
4068
4153
 
4069
4154
  var _useState = React.useState(),
4070
4155
  _useState2 = _slicedToArray(_useState, 2),
@@ -4093,7 +4178,7 @@
4093
4178
  }
4094
4179
  };
4095
4180
 
4096
- if (walletState == 'connected') {
4181
+ if (walletState == 'connected' || recover != undefined) {
4097
4182
  return /*#__PURE__*/React__default["default"].createElement(WalletContext.Provider, {
4098
4183
  value: {
4099
4184
  account: account,
@@ -4625,10 +4710,14 @@
4625
4710
  throw 'You need to set a supported blockchain!';
4626
4711
  }
4627
4712
 
4628
- if (typeof configuration.token === 'undefined') {
4713
+ if (typeof configuration.token === 'undefined' && typeof configuration.fromToken === 'undefined' && typeof configuration.fromAmount === 'undefined' && typeof configuration.toToken === 'undefined') {
4629
4714
  throw 'You need to set the token you want to receive as payment!';
4630
4715
  }
4631
4716
 
4717
+ if (typeof configuration.token === 'undefined' && typeof configuration.fromToken !== 'undefined' && typeof configuration.fromAmount === 'undefined' && typeof configuration.toToken === 'undefined') {
4718
+ throw 'You need to set the fromToken, fromAmount and toToken!';
4719
+ }
4720
+
4632
4721
  if (typeof configuration.receiver === 'undefined') {
4633
4722
  throw 'You need to set the receiver address that you want to receive the payment!';
4634
4723
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@depay/widgets",
3
3
  "moduleName": "DePayWidgets",
4
- "version": "6.13.0",
4
+ "version": "6.16.0",
5
5
  "description": "Web3 Payments with any token. DePay simplifies and improves Web3 Payments with the power of DeFi. Accept any token with on-the-fly conversion.",
6
6
  "main": "./dist/umd/index.js",
7
7
  "module": "./dist/esm/index.js",
@@ -43,8 +43,8 @@
43
43
  "@depay/web3-client": "^8.0.0",
44
44
  "@depay/web3-constants": "^5.0.0",
45
45
  "@depay/web3-exchanges": "^8.1.0",
46
- "@depay/web3-payments": "^9.2.0",
47
- "@depay/web3-tokens": "^8.0.0",
46
+ "@depay/web3-payments": "^9.3.3",
47
+ "@depay/web3-tokens": "^8.0.1",
48
48
  "@depay/web3-wallets": "^9.0.0",
49
49
  "decimal.js": "^10.3.1",
50
50
  "react-rangeslider": "^2.2.0"
@@ -60,7 +60,7 @@
60
60
  "@babel/preset-env": "^7.12.7",
61
61
  "@babel/preset-react": "^7.12.7",
62
62
  "@cypress/react": "^5.9.1",
63
- "@depay/web3-mock": "^11.6.0",
63
+ "@depay/web3-mock": "^11.6.2",
64
64
  "@rollup/plugin-babel": "^5.3.0",
65
65
  "@rollup/plugin-commonjs": "^17.1.0",
66
66
  "@rollup/plugin-node-resolve": "^11.1.1",