@xchainjs/xchain-mayachain-amm 2.0.10 → 2.0.11

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/lib/index.esm.js CHANGED
@@ -1,14 +1,15 @@
1
- import { Client, defaultBTCParams } from '@xchainjs/xchain-bitcoin';
1
+ import { AssetAETH, Client as Client$6, defaultArbParams } from '@xchainjs/xchain-arbitrum';
2
+ import { BTCChain, Client as Client$5, defaultBTCParams } from '@xchainjs/xchain-bitcoin';
2
3
  import { Network } from '@xchainjs/xchain-client';
3
- import { Client as Client$2, defaultDashParams } from '@xchainjs/xchain-dash';
4
- import { Client as Client$1, defaultEthParams, AssetETH as AssetETH$1 } from '@xchainjs/xchain-ethereum';
4
+ import { DASHChain, Client as Client$3, defaultDashParams } from '@xchainjs/xchain-dash';
5
+ import { AssetETH as AssetETH$1, ETHChain, Client as Client$4, defaultEthParams } from '@xchainjs/xchain-ethereum';
5
6
  import 'crypto';
6
7
  import require$$0$4$1 from 'buffer';
7
8
  import { ethers } from 'ethers';
8
- import { Client as Client$3, defaultKujiParams } from '@xchainjs/xchain-kujira';
9
- import { Client as Client$5, MAYAChain } from '@xchainjs/xchain-mayachain';
9
+ import { KUJIChain, Client as Client$2, defaultKujiParams, AssetKUJI } from '@xchainjs/xchain-kujira';
10
+ import { MAYAChain, Client } from '@xchainjs/xchain-mayachain';
10
11
  import { MayachainQuery } from '@xchainjs/xchain-mayachain-query';
11
- import { Client as Client$4 } from '@xchainjs/xchain-thorchain';
12
+ import { THORChain, Client as Client$1, AssetRuneNative } from '@xchainjs/xchain-thorchain';
12
13
  import { Wallet as Wallet$1 } from '@xchainjs/xchain-wallet';
13
14
 
14
15
  /******************************************************************************
@@ -89526,6 +89527,148 @@ const abi = {
89526
89527
  erc20: erc20ABI, // ERC20 ABI
89527
89528
  };
89528
89529
 
89530
+ /**
89531
+ * Check if a chain is EVM and supported by the protocol
89532
+ * @param {Chain} chain to check
89533
+ * @returns true if chain is EVM, otherwise, false
89534
+ */
89535
+ const isProtocolEVMChain = (chain) => {
89536
+ return [AssetETH$1.chain, AssetAETH.chain].includes(chain);
89537
+ };
89538
+ /**
89539
+ * Check if asset is ERC20
89540
+ * @param {Asset} asset to check
89541
+ * @returns true if asset is ERC20, otherwise, false
89542
+ */
89543
+ const isProtocolERC20Asset = (asset) => {
89544
+ return isProtocolEVMChain(asset.chain)
89545
+ ? [AssetETH$1, AssetAETH].findIndex((nativeEVMAsset) => eqAsset(nativeEVMAsset, asset)) === -1 && !asset.synth
89546
+ : false;
89547
+ };
89548
+ /**
89549
+ * Check if a chain is EVM and supported by the protocol
89550
+ * @param {Chain} chain to check
89551
+ * @returns true if chain is EVM, otherwise, false
89552
+ */
89553
+ const isProtocolBFTChain = (chain) => {
89554
+ return [AssetKUJI.chain, AssetRuneNative.chain].includes(chain);
89555
+ };
89556
+ const validateAddress = (network, chain, address) => {
89557
+ switch (chain) {
89558
+ case BTCChain:
89559
+ return new Client$5(Object.assign(Object.assign({}, defaultBTCParams), { network })).validateAddress(address);
89560
+ case ETHChain:
89561
+ return new Client$4(Object.assign(Object.assign({}, defaultEthParams), { network })).validateAddress(address);
89562
+ case DASHChain:
89563
+ return new Client$3(Object.assign(Object.assign({}, defaultDashParams), { network })).validateAddress(address);
89564
+ case KUJIChain:
89565
+ return new Client$2(Object.assign(Object.assign({}, defaultKujiParams), { network })).validateAddress(address);
89566
+ case THORChain:
89567
+ return new Client$1({ network }).validateAddress(address);
89568
+ case MAYAChain:
89569
+ return new Client({ network }).validateAddress(address);
89570
+ default:
89571
+ throw Error('Unsupported chain');
89572
+ }
89573
+ };
89574
+
89575
+ class MayachainAction {
89576
+ static makeAction(actionParams) {
89577
+ return __awaiter$8(this, void 0, void 0, function* () {
89578
+ return this.isNonProtocolParams(actionParams)
89579
+ ? this.makeNonProtocolAction(actionParams)
89580
+ : this.makeProtocolAction(actionParams);
89581
+ });
89582
+ }
89583
+ static makeProtocolAction({ wallet, assetAmount, memo }) {
89584
+ return __awaiter$8(this, void 0, void 0, function* () {
89585
+ const hash = yield wallet.deposit({
89586
+ chain: MAYAChain,
89587
+ asset: assetAmount.asset,
89588
+ amount: assetAmount.baseAmount,
89589
+ memo,
89590
+ });
89591
+ return {
89592
+ hash,
89593
+ url: yield wallet.getExplorerTxUrl(MAYAChain, hash),
89594
+ };
89595
+ });
89596
+ }
89597
+ static makeNonProtocolAction({ wallet, assetAmount, recipient, memo, }) {
89598
+ return __awaiter$8(this, void 0, void 0, function* () {
89599
+ // Non EVM actions
89600
+ if (!isProtocolEVMChain(assetAmount.asset.chain)) {
89601
+ if (isProtocolBFTChain(assetAmount.asset.chain)) {
89602
+ const hash = yield wallet.transfer({
89603
+ asset: assetAmount.asset,
89604
+ amount: assetAmount.baseAmount,
89605
+ recipient,
89606
+ memo,
89607
+ });
89608
+ return {
89609
+ hash,
89610
+ url: yield wallet.getExplorerTxUrl(assetAmount.asset.chain, hash),
89611
+ };
89612
+ }
89613
+ const feeRates = yield wallet.getFeeRates(assetAmount.asset.chain);
89614
+ const hash = yield wallet.transfer({
89615
+ asset: assetAmount.asset,
89616
+ amount: assetAmount.baseAmount,
89617
+ recipient,
89618
+ memo,
89619
+ feeRate: feeRates.fast,
89620
+ });
89621
+ return {
89622
+ hash,
89623
+ url: yield wallet.getExplorerTxUrl(assetAmount.asset.chain, hash),
89624
+ };
89625
+ }
89626
+ // EVM actions
89627
+ const mayachainQuery = new MayachainQuery();
89628
+ const inboundDetails = yield mayachainQuery.getChainInboundDetails(assetAmount.asset.chain);
89629
+ if (!inboundDetails.router)
89630
+ throw Error(`Unknown router for ${assetAmount.asset.chain} chain`);
89631
+ const isERC20 = isProtocolERC20Asset(assetAmount.asset);
89632
+ const checkSummedContractAddress = isERC20
89633
+ ? ethers.utils.getAddress(getContractAddressFromAsset(assetAmount.asset))
89634
+ : ethers.constants.AddressZero;
89635
+ const expiration = Math.floor(new Date(new Date().getTime() + 15 * 60000).getTime() / 1000);
89636
+ const depositParams = [
89637
+ recipient,
89638
+ checkSummedContractAddress,
89639
+ assetAmount.baseAmount.amount().toFixed(),
89640
+ memo,
89641
+ expiration,
89642
+ ];
89643
+ const routerContract = new ethers.Contract(inboundDetails.router, abi.router);
89644
+ const gasPrices = yield wallet.getFeeRates(assetAmount.asset.chain);
89645
+ const unsignedTx = yield routerContract.populateTransaction.depositWithExpiry(...depositParams);
89646
+ const nativeAsset = wallet.getAssetInfo(assetAmount.asset.chain);
89647
+ const hash = yield wallet.transfer({
89648
+ asset: nativeAsset.asset,
89649
+ amount: isERC20 ? baseAmount(0, nativeAsset.decimal) : assetAmount.baseAmount,
89650
+ memo: unsignedTx.data,
89651
+ recipient: inboundDetails.router,
89652
+ gasPrice: gasPrices.fast,
89653
+ isMemoEncoded: true,
89654
+ gasLimit: ethers.BigNumber.from(160000),
89655
+ });
89656
+ return {
89657
+ hash,
89658
+ url: yield wallet.getExplorerTxUrl(assetAmount.asset.chain, hash),
89659
+ };
89660
+ });
89661
+ }
89662
+ static isNonProtocolParams(params) {
89663
+ if ((params.assetAmount.asset.chain === MAYAChain || params.assetAmount.asset.synth) &&
89664
+ 'address' in params &&
89665
+ !!params.address) {
89666
+ throw Error('Inconsistent params. Native actions do not support recipient');
89667
+ }
89668
+ return params.assetAmount.asset.chain !== MAYAChain && !params.assetAmount.asset.synth;
89669
+ }
89670
+ }
89671
+
89529
89672
  /**
89530
89673
  * Mayachain Automated Market Maker (AMM) class.
89531
89674
  * MAYAChainAMM class for interacting with THORChain.
@@ -89541,12 +89684,13 @@ class MayachainAMM {
89541
89684
  * @returns {MayachainAMM} Returns the MayachainAMM instance.
89542
89685
  */
89543
89686
  constructor(mayachainQuery = new MayachainQuery(), wallet = new Wallet$1({
89544
- BTC: new Client(Object.assign(Object.assign({}, defaultBTCParams), { network: Network.Mainnet })),
89545
- ETH: new Client$1(Object.assign(Object.assign({}, defaultEthParams), { network: Network.Mainnet })),
89546
- DASH: new Client$2(Object.assign(Object.assign({}, defaultDashParams), { network: Network.Mainnet })),
89547
- KUJI: new Client$3(Object.assign(Object.assign({}, defaultKujiParams), { network: Network.Mainnet })),
89548
- THOR: new Client$4({ network: Network.Mainnet }),
89549
- MAYA: new Client$5({ network: Network.Mainnet }),
89687
+ BTC: new Client$5(Object.assign(Object.assign({}, defaultBTCParams), { network: Network.Mainnet })),
89688
+ ETH: new Client$4(Object.assign(Object.assign({}, defaultEthParams), { network: Network.Mainnet })),
89689
+ DASH: new Client$3(Object.assign(Object.assign({}, defaultDashParams), { network: Network.Mainnet })),
89690
+ KUJI: new Client$2(Object.assign(Object.assign({}, defaultKujiParams), { network: Network.Mainnet })),
89691
+ ARB: new Client$6(Object.assign(Object.assign({}, defaultArbParams), { network: Network.Mainnet })),
89692
+ THOR: new Client$1({ network: Network.Mainnet }),
89693
+ MAYA: new Client({ network: Network.Mainnet }),
89550
89694
  })) {
89551
89695
  this.mayachainQuery = mayachainQuery;
89552
89696
  this.wallet = wallet;
@@ -89613,12 +89757,12 @@ class MayachainAMM {
89613
89757
  const errors = [];
89614
89758
  // Validate destination address if provided
89615
89759
  if (destinationAddress &&
89616
- !this.wallet.validateAddress(destinationAsset.synth ? MAYAChain : destinationAsset.chain, destinationAddress)) {
89760
+ !validateAddress(this.mayachainQuery.getNetwork(), destinationAsset.synth ? MAYAChain : destinationAsset.chain, destinationAddress)) {
89617
89761
  errors.push(`destinationAddress ${destinationAddress} is not a valid address`);
89618
89762
  }
89619
89763
  // Validate affiliate address if provided
89620
89764
  if (affiliateAddress) {
89621
- const isMayaAddress = this.wallet.validateAddress(MAYAChain, affiliateAddress);
89765
+ const isMayaAddress = validateAddress(this.mayachainQuery.getNetwork(), MAYAChain, affiliateAddress);
89622
89766
  const isMayaName = yield this.isMAYAName(affiliateAddress);
89623
89767
  if (!(isMayaAddress || isMayaName))
89624
89768
  errors.push(`affiliateAddress ${affiliateAddress} is not a valid MAYA address`);
@@ -89628,7 +89772,7 @@ class MayachainAMM {
89628
89772
  errors.push(`affiliateBps ${affiliateBps} out of range [0 - 10000]`);
89629
89773
  }
89630
89774
  // Validate approval if asset is an ERC20 token and fromAddress is provided
89631
- if (this.isERC20Asset(fromAsset) && fromAddress) {
89775
+ if (isProtocolERC20Asset(fromAsset) && fromAddress) {
89632
89776
  const approveErrors = yield this.isRouterApprovedToSpend({
89633
89777
  asset: fromAsset,
89634
89778
  address: fromAddress,
@@ -89659,10 +89803,12 @@ class MayachainAMM {
89659
89803
  // Check if the swap can be performed
89660
89804
  if (!quoteSwap.canSwap)
89661
89805
  throw Error(`Can not swap. ${quoteSwap.errors.join(' ')}`);
89662
- // Perform the swap based on the asset chain
89663
- return fromAsset.chain === MAYAChain || fromAsset.synth
89664
- ? this.doProtocolAssetSwap(amount, quoteSwap.memo)
89665
- : this.doNonProtocolAssetSwap(amount, quoteSwap.toAddress, quoteSwap.memo);
89806
+ return MayachainAction.makeAction({
89807
+ wallet: this.wallet,
89808
+ assetAmount: amount,
89809
+ memo: quoteSwap.memo,
89810
+ recipient: `${quoteSwap.toAddress}`,
89811
+ });
89666
89812
  });
89667
89813
  }
89668
89814
  /**
@@ -89705,108 +89851,35 @@ class MayachainAMM {
89705
89851
  });
89706
89852
  }
89707
89853
  /**
89708
- * Check if a name is a valid MAYAName
89709
- * @param {string} name MAYAName to check
89710
- * @returns {boolean} True if the name is registered as a MAYAName, otherwise false
89854
+ * Get MAYAname details
89855
+ * @param {string} MAYAName
89856
+ * @returns {MAYANameDetails | undefined} MAYANames details or undefined it is does not exist
89711
89857
  */
89712
- isMAYAName(name) {
89858
+ getMAYANameDetails(MAYAName) {
89713
89859
  return __awaiter$8(this, void 0, void 0, function* () {
89714
- return !!(yield this.mayachainQuery.getMAYANameDetails(name));
89860
+ return this.mayachainQuery.getMAYANameDetails(MAYAName);
89715
89861
  });
89716
89862
  }
89717
89863
  /**
89718
- * Perform a swap from a native protocol asset to any other asset
89719
- * @param {CryptoAmount} amount Amount to swap
89720
- * @param {string} memo Memo to add to the transaction
89721
- * @returns {TxSubmitted} Transaction hash and URL of the swap
89864
+ * Get the MAYANames owned by an address
89865
+ * @param {Address} owner - Thorchain address
89866
+ * @returns {MAYANameDetails[]} List of MAYANames owned by the address
89722
89867
  */
89723
- doProtocolAssetSwap(amount, memo) {
89868
+ getMAYANamesByOwner(owner) {
89724
89869
  return __awaiter$8(this, void 0, void 0, function* () {
89725
- // Deposit the amount and return transaction hash and URL
89726
- const hash = yield this.wallet.deposit({ chain: MAYAChain, asset: amount.asset, amount: amount.baseAmount, memo });
89727
- return {
89728
- hash,
89729
- url: yield this.wallet.getExplorerTxUrl(MAYAChain, hash),
89730
- };
89870
+ return this.mayachainQuery.getMAYANamesByOwner(owner);
89731
89871
  });
89732
89872
  }
89733
89873
  /**
89734
- * Perform a swap between assets
89735
- * @param {CryptoAmount} amount Amount to swap
89736
- * @param {string} memo Memo to add to the transaction to successfully make the swap
89737
- * @param {string} recipient inbound address to make swap transaction to
89738
- * @returns {TxSubmitted} Transaction hash and URL of the swap
89874
+ * Check if a name is a valid MAYAName
89875
+ * @param {string} name MAYAName to check
89876
+ * @returns {boolean} True if the name is registered as a MAYAName, otherwise false
89739
89877
  */
89740
- doNonProtocolAssetSwap(amount, recipient, memo) {
89878
+ isMAYAName(name) {
89741
89879
  return __awaiter$8(this, void 0, void 0, function* () {
89742
- // For non-EVM assets, perform a transfer and return transaction hash and URL
89743
- if (!this.isEVMChain(amount.asset.chain)) {
89744
- const hash = yield this.wallet.transfer({
89745
- asset: amount.asset,
89746
- amount: amount.baseAmount,
89747
- recipient,
89748
- memo,
89749
- });
89750
- return {
89751
- hash,
89752
- url: yield this.wallet.getExplorerTxUrl(amount.asset.chain, hash),
89753
- };
89754
- }
89755
- // For EVM assets, perform a deposit with expiry and return transaction hash and URL
89756
- const inboundDetails = yield this.mayachainQuery.getChainInboundDetails(amount.asset.chain);
89757
- if (!inboundDetails.router)
89758
- throw Error(`Unknown router for ${amount.asset.chain} chain`);
89759
- const isERC20 = this.isERC20Asset(amount.asset);
89760
- const checkSummedContractAddress = isERC20
89761
- ? ethers.utils.getAddress(getContractAddressFromAsset(amount.asset))
89762
- : ethers.constants.AddressZero;
89763
- const expiration = Math.floor(new Date(new Date().getTime() + 15 * 60000).getTime() / 1000);
89764
- const depositParams = [
89765
- recipient,
89766
- checkSummedContractAddress,
89767
- amount.baseAmount.amount().toFixed(),
89768
- memo,
89769
- expiration,
89770
- ];
89771
- const routerContract = new ethers.Contract(inboundDetails.router, abi.router);
89772
- const unsignedTx = yield routerContract.populateTransaction.depositWithExpiry(...depositParams);
89773
- const gasPrices = yield this.wallet.getGasFeeRates(amount.asset.chain);
89774
- const nativeAsset = this.wallet.getAssetInfo(amount.asset.chain);
89775
- const hash = yield this.wallet.transfer({
89776
- asset: nativeAsset.asset,
89777
- amount: isERC20 ? baseAmount(0, nativeAsset.decimal) : amount.baseAmount,
89778
- memo: unsignedTx.data,
89779
- recipient: inboundDetails.router,
89780
- gasPrice: gasPrices.fast,
89781
- isMemoEncoded: true,
89782
- gasLimit: ethers.BigNumber.from(160000),
89783
- });
89784
- return {
89785
- hash,
89786
- url: yield this.wallet.getExplorerTxUrl(amount.asset.chain, hash),
89787
- };
89880
+ return !!(yield this.mayachainQuery.getMAYANameDetails(name));
89788
89881
  });
89789
89882
  }
89790
- /**
89791
- * Check if the asset is an ERC20 token
89792
- * @param {Asset} asset Asset to check
89793
- * @returns True if the asset is an ERC20 token, otherwise false
89794
- */
89795
- isERC20Asset(asset) {
89796
- // Check if the asset's chain is an EVM chain and if the symbol matches AssetETH.symbol
89797
- return this.isEVMChain(asset.chain)
89798
- ? [AssetETH$1].findIndex((nativeEVMAsset) => eqAsset(nativeEVMAsset, asset)) === -1 && !asset.synth
89799
- : false;
89800
- }
89801
- /**
89802
- * Check if the chain is an EVM (Ethereum Virtual Machine) chain
89803
- * @param {Chain} chain Chain to check
89804
- * @returns True if the chain is an EVM chain, otherwise false
89805
- */
89806
- isEVMChain(chain) {
89807
- // Check if the chain matches AssetETH.chain
89808
- return [AssetETH$1.chain].includes(chain);
89809
- }
89810
89883
  }
89811
89884
 
89812
89885
  export { MayachainAMM };
package/lib/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var xchainArbitrum = require('@xchainjs/xchain-arbitrum');
5
6
  var xchainBitcoin = require('@xchainjs/xchain-bitcoin');
6
7
  var xchainClient = require('@xchainjs/xchain-client');
7
8
  var xchainDash = require('@xchainjs/xchain-dash');
@@ -89534,6 +89535,148 @@ const abi = {
89534
89535
  erc20: erc20ABI, // ERC20 ABI
89535
89536
  };
89536
89537
 
89538
+ /**
89539
+ * Check if a chain is EVM and supported by the protocol
89540
+ * @param {Chain} chain to check
89541
+ * @returns true if chain is EVM, otherwise, false
89542
+ */
89543
+ const isProtocolEVMChain = (chain) => {
89544
+ return [xchainEthereum.AssetETH.chain, xchainArbitrum.AssetAETH.chain].includes(chain);
89545
+ };
89546
+ /**
89547
+ * Check if asset is ERC20
89548
+ * @param {Asset} asset to check
89549
+ * @returns true if asset is ERC20, otherwise, false
89550
+ */
89551
+ const isProtocolERC20Asset = (asset) => {
89552
+ return isProtocolEVMChain(asset.chain)
89553
+ ? [xchainEthereum.AssetETH, xchainArbitrum.AssetAETH].findIndex((nativeEVMAsset) => eqAsset(nativeEVMAsset, asset)) === -1 && !asset.synth
89554
+ : false;
89555
+ };
89556
+ /**
89557
+ * Check if a chain is EVM and supported by the protocol
89558
+ * @param {Chain} chain to check
89559
+ * @returns true if chain is EVM, otherwise, false
89560
+ */
89561
+ const isProtocolBFTChain = (chain) => {
89562
+ return [xchainKujira.AssetKUJI.chain, xchainThorchain.AssetRuneNative.chain].includes(chain);
89563
+ };
89564
+ const validateAddress = (network, chain, address) => {
89565
+ switch (chain) {
89566
+ case xchainBitcoin.BTCChain:
89567
+ return new xchainBitcoin.Client(Object.assign(Object.assign({}, xchainBitcoin.defaultBTCParams), { network })).validateAddress(address);
89568
+ case xchainEthereum.ETHChain:
89569
+ return new xchainEthereum.Client(Object.assign(Object.assign({}, xchainEthereum.defaultEthParams), { network })).validateAddress(address);
89570
+ case xchainDash.DASHChain:
89571
+ return new xchainDash.Client(Object.assign(Object.assign({}, xchainDash.defaultDashParams), { network })).validateAddress(address);
89572
+ case xchainKujira.KUJIChain:
89573
+ return new xchainKujira.Client(Object.assign(Object.assign({}, xchainKujira.defaultKujiParams), { network })).validateAddress(address);
89574
+ case xchainThorchain.THORChain:
89575
+ return new xchainThorchain.Client({ network }).validateAddress(address);
89576
+ case xchainMayachain.MAYAChain:
89577
+ return new xchainMayachain.Client({ network }).validateAddress(address);
89578
+ default:
89579
+ throw Error('Unsupported chain');
89580
+ }
89581
+ };
89582
+
89583
+ class MayachainAction {
89584
+ static makeAction(actionParams) {
89585
+ return __awaiter$8(this, void 0, void 0, function* () {
89586
+ return this.isNonProtocolParams(actionParams)
89587
+ ? this.makeNonProtocolAction(actionParams)
89588
+ : this.makeProtocolAction(actionParams);
89589
+ });
89590
+ }
89591
+ static makeProtocolAction({ wallet, assetAmount, memo }) {
89592
+ return __awaiter$8(this, void 0, void 0, function* () {
89593
+ const hash = yield wallet.deposit({
89594
+ chain: xchainMayachain.MAYAChain,
89595
+ asset: assetAmount.asset,
89596
+ amount: assetAmount.baseAmount,
89597
+ memo,
89598
+ });
89599
+ return {
89600
+ hash,
89601
+ url: yield wallet.getExplorerTxUrl(xchainMayachain.MAYAChain, hash),
89602
+ };
89603
+ });
89604
+ }
89605
+ static makeNonProtocolAction({ wallet, assetAmount, recipient, memo, }) {
89606
+ return __awaiter$8(this, void 0, void 0, function* () {
89607
+ // Non EVM actions
89608
+ if (!isProtocolEVMChain(assetAmount.asset.chain)) {
89609
+ if (isProtocolBFTChain(assetAmount.asset.chain)) {
89610
+ const hash = yield wallet.transfer({
89611
+ asset: assetAmount.asset,
89612
+ amount: assetAmount.baseAmount,
89613
+ recipient,
89614
+ memo,
89615
+ });
89616
+ return {
89617
+ hash,
89618
+ url: yield wallet.getExplorerTxUrl(assetAmount.asset.chain, hash),
89619
+ };
89620
+ }
89621
+ const feeRates = yield wallet.getFeeRates(assetAmount.asset.chain);
89622
+ const hash = yield wallet.transfer({
89623
+ asset: assetAmount.asset,
89624
+ amount: assetAmount.baseAmount,
89625
+ recipient,
89626
+ memo,
89627
+ feeRate: feeRates.fast,
89628
+ });
89629
+ return {
89630
+ hash,
89631
+ url: yield wallet.getExplorerTxUrl(assetAmount.asset.chain, hash),
89632
+ };
89633
+ }
89634
+ // EVM actions
89635
+ const mayachainQuery = new xchainMayachainQuery.MayachainQuery();
89636
+ const inboundDetails = yield mayachainQuery.getChainInboundDetails(assetAmount.asset.chain);
89637
+ if (!inboundDetails.router)
89638
+ throw Error(`Unknown router for ${assetAmount.asset.chain} chain`);
89639
+ const isERC20 = isProtocolERC20Asset(assetAmount.asset);
89640
+ const checkSummedContractAddress = isERC20
89641
+ ? ethers.ethers.utils.getAddress(getContractAddressFromAsset(assetAmount.asset))
89642
+ : ethers.ethers.constants.AddressZero;
89643
+ const expiration = Math.floor(new Date(new Date().getTime() + 15 * 60000).getTime() / 1000);
89644
+ const depositParams = [
89645
+ recipient,
89646
+ checkSummedContractAddress,
89647
+ assetAmount.baseAmount.amount().toFixed(),
89648
+ memo,
89649
+ expiration,
89650
+ ];
89651
+ const routerContract = new ethers.ethers.Contract(inboundDetails.router, abi.router);
89652
+ const gasPrices = yield wallet.getFeeRates(assetAmount.asset.chain);
89653
+ const unsignedTx = yield routerContract.populateTransaction.depositWithExpiry(...depositParams);
89654
+ const nativeAsset = wallet.getAssetInfo(assetAmount.asset.chain);
89655
+ const hash = yield wallet.transfer({
89656
+ asset: nativeAsset.asset,
89657
+ amount: isERC20 ? baseAmount(0, nativeAsset.decimal) : assetAmount.baseAmount,
89658
+ memo: unsignedTx.data,
89659
+ recipient: inboundDetails.router,
89660
+ gasPrice: gasPrices.fast,
89661
+ isMemoEncoded: true,
89662
+ gasLimit: ethers.ethers.BigNumber.from(160000),
89663
+ });
89664
+ return {
89665
+ hash,
89666
+ url: yield wallet.getExplorerTxUrl(assetAmount.asset.chain, hash),
89667
+ };
89668
+ });
89669
+ }
89670
+ static isNonProtocolParams(params) {
89671
+ if ((params.assetAmount.asset.chain === xchainMayachain.MAYAChain || params.assetAmount.asset.synth) &&
89672
+ 'address' in params &&
89673
+ !!params.address) {
89674
+ throw Error('Inconsistent params. Native actions do not support recipient');
89675
+ }
89676
+ return params.assetAmount.asset.chain !== xchainMayachain.MAYAChain && !params.assetAmount.asset.synth;
89677
+ }
89678
+ }
89679
+
89537
89680
  /**
89538
89681
  * Mayachain Automated Market Maker (AMM) class.
89539
89682
  * MAYAChainAMM class for interacting with THORChain.
@@ -89553,6 +89696,7 @@ class MayachainAMM {
89553
89696
  ETH: new xchainEthereum.Client(Object.assign(Object.assign({}, xchainEthereum.defaultEthParams), { network: xchainClient.Network.Mainnet })),
89554
89697
  DASH: new xchainDash.Client(Object.assign(Object.assign({}, xchainDash.defaultDashParams), { network: xchainClient.Network.Mainnet })),
89555
89698
  KUJI: new xchainKujira.Client(Object.assign(Object.assign({}, xchainKujira.defaultKujiParams), { network: xchainClient.Network.Mainnet })),
89699
+ ARB: new xchainArbitrum.Client(Object.assign(Object.assign({}, xchainArbitrum.defaultArbParams), { network: xchainClient.Network.Mainnet })),
89556
89700
  THOR: new xchainThorchain.Client({ network: xchainClient.Network.Mainnet }),
89557
89701
  MAYA: new xchainMayachain.Client({ network: xchainClient.Network.Mainnet }),
89558
89702
  })) {
@@ -89621,12 +89765,12 @@ class MayachainAMM {
89621
89765
  const errors = [];
89622
89766
  // Validate destination address if provided
89623
89767
  if (destinationAddress &&
89624
- !this.wallet.validateAddress(destinationAsset.synth ? xchainMayachain.MAYAChain : destinationAsset.chain, destinationAddress)) {
89768
+ !validateAddress(this.mayachainQuery.getNetwork(), destinationAsset.synth ? xchainMayachain.MAYAChain : destinationAsset.chain, destinationAddress)) {
89625
89769
  errors.push(`destinationAddress ${destinationAddress} is not a valid address`);
89626
89770
  }
89627
89771
  // Validate affiliate address if provided
89628
89772
  if (affiliateAddress) {
89629
- const isMayaAddress = this.wallet.validateAddress(xchainMayachain.MAYAChain, affiliateAddress);
89773
+ const isMayaAddress = validateAddress(this.mayachainQuery.getNetwork(), xchainMayachain.MAYAChain, affiliateAddress);
89630
89774
  const isMayaName = yield this.isMAYAName(affiliateAddress);
89631
89775
  if (!(isMayaAddress || isMayaName))
89632
89776
  errors.push(`affiliateAddress ${affiliateAddress} is not a valid MAYA address`);
@@ -89636,7 +89780,7 @@ class MayachainAMM {
89636
89780
  errors.push(`affiliateBps ${affiliateBps} out of range [0 - 10000]`);
89637
89781
  }
89638
89782
  // Validate approval if asset is an ERC20 token and fromAddress is provided
89639
- if (this.isERC20Asset(fromAsset) && fromAddress) {
89783
+ if (isProtocolERC20Asset(fromAsset) && fromAddress) {
89640
89784
  const approveErrors = yield this.isRouterApprovedToSpend({
89641
89785
  asset: fromAsset,
89642
89786
  address: fromAddress,
@@ -89667,10 +89811,12 @@ class MayachainAMM {
89667
89811
  // Check if the swap can be performed
89668
89812
  if (!quoteSwap.canSwap)
89669
89813
  throw Error(`Can not swap. ${quoteSwap.errors.join(' ')}`);
89670
- // Perform the swap based on the asset chain
89671
- return fromAsset.chain === xchainMayachain.MAYAChain || fromAsset.synth
89672
- ? this.doProtocolAssetSwap(amount, quoteSwap.memo)
89673
- : this.doNonProtocolAssetSwap(amount, quoteSwap.toAddress, quoteSwap.memo);
89814
+ return MayachainAction.makeAction({
89815
+ wallet: this.wallet,
89816
+ assetAmount: amount,
89817
+ memo: quoteSwap.memo,
89818
+ recipient: `${quoteSwap.toAddress}`,
89819
+ });
89674
89820
  });
89675
89821
  }
89676
89822
  /**
@@ -89713,108 +89859,35 @@ class MayachainAMM {
89713
89859
  });
89714
89860
  }
89715
89861
  /**
89716
- * Check if a name is a valid MAYAName
89717
- * @param {string} name MAYAName to check
89718
- * @returns {boolean} True if the name is registered as a MAYAName, otherwise false
89862
+ * Get MAYAname details
89863
+ * @param {string} MAYAName
89864
+ * @returns {MAYANameDetails | undefined} MAYANames details or undefined it is does not exist
89719
89865
  */
89720
- isMAYAName(name) {
89866
+ getMAYANameDetails(MAYAName) {
89721
89867
  return __awaiter$8(this, void 0, void 0, function* () {
89722
- return !!(yield this.mayachainQuery.getMAYANameDetails(name));
89868
+ return this.mayachainQuery.getMAYANameDetails(MAYAName);
89723
89869
  });
89724
89870
  }
89725
89871
  /**
89726
- * Perform a swap from a native protocol asset to any other asset
89727
- * @param {CryptoAmount} amount Amount to swap
89728
- * @param {string} memo Memo to add to the transaction
89729
- * @returns {TxSubmitted} Transaction hash and URL of the swap
89872
+ * Get the MAYANames owned by an address
89873
+ * @param {Address} owner - Thorchain address
89874
+ * @returns {MAYANameDetails[]} List of MAYANames owned by the address
89730
89875
  */
89731
- doProtocolAssetSwap(amount, memo) {
89876
+ getMAYANamesByOwner(owner) {
89732
89877
  return __awaiter$8(this, void 0, void 0, function* () {
89733
- // Deposit the amount and return transaction hash and URL
89734
- const hash = yield this.wallet.deposit({ chain: xchainMayachain.MAYAChain, asset: amount.asset, amount: amount.baseAmount, memo });
89735
- return {
89736
- hash,
89737
- url: yield this.wallet.getExplorerTxUrl(xchainMayachain.MAYAChain, hash),
89738
- };
89878
+ return this.mayachainQuery.getMAYANamesByOwner(owner);
89739
89879
  });
89740
89880
  }
89741
89881
  /**
89742
- * Perform a swap between assets
89743
- * @param {CryptoAmount} amount Amount to swap
89744
- * @param {string} memo Memo to add to the transaction to successfully make the swap
89745
- * @param {string} recipient inbound address to make swap transaction to
89746
- * @returns {TxSubmitted} Transaction hash and URL of the swap
89882
+ * Check if a name is a valid MAYAName
89883
+ * @param {string} name MAYAName to check
89884
+ * @returns {boolean} True if the name is registered as a MAYAName, otherwise false
89747
89885
  */
89748
- doNonProtocolAssetSwap(amount, recipient, memo) {
89886
+ isMAYAName(name) {
89749
89887
  return __awaiter$8(this, void 0, void 0, function* () {
89750
- // For non-EVM assets, perform a transfer and return transaction hash and URL
89751
- if (!this.isEVMChain(amount.asset.chain)) {
89752
- const hash = yield this.wallet.transfer({
89753
- asset: amount.asset,
89754
- amount: amount.baseAmount,
89755
- recipient,
89756
- memo,
89757
- });
89758
- return {
89759
- hash,
89760
- url: yield this.wallet.getExplorerTxUrl(amount.asset.chain, hash),
89761
- };
89762
- }
89763
- // For EVM assets, perform a deposit with expiry and return transaction hash and URL
89764
- const inboundDetails = yield this.mayachainQuery.getChainInboundDetails(amount.asset.chain);
89765
- if (!inboundDetails.router)
89766
- throw Error(`Unknown router for ${amount.asset.chain} chain`);
89767
- const isERC20 = this.isERC20Asset(amount.asset);
89768
- const checkSummedContractAddress = isERC20
89769
- ? ethers.ethers.utils.getAddress(getContractAddressFromAsset(amount.asset))
89770
- : ethers.ethers.constants.AddressZero;
89771
- const expiration = Math.floor(new Date(new Date().getTime() + 15 * 60000).getTime() / 1000);
89772
- const depositParams = [
89773
- recipient,
89774
- checkSummedContractAddress,
89775
- amount.baseAmount.amount().toFixed(),
89776
- memo,
89777
- expiration,
89778
- ];
89779
- const routerContract = new ethers.ethers.Contract(inboundDetails.router, abi.router);
89780
- const unsignedTx = yield routerContract.populateTransaction.depositWithExpiry(...depositParams);
89781
- const gasPrices = yield this.wallet.getGasFeeRates(amount.asset.chain);
89782
- const nativeAsset = this.wallet.getAssetInfo(amount.asset.chain);
89783
- const hash = yield this.wallet.transfer({
89784
- asset: nativeAsset.asset,
89785
- amount: isERC20 ? baseAmount(0, nativeAsset.decimal) : amount.baseAmount,
89786
- memo: unsignedTx.data,
89787
- recipient: inboundDetails.router,
89788
- gasPrice: gasPrices.fast,
89789
- isMemoEncoded: true,
89790
- gasLimit: ethers.ethers.BigNumber.from(160000),
89791
- });
89792
- return {
89793
- hash,
89794
- url: yield this.wallet.getExplorerTxUrl(amount.asset.chain, hash),
89795
- };
89888
+ return !!(yield this.mayachainQuery.getMAYANameDetails(name));
89796
89889
  });
89797
89890
  }
89798
- /**
89799
- * Check if the asset is an ERC20 token
89800
- * @param {Asset} asset Asset to check
89801
- * @returns True if the asset is an ERC20 token, otherwise false
89802
- */
89803
- isERC20Asset(asset) {
89804
- // Check if the asset's chain is an EVM chain and if the symbol matches AssetETH.symbol
89805
- return this.isEVMChain(asset.chain)
89806
- ? [xchainEthereum.AssetETH].findIndex((nativeEVMAsset) => eqAsset(nativeEVMAsset, asset)) === -1 && !asset.synth
89807
- : false;
89808
- }
89809
- /**
89810
- * Check if the chain is an EVM (Ethereum Virtual Machine) chain
89811
- * @param {Chain} chain Chain to check
89812
- * @returns True if the chain is an EVM chain, otherwise false
89813
- */
89814
- isEVMChain(chain) {
89815
- // Check if the chain matches AssetETH.chain
89816
- return [xchainEthereum.AssetETH.chain].includes(chain);
89817
- }
89818
89891
  }
89819
89892
 
89820
89893
  exports.MayachainAMM = MayachainAMM;
@@ -0,0 +1,21 @@
1
+ import { Address, CryptoAmount } from '@xchainjs/xchain-util';
2
+ import { Wallet } from '@xchainjs/xchain-wallet';
3
+ import { TxSubmitted } from './types';
4
+ export type NonProtocolActionParams = {
5
+ wallet: Wallet;
6
+ assetAmount: CryptoAmount;
7
+ recipient: Address;
8
+ memo: string;
9
+ };
10
+ export type ProtocolActionParams = {
11
+ wallet: Wallet;
12
+ assetAmount: CryptoAmount;
13
+ memo: string;
14
+ };
15
+ export type ActionParams = ProtocolActionParams | NonProtocolActionParams;
16
+ export declare class MayachainAction {
17
+ static makeAction(actionParams: ActionParams): Promise<TxSubmitted>;
18
+ private static makeProtocolAction;
19
+ private static makeNonProtocolAction;
20
+ private static isNonProtocolParams;
21
+ }
@@ -1,4 +1,5 @@
1
- import { MayachainQuery, QuoteSwap, QuoteSwapParams } from '@xchainjs/xchain-mayachain-query';
1
+ import { MAYANameDetails, MayachainQuery, QuoteSwap, QuoteSwapParams } from '@xchainjs/xchain-mayachain-query';
2
+ import { Address } from '@xchainjs/xchain-util';
2
3
  import { Wallet } from '@xchainjs/xchain-wallet';
3
4
  import { ApproveParams, IsApprovedParams, TxSubmitted } from './types';
4
5
  /**
@@ -51,36 +52,21 @@ export declare class MayachainAMM {
51
52
  */
52
53
  isRouterApprovedToSpend({ asset, amount, address }: IsApprovedParams): Promise<string[]>;
53
54
  /**
54
- * Check if a name is a valid MAYAName
55
- * @param {string} name MAYAName to check
56
- * @returns {boolean} True if the name is registered as a MAYAName, otherwise false
57
- */
58
- private isMAYAName;
59
- /**
60
- * Perform a swap from a native protocol asset to any other asset
61
- * @param {CryptoAmount} amount Amount to swap
62
- * @param {string} memo Memo to add to the transaction
63
- * @returns {TxSubmitted} Transaction hash and URL of the swap
55
+ * Get MAYAname details
56
+ * @param {string} MAYAName
57
+ * @returns {MAYANameDetails | undefined} MAYANames details or undefined it is does not exist
64
58
  */
65
- private doProtocolAssetSwap;
59
+ getMAYANameDetails(MAYAName: string): Promise<MAYANameDetails | undefined>;
66
60
  /**
67
- * Perform a swap between assets
68
- * @param {CryptoAmount} amount Amount to swap
69
- * @param {string} memo Memo to add to the transaction to successfully make the swap
70
- * @param {string} recipient inbound address to make swap transaction to
71
- * @returns {TxSubmitted} Transaction hash and URL of the swap
72
- */
73
- private doNonProtocolAssetSwap;
74
- /**
75
- * Check if the asset is an ERC20 token
76
- * @param {Asset} asset Asset to check
77
- * @returns True if the asset is an ERC20 token, otherwise false
61
+ * Get the MAYANames owned by an address
62
+ * @param {Address} owner - Thorchain address
63
+ * @returns {MAYANameDetails[]} List of MAYANames owned by the address
78
64
  */
79
- private isERC20Asset;
65
+ getMAYANamesByOwner(owner: Address): Promise<MAYANameDetails[]>;
80
66
  /**
81
- * Check if the chain is an EVM (Ethereum Virtual Machine) chain
82
- * @param {Chain} chain Chain to check
83
- * @returns True if the chain is an EVM chain, otherwise false
67
+ * Check if a name is a valid MAYAName
68
+ * @param {string} name MAYAName to check
69
+ * @returns {boolean} True if the name is registered as a MAYAName, otherwise false
84
70
  */
85
- private isEVMChain;
71
+ private isMAYAName;
86
72
  }
package/lib/utils.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { Network } from '@xchainjs/xchain-client';
2
+ import { Address, Asset, Chain } from '@xchainjs/xchain-util';
3
+ /**
4
+ * Check if a chain is EVM and supported by the protocol
5
+ * @param {Chain} chain to check
6
+ * @returns true if chain is EVM, otherwise, false
7
+ */
8
+ export declare const isProtocolEVMChain: (chain: Chain) => boolean;
9
+ /**
10
+ * Check if asset is ERC20
11
+ * @param {Asset} asset to check
12
+ * @returns true if asset is ERC20, otherwise, false
13
+ */
14
+ export declare const isProtocolERC20Asset: (asset: Asset) => boolean;
15
+ /**
16
+ * Check if a chain is EVM and supported by the protocol
17
+ * @param {Chain} chain to check
18
+ * @returns true if chain is EVM, otherwise, false
19
+ */
20
+ export declare const isProtocolBFTChain: (chain: Chain) => boolean;
21
+ export declare const validateAddress: (network: Network, chain: Chain, address: Address) => boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xchainjs/xchain-mayachain-amm",
3
- "version": "2.0.10",
3
+ "version": "2.0.11",
4
4
  "description": "module that exposes estimating & swapping cryptocurrency assets on mayachain",
5
5
  "keywords": [
6
6
  "MAYAChain",
@@ -36,13 +36,14 @@
36
36
  "url": "https://github.com/xchainjs/xchainjs-lib/issues"
37
37
  },
38
38
  "dependencies": {
39
+ "@xchainjs/xchain-arbitrum": "0.1.14",
39
40
  "@xchainjs/xchain-bitcoin": "0.23.19",
40
41
  "@xchainjs/xchain-client": "0.16.8",
41
42
  "@xchainjs/xchain-dash": "0.3.6",
42
43
  "@xchainjs/xchain-ethereum": "0.32.6",
43
44
  "@xchainjs/xchain-kujira": "0.1.21",
44
45
  "@xchainjs/xchain-mayachain": "1.0.11",
45
- "@xchainjs/xchain-mayachain-query": "0.1.17",
46
+ "@xchainjs/xchain-mayachain-query": "0.1.18",
46
47
  "@xchainjs/xchain-thorchain": "1.1.1",
47
48
  "@xchainjs/xchain-wallet": "0.1.19",
48
49
  "ethers": "5.7.2"