@depay/widgets 6.13.1 → 6.14.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/README.md CHANGED
@@ -194,6 +194,25 @@ DePayWidgets.Payment({
194
194
  });
195
195
  ```
196
196
 
197
+ #### fromToken, fromAmount + toToken
198
+
199
+ In case where you want to configure payments based on the source token + amount, rather than target token and amount, you can pass `fromToken`, `fromAmount` and `toToken` to `accept`.
200
+
201
+ In those cases make sure to NOT configure `token` nor `amount`!
202
+
203
+ ```javascript
204
+ DePayWidgets.Payment({
205
+ accept: [{
206
+ blockchain: 'bsc',
207
+ fromToken: '0xe9e7cea3dedca5984780bafc599bd69add087d56',
208
+ fromAmount: 0.1,
209
+ toToken: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
210
+ receiver: '0x08B277154218CCF3380CAE48d630DA13462E3950'
211
+ }]
212
+ });
213
+
214
+ // This will open a payment widget to send 0.1 BUSD to the receiver, converting it to BNB along the way.
215
+ ```
197
216
 
198
217
  #### track
199
218
 
@@ -61718,13 +61718,14 @@ let route$8 = ({
61718
61718
  var ChangableAmountProvider = (function (props) {
61719
61719
  var configurationsMissAmounts = function configurationsMissAmounts(configurations) {
61720
61720
  return !configurations.every(function (configuration) {
61721
- return typeof configuration.amount != 'undefined';
61721
+ return typeof configuration.amount != 'undefined' || typeof configuration.fromAmount != 'undefined';
61722
61722
  });
61723
61723
  };
61724
61724
 
61725
61725
  var _useContext = react.useContext(ConfigurationContext),
61726
- amountConfiguration = _useContext.amount,
61727
- recover = _useContext.recover;
61726
+ amountConfiguration = _useContext.amount;
61727
+ _useContext.toAmount;
61728
+ var recover = _useContext.recover;
61728
61729
 
61729
61730
  var _useState = react.useState(recover == undefined ? configurationsMissAmounts(props.accept) : false),
61730
61731
  _useState2 = _slicedToArray(_useState, 2),
@@ -67681,14 +67682,16 @@ let transactionValue = ({ paymentRoute, exchangeRoute })=> {
67681
67682
 
67682
67683
  function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
67683
67684
  class PaymentRoute {
67684
- constructor({ blockchain, fromToken, toToken, toDecimals, toAmount, fromAddress, toAddress, toContract }) {
67685
+ constructor({ blockchain, fromAddress, fromToken, fromDecimals, fromAmount, toToken, toDecimals, toAmount, toAddress, toContract }) {
67685
67686
  this.blockchain = blockchain;
67687
+ this.fromAddress = fromAddress;
67686
67688
  this.fromToken = fromToken;
67689
+ this.fromAmount = _optionalChain([fromAmount, 'optionalAccess', _ => _.toString, 'call', _2 => _2()]);
67690
+ this.fromDecimals = _optionalChain([fromDecimals, 'optionalAccess', _3 => _3.toString, 'call', _4 => _4()]);
67687
67691
  this.fromBalance = 0;
67688
67692
  this.toToken = toToken;
67693
+ this.toAmount = _optionalChain([toAmount, 'optionalAccess', _5 => _5.toString, 'call', _6 => _6()]);
67689
67694
  this.toDecimals = toDecimals;
67690
- this.toAmount = _optionalChain([toAmount, 'optionalAccess', _ => _.toString, 'call', _2 => _2()]);
67691
- this.fromAddress = fromAddress;
67692
67695
  this.toAddress = toAddress;
67693
67696
  this.toContract = toContract;
67694
67697
  this.exchangeRoutes = [];
@@ -67748,22 +67751,43 @@ function convertToRoutes({ tokens, accept }) {
67748
67751
  return Promise.all(tokens.map(async (fromToken)=>{
67749
67752
  let relevantConfigurations = accept.filter((configuration)=>(configuration.blockchain == fromToken.blockchain));
67750
67753
  return Promise.all(relevantConfigurations.map(async (configuration)=>{
67751
- let blockchain = configuration.blockchain;
67752
- let toToken = new Token({ blockchain, address: configuration.token });
67753
- let toDecimals = await toToken.decimals();
67754
- let toAmount = (await toToken.BigNumber(configuration.amount)).toString();
67755
- return new PaymentRoute({
67756
- blockchain,
67757
- fromToken: fromToken,
67758
- toToken: toToken,
67759
- toAmount: toAmount,
67760
- toDecimals: toDecimals,
67761
- fromAddress: configuration.fromAddress,
67762
- toAddress: configuration.toAddress,
67763
- toContract: configuration.toContract
67764
- })
67754
+ if(configuration.token && configuration.amount) {
67755
+ let blockchain = configuration.blockchain;
67756
+ let toToken = new Token({ blockchain, address: configuration.token });
67757
+ let toDecimals = await toToken.decimals();
67758
+ let toAmount = (await toToken.BigNumber(configuration.amount)).toString();
67759
+
67760
+ return new PaymentRoute({
67761
+ blockchain,
67762
+ fromToken,
67763
+ toToken,
67764
+ toAmount,
67765
+ toDecimals,
67766
+ fromAddress: configuration.fromAddress,
67767
+ toAddress: configuration.toAddress,
67768
+ toContract: configuration.toContract
67769
+ })
67770
+ } else if(configuration.fromToken && configuration.fromAmount && fromToken.address.toLowerCase() == configuration.fromToken.toLowerCase()) {
67771
+ let blockchain = configuration.blockchain;
67772
+ let fromAmount = (await fromToken.BigNumber(configuration.fromAmount)).toString();
67773
+ let fromDecimals = await fromToken.decimals();
67774
+ let toToken = new Token({ blockchain, address: configuration.toToken });
67775
+ let toDecimals = await toToken.decimals();
67776
+
67777
+ return new PaymentRoute({
67778
+ blockchain,
67779
+ fromToken,
67780
+ fromAmount,
67781
+ fromDecimals,
67782
+ toToken,
67783
+ toDecimals,
67784
+ fromAddress: configuration.fromAddress,
67785
+ toAddress: configuration.toAddress,
67786
+ toContract: configuration.toContract
67787
+ })
67788
+ }
67765
67789
  }))
67766
- })).then((routes)=> routes.flat())
67790
+ })).then((routes)=> routes.flat().filter(el => el))
67767
67791
  }
67768
67792
 
67769
67793
  async function route({ accept, whitelist, blacklist, apiKey, event, fee }) {
@@ -67819,14 +67843,25 @@ let addExchangeRoutes = async (routes) => {
67819
67843
  return await Promise.all(
67820
67844
  routes.map((route) => {
67821
67845
  if(route.directTransfer) { return [] }
67822
- return route$8({
67823
- blockchain: route.blockchain,
67824
- tokenIn: route.fromToken.address,
67825
- tokenOut: route.toToken.address,
67826
- amountOutMin: route.toAmount,
67827
- fromAddress: route.fromAddress,
67828
- toAddress: route.toAddress
67829
- })
67846
+ if(route.toToken && route.toAmount) {
67847
+ return route$8({
67848
+ blockchain: route.blockchain,
67849
+ tokenIn: route.fromToken.address,
67850
+ tokenOut: route.toToken.address,
67851
+ amountOutMin: route.toAmount,
67852
+ fromAddress: route.fromAddress,
67853
+ toAddress: route.toAddress
67854
+ })
67855
+ } else if(route.fromToken && route.fromAmount) {
67856
+ return route$8({
67857
+ blockchain: route.blockchain,
67858
+ tokenIn: route.fromToken.address,
67859
+ tokenOut: route.toToken.address,
67860
+ amountIn: route.fromAmount,
67861
+ fromAddress: route.fromAddress,
67862
+ toAddress: route.toAddress
67863
+ })
67864
+ }
67830
67865
  }),
67831
67866
  ).then((exchangeRoutes) => {
67832
67867
  return routes.map((route, index) => {
@@ -67856,8 +67891,10 @@ let filterInsufficientBalance = async(routes) => {
67856
67891
  return routes.filter((route) => {
67857
67892
  if (route.fromToken.address.toLowerCase() == route.toToken.address.toLowerCase()) {
67858
67893
  return BigNumber.from(route.fromBalance).gte(BigNumber.from(route.toAmount))
67859
- } else {
67894
+ } else if(route.fromAmount && route.toAmount) {
67860
67895
  return BigNumber.from(route.fromBalance).gte(BigNumber.from(route.exchangeRoutes[0].amountInMax))
67896
+ } else if(route.exchangeRoutes[0] && route.exchangeRoutes[0].amountIn) {
67897
+ return BigNumber.from(route.fromBalance).gte(BigNumber.from(route.exchangeRoutes[0].amountIn))
67861
67898
  }
67862
67899
  })
67863
67900
  };
@@ -71759,10 +71796,14 @@ var preflight$1 = /*#__PURE__*/function () {
71759
71796
  throw 'You need to set a supported blockchain!';
71760
71797
  }
71761
71798
 
71762
- if (typeof configuration.token === 'undefined') {
71799
+ if (typeof configuration.token === 'undefined' && typeof configuration.fromToken === 'undefined' && typeof configuration.fromAmount === 'undefined' && typeof configuration.toToken === 'undefined') {
71763
71800
  throw 'You need to set the token you want to receive as payment!';
71764
71801
  }
71765
71802
 
71803
+ if (typeof configuration.token === 'undefined' && typeof configuration.fromToken !== 'undefined' && typeof configuration.fromAmount === 'undefined' && typeof configuration.toToken === 'undefined') {
71804
+ throw 'You need to set the fromToken, fromAmount and toToken!';
71805
+ }
71806
+
71766
71807
  if (typeof configuration.receiver === 'undefined') {
71767
71808
  throw 'You need to set the receiver address that you want to receive the payment!';
71768
71809
  }
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),
@@ -4633,10 +4634,14 @@ var preflight$1 = /*#__PURE__*/function () {
4633
4634
  throw 'You need to set a supported blockchain!';
4634
4635
  }
4635
4636
 
4636
- if (typeof configuration.token === 'undefined') {
4637
+ if (typeof configuration.token === 'undefined' && typeof configuration.fromToken === 'undefined' && typeof configuration.fromAmount === 'undefined' && typeof configuration.toToken === 'undefined') {
4637
4638
  throw 'You need to set the token you want to receive as payment!';
4638
4639
  }
4639
4640
 
4641
+ if (typeof configuration.token === 'undefined' && typeof configuration.fromToken !== 'undefined' && typeof configuration.fromAmount === 'undefined' && typeof configuration.toToken === 'undefined') {
4642
+ throw 'You need to set the fromToken, fromAmount and toToken!';
4643
+ }
4644
+
4640
4645
  if (typeof configuration.receiver === 'undefined') {
4641
4646
  throw 'You need to set the receiver address that you want to receive the payment!';
4642
4647
  }
@@ -61724,13 +61724,14 @@
61724
61724
  var ChangableAmountProvider = (function (props) {
61725
61725
  var configurationsMissAmounts = function configurationsMissAmounts(configurations) {
61726
61726
  return !configurations.every(function (configuration) {
61727
- return typeof configuration.amount != 'undefined';
61727
+ return typeof configuration.amount != 'undefined' || typeof configuration.fromAmount != 'undefined';
61728
61728
  });
61729
61729
  };
61730
61730
 
61731
61731
  var _useContext = react.useContext(ConfigurationContext),
61732
- amountConfiguration = _useContext.amount,
61733
- recover = _useContext.recover;
61732
+ amountConfiguration = _useContext.amount;
61733
+ _useContext.toAmount;
61734
+ var recover = _useContext.recover;
61734
61735
 
61735
61736
  var _useState = react.useState(recover == undefined ? configurationsMissAmounts(props.accept) : false),
61736
61737
  _useState2 = _slicedToArray(_useState, 2),
@@ -67687,14 +67688,16 @@
67687
67688
 
67688
67689
  function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
67689
67690
  class PaymentRoute {
67690
- constructor({ blockchain, fromToken, toToken, toDecimals, toAmount, fromAddress, toAddress, toContract }) {
67691
+ constructor({ blockchain, fromAddress, fromToken, fromDecimals, fromAmount, toToken, toDecimals, toAmount, toAddress, toContract }) {
67691
67692
  this.blockchain = blockchain;
67693
+ this.fromAddress = fromAddress;
67692
67694
  this.fromToken = fromToken;
67695
+ this.fromAmount = _optionalChain([fromAmount, 'optionalAccess', _ => _.toString, 'call', _2 => _2()]);
67696
+ this.fromDecimals = _optionalChain([fromDecimals, 'optionalAccess', _3 => _3.toString, 'call', _4 => _4()]);
67693
67697
  this.fromBalance = 0;
67694
67698
  this.toToken = toToken;
67699
+ this.toAmount = _optionalChain([toAmount, 'optionalAccess', _5 => _5.toString, 'call', _6 => _6()]);
67695
67700
  this.toDecimals = toDecimals;
67696
- this.toAmount = _optionalChain([toAmount, 'optionalAccess', _ => _.toString, 'call', _2 => _2()]);
67697
- this.fromAddress = fromAddress;
67698
67701
  this.toAddress = toAddress;
67699
67702
  this.toContract = toContract;
67700
67703
  this.exchangeRoutes = [];
@@ -67754,22 +67757,43 @@
67754
67757
  return Promise.all(tokens.map(async (fromToken)=>{
67755
67758
  let relevantConfigurations = accept.filter((configuration)=>(configuration.blockchain == fromToken.blockchain));
67756
67759
  return Promise.all(relevantConfigurations.map(async (configuration)=>{
67757
- let blockchain = configuration.blockchain;
67758
- let toToken = new Token({ blockchain, address: configuration.token });
67759
- let toDecimals = await toToken.decimals();
67760
- let toAmount = (await toToken.BigNumber(configuration.amount)).toString();
67761
- return new PaymentRoute({
67762
- blockchain,
67763
- fromToken: fromToken,
67764
- toToken: toToken,
67765
- toAmount: toAmount,
67766
- toDecimals: toDecimals,
67767
- fromAddress: configuration.fromAddress,
67768
- toAddress: configuration.toAddress,
67769
- toContract: configuration.toContract
67770
- })
67760
+ if(configuration.token && configuration.amount) {
67761
+ let blockchain = configuration.blockchain;
67762
+ let toToken = new Token({ blockchain, address: configuration.token });
67763
+ let toDecimals = await toToken.decimals();
67764
+ let toAmount = (await toToken.BigNumber(configuration.amount)).toString();
67765
+
67766
+ return new PaymentRoute({
67767
+ blockchain,
67768
+ fromToken,
67769
+ toToken,
67770
+ toAmount,
67771
+ toDecimals,
67772
+ fromAddress: configuration.fromAddress,
67773
+ toAddress: configuration.toAddress,
67774
+ toContract: configuration.toContract
67775
+ })
67776
+ } else if(configuration.fromToken && configuration.fromAmount && fromToken.address.toLowerCase() == configuration.fromToken.toLowerCase()) {
67777
+ let blockchain = configuration.blockchain;
67778
+ let fromAmount = (await fromToken.BigNumber(configuration.fromAmount)).toString();
67779
+ let fromDecimals = await fromToken.decimals();
67780
+ let toToken = new Token({ blockchain, address: configuration.toToken });
67781
+ let toDecimals = await toToken.decimals();
67782
+
67783
+ return new PaymentRoute({
67784
+ blockchain,
67785
+ fromToken,
67786
+ fromAmount,
67787
+ fromDecimals,
67788
+ toToken,
67789
+ toDecimals,
67790
+ fromAddress: configuration.fromAddress,
67791
+ toAddress: configuration.toAddress,
67792
+ toContract: configuration.toContract
67793
+ })
67794
+ }
67771
67795
  }))
67772
- })).then((routes)=> routes.flat())
67796
+ })).then((routes)=> routes.flat().filter(el => el))
67773
67797
  }
67774
67798
 
67775
67799
  async function route({ accept, whitelist, blacklist, apiKey, event, fee }) {
@@ -67825,14 +67849,25 @@
67825
67849
  return await Promise.all(
67826
67850
  routes.map((route) => {
67827
67851
  if(route.directTransfer) { return [] }
67828
- return route$8({
67829
- blockchain: route.blockchain,
67830
- tokenIn: route.fromToken.address,
67831
- tokenOut: route.toToken.address,
67832
- amountOutMin: route.toAmount,
67833
- fromAddress: route.fromAddress,
67834
- toAddress: route.toAddress
67835
- })
67852
+ if(route.toToken && route.toAmount) {
67853
+ return route$8({
67854
+ blockchain: route.blockchain,
67855
+ tokenIn: route.fromToken.address,
67856
+ tokenOut: route.toToken.address,
67857
+ amountOutMin: route.toAmount,
67858
+ fromAddress: route.fromAddress,
67859
+ toAddress: route.toAddress
67860
+ })
67861
+ } else if(route.fromToken && route.fromAmount) {
67862
+ return route$8({
67863
+ blockchain: route.blockchain,
67864
+ tokenIn: route.fromToken.address,
67865
+ tokenOut: route.toToken.address,
67866
+ amountIn: route.fromAmount,
67867
+ fromAddress: route.fromAddress,
67868
+ toAddress: route.toAddress
67869
+ })
67870
+ }
67836
67871
  }),
67837
67872
  ).then((exchangeRoutes) => {
67838
67873
  return routes.map((route, index) => {
@@ -67862,8 +67897,10 @@
67862
67897
  return routes.filter((route) => {
67863
67898
  if (route.fromToken.address.toLowerCase() == route.toToken.address.toLowerCase()) {
67864
67899
  return BigNumber.from(route.fromBalance).gte(BigNumber.from(route.toAmount))
67865
- } else {
67900
+ } else if(route.fromAmount && route.toAmount) {
67866
67901
  return BigNumber.from(route.fromBalance).gte(BigNumber.from(route.exchangeRoutes[0].amountInMax))
67902
+ } else if(route.exchangeRoutes[0] && route.exchangeRoutes[0].amountIn) {
67903
+ return BigNumber.from(route.fromBalance).gte(BigNumber.from(route.exchangeRoutes[0].amountIn))
67867
67904
  }
67868
67905
  })
67869
67906
  };
@@ -71765,10 +71802,14 @@
71765
71802
  throw 'You need to set a supported blockchain!';
71766
71803
  }
71767
71804
 
71768
- if (typeof configuration.token === 'undefined') {
71805
+ if (typeof configuration.token === 'undefined' && typeof configuration.fromToken === 'undefined' && typeof configuration.fromAmount === 'undefined' && typeof configuration.toToken === 'undefined') {
71769
71806
  throw 'You need to set the token you want to receive as payment!';
71770
71807
  }
71771
71808
 
71809
+ if (typeof configuration.token === 'undefined' && typeof configuration.fromToken !== 'undefined' && typeof configuration.fromAmount === 'undefined' && typeof configuration.toToken === 'undefined') {
71810
+ throw 'You need to set the fromToken, fromAmount and toToken!';
71811
+ }
71812
+
71772
71813
  if (typeof configuration.receiver === 'undefined') {
71773
71814
  throw 'You need to set the receiver address that you want to receive the payment!';
71774
71815
  }
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),
@@ -4628,10 +4629,14 @@
4628
4629
  throw 'You need to set a supported blockchain!';
4629
4630
  }
4630
4631
 
4631
- if (typeof configuration.token === 'undefined') {
4632
+ if (typeof configuration.token === 'undefined' && typeof configuration.fromToken === 'undefined' && typeof configuration.fromAmount === 'undefined' && typeof configuration.toToken === 'undefined') {
4632
4633
  throw 'You need to set the token you want to receive as payment!';
4633
4634
  }
4634
4635
 
4636
+ if (typeof configuration.token === 'undefined' && typeof configuration.fromToken !== 'undefined' && typeof configuration.fromAmount === 'undefined' && typeof configuration.toToken === 'undefined') {
4637
+ throw 'You need to set the fromToken, fromAmount and toToken!';
4638
+ }
4639
+
4635
4640
  if (typeof configuration.receiver === 'undefined') {
4636
4641
  throw 'You need to set the receiver address that you want to receive the payment!';
4637
4642
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@depay/widgets",
3
3
  "moduleName": "DePayWidgets",
4
- "version": "6.13.1",
4
+ "version": "6.14.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,7 +43,7 @@
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",
46
+ "@depay/web3-payments": "^9.3.0",
47
47
  "@depay/web3-tokens": "^8.0.0",
48
48
  "@depay/web3-wallets": "^9.0.0",
49
49
  "decimal.js": "^10.3.1",