@xchainjs/xchain-mayachain-amm 2.0.9 → 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
  /******************************************************************************
@@ -8915,6 +8916,7 @@ var AssetCurrencySymbol;
8915
8916
  AssetCurrencySymbol["DASH"] = "\u0110";
8916
8917
  AssetCurrencySymbol["LTC"] = "\u0141";
8917
8918
  AssetCurrencySymbol["DOGE"] = "\u00D0";
8919
+ AssetCurrencySymbol["CACAO"] = "\uD800\uDF02";
8918
8920
  })(AssetCurrencySymbol || (AssetCurrencySymbol = {}));
8919
8921
  /**
8920
8922
  * Formats an asset amount using its currency symbol
@@ -89525,6 +89527,148 @@ const abi = {
89525
89527
  erc20: erc20ABI, // ERC20 ABI
89526
89528
  };
89527
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
+
89528
89672
  /**
89529
89673
  * Mayachain Automated Market Maker (AMM) class.
89530
89674
  * MAYAChainAMM class for interacting with THORChain.
@@ -89540,12 +89684,13 @@ class MayachainAMM {
89540
89684
  * @returns {MayachainAMM} Returns the MayachainAMM instance.
89541
89685
  */
89542
89686
  constructor(mayachainQuery = new MayachainQuery(), wallet = new Wallet$1({
89543
- BTC: new Client(Object.assign(Object.assign({}, defaultBTCParams), { network: Network.Mainnet })),
89544
- ETH: new Client$1(Object.assign(Object.assign({}, defaultEthParams), { network: Network.Mainnet })),
89545
- DASH: new Client$2(Object.assign(Object.assign({}, defaultDashParams), { network: Network.Mainnet })),
89546
- KUJI: new Client$3(Object.assign(Object.assign({}, defaultKujiParams), { network: Network.Mainnet })),
89547
- THOR: new Client$4({ network: Network.Mainnet }),
89548
- 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 }),
89549
89694
  })) {
89550
89695
  this.mayachainQuery = mayachainQuery;
89551
89696
  this.wallet = wallet;
@@ -89612,12 +89757,12 @@ class MayachainAMM {
89612
89757
  const errors = [];
89613
89758
  // Validate destination address if provided
89614
89759
  if (destinationAddress &&
89615
- !this.wallet.validateAddress(destinationAsset.synth ? MAYAChain : destinationAsset.chain, destinationAddress)) {
89760
+ !validateAddress(this.mayachainQuery.getNetwork(), destinationAsset.synth ? MAYAChain : destinationAsset.chain, destinationAddress)) {
89616
89761
  errors.push(`destinationAddress ${destinationAddress} is not a valid address`);
89617
89762
  }
89618
89763
  // Validate affiliate address if provided
89619
89764
  if (affiliateAddress) {
89620
- const isMayaAddress = this.wallet.validateAddress(MAYAChain, affiliateAddress);
89765
+ const isMayaAddress = validateAddress(this.mayachainQuery.getNetwork(), MAYAChain, affiliateAddress);
89621
89766
  const isMayaName = yield this.isMAYAName(affiliateAddress);
89622
89767
  if (!(isMayaAddress || isMayaName))
89623
89768
  errors.push(`affiliateAddress ${affiliateAddress} is not a valid MAYA address`);
@@ -89627,7 +89772,7 @@ class MayachainAMM {
89627
89772
  errors.push(`affiliateBps ${affiliateBps} out of range [0 - 10000]`);
89628
89773
  }
89629
89774
  // Validate approval if asset is an ERC20 token and fromAddress is provided
89630
- if (this.isERC20Asset(fromAsset) && fromAddress) {
89775
+ if (isProtocolERC20Asset(fromAsset) && fromAddress) {
89631
89776
  const approveErrors = yield this.isRouterApprovedToSpend({
89632
89777
  asset: fromAsset,
89633
89778
  address: fromAddress,
@@ -89658,10 +89803,12 @@ class MayachainAMM {
89658
89803
  // Check if the swap can be performed
89659
89804
  if (!quoteSwap.canSwap)
89660
89805
  throw Error(`Can not swap. ${quoteSwap.errors.join(' ')}`);
89661
- // Perform the swap based on the asset chain
89662
- return fromAsset.chain === MAYAChain || fromAsset.synth
89663
- ? this.doProtocolAssetSwap(amount, quoteSwap.memo)
89664
- : 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
+ });
89665
89812
  });
89666
89813
  }
89667
89814
  /**
@@ -89704,108 +89851,35 @@ class MayachainAMM {
89704
89851
  });
89705
89852
  }
89706
89853
  /**
89707
- * Check if a name is a valid MAYAName
89708
- * @param {string} name MAYAName to check
89709
- * @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
89710
89857
  */
89711
- isMAYAName(name) {
89858
+ getMAYANameDetails(MAYAName) {
89712
89859
  return __awaiter$8(this, void 0, void 0, function* () {
89713
- return !!(yield this.mayachainQuery.getMAYANameDetails(name));
89860
+ return this.mayachainQuery.getMAYANameDetails(MAYAName);
89714
89861
  });
89715
89862
  }
89716
89863
  /**
89717
- * Perform a swap from a native protocol asset to any other asset
89718
- * @param {CryptoAmount} amount Amount to swap
89719
- * @param {string} memo Memo to add to the transaction
89720
- * @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
89721
89867
  */
89722
- doProtocolAssetSwap(amount, memo) {
89868
+ getMAYANamesByOwner(owner) {
89723
89869
  return __awaiter$8(this, void 0, void 0, function* () {
89724
- // Deposit the amount and return transaction hash and URL
89725
- const hash = yield this.wallet.deposit({ chain: MAYAChain, asset: amount.asset, amount: amount.baseAmount, memo });
89726
- return {
89727
- hash,
89728
- url: yield this.wallet.getExplorerTxUrl(MAYAChain, hash),
89729
- };
89870
+ return this.mayachainQuery.getMAYANamesByOwner(owner);
89730
89871
  });
89731
89872
  }
89732
89873
  /**
89733
- * Perform a swap between assets
89734
- * @param {CryptoAmount} amount Amount to swap
89735
- * @param {string} memo Memo to add to the transaction to successfully make the swap
89736
- * @param {string} recipient inbound address to make swap transaction to
89737
- * @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
89738
89877
  */
89739
- doNonProtocolAssetSwap(amount, recipient, memo) {
89878
+ isMAYAName(name) {
89740
89879
  return __awaiter$8(this, void 0, void 0, function* () {
89741
- // For non-EVM assets, perform a transfer and return transaction hash and URL
89742
- if (!this.isEVMChain(amount.asset.chain)) {
89743
- const hash = yield this.wallet.transfer({
89744
- asset: amount.asset,
89745
- amount: amount.baseAmount,
89746
- recipient,
89747
- memo,
89748
- });
89749
- return {
89750
- hash,
89751
- url: yield this.wallet.getExplorerTxUrl(amount.asset.chain, hash),
89752
- };
89753
- }
89754
- // For EVM assets, perform a deposit with expiry and return transaction hash and URL
89755
- const inboundDetails = yield this.mayachainQuery.getChainInboundDetails(amount.asset.chain);
89756
- if (!inboundDetails.router)
89757
- throw Error(`Unknown router for ${amount.asset.chain} chain`);
89758
- const isERC20 = this.isERC20Asset(amount.asset);
89759
- const checkSummedContractAddress = isERC20
89760
- ? ethers.utils.getAddress(getContractAddressFromAsset(amount.asset))
89761
- : ethers.constants.AddressZero;
89762
- const expiration = Math.floor(new Date(new Date().getTime() + 15 * 60000).getTime() / 1000);
89763
- const depositParams = [
89764
- recipient,
89765
- checkSummedContractAddress,
89766
- amount.baseAmount.amount().toFixed(),
89767
- memo,
89768
- expiration,
89769
- ];
89770
- const routerContract = new ethers.Contract(inboundDetails.router, abi.router);
89771
- const unsignedTx = yield routerContract.populateTransaction.depositWithExpiry(...depositParams);
89772
- const gasPrices = yield this.wallet.getGasFeeRates(amount.asset.chain);
89773
- const nativeAsset = this.wallet.getAssetInfo(amount.asset.chain);
89774
- const hash = yield this.wallet.transfer({
89775
- asset: nativeAsset.asset,
89776
- amount: isERC20 ? baseAmount(0, nativeAsset.decimal) : amount.baseAmount,
89777
- memo: unsignedTx.data,
89778
- recipient: inboundDetails.router,
89779
- gasPrice: gasPrices.fast,
89780
- isMemoEncoded: true,
89781
- gasLimit: ethers.BigNumber.from(160000),
89782
- });
89783
- return {
89784
- hash,
89785
- url: yield this.wallet.getExplorerTxUrl(amount.asset.chain, hash),
89786
- };
89880
+ return !!(yield this.mayachainQuery.getMAYANameDetails(name));
89787
89881
  });
89788
89882
  }
89789
- /**
89790
- * Check if the asset is an ERC20 token
89791
- * @param {Asset} asset Asset to check
89792
- * @returns True if the asset is an ERC20 token, otherwise false
89793
- */
89794
- isERC20Asset(asset) {
89795
- // Check if the asset's chain is an EVM chain and if the symbol matches AssetETH.symbol
89796
- return this.isEVMChain(asset.chain)
89797
- ? [AssetETH$1].findIndex((nativeEVMAsset) => eqAsset(nativeEVMAsset, asset)) === -1 && !asset.synth
89798
- : false;
89799
- }
89800
- /**
89801
- * Check if the chain is an EVM (Ethereum Virtual Machine) chain
89802
- * @param {Chain} chain Chain to check
89803
- * @returns True if the chain is an EVM chain, otherwise false
89804
- */
89805
- isEVMChain(chain) {
89806
- // Check if the chain matches AssetETH.chain
89807
- return [AssetETH$1.chain].includes(chain);
89808
- }
89809
89883
  }
89810
89884
 
89811
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');
@@ -8923,6 +8924,7 @@ var AssetCurrencySymbol;
8923
8924
  AssetCurrencySymbol["DASH"] = "\u0110";
8924
8925
  AssetCurrencySymbol["LTC"] = "\u0141";
8925
8926
  AssetCurrencySymbol["DOGE"] = "\u00D0";
8927
+ AssetCurrencySymbol["CACAO"] = "\uD800\uDF02";
8926
8928
  })(AssetCurrencySymbol || (AssetCurrencySymbol = {}));
8927
8929
  /**
8928
8930
  * Formats an asset amount using its currency symbol
@@ -89533,6 +89535,148 @@ const abi = {
89533
89535
  erc20: erc20ABI, // ERC20 ABI
89534
89536
  };
89535
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
+
89536
89680
  /**
89537
89681
  * Mayachain Automated Market Maker (AMM) class.
89538
89682
  * MAYAChainAMM class for interacting with THORChain.
@@ -89552,6 +89696,7 @@ class MayachainAMM {
89552
89696
  ETH: new xchainEthereum.Client(Object.assign(Object.assign({}, xchainEthereum.defaultEthParams), { network: xchainClient.Network.Mainnet })),
89553
89697
  DASH: new xchainDash.Client(Object.assign(Object.assign({}, xchainDash.defaultDashParams), { network: xchainClient.Network.Mainnet })),
89554
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 })),
89555
89700
  THOR: new xchainThorchain.Client({ network: xchainClient.Network.Mainnet }),
89556
89701
  MAYA: new xchainMayachain.Client({ network: xchainClient.Network.Mainnet }),
89557
89702
  })) {
@@ -89620,12 +89765,12 @@ class MayachainAMM {
89620
89765
  const errors = [];
89621
89766
  // Validate destination address if provided
89622
89767
  if (destinationAddress &&
89623
- !this.wallet.validateAddress(destinationAsset.synth ? xchainMayachain.MAYAChain : destinationAsset.chain, destinationAddress)) {
89768
+ !validateAddress(this.mayachainQuery.getNetwork(), destinationAsset.synth ? xchainMayachain.MAYAChain : destinationAsset.chain, destinationAddress)) {
89624
89769
  errors.push(`destinationAddress ${destinationAddress} is not a valid address`);
89625
89770
  }
89626
89771
  // Validate affiliate address if provided
89627
89772
  if (affiliateAddress) {
89628
- const isMayaAddress = this.wallet.validateAddress(xchainMayachain.MAYAChain, affiliateAddress);
89773
+ const isMayaAddress = validateAddress(this.mayachainQuery.getNetwork(), xchainMayachain.MAYAChain, affiliateAddress);
89629
89774
  const isMayaName = yield this.isMAYAName(affiliateAddress);
89630
89775
  if (!(isMayaAddress || isMayaName))
89631
89776
  errors.push(`affiliateAddress ${affiliateAddress} is not a valid MAYA address`);
@@ -89635,7 +89780,7 @@ class MayachainAMM {
89635
89780
  errors.push(`affiliateBps ${affiliateBps} out of range [0 - 10000]`);
89636
89781
  }
89637
89782
  // Validate approval if asset is an ERC20 token and fromAddress is provided
89638
- if (this.isERC20Asset(fromAsset) && fromAddress) {
89783
+ if (isProtocolERC20Asset(fromAsset) && fromAddress) {
89639
89784
  const approveErrors = yield this.isRouterApprovedToSpend({
89640
89785
  asset: fromAsset,
89641
89786
  address: fromAddress,
@@ -89666,10 +89811,12 @@ class MayachainAMM {
89666
89811
  // Check if the swap can be performed
89667
89812
  if (!quoteSwap.canSwap)
89668
89813
  throw Error(`Can not swap. ${quoteSwap.errors.join(' ')}`);
89669
- // Perform the swap based on the asset chain
89670
- return fromAsset.chain === xchainMayachain.MAYAChain || fromAsset.synth
89671
- ? this.doProtocolAssetSwap(amount, quoteSwap.memo)
89672
- : 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
+ });
89673
89820
  });
89674
89821
  }
89675
89822
  /**
@@ -89712,108 +89859,35 @@ class MayachainAMM {
89712
89859
  });
89713
89860
  }
89714
89861
  /**
89715
- * Check if a name is a valid MAYAName
89716
- * @param {string} name MAYAName to check
89717
- * @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
89718
89865
  */
89719
- isMAYAName(name) {
89866
+ getMAYANameDetails(MAYAName) {
89720
89867
  return __awaiter$8(this, void 0, void 0, function* () {
89721
- return !!(yield this.mayachainQuery.getMAYANameDetails(name));
89868
+ return this.mayachainQuery.getMAYANameDetails(MAYAName);
89722
89869
  });
89723
89870
  }
89724
89871
  /**
89725
- * Perform a swap from a native protocol asset to any other asset
89726
- * @param {CryptoAmount} amount Amount to swap
89727
- * @param {string} memo Memo to add to the transaction
89728
- * @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
89729
89875
  */
89730
- doProtocolAssetSwap(amount, memo) {
89876
+ getMAYANamesByOwner(owner) {
89731
89877
  return __awaiter$8(this, void 0, void 0, function* () {
89732
- // Deposit the amount and return transaction hash and URL
89733
- const hash = yield this.wallet.deposit({ chain: xchainMayachain.MAYAChain, asset: amount.asset, amount: amount.baseAmount, memo });
89734
- return {
89735
- hash,
89736
- url: yield this.wallet.getExplorerTxUrl(xchainMayachain.MAYAChain, hash),
89737
- };
89878
+ return this.mayachainQuery.getMAYANamesByOwner(owner);
89738
89879
  });
89739
89880
  }
89740
89881
  /**
89741
- * Perform a swap between assets
89742
- * @param {CryptoAmount} amount Amount to swap
89743
- * @param {string} memo Memo to add to the transaction to successfully make the swap
89744
- * @param {string} recipient inbound address to make swap transaction to
89745
- * @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
89746
89885
  */
89747
- doNonProtocolAssetSwap(amount, recipient, memo) {
89886
+ isMAYAName(name) {
89748
89887
  return __awaiter$8(this, void 0, void 0, function* () {
89749
- // For non-EVM assets, perform a transfer and return transaction hash and URL
89750
- if (!this.isEVMChain(amount.asset.chain)) {
89751
- const hash = yield this.wallet.transfer({
89752
- asset: amount.asset,
89753
- amount: amount.baseAmount,
89754
- recipient,
89755
- memo,
89756
- });
89757
- return {
89758
- hash,
89759
- url: yield this.wallet.getExplorerTxUrl(amount.asset.chain, hash),
89760
- };
89761
- }
89762
- // For EVM assets, perform a deposit with expiry and return transaction hash and URL
89763
- const inboundDetails = yield this.mayachainQuery.getChainInboundDetails(amount.asset.chain);
89764
- if (!inboundDetails.router)
89765
- throw Error(`Unknown router for ${amount.asset.chain} chain`);
89766
- const isERC20 = this.isERC20Asset(amount.asset);
89767
- const checkSummedContractAddress = isERC20
89768
- ? ethers.ethers.utils.getAddress(getContractAddressFromAsset(amount.asset))
89769
- : ethers.ethers.constants.AddressZero;
89770
- const expiration = Math.floor(new Date(new Date().getTime() + 15 * 60000).getTime() / 1000);
89771
- const depositParams = [
89772
- recipient,
89773
- checkSummedContractAddress,
89774
- amount.baseAmount.amount().toFixed(),
89775
- memo,
89776
- expiration,
89777
- ];
89778
- const routerContract = new ethers.ethers.Contract(inboundDetails.router, abi.router);
89779
- const unsignedTx = yield routerContract.populateTransaction.depositWithExpiry(...depositParams);
89780
- const gasPrices = yield this.wallet.getGasFeeRates(amount.asset.chain);
89781
- const nativeAsset = this.wallet.getAssetInfo(amount.asset.chain);
89782
- const hash = yield this.wallet.transfer({
89783
- asset: nativeAsset.asset,
89784
- amount: isERC20 ? baseAmount(0, nativeAsset.decimal) : amount.baseAmount,
89785
- memo: unsignedTx.data,
89786
- recipient: inboundDetails.router,
89787
- gasPrice: gasPrices.fast,
89788
- isMemoEncoded: true,
89789
- gasLimit: ethers.ethers.BigNumber.from(160000),
89790
- });
89791
- return {
89792
- hash,
89793
- url: yield this.wallet.getExplorerTxUrl(amount.asset.chain, hash),
89794
- };
89888
+ return !!(yield this.mayachainQuery.getMAYANameDetails(name));
89795
89889
  });
89796
89890
  }
89797
- /**
89798
- * Check if the asset is an ERC20 token
89799
- * @param {Asset} asset Asset to check
89800
- * @returns True if the asset is an ERC20 token, otherwise false
89801
- */
89802
- isERC20Asset(asset) {
89803
- // Check if the asset's chain is an EVM chain and if the symbol matches AssetETH.symbol
89804
- return this.isEVMChain(asset.chain)
89805
- ? [xchainEthereum.AssetETH].findIndex((nativeEVMAsset) => eqAsset(nativeEVMAsset, asset)) === -1 && !asset.synth
89806
- : false;
89807
- }
89808
- /**
89809
- * Check if the chain is an EVM (Ethereum Virtual Machine) chain
89810
- * @param {Chain} chain Chain to check
89811
- * @returns True if the chain is an EVM chain, otherwise false
89812
- */
89813
- isEVMChain(chain) {
89814
- // Check if the chain matches AssetETH.chain
89815
- return [xchainEthereum.AssetETH.chain].includes(chain);
89816
- }
89817
89891
  }
89818
89892
 
89819
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.9",
3
+ "version": "2.0.11",
4
4
  "description": "module that exposes estimating & swapping cryptocurrency assets on mayachain",
5
5
  "keywords": [
6
6
  "MAYAChain",
@@ -36,15 +36,16 @@
36
36
  "url": "https://github.com/xchainjs/xchainjs-lib/issues"
37
37
  },
38
38
  "dependencies": {
39
- "@xchainjs/xchain-bitcoin": "0.23.18",
40
- "@xchainjs/xchain-client": "0.16.7",
41
- "@xchainjs/xchain-dash": "0.3.5",
42
- "@xchainjs/xchain-ethereum": "0.32.5",
43
- "@xchainjs/xchain-kujira": "0.1.20",
44
- "@xchainjs/xchain-mayachain": "1.0.10",
45
- "@xchainjs/xchain-mayachain-query": "0.1.16",
46
- "@xchainjs/xchain-thorchain": "1.1.0",
47
- "@xchainjs/xchain-wallet": "0.1.18",
39
+ "@xchainjs/xchain-arbitrum": "0.1.14",
40
+ "@xchainjs/xchain-bitcoin": "0.23.19",
41
+ "@xchainjs/xchain-client": "0.16.8",
42
+ "@xchainjs/xchain-dash": "0.3.6",
43
+ "@xchainjs/xchain-ethereum": "0.32.6",
44
+ "@xchainjs/xchain-kujira": "0.1.21",
45
+ "@xchainjs/xchain-mayachain": "1.0.11",
46
+ "@xchainjs/xchain-mayachain-query": "0.1.18",
47
+ "@xchainjs/xchain-thorchain": "1.1.1",
48
+ "@xchainjs/xchain-wallet": "0.1.19",
48
49
  "ethers": "5.7.2"
49
50
  },
50
51
  "devDependencies": {