@xchainjs/xchain-mayachain-amm 1.0.1 → 1.0.3

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
@@ -2,7 +2,7 @@ import { Client as Client$2, defaultBTCParams } from '@xchainjs/xchain-bitcoin';
2
2
  import { Network } from '@xchainjs/xchain-client';
3
3
  import { Client as Client$4, defaultDashParams } from '@xchainjs/xchain-dash';
4
4
  import { Client as Client$3, defaultEthParams, AssetETH } from '@xchainjs/xchain-ethereum';
5
- import { abi } from '@xchainjs/xchain-evm';
5
+ import { MAX_APPROVAL, abi } from '@xchainjs/xchain-evm';
6
6
  import { Client as Client$1 } from '@xchainjs/xchain-cosmos-sdk';
7
7
  import { baseAmount, eqAsset, CryptoAmount, getContractAddressFromAsset as getContractAddressFromAsset$1 } from '@xchainjs/xchain-util';
8
8
  import { Client as Client$6, MAYAChain } from '@xchainjs/xchain-mayachain';
@@ -12380,6 +12380,26 @@ class Wallet {
12380
12380
  return client.deposit({ asset, amount, memo, walletIndex, sequence, gasLimit });
12381
12381
  });
12382
12382
  }
12383
+ /**
12384
+ * Check if an spenderAddress is allowed to spend in name of another address certain asset amount
12385
+ * @param {Asset} asset to check
12386
+ * @param {BaseAmount} amount to check
12387
+ * @param {string} fromAddress owner of the amount asset
12388
+ * @param {string} spenderAddress spender to check if it is allowed to spend
12389
+ * @returns true if the spenderAddress is allowed to spend the amount, otherwise, false
12390
+ * @throws {Error} if asset is a non ERC20 asset
12391
+ */
12392
+ approve(asset, amount, spenderAddress) {
12393
+ return __awaiter$5(this, void 0, void 0, function* () {
12394
+ const client = this.getClient(asset.chain);
12395
+ if (!('approve' in client))
12396
+ throw Error('Can not make approve over non EVM client');
12397
+ if (asset.chain === asset.ticker)
12398
+ throw Error('Can not make approve over native asset');
12399
+ const contractAddress = getContractAddressFromAsset(asset);
12400
+ return yield client.approve({ contractAddress, amount, spenderAddress });
12401
+ });
12402
+ }
12383
12403
  /**
12384
12404
  * Check if an spenderAddress is allowed to spend in name of another address certain asset amount
12385
12405
  * @param {Asset} asset to check
@@ -12537,12 +12557,12 @@ class MayachainAMM {
12537
12557
  errors.push(`affiliateBps ${affiliateBps} out of range [0 - 10000]`);
12538
12558
  }
12539
12559
  if (this.isERC20Asset(fromAsset) && fromAddress) {
12540
- const inboundDetails = yield this.mayachainQuery.getChainInboundDetails(fromAsset.chain);
12541
- if (!inboundDetails.router)
12542
- throw Error(`Unknown router address for ${fromAsset.chain}`);
12543
- const isApprovedResult = yield this.wallet.isApproved(fromAsset, amount.baseAmount, fromAddress, inboundDetails.router);
12544
- if (!isApprovedResult)
12545
- errors.push('Maya router has not been approved to spend this amount');
12560
+ const approveErrors = yield this.isRouterApprovedToSpend({
12561
+ asset: fromAsset,
12562
+ address: fromAddress,
12563
+ amount,
12564
+ });
12565
+ errors.push(...approveErrors);
12546
12566
  }
12547
12567
  return errors;
12548
12568
  });
@@ -12571,6 +12591,41 @@ class MayachainAMM {
12571
12591
  : this.doNonProtocolAssetSwap(amount, quoteSwap.toAddress, quoteSwap.memo);
12572
12592
  });
12573
12593
  }
12594
+ /**
12595
+ * Approve Mayachain router in the chain of the asset to spend the amount in name of the address
12596
+ * @param {ApproveParams} approveParams contains the asset and amount the router will be allowed. If amount is not defined,
12597
+ * an infinity approve will be done
12598
+ */
12599
+ approveRouterToSpend({ asset, amount }) {
12600
+ return __awaiter$6(this, void 0, void 0, function* () {
12601
+ const inboundDetails = yield this.mayachainQuery.getChainInboundDetails(asset.chain);
12602
+ if (!inboundDetails.router)
12603
+ throw Error(`Unknown router address for ${asset.chain}`);
12604
+ const tx = yield this.wallet.approve(asset, (amount === null || amount === void 0 ? void 0 : amount.baseAmount) || baseAmount(MAX_APPROVAL.toString(), yield this.mayachainQuery.getAssetDecimals(asset)), inboundDetails.router);
12605
+ return {
12606
+ hash: tx.hash,
12607
+ url: yield this.wallet.getExplorerTxUrl(asset.chain, tx.hash),
12608
+ };
12609
+ });
12610
+ }
12611
+ /**
12612
+ * Validate if the asset router is allowed to spend the asset amount in name of the address
12613
+ * @param {IsApprovedParams} isApprovedParams contains the asset and the amount the router is supposed to spend
12614
+ * int name of address
12615
+ * @returns {string[]} the reasons the router of the asset is not allowed to spend the amount. If it is empty, the asset router is allowed to spend the amount
12616
+ */
12617
+ isRouterApprovedToSpend({ asset, amount, address }) {
12618
+ return __awaiter$6(this, void 0, void 0, function* () {
12619
+ const errors = [];
12620
+ const inboundDetails = yield this.mayachainQuery.getChainInboundDetails(asset.chain);
12621
+ if (!inboundDetails.router)
12622
+ throw Error(`Unknown router address for ${asset.chain}`);
12623
+ const isApprovedResult = yield this.wallet.isApproved(asset, amount.baseAmount, address, inboundDetails.router);
12624
+ if (!isApprovedResult)
12625
+ errors.push('Maya router has not been approved to spend this amount');
12626
+ return errors;
12627
+ });
12628
+ }
12574
12629
  /**
12575
12630
  * Check if a name is a valid MAYAName
12576
12631
  * @param {string} name MAYAName
package/lib/index.js CHANGED
@@ -12384,6 +12384,26 @@ class Wallet {
12384
12384
  return client.deposit({ asset, amount, memo, walletIndex, sequence, gasLimit });
12385
12385
  });
12386
12386
  }
12387
+ /**
12388
+ * Check if an spenderAddress is allowed to spend in name of another address certain asset amount
12389
+ * @param {Asset} asset to check
12390
+ * @param {BaseAmount} amount to check
12391
+ * @param {string} fromAddress owner of the amount asset
12392
+ * @param {string} spenderAddress spender to check if it is allowed to spend
12393
+ * @returns true if the spenderAddress is allowed to spend the amount, otherwise, false
12394
+ * @throws {Error} if asset is a non ERC20 asset
12395
+ */
12396
+ approve(asset, amount, spenderAddress) {
12397
+ return __awaiter$5(this, void 0, void 0, function* () {
12398
+ const client = this.getClient(asset.chain);
12399
+ if (!('approve' in client))
12400
+ throw Error('Can not make approve over non EVM client');
12401
+ if (asset.chain === asset.ticker)
12402
+ throw Error('Can not make approve over native asset');
12403
+ const contractAddress = getContractAddressFromAsset(asset);
12404
+ return yield client.approve({ contractAddress, amount, spenderAddress });
12405
+ });
12406
+ }
12387
12407
  /**
12388
12408
  * Check if an spenderAddress is allowed to spend in name of another address certain asset amount
12389
12409
  * @param {Asset} asset to check
@@ -12541,12 +12561,12 @@ class MayachainAMM {
12541
12561
  errors.push(`affiliateBps ${affiliateBps} out of range [0 - 10000]`);
12542
12562
  }
12543
12563
  if (this.isERC20Asset(fromAsset) && fromAddress) {
12544
- const inboundDetails = yield this.mayachainQuery.getChainInboundDetails(fromAsset.chain);
12545
- if (!inboundDetails.router)
12546
- throw Error(`Unknown router address for ${fromAsset.chain}`);
12547
- const isApprovedResult = yield this.wallet.isApproved(fromAsset, amount.baseAmount, fromAddress, inboundDetails.router);
12548
- if (!isApprovedResult)
12549
- errors.push('Maya router has not been approved to spend this amount');
12564
+ const approveErrors = yield this.isRouterApprovedToSpend({
12565
+ asset: fromAsset,
12566
+ address: fromAddress,
12567
+ amount,
12568
+ });
12569
+ errors.push(...approveErrors);
12550
12570
  }
12551
12571
  return errors;
12552
12572
  });
@@ -12575,6 +12595,41 @@ class MayachainAMM {
12575
12595
  : this.doNonProtocolAssetSwap(amount, quoteSwap.toAddress, quoteSwap.memo);
12576
12596
  });
12577
12597
  }
12598
+ /**
12599
+ * Approve Mayachain router in the chain of the asset to spend the amount in name of the address
12600
+ * @param {ApproveParams} approveParams contains the asset and amount the router will be allowed. If amount is not defined,
12601
+ * an infinity approve will be done
12602
+ */
12603
+ approveRouterToSpend({ asset, amount }) {
12604
+ return __awaiter$6(this, void 0, void 0, function* () {
12605
+ const inboundDetails = yield this.mayachainQuery.getChainInboundDetails(asset.chain);
12606
+ if (!inboundDetails.router)
12607
+ throw Error(`Unknown router address for ${asset.chain}`);
12608
+ const tx = yield this.wallet.approve(asset, (amount === null || amount === void 0 ? void 0 : amount.baseAmount) || xchainUtil.baseAmount(xchainEvm.MAX_APPROVAL.toString(), yield this.mayachainQuery.getAssetDecimals(asset)), inboundDetails.router);
12609
+ return {
12610
+ hash: tx.hash,
12611
+ url: yield this.wallet.getExplorerTxUrl(asset.chain, tx.hash),
12612
+ };
12613
+ });
12614
+ }
12615
+ /**
12616
+ * Validate if the asset router is allowed to spend the asset amount in name of the address
12617
+ * @param {IsApprovedParams} isApprovedParams contains the asset and the amount the router is supposed to spend
12618
+ * int name of address
12619
+ * @returns {string[]} the reasons the router of the asset is not allowed to spend the amount. If it is empty, the asset router is allowed to spend the amount
12620
+ */
12621
+ isRouterApprovedToSpend({ asset, amount, address }) {
12622
+ return __awaiter$6(this, void 0, void 0, function* () {
12623
+ const errors = [];
12624
+ const inboundDetails = yield this.mayachainQuery.getChainInboundDetails(asset.chain);
12625
+ if (!inboundDetails.router)
12626
+ throw Error(`Unknown router address for ${asset.chain}`);
12627
+ const isApprovedResult = yield this.wallet.isApproved(asset, amount.baseAmount, address, inboundDetails.router);
12628
+ if (!isApprovedResult)
12629
+ errors.push('Maya router has not been approved to spend this amount');
12630
+ return errors;
12631
+ });
12632
+ }
12578
12633
  /**
12579
12634
  * Check if a name is a valid MAYAName
12580
12635
  * @param {string} name MAYAName
@@ -1,6 +1,6 @@
1
1
  import { MayachainQuery, QuoteSwap, QuoteSwapParams } from '@xchainjs/xchain-mayachain-query';
2
2
  import { Wallet } from '@xchainjs/xchain-wallet';
3
- import { TxSubmitted } from './types';
3
+ import { ApproveParams, IsApprovedParams, TxSubmitted } from './types';
4
4
  /**
5
5
  * MAYAChainAMM class for interacting with THORChain.
6
6
  * Recommended main class to use for swapping with MAYAChain
@@ -33,6 +33,19 @@ export declare class MayachainAMM {
33
33
  * @returns {TxSubmitted} the swap transaction hash and url
34
34
  */
35
35
  doSwap({ fromAsset, fromAddress, amount, destinationAsset, destinationAddress, affiliateAddress, affiliateBps, toleranceBps, }: QuoteSwapParams): Promise<TxSubmitted>;
36
+ /**
37
+ * Approve Mayachain router in the chain of the asset to spend the amount in name of the address
38
+ * @param {ApproveParams} approveParams contains the asset and amount the router will be allowed. If amount is not defined,
39
+ * an infinity approve will be done
40
+ */
41
+ approveRouterToSpend({ asset, amount }: ApproveParams): Promise<TxSubmitted>;
42
+ /**
43
+ * Validate if the asset router is allowed to spend the asset amount in name of the address
44
+ * @param {IsApprovedParams} isApprovedParams contains the asset and the amount the router is supposed to spend
45
+ * int name of address
46
+ * @returns {string[]} the reasons the router of the asset is not allowed to spend the amount. If it is empty, the asset router is allowed to spend the amount
47
+ */
48
+ isRouterApprovedToSpend({ asset, amount, address }: IsApprovedParams): Promise<string[]>;
36
49
  /**
37
50
  * Check if a name is a valid MAYAName
38
51
  * @param {string} name MAYAName
package/lib/types.d.ts CHANGED
@@ -1,4 +1,14 @@
1
+ import { Address, Asset, CryptoAmount } from '@xchainjs/xchain-util';
1
2
  export type TxSubmitted = {
2
3
  hash: string;
3
4
  url: string;
4
5
  };
6
+ export type ApproveParams = {
7
+ asset: Asset;
8
+ amount: CryptoAmount | undefined;
9
+ };
10
+ export type IsApprovedParams = {
11
+ asset: Asset;
12
+ amount: CryptoAmount;
13
+ address: Address;
14
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xchainjs/xchain-mayachain-amm",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "module that exposes estimating & swapping cryptocurrency assets on mayachain",
5
5
  "keywords": [
6
6
  "MAYAChain",
@@ -38,7 +38,8 @@
38
38
  "dependencies": {
39
39
  "@xchainjs/xchain-ethereum": "0.31.5",
40
40
  "@xchainjs/xchain-kujira": "0.1.7",
41
- "@xchainjs/xchain-wallet": "0.1.0"
41
+ "@xchainjs/xchain-wallet": "0.1.1",
42
+ "@xchainjs/xchain-mayachain-query": "0.1.7"
42
43
  },
43
44
  "devDependencies": {
44
45
  "@cosmos-client/core": "0.46.1",
@@ -49,9 +50,9 @@
49
50
  "@xchainjs/xchain-crypto": "^0.3.1",
50
51
  "@xchainjs/xchain-dash": "^0.2.9",
51
52
  "@xchainjs/xchain-mayanode": "^0.1.3",
52
- "@xchainjs/xchain-mayachain-query": "^0.1.6",
53
+ "@xchainjs/xchain-mayachain-query": "^0.1.7",
53
54
  "@xchainjs/xchain-evm": "^0.4.4",
54
- "@xchainjs/xchain-mayachain": "^0.2.14",
55
+ "@xchainjs/xchain-mayachain": "^0.2.15",
55
56
  "@xchainjs/xchain-mayamidgard": "^0.1.1",
56
57
  "@xchainjs/xchain-thorchain": "^0.28.17",
57
58
  "@xchainjs/xchain-util": "^0.13.1",
@@ -76,9 +77,9 @@
76
77
  "@xchainjs/xchain-crypto": "^0.3.1",
77
78
  "@xchainjs/xchain-dash": "^0.2.9",
78
79
  "@xchainjs/xchain-mayanode": "^0.1.3",
79
- "@xchainjs/xchain-mayachain-query": "^0.1.6",
80
+ "@xchainjs/xchain-mayachain-query": "^0.1.7",
80
81
  "@xchainjs/xchain-evm": "^0.4.4",
81
- "@xchainjs/xchain-mayachain": "^0.2.14",
82
+ "@xchainjs/xchain-mayachain": "^0.2.15",
82
83
  "@xchainjs/xchain-mayamidgard": "^0.1.1",
83
84
  "@xchainjs/xchain-thorchain": "^0.28.17",
84
85
  "@xchainjs/xchain-util": "^0.13.1",