@xchainjs/xchain-mayachain-amm 2.0.10 → 2.0.12

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
@@ -1,70 +1,73 @@
1
- # `@xchainjs/xchain-mayachain-amm`
1
+ <div align="center">
2
+ <h1 align="center">MAYAChain AMM</h1>
2
3
 
3
- ## Modules
4
+ <p align="center">
5
+ <a href='https://www.npmjs.com/package/@xchainjs/xchain-mayachain-amm' target='_blank'>
6
+ <img alt="NPM Version" src="https://img.shields.io/npm/v/%40xchainjs%2Fxchain-mayachain-amm" />
7
+ </a>
8
+ <a href='https://www.npmjs.com/package/@xchainjs/xchain-mayachain-amm' target='_blank'>
9
+ <img alt="NPM Downloads" src="https://img.shields.io/npm/d18m/%40xchainjs%2Fxchain-mayachain-amm" />
10
+ </a>
11
+ </p>
12
+ </div>
4
13
 
5
- Mayachain AMM module
14
+ <br />
15
+
16
+
17
+ MAYAChain AMM is a module that allows users and developers to interact with the MAYAChain protocol without having to worry about the underlining particularities of the protocol.
6
18
 
7
19
  ## Installation
8
20
 
9
- ```
21
+ ```sh
10
22
  yarn add @xchainjs/xchain-mayachain-amm
11
23
  ```
12
24
 
13
- Following peer dependencies have to be installed into your project. These are not included in `@xchainjs/xchain-mayachain-amm`.
14
-
15
- ```bash
16
- "dependencies": {
17
- "@cosmos-client/core": "0.46.1",
18
- "@psf/bitcoincashjs-lib": "^4.0.3",
19
- "@xchainjs/xchain-bitcoin": "^0.23.6",
20
- "@xchainjs/xchain-client": "^0.15.4",
21
- "@xchainjs/xchain-cosmos-sdk": "^0.1.3",
22
- "@xchainjs/xchain-crypto": "^0.3.0",
23
- "@xchainjs/xchain-dash": "^0.2.5",
24
- "@xchainjs/xchain-ethereum": "^0.31.1",
25
- "@xchainjs/xchain-mayanode": "^0.1.2",
26
- "@xchainjs/xchain-mayachain-query": "^0.1.0",
27
- "@xchainjs/xchain-evm": "^0.4.1",
28
- "@xchainjs/xchain-kujira": "^0.1.2",
29
- "@xchainjs/xchain-mayachain": "^0.2.10",
30
- "@xchainjs/xchain-mayamidgard": "^0.1.0",
31
- "@xchainjs/xchain-thorchain": "^0.28.13",
32
- "@xchainjs/xchain-util": "^0.13.1",
33
- "@xchainjs/xchain-utxo-providers": "^0.2.8",
34
- "axios": "^1.3.6",
35
- "axios-retry": "^3.2.5",
36
- "bignumber.js": "^9.0.0",
37
- "bitcoinjs-lib": "5.2.0",
38
- "coininfo": "^5.1.0",
39
- "coinselect": "^3.1.12",
40
- "dotenv": "^16.0.0",
41
- "ethers": "^5.6.6",
42
- "wif": "^2.0.6"
43
- }
25
+ or
44
26
 
45
- ```
27
+ ```sh
28
+ npm install @xchainjs/xchain-mayachain-amm
46
29
 
47
- ## For live examples
30
+ ```
48
31
 
32
+ ## Initialization
49
33
 
50
- ## Documentation
34
+ Using Mayachain AMM, you can initialize the main class of the module in consultation mode if you do not provide any parameters, this means you could retrieve information from the protocol, but you will not be able to make actions the protocol needs you to sign.
51
35
 
36
+ ```ts
37
+ import { MayachainAMM } from '@xchainjs/xchain-mayachain-amm'
52
38
 
53
- ## Compiler options
39
+ const mayachainAmm = new MayachainAMM()
40
+ ```
54
41
 
55
- tsconfig compiler options
42
+ Otherwise, if you want to be able make actions the protocol needs you to sign, you will need to initialize the main class of the protocol as follows
56
43
 
57
44
  ```ts
58
- {
59
- "compilerOptions": {
60
- "module":"commonjs",
61
- "target": "es5",
62
- "noEmitOnError": true,
63
- "resolveJsonModule": true,
64
- "esModuleInterop": true,
65
- "lib": ["es6", "dom", "es2016", "es2017"]
66
- }
67
- }
45
+ import { MayachainAMM } from '@xchainjs/xchain-mayachain-amm'
46
+ import { MayachainQuery } from '@xchainjs/xchain-mayachain-query'
47
+ import { Wallet } from '@xchainjs/xchain-wallet'
48
+
49
+ const mayaChainQuery = new MayachainQuery()
50
+ const wallet = new Wallet({
51
+ // Your XChainJS clients
52
+ })
53
+ const mayachainAmm = new MayachainAMM(mayaChainQuery, wallet)
68
54
  ```
69
55
 
70
- ### Setting environment variables
56
+ ## Features
57
+
58
+ Using MAYAChain AMM package, you could easily implement the following features
59
+
60
+ ### Swaps
61
+
62
+ - Estimate swaps
63
+ - Do swaps
64
+ - Approve MAYAChain router to spend to be able to do ERC-20 swaps
65
+
66
+ ### MAYANames
67
+
68
+ - Get MAYAName details
69
+ - Get MAYANames by owner
70
+
71
+ ## Examples
72
+
73
+ You can find examples using the MAYAChain AMM package in the [mayachain-amm](https://github.com/xchainjs/xchainjs-lib/tree/master/examples/mayachain-amm) examples folder.
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, AssetCacao, CACAO_DECIMAL } 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,107 +89851,147 @@ 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
+ * Estimate the cost of a MAYAName registration
89875
+ * @param {RegisterMAYAName} params Params to make the registration
89876
+ * @returns {QuoteMAYAName} Memo to make the registration and the estimation of the operation
89739
89877
  */
89740
- doNonProtocolAssetSwap(amount, recipient, memo) {
89878
+ estimateMAYANameRegistration(params) {
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
- });
89880
+ const errors = [];
89881
+ if (!validateAddress(this.mayachainQuery.getNetwork(), params.chain, params.chainAddress)) {
89882
+ errors.push(`Invalid address ${params.chainAddress} for ${params.chain} chain`);
89883
+ }
89884
+ if (!validateAddress(this.mayachainQuery.getNetwork(), MAYAChain, params.owner)) {
89885
+ errors.push(`Invalid owner ${params.owner} due it is not a MAYAChain address`);
89886
+ }
89887
+ if (errors.length) {
89750
89888
  return {
89751
- hash,
89752
- url: yield this.wallet.getExplorerTxUrl(amount.asset.chain, hash),
89889
+ memo: '',
89890
+ errors,
89891
+ value: new CryptoAmount(baseAmount(0, CACAO_DECIMAL), AssetCacao),
89892
+ allowed: false,
89753
89893
  };
89754
89894
  }
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),
89895
+ try {
89896
+ const estimated = yield this.mayachainQuery.estimateMAYAName(Object.assign({}, params));
89897
+ return Object.assign(Object.assign({}, estimated), { allowed: true, errors: [] });
89898
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
89899
+ }
89900
+ catch (e) {
89901
+ return {
89902
+ memo: '',
89903
+ errors: ['message' in e ? e.message : `Unknown error: ${e}`],
89904
+ value: new CryptoAmount(baseAmount(0, CACAO_DECIMAL), AssetCacao),
89905
+ allowed: false,
89906
+ };
89907
+ }
89908
+ });
89909
+ }
89910
+ /**
89911
+ * Estimate the cost of an update of a MAYAName
89912
+ * @param {QuoteMAYANameParams} params Params to make the update
89913
+ * @returns {QuoteMAYAName} Memo to make the update and the estimation of the operation
89914
+ */
89915
+ estimateMAYANameUpdate(params) {
89916
+ return __awaiter$8(this, void 0, void 0, function* () {
89917
+ const errors = [];
89918
+ if ((params.chain && !params.chainAddress) || (!params.chain && params.chainAddress)) {
89919
+ errors.push(`Alias not provided correctly`);
89920
+ }
89921
+ if (params.chain &&
89922
+ params.chainAddress &&
89923
+ !validateAddress(this.mayachainQuery.getNetwork(), params.chain, params.chainAddress)) {
89924
+ errors.push(`Invalid alias ${params.chainAddress} for ${params.chain} chain`);
89925
+ }
89926
+ if (params.owner && !validateAddress(this.mayachainQuery.getNetwork(), MAYAChain, params.owner)) {
89927
+ errors.push(`Invalid owner ${params.owner} due it is not a MAYAChain address`);
89928
+ }
89929
+ if (errors.length) {
89930
+ return {
89931
+ memo: '',
89932
+ errors,
89933
+ value: new CryptoAmount(baseAmount(0, CACAO_DECIMAL), AssetCacao),
89934
+ allowed: false,
89935
+ };
89936
+ }
89937
+ try {
89938
+ const estimated = yield this.mayachainQuery.estimateMAYAName(Object.assign(Object.assign({}, params), { isUpdate: true }));
89939
+ return Object.assign(Object.assign({}, estimated), { allowed: true, errors: [] });
89940
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
89941
+ }
89942
+ catch (e) {
89943
+ return {
89944
+ memo: '',
89945
+ errors: ['message' in e ? e.message : `Unknown error: ${e}`],
89946
+ value: new CryptoAmount(baseAmount(0, CACAO_DECIMAL), AssetCacao),
89947
+ allowed: false,
89948
+ };
89949
+ }
89950
+ });
89951
+ }
89952
+ /**
89953
+ * Register a MAYAName
89954
+ * @param {RegisterMAYAName} params Params to make the registration
89955
+ * @returns {QuoteMAYAName} Memo to make the update or the registration and the estimation of the operation
89956
+ */
89957
+ registerMAYAName(params) {
89958
+ return __awaiter$8(this, void 0, void 0, function* () {
89959
+ const quote = yield this.estimateMAYANameRegistration(params);
89960
+ if (!quote.allowed)
89961
+ throw Error(`Can not register MAYAName. ${quote.errors.join(' ')}`);
89962
+ return MayachainAction.makeAction({
89963
+ wallet: this.wallet,
89964
+ assetAmount: quote.value,
89965
+ memo: quote.memo,
89783
89966
  });
89784
- return {
89785
- hash,
89786
- url: yield this.wallet.getExplorerTxUrl(amount.asset.chain, hash),
89787
- };
89788
89967
  });
89789
89968
  }
89790
89969
  /**
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
89970
+ * Update a MAYAName
89971
+ * @param {QuoteMAYANameParams} params Params to make the update
89972
+ * @returns {QuoteMAYAName} Memo to make the update or the registration and the estimation of the operation
89794
89973
  */
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;
89974
+ updateMAYAName(params) {
89975
+ return __awaiter$8(this, void 0, void 0, function* () {
89976
+ const quote = yield this.estimateMAYANameUpdate(params);
89977
+ if (!quote.allowed)
89978
+ throw Error(`Can not update MAYAName. ${quote.errors.join(' ')}`);
89979
+ return MayachainAction.makeAction({
89980
+ wallet: this.wallet,
89981
+ assetAmount: quote.value,
89982
+ memo: quote.memo,
89983
+ });
89984
+ });
89800
89985
  }
89801
89986
  /**
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
89987
+ * Check if a name is a valid MAYAName
89988
+ * @param {string} name MAYAName to check
89989
+ * @returns {boolean} True if the name is registered as a MAYAName, otherwise false
89805
89990
  */
89806
- isEVMChain(chain) {
89807
- // Check if the chain matches AssetETH.chain
89808
- return [AssetETH$1.chain].includes(chain);
89991
+ isMAYAName(name) {
89992
+ return __awaiter$8(this, void 0, void 0, function* () {
89993
+ return !!(yield this.mayachainQuery.getMAYANameDetails(name));
89994
+ });
89809
89995
  }
89810
89996
  }
89811
89997
 
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,107 +89859,147 @@ 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
+ * Estimate the cost of a MAYAName registration
89883
+ * @param {RegisterMAYAName} params Params to make the registration
89884
+ * @returns {QuoteMAYAName} Memo to make the registration and the estimation of the operation
89747
89885
  */
89748
- doNonProtocolAssetSwap(amount, recipient, memo) {
89886
+ estimateMAYANameRegistration(params) {
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
- });
89888
+ const errors = [];
89889
+ if (!validateAddress(this.mayachainQuery.getNetwork(), params.chain, params.chainAddress)) {
89890
+ errors.push(`Invalid address ${params.chainAddress} for ${params.chain} chain`);
89891
+ }
89892
+ if (!validateAddress(this.mayachainQuery.getNetwork(), xchainMayachain.MAYAChain, params.owner)) {
89893
+ errors.push(`Invalid owner ${params.owner} due it is not a MAYAChain address`);
89894
+ }
89895
+ if (errors.length) {
89758
89896
  return {
89759
- hash,
89760
- url: yield this.wallet.getExplorerTxUrl(amount.asset.chain, hash),
89897
+ memo: '',
89898
+ errors,
89899
+ value: new CryptoAmount(baseAmount(0, xchainMayachain.CACAO_DECIMAL), xchainMayachain.AssetCacao),
89900
+ allowed: false,
89761
89901
  };
89762
89902
  }
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),
89903
+ try {
89904
+ const estimated = yield this.mayachainQuery.estimateMAYAName(Object.assign({}, params));
89905
+ return Object.assign(Object.assign({}, estimated), { allowed: true, errors: [] });
89906
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
89907
+ }
89908
+ catch (e) {
89909
+ return {
89910
+ memo: '',
89911
+ errors: ['message' in e ? e.message : `Unknown error: ${e}`],
89912
+ value: new CryptoAmount(baseAmount(0, xchainMayachain.CACAO_DECIMAL), xchainMayachain.AssetCacao),
89913
+ allowed: false,
89914
+ };
89915
+ }
89916
+ });
89917
+ }
89918
+ /**
89919
+ * Estimate the cost of an update of a MAYAName
89920
+ * @param {QuoteMAYANameParams} params Params to make the update
89921
+ * @returns {QuoteMAYAName} Memo to make the update and the estimation of the operation
89922
+ */
89923
+ estimateMAYANameUpdate(params) {
89924
+ return __awaiter$8(this, void 0, void 0, function* () {
89925
+ const errors = [];
89926
+ if ((params.chain && !params.chainAddress) || (!params.chain && params.chainAddress)) {
89927
+ errors.push(`Alias not provided correctly`);
89928
+ }
89929
+ if (params.chain &&
89930
+ params.chainAddress &&
89931
+ !validateAddress(this.mayachainQuery.getNetwork(), params.chain, params.chainAddress)) {
89932
+ errors.push(`Invalid alias ${params.chainAddress} for ${params.chain} chain`);
89933
+ }
89934
+ if (params.owner && !validateAddress(this.mayachainQuery.getNetwork(), xchainMayachain.MAYAChain, params.owner)) {
89935
+ errors.push(`Invalid owner ${params.owner} due it is not a MAYAChain address`);
89936
+ }
89937
+ if (errors.length) {
89938
+ return {
89939
+ memo: '',
89940
+ errors,
89941
+ value: new CryptoAmount(baseAmount(0, xchainMayachain.CACAO_DECIMAL), xchainMayachain.AssetCacao),
89942
+ allowed: false,
89943
+ };
89944
+ }
89945
+ try {
89946
+ const estimated = yield this.mayachainQuery.estimateMAYAName(Object.assign(Object.assign({}, params), { isUpdate: true }));
89947
+ return Object.assign(Object.assign({}, estimated), { allowed: true, errors: [] });
89948
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
89949
+ }
89950
+ catch (e) {
89951
+ return {
89952
+ memo: '',
89953
+ errors: ['message' in e ? e.message : `Unknown error: ${e}`],
89954
+ value: new CryptoAmount(baseAmount(0, xchainMayachain.CACAO_DECIMAL), xchainMayachain.AssetCacao),
89955
+ allowed: false,
89956
+ };
89957
+ }
89958
+ });
89959
+ }
89960
+ /**
89961
+ * Register a MAYAName
89962
+ * @param {RegisterMAYAName} params Params to make the registration
89963
+ * @returns {QuoteMAYAName} Memo to make the update or the registration and the estimation of the operation
89964
+ */
89965
+ registerMAYAName(params) {
89966
+ return __awaiter$8(this, void 0, void 0, function* () {
89967
+ const quote = yield this.estimateMAYANameRegistration(params);
89968
+ if (!quote.allowed)
89969
+ throw Error(`Can not register MAYAName. ${quote.errors.join(' ')}`);
89970
+ return MayachainAction.makeAction({
89971
+ wallet: this.wallet,
89972
+ assetAmount: quote.value,
89973
+ memo: quote.memo,
89791
89974
  });
89792
- return {
89793
- hash,
89794
- url: yield this.wallet.getExplorerTxUrl(amount.asset.chain, hash),
89795
- };
89796
89975
  });
89797
89976
  }
89798
89977
  /**
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
89978
+ * Update a MAYAName
89979
+ * @param {QuoteMAYANameParams} params Params to make the update
89980
+ * @returns {QuoteMAYAName} Memo to make the update or the registration and the estimation of the operation
89802
89981
  */
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;
89982
+ updateMAYAName(params) {
89983
+ return __awaiter$8(this, void 0, void 0, function* () {
89984
+ const quote = yield this.estimateMAYANameUpdate(params);
89985
+ if (!quote.allowed)
89986
+ throw Error(`Can not update MAYAName. ${quote.errors.join(' ')}`);
89987
+ return MayachainAction.makeAction({
89988
+ wallet: this.wallet,
89989
+ assetAmount: quote.value,
89990
+ memo: quote.memo,
89991
+ });
89992
+ });
89808
89993
  }
89809
89994
  /**
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
89995
+ * Check if a name is a valid MAYAName
89996
+ * @param {string} name MAYAName to check
89997
+ * @returns {boolean} True if the name is registered as a MAYAName, otherwise false
89813
89998
  */
89814
- isEVMChain(chain) {
89815
- // Check if the chain matches AssetETH.chain
89816
- return [xchainEthereum.AssetETH.chain].includes(chain);
89999
+ isMAYAName(name) {
90000
+ return __awaiter$8(this, void 0, void 0, function* () {
90001
+ return !!(yield this.mayachainQuery.getMAYANameDetails(name));
90002
+ });
89817
90003
  }
89818
90004
  }
89819
90005
 
@@ -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,6 +1,7 @@
1
- import { MayachainQuery, QuoteSwap, QuoteSwapParams } from '@xchainjs/xchain-mayachain-query';
1
+ import { MAYANameDetails, MayachainQuery, QuoteSwap, QuoteSwapParams, RegisterMAYAName, UpdateMAYAName } from '@xchainjs/xchain-mayachain-query';
2
+ import { Address } from '@xchainjs/xchain-util';
2
3
  import { Wallet } from '@xchainjs/xchain-wallet';
3
- import { ApproveParams, IsApprovedParams, TxSubmitted } from './types';
4
+ import { ApproveParams, IsApprovedParams, QuoteMAYAName, TxSubmitted } from './types';
4
5
  /**
5
6
  * Mayachain Automated Market Maker (AMM) class.
6
7
  * MAYAChainAMM class for interacting with THORChain.
@@ -51,36 +52,45 @@ 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
55
+ * Get MAYAname details
56
+ * @param {string} MAYAName
57
+ * @returns {MAYANameDetails | undefined} MAYANames details or undefined it is does not exist
57
58
  */
58
- private isMAYAName;
59
+ getMAYANameDetails(MAYAName: string): Promise<MAYANameDetails | undefined>;
59
60
  /**
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
61
+ * Get the MAYANames owned by an address
62
+ * @param {Address} owner - Thorchain address
63
+ * @returns {MAYANameDetails[]} List of MAYANames owned by the address
64
64
  */
65
- private doProtocolAssetSwap;
65
+ getMAYANamesByOwner(owner: Address): Promise<MAYANameDetails[]>;
66
66
  /**
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
67
+ * Estimate the cost of a MAYAName registration
68
+ * @param {RegisterMAYAName} params Params to make the registration
69
+ * @returns {QuoteMAYAName} Memo to make the registration and the estimation of the operation
70
+ */
71
+ estimateMAYANameRegistration(params: RegisterMAYAName): Promise<QuoteMAYAName>;
72
+ /**
73
+ * Estimate the cost of an update of a MAYAName
74
+ * @param {QuoteMAYANameParams} params Params to make the update
75
+ * @returns {QuoteMAYAName} Memo to make the update and the estimation of the operation
72
76
  */
73
- private doNonProtocolAssetSwap;
77
+ estimateMAYANameUpdate(params: UpdateMAYAName): Promise<QuoteMAYAName>;
74
78
  /**
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
79
+ * Register a MAYAName
80
+ * @param {RegisterMAYAName} params Params to make the registration
81
+ * @returns {QuoteMAYAName} Memo to make the update or the registration and the estimation of the operation
78
82
  */
79
- private isERC20Asset;
83
+ registerMAYAName(params: RegisterMAYAName): Promise<TxSubmitted>;
80
84
  /**
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
85
+ * Update a MAYAName
86
+ * @param {QuoteMAYANameParams} params Params to make the update
87
+ * @returns {QuoteMAYAName} Memo to make the update or the registration and the estimation of the operation
84
88
  */
85
- private isEVMChain;
89
+ updateMAYAName(params: UpdateMAYAName): Promise<TxSubmitted>;
90
+ /**
91
+ * Check if a name is a valid MAYAName
92
+ * @param {string} name MAYAName to check
93
+ * @returns {boolean} True if the name is registered as a MAYAName, otherwise false
94
+ */
95
+ private isMAYAName;
86
96
  }
package/lib/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { QuoteMAYAName as BaseQuoteMAYAName } from '@xchainjs/xchain-mayachain-query';
1
2
  import { Address, Asset, CryptoAmount } from '@xchainjs/xchain-util';
2
3
  export type TxSubmitted = {
3
4
  hash: string;
@@ -12,3 +13,16 @@ export type IsApprovedParams = {
12
13
  amount: CryptoAmount;
13
14
  address: Address;
14
15
  };
16
+ /**
17
+ * MAYAName quote parameters
18
+ */
19
+ export type QuoteMAYAName = BaseQuoteMAYAName & {
20
+ /**
21
+ * If the action can be or not can be done
22
+ */
23
+ allowed: boolean;
24
+ /**
25
+ * If any, list of errors with the reason the operation is not allowed
26
+ */
27
+ errors: string[];
28
+ };
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.12",
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.19",
46
47
  "@xchainjs/xchain-thorchain": "1.1.1",
47
48
  "@xchainjs/xchain-wallet": "0.1.19",
48
49
  "ethers": "5.7.2"