@xchainjs/xchain-thorchain-amm 0.5.3 → 0.5.4

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,18 +1,18 @@
1
- import { Client as Client$8, defaultAvaxParams, AVAXChain, AssetAVAX } from '@xchainjs/xchain-avax';
2
- import { Client as Client$6 } from '@xchainjs/xchain-binance';
1
+ import { Client as Client$9, defaultAvaxParams } from '@xchainjs/xchain-avax';
2
+ import { Client as Client$5 } from '@xchainjs/xchain-binance';
3
3
  import { Client as Client$1 } from '@xchainjs/xchain-bitcoin';
4
4
  import { Client } from '@xchainjs/xchain-bitcoincash';
5
- import { Client as Client$9, defaultBscParams, BSCChain, AssetBSC } from '@xchainjs/xchain-bsc';
5
+ import { Client as Client$a, defaultBscParams } from '@xchainjs/xchain-bsc';
6
6
  import { FeeOption } from '@xchainjs/xchain-client';
7
- import { Client as Client$7 } from '@xchainjs/xchain-cosmos';
7
+ import { Client as Client$6 } from '@xchainjs/xchain-cosmos';
8
8
  import { Client as Client$2 } from '@xchainjs/xchain-doge';
9
- import { AssetETH, abi, ETH_DECIMAL, MAX_APPROVAL, Client as Client$4, ETHChain } from '@xchainjs/xchain-ethereum';
9
+ import { Client as Client$8, defaultEthParams } from '@xchainjs/xchain-ethereum';
10
10
  import { Client as Client$3 } from '@xchainjs/xchain-litecoin';
11
- import { Client as Client$a, MAYAChain } from '@xchainjs/xchain-mayachain';
12
- import { Client as Client$5, THORChain } from '@xchainjs/xchain-thorchain';
11
+ import { Client as Client$7 } from '@xchainjs/xchain-mayachain';
12
+ import { Client as Client$4, THORChain } from '@xchainjs/xchain-thorchain';
13
+ import { abi, MAX_APPROVAL } from '@xchainjs/xchain-evm';
13
14
  import { eqAsset, getContractAddressFromAsset, baseAmount } from '@xchainjs/xchain-util';
14
15
  import { ethers } from 'ethers';
15
- import { abi as abi$1, MAX_APPROVAL as MAX_APPROVAL$1 } from '@xchainjs/xchain-evm';
16
16
  import { ThorchainQuery } from '@xchainjs/xchain-thorchain-query';
17
17
 
18
18
  /******************************************************************************
@@ -40,106 +40,6 @@ function __awaiter(thisArg, _arguments, P, generator) {
40
40
  });
41
41
  }
42
42
 
43
- const APPROVE_GASLIMIT_FALLBACK = '200000';
44
- const FIFTEEN_MIN_IN_SECS$1 = 15 * 60;
45
- class EthHelper {
46
- constructor(client, thorchainCache) {
47
- this.ethClient = client;
48
- this.client = client;
49
- this.thorchainCache = thorchainCache;
50
- }
51
- /**
52
- * Transaction to THORChain inbound address.
53
- *
54
- * @param {DepositParams} params The transaction options.
55
- * @returns {TxHash} The transaction hash.
56
- *
57
- * @throws {"halted chain"} Thrown if chain is halted.
58
- * @throws {"halted trading"} Thrown if trading is halted.
59
- * @throws {"amount is not approved"} Thrown if the amount is not allowed to spend
60
- * @throws {"router address is not defined"} Thrown if router address is not defined
61
- */
62
- sendDeposit(params) {
63
- return __awaiter(this, void 0, void 0, function* () {
64
- const inboundAsgard = (yield this.thorchainCache.getInboundDetails())[params.asset.chain];
65
- if (!(inboundAsgard === null || inboundAsgard === void 0 ? void 0 : inboundAsgard.router)) {
66
- throw new Error('router address is not defined');
67
- }
68
- if (!(inboundAsgard === null || inboundAsgard === void 0 ? void 0 : inboundAsgard.address)) {
69
- throw new Error('Vault address is not defined');
70
- }
71
- const address = this.client.getAddress(params.walletIndex);
72
- const gasPrice = yield this.ethClient.estimateGasPrices();
73
- if (eqAsset(params.asset, AssetETH)) {
74
- //ETH is a simple transfer
75
- return yield this.client.transfer({
76
- walletIndex: params.walletIndex || 0,
77
- asset: params.asset,
78
- amount: params.amount,
79
- recipient: inboundAsgard.address,
80
- memo: params.memo,
81
- });
82
- }
83
- else {
84
- //erc-20 must be depsited to the router
85
- const isApprovedResult = yield this.isTCRouterApprovedToSpend(params.asset, params.amount, params.walletIndex);
86
- if (!isApprovedResult) {
87
- throw new Error('TC router has not been approved to spend this amount');
88
- }
89
- const contractAddress = getContractAddressFromAsset(params.asset);
90
- const checkSummedContractAddress = ethers.utils.getAddress(contractAddress);
91
- const latestBlockTimeUnixSecs = (yield this.ethClient.getProvider().getBlock('latest')).timestamp;
92
- const expiry = latestBlockTimeUnixSecs + FIFTEEN_MIN_IN_SECS$1;
93
- const depositParams = [
94
- inboundAsgard.address,
95
- checkSummedContractAddress,
96
- params.amount.amount().toFixed(),
97
- params.memo,
98
- expiry,
99
- ];
100
- const routerContract = new ethers.Contract(inboundAsgard.router, abi.router);
101
- const gasLimit = '160000';
102
- const unsignedTx = yield routerContract.populateTransaction.depositWithExpiry(...depositParams, {
103
- from: address,
104
- value: 0,
105
- gasPrice: gasPrice.fast.amount().toFixed(),
106
- gasLimit,
107
- });
108
- const { hash } = yield this.ethClient.getWallet(params.walletIndex).sendTransaction(unsignedTx);
109
- return hash;
110
- }
111
- });
112
- }
113
- isTCRouterApprovedToSpend(asset, amount, walletIndex = 0) {
114
- return __awaiter(this, void 0, void 0, function* () {
115
- const router = yield this.thorchainCache.getRouterAddressForChain(asset.chain);
116
- const contractAddress = getContractAddressFromAsset(asset);
117
- return yield this.ethClient.isApproved({
118
- amount: amount,
119
- spenderAddress: router,
120
- contractAddress,
121
- walletIndex: walletIndex,
122
- });
123
- });
124
- }
125
- approveTCRouterToSpend(asset, amount = MAX_APPROVAL, walletIndex = 0) {
126
- return __awaiter(this, void 0, void 0, function* () {
127
- const contractAddress = getContractAddressFromAsset(asset);
128
- const router = yield this.thorchainCache.getRouterAddressForChain(asset.chain);
129
- // const gasPrice = await ethClient.estimateGasPrices()
130
- // const gasLimit = calcInboundFee(asset, gasPrice.fast.amount())
131
- const approveParams = {
132
- contractAddress,
133
- spenderAddress: router,
134
- amount: baseAmount(amount.toString(), ETH_DECIMAL),
135
- walletIndex,
136
- gasLimitFallback: APPROVE_GASLIMIT_FALLBACK,
137
- };
138
- return yield this.ethClient.approve(approveParams);
139
- });
140
- }
141
- }
142
-
143
43
  const FIFTEEN_MIN_IN_SECS = 15 * 60;
144
44
  class EvmHelper {
145
45
  constructor(client, thorchainCache) {
@@ -196,7 +96,7 @@ class EvmHelper {
196
96
  params.memo,
197
97
  expiry,
198
98
  ];
199
- const routerContract = new ethers.Contract(inboundAsgard.router, abi$1.router);
99
+ const routerContract = new ethers.Contract(inboundAsgard.router, abi.router);
200
100
  const gasLimit = '160000';
201
101
  const unsignedTx = yield routerContract.populateTransaction.depositWithExpiry(...depositParams, {
202
102
  from: address,
@@ -221,7 +121,7 @@ class EvmHelper {
221
121
  });
222
122
  });
223
123
  }
224
- approveTCRouterToSpend(asset, amount = MAX_APPROVAL$1, walletIndex = 0) {
124
+ approveTCRouterToSpend(asset, amount = MAX_APPROVAL, walletIndex = 0) {
225
125
  return __awaiter(this, void 0, void 0, function* () {
226
126
  const contractAddress = getContractAddressFromAsset(asset);
227
127
  const router = yield this.thorchainCache.getRouterAddressForChain(asset.chain);
@@ -255,13 +155,13 @@ class Wallet {
255
155
  BTC: new Client$1(),
256
156
  DOGE: new Client$2(),
257
157
  LTC: new Client$3(),
258
- ETH: new Client$4(settings),
259
- THOR: new Client$5(settings),
260
- BNB: new Client$6(settings),
261
- GAIA: new Client$7(settings),
262
- AVAX: new Client$8(Object.assign(Object.assign({}, defaultAvaxParams), { network: settings.network, phrase })),
263
- BSC: new Client$9(Object.assign(Object.assign({}, defaultBscParams), { network: settings.network, phrase })),
264
- MAYA: new Client$a(settings),
158
+ THOR: new Client$4(settings),
159
+ BNB: new Client$5(settings),
160
+ GAIA: new Client$6(settings),
161
+ MAYA: new Client$7(settings),
162
+ ETH: new Client$8(Object.assign(Object.assign({}, defaultEthParams), { network: settings.network, phrase })),
163
+ AVAX: new Client$9(Object.assign(Object.assign({}, defaultAvaxParams), { network: settings.network, phrase })),
164
+ BSC: new Client$a(Object.assign(Object.assign({}, defaultBscParams), { network: settings.network, phrase })),
265
165
  };
266
166
  this.clients.BCH.setNetwork(settings.network);
267
167
  this.clients.BCH.setPhrase(settings.phrase, 0);
@@ -271,9 +171,8 @@ class Wallet {
271
171
  this.clients.DOGE.setPhrase(settings.phrase, 0);
272
172
  this.clients.LTC.setNetwork(settings.network);
273
173
  this.clients.LTC.setPhrase(settings.phrase, 0);
274
- this.ethHelper = new EthHelper(this.clients.ETH, this.thorchainQuery.thorchainCache);
275
174
  this.evmHelpers = {
276
- // ETH: new EvmHelper(this.clients.ETH, this.thorchainQuery.thorchainCache),
175
+ ETH: new EvmHelper(this.clients.ETH, this.thorchainQuery.thorchainCache),
277
176
  BSC: new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache),
278
177
  AVAX: new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache),
279
178
  };
@@ -344,31 +243,10 @@ class Wallet {
344
243
  // if input == synth return errors.
345
244
  if (swap.input.asset.synth)
346
245
  return errors;
347
- if (swap.input.asset.chain === ETHChain) {
348
- if (eqAsset(swap.input.asset, AssetETH) !== true) {
349
- const isApprovedResult = yield this.ethHelper.isTCRouterApprovedToSpend(swap.input.asset, swap.input.baseAmount, swap.walletIndex);
350
- console.log(isApprovedResult);
351
- if (!isApprovedResult) {
352
- errors.push('TC router has not been approved to spend this amount');
353
- }
354
- }
355
- }
356
- else if (swap.input.asset.chain === AVAXChain) {
357
- if (eqAsset(swap.input.asset, AssetAVAX) !== true) {
358
- const isApprovedResult = yield this.evmHelpers[`AVAX`].isTCRouterApprovedToSpend(swap.input.asset, swap.input.baseAmount, swap.walletIndex);
359
- console.log(isApprovedResult);
360
- if (!isApprovedResult) {
361
- errors.push('TC router has not been approved to spend this amount');
362
- }
363
- }
364
- }
365
- else if (swap.input.asset.chain === BSCChain) {
366
- if (eqAsset(swap.input.asset, AssetBSC) !== true) {
367
- const isApprovedResult = yield this.evmHelpers[`BSC`].isTCRouterApprovedToSpend(swap.input.asset, swap.input.baseAmount, swap.walletIndex);
368
- console.log(isApprovedResult);
369
- if (!isApprovedResult) {
370
- errors.push('TC router has not been approved to spend this amount');
371
- }
246
+ if (this.isERC20Asset(swap.input.asset)) {
247
+ const isApprovedResult = yield this.evmHelpers[swap.input.asset.chain].isTCRouterApprovedToSpend(swap.input.asset, swap.input.baseAmount, swap.walletIndex);
248
+ if (!isApprovedResult) {
249
+ errors.push('TC router has not been approved to spend this amount');
372
250
  }
373
251
  }
374
252
  return errors;
@@ -407,7 +285,7 @@ class Wallet {
407
285
  const inbound = (yield this.thorchainQuery.thorchainCache.getInboundDetails())[swap.input.asset.chain];
408
286
  if (!(inbound === null || inbound === void 0 ? void 0 : inbound.address))
409
287
  throw Error(`no asgard address found for ${swap.input.asset.chain}`);
410
- if (swap.input.asset.chain === ETHChain) {
288
+ if (this.isEVMChain(swap.input.asset)) {
411
289
  const params = {
412
290
  walletIndex: 0,
413
291
  asset: swap.input.asset,
@@ -415,41 +293,7 @@ class Wallet {
415
293
  feeOption: swap.feeOption || FeeOption.Fast,
416
294
  memo: swap.memo,
417
295
  };
418
- const hash = yield this.ethHelper.sendDeposit(params);
419
- return { hash, url: client.getExplorerTxUrl(hash) };
420
- }
421
- else if (swap.input.asset.chain === AVAXChain) {
422
- const params = {
423
- walletIndex: 0,
424
- asset: swap.input.asset,
425
- amount: swap.input.baseAmount,
426
- feeOption: swap.feeOption || FeeOption.Fast,
427
- memo: swap.memo,
428
- };
429
- const hash = yield this.evmHelpers['AVAX'].sendDeposit(params);
430
- return { hash, url: client.getExplorerTxUrl(hash) };
431
- }
432
- else if (swap.input.asset.chain === BSCChain) {
433
- const params = {
434
- walletIndex: 0,
435
- asset: swap.input.asset,
436
- amount: swap.input.baseAmount,
437
- feeOption: swap.feeOption || FeeOption.Fast,
438
- memo: swap.memo,
439
- };
440
- const hash = yield this.evmHelpers['BSC'].sendDeposit(params);
441
- return { hash, url: client.getExplorerTxUrl(hash) };
442
- }
443
- else if (swap.input.asset.chain === MAYAChain) {
444
- // add mayachain
445
- const params = {
446
- walletIndex: 0,
447
- asset: swap.input.asset,
448
- amount: swap.input.baseAmount,
449
- recipient: inbound.address,
450
- memo: swap.memo,
451
- };
452
- const hash = yield client.transfer(params);
296
+ const hash = yield this.evmHelpers[swap.input.asset.chain].sendDeposit(params);
453
297
  return { hash, url: client.getExplorerTxUrl(hash) };
454
298
  }
455
299
  else {
@@ -549,7 +393,7 @@ class Wallet {
549
393
  addSavers(assetAmount, memo, toAddress) {
550
394
  return __awaiter(this, void 0, void 0, function* () {
551
395
  const assetClient = this.clients[assetAmount.asset.chain];
552
- if (assetAmount.asset.chain === ETHChain) {
396
+ if (this.isEVMChain(assetAmount.asset)) {
553
397
  const addParams = {
554
398
  wallIndex: 0,
555
399
  asset: assetAmount.asset,
@@ -557,30 +401,7 @@ class Wallet {
557
401
  feeOption: FeeOption.Fast,
558
402
  memo: memo,
559
403
  };
560
- const hash = yield this.ethHelper.sendDeposit(addParams);
561
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
562
- }
563
- else if (assetAmount.asset.chain === AVAXChain) {
564
- const addParams = {
565
- wallIndex: 0,
566
- asset: assetAmount.asset,
567
- amount: assetAmount.baseAmount,
568
- feeOption: FeeOption.Fast,
569
- memo: memo,
570
- };
571
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
572
- const hash = yield evmHelper.sendDeposit(addParams);
573
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
574
- }
575
- else if (assetAmount.asset.chain === BSCChain) {
576
- const addParams = {
577
- wallIndex: 0,
578
- asset: assetAmount.asset,
579
- amount: assetAmount.baseAmount,
580
- feeOption: FeeOption.Fast,
581
- memo: memo,
582
- };
583
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
404
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
584
405
  const hash = yield evmHelper.sendDeposit(addParams);
585
406
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
586
407
  }
@@ -610,49 +431,26 @@ class Wallet {
610
431
  * @param waitTimeSeconds - expected wait for the transaction to be processed
611
432
  * @returns
612
433
  */
613
- withdrawSavers(dustAssetAmount, memo, toAddress) {
434
+ withdrawSavers(assetAmount, memo, toAddress) {
614
435
  return __awaiter(this, void 0, void 0, function* () {
615
- const assetClient = this.clients[dustAssetAmount.asset.chain];
616
- if (dustAssetAmount.asset.chain === ETHChain) {
617
- const addParams = {
618
- wallIndex: 0,
619
- asset: dustAssetAmount.asset,
620
- amount: dustAssetAmount.baseAmount,
621
- feeOption: FeeOption.Fast,
622
- memo: memo,
623
- };
624
- const hash = yield this.ethHelper.sendDeposit(addParams);
625
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
626
- }
627
- else if (dustAssetAmount.asset.chain === AVAXChain) {
628
- const addParams = {
629
- wallIndex: 0,
630
- asset: dustAssetAmount.asset,
631
- amount: dustAssetAmount.baseAmount,
632
- feeOption: FeeOption.Fast,
633
- memo: memo,
634
- };
635
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
636
- const hash = yield evmHelper.sendDeposit(addParams);
637
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
638
- }
639
- else if (dustAssetAmount.asset.chain === BSCChain) {
436
+ const assetClient = this.clients[assetAmount.asset.chain];
437
+ if (this.isEVMChain(assetAmount.asset)) {
640
438
  const addParams = {
641
439
  wallIndex: 0,
642
- asset: dustAssetAmount.asset,
643
- amount: dustAssetAmount.baseAmount,
440
+ asset: assetAmount.asset,
441
+ amount: assetAmount.baseAmount,
644
442
  feeOption: FeeOption.Fast,
645
443
  memo: memo,
646
444
  };
647
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
445
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
648
446
  const hash = yield evmHelper.sendDeposit(addParams);
649
447
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
650
448
  }
651
449
  else {
652
450
  const addParams = {
653
451
  wallIndex: 0,
654
- asset: dustAssetAmount.asset,
655
- amount: dustAssetAmount.baseAmount,
452
+ asset: assetAmount.asset,
453
+ amount: assetAmount.baseAmount,
656
454
  recipient: toAddress,
657
455
  memo: memo,
658
456
  };
@@ -670,7 +468,7 @@ class Wallet {
670
468
  loanOpen(params) {
671
469
  return __awaiter(this, void 0, void 0, function* () {
672
470
  const assetClient = this.clients[params.amount.asset.chain];
673
- if (params.amount.asset.chain === ETHChain) {
471
+ if (this.isEVMChain(params.amount.asset)) {
674
472
  const addParams = {
675
473
  wallIndex: 0,
676
474
  asset: params.amount.asset,
@@ -678,30 +476,7 @@ class Wallet {
678
476
  feeOption: FeeOption.Fast,
679
477
  memo: params.memo,
680
478
  };
681
- const hash = yield this.ethHelper.sendDeposit(addParams);
682
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
683
- }
684
- else if (params.amount.asset.chain === AVAXChain) {
685
- const addParams = {
686
- wallIndex: 0,
687
- asset: params.amount.asset,
688
- amount: params.amount.baseAmount,
689
- feeOption: FeeOption.Fast,
690
- memo: params.memo,
691
- };
692
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
693
- const hash = yield evmHelper.sendDeposit(addParams);
694
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
695
- }
696
- else if (params.amount.asset.chain === BSCChain) {
697
- const addParams = {
698
- wallIndex: 0,
699
- asset: params.amount.asset,
700
- amount: params.amount.baseAmount,
701
- feeOption: FeeOption.Fast,
702
- memo: params.memo,
703
- };
704
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
479
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
705
480
  const hash = yield evmHelper.sendDeposit(addParams);
706
481
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
707
482
  }
@@ -727,7 +502,7 @@ class Wallet {
727
502
  loanClose(params) {
728
503
  return __awaiter(this, void 0, void 0, function* () {
729
504
  const assetClient = this.clients[params.amount.asset.chain];
730
- if (params.amount.asset.chain === ETHChain) {
505
+ if (this.isEVMChain(params.amount.asset)) {
731
506
  const addParams = {
732
507
  wallIndex: 0,
733
508
  asset: params.amount.asset,
@@ -735,30 +510,7 @@ class Wallet {
735
510
  feeOption: FeeOption.Fast,
736
511
  memo: params.memo,
737
512
  };
738
- const hash = yield this.ethHelper.sendDeposit(addParams);
739
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
740
- }
741
- else if (params.amount.asset.chain === AVAXChain) {
742
- const addParams = {
743
- wallIndex: 0,
744
- asset: params.amount.asset,
745
- amount: params.amount.baseAmount,
746
- feeOption: FeeOption.Fast,
747
- memo: params.memo,
748
- };
749
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
750
- const hash = yield evmHelper.sendDeposit(addParams);
751
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
752
- }
753
- else if (params.amount.asset.chain === BSCChain) {
754
- const addParams = {
755
- wallIndex: 0,
756
- asset: params.amount.asset,
757
- amount: params.amount.baseAmount,
758
- feeOption: FeeOption.Fast,
759
- memo: params.memo,
760
- };
761
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
513
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
762
514
  const hash = yield evmHelper.sendDeposit(addParams);
763
515
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
764
516
  }
@@ -792,30 +544,7 @@ class Wallet {
792
544
  */
793
545
  addAssetLP(params, constructedMemo, assetClient, inboundAsgard) {
794
546
  return __awaiter(this, void 0, void 0, function* () {
795
- if (params.asset.asset.chain === ETHChain) {
796
- const addParams = {
797
- wallIndex: 0,
798
- asset: params.asset.asset,
799
- amount: params.asset.baseAmount,
800
- feeOption: FeeOption.Fast,
801
- memo: constructedMemo,
802
- };
803
- const hash = yield this.ethHelper.sendDeposit(addParams);
804
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
805
- }
806
- else if (params.asset.asset.chain === AVAXChain) {
807
- const addParams = {
808
- wallIndex: 0,
809
- asset: params.asset.asset,
810
- amount: params.asset.baseAmount,
811
- feeOption: FeeOption.Fast,
812
- memo: constructedMemo,
813
- };
814
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
815
- const hash = yield evmHelper.sendDeposit(addParams);
816
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
817
- }
818
- else if (params.asset.asset.chain === BSCChain) {
547
+ if (this.isEVMChain(params.asset.asset)) {
819
548
  const addParams = {
820
549
  wallIndex: 0,
821
550
  asset: params.asset.asset,
@@ -823,7 +552,7 @@ class Wallet {
823
552
  feeOption: FeeOption.Fast,
824
553
  memo: constructedMemo,
825
554
  };
826
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
555
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
827
556
  const hash = yield evmHelper.sendDeposit(addParams);
828
557
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
829
558
  }
@@ -857,7 +586,7 @@ class Wallet {
857
586
  */
858
587
  withdrawAssetLP(params, constructedMemo, assetClient, inboundAsgard) {
859
588
  return __awaiter(this, void 0, void 0, function* () {
860
- if (params.assetFee.asset.chain === ETHChain) {
589
+ if (this.isEVMChain(params.assetFee.asset)) {
861
590
  const withdrawParams = {
862
591
  wallIndex: 0,
863
592
  asset: params.assetFee.asset,
@@ -865,31 +594,7 @@ class Wallet {
865
594
  feeOption: FeeOption.Fast,
866
595
  memo: constructedMemo,
867
596
  };
868
- // console.log(withdrawParams.amount.amount().toNumber())
869
- const hash = yield this.ethHelper.sendDeposit(withdrawParams);
870
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
871
- }
872
- else if (params.assetFee.asset.chain === AVAXChain) {
873
- const withdrawParams = {
874
- wallIndex: 0,
875
- asset: params.assetFee.asset,
876
- amount: params.assetFee.baseAmount,
877
- feeOption: FeeOption.Fast,
878
- memo: constructedMemo,
879
- };
880
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
881
- const hash = yield evmHelper.sendDeposit(withdrawParams);
882
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
883
- }
884
- else if (params.assetFee.asset.chain === BSCChain) {
885
- const withdrawParams = {
886
- wallIndex: 0,
887
- asset: params.assetFee.asset,
888
- amount: params.assetFee.baseAmount,
889
- feeOption: FeeOption.Fast,
890
- memo: constructedMemo,
891
- };
892
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
597
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
893
598
  const hash = yield evmHelper.sendDeposit(withdrawParams);
894
599
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
895
600
  }
@@ -948,6 +653,14 @@ class Wallet {
948
653
  return { hash, url: thorchainClient.getExplorerTxUrl(hash) };
949
654
  });
950
655
  }
656
+ isERC20Asset(asset) {
657
+ const isGasAsset = ['ETH', 'BSC', 'AVAX'].includes(asset.symbol);
658
+ return this.isEVMChain(asset) && !isGasAsset;
659
+ }
660
+ isEVMChain(asset) {
661
+ const isEvmChain = ['ETH', 'BSC', 'AVAX'].includes(asset.chain);
662
+ return isEvmChain;
663
+ }
951
664
  }
952
665
 
953
666
  const defaultQuery = new ThorchainQuery();
package/lib/index.js CHANGED
@@ -14,9 +14,9 @@ var xchainEthereum = require('@xchainjs/xchain-ethereum');
14
14
  var xchainLitecoin = require('@xchainjs/xchain-litecoin');
15
15
  var xchainMayachain = require('@xchainjs/xchain-mayachain');
16
16
  var xchainThorchain = require('@xchainjs/xchain-thorchain');
17
+ var xchainEvm = require('@xchainjs/xchain-evm');
17
18
  var xchainUtil = require('@xchainjs/xchain-util');
18
19
  var ethers = require('ethers');
19
- var xchainEvm = require('@xchainjs/xchain-evm');
20
20
  var xchainThorchainQuery = require('@xchainjs/xchain-thorchain-query');
21
21
 
22
22
  /******************************************************************************
@@ -44,106 +44,6 @@ function __awaiter(thisArg, _arguments, P, generator) {
44
44
  });
45
45
  }
46
46
 
47
- const APPROVE_GASLIMIT_FALLBACK = '200000';
48
- const FIFTEEN_MIN_IN_SECS$1 = 15 * 60;
49
- class EthHelper {
50
- constructor(client, thorchainCache) {
51
- this.ethClient = client;
52
- this.client = client;
53
- this.thorchainCache = thorchainCache;
54
- }
55
- /**
56
- * Transaction to THORChain inbound address.
57
- *
58
- * @param {DepositParams} params The transaction options.
59
- * @returns {TxHash} The transaction hash.
60
- *
61
- * @throws {"halted chain"} Thrown if chain is halted.
62
- * @throws {"halted trading"} Thrown if trading is halted.
63
- * @throws {"amount is not approved"} Thrown if the amount is not allowed to spend
64
- * @throws {"router address is not defined"} Thrown if router address is not defined
65
- */
66
- sendDeposit(params) {
67
- return __awaiter(this, void 0, void 0, function* () {
68
- const inboundAsgard = (yield this.thorchainCache.getInboundDetails())[params.asset.chain];
69
- if (!(inboundAsgard === null || inboundAsgard === void 0 ? void 0 : inboundAsgard.router)) {
70
- throw new Error('router address is not defined');
71
- }
72
- if (!(inboundAsgard === null || inboundAsgard === void 0 ? void 0 : inboundAsgard.address)) {
73
- throw new Error('Vault address is not defined');
74
- }
75
- const address = this.client.getAddress(params.walletIndex);
76
- const gasPrice = yield this.ethClient.estimateGasPrices();
77
- if (xchainUtil.eqAsset(params.asset, xchainEthereum.AssetETH)) {
78
- //ETH is a simple transfer
79
- return yield this.client.transfer({
80
- walletIndex: params.walletIndex || 0,
81
- asset: params.asset,
82
- amount: params.amount,
83
- recipient: inboundAsgard.address,
84
- memo: params.memo,
85
- });
86
- }
87
- else {
88
- //erc-20 must be depsited to the router
89
- const isApprovedResult = yield this.isTCRouterApprovedToSpend(params.asset, params.amount, params.walletIndex);
90
- if (!isApprovedResult) {
91
- throw new Error('TC router has not been approved to spend this amount');
92
- }
93
- const contractAddress = xchainUtil.getContractAddressFromAsset(params.asset);
94
- const checkSummedContractAddress = ethers.ethers.utils.getAddress(contractAddress);
95
- const latestBlockTimeUnixSecs = (yield this.ethClient.getProvider().getBlock('latest')).timestamp;
96
- const expiry = latestBlockTimeUnixSecs + FIFTEEN_MIN_IN_SECS$1;
97
- const depositParams = [
98
- inboundAsgard.address,
99
- checkSummedContractAddress,
100
- params.amount.amount().toFixed(),
101
- params.memo,
102
- expiry,
103
- ];
104
- const routerContract = new ethers.ethers.Contract(inboundAsgard.router, xchainEthereum.abi.router);
105
- const gasLimit = '160000';
106
- const unsignedTx = yield routerContract.populateTransaction.depositWithExpiry(...depositParams, {
107
- from: address,
108
- value: 0,
109
- gasPrice: gasPrice.fast.amount().toFixed(),
110
- gasLimit,
111
- });
112
- const { hash } = yield this.ethClient.getWallet(params.walletIndex).sendTransaction(unsignedTx);
113
- return hash;
114
- }
115
- });
116
- }
117
- isTCRouterApprovedToSpend(asset, amount, walletIndex = 0) {
118
- return __awaiter(this, void 0, void 0, function* () {
119
- const router = yield this.thorchainCache.getRouterAddressForChain(asset.chain);
120
- const contractAddress = xchainUtil.getContractAddressFromAsset(asset);
121
- return yield this.ethClient.isApproved({
122
- amount: amount,
123
- spenderAddress: router,
124
- contractAddress,
125
- walletIndex: walletIndex,
126
- });
127
- });
128
- }
129
- approveTCRouterToSpend(asset, amount = xchainEthereum.MAX_APPROVAL, walletIndex = 0) {
130
- return __awaiter(this, void 0, void 0, function* () {
131
- const contractAddress = xchainUtil.getContractAddressFromAsset(asset);
132
- const router = yield this.thorchainCache.getRouterAddressForChain(asset.chain);
133
- // const gasPrice = await ethClient.estimateGasPrices()
134
- // const gasLimit = calcInboundFee(asset, gasPrice.fast.amount())
135
- const approveParams = {
136
- contractAddress,
137
- spenderAddress: router,
138
- amount: xchainUtil.baseAmount(amount.toString(), xchainEthereum.ETH_DECIMAL),
139
- walletIndex,
140
- gasLimitFallback: APPROVE_GASLIMIT_FALLBACK,
141
- };
142
- return yield this.ethClient.approve(approveParams);
143
- });
144
- }
145
- }
146
-
147
47
  const FIFTEEN_MIN_IN_SECS = 15 * 60;
148
48
  class EvmHelper {
149
49
  constructor(client, thorchainCache) {
@@ -259,13 +159,13 @@ class Wallet {
259
159
  BTC: new xchainBitcoin.Client(),
260
160
  DOGE: new xchainDoge.Client(),
261
161
  LTC: new xchainLitecoin.Client(),
262
- ETH: new xchainEthereum.Client(settings),
263
162
  THOR: new xchainThorchain.Client(settings),
264
163
  BNB: new xchainBinance.Client(settings),
265
164
  GAIA: new xchainCosmos.Client(settings),
165
+ MAYA: new xchainMayachain.Client(settings),
166
+ ETH: new xchainEthereum.Client(Object.assign(Object.assign({}, xchainEthereum.defaultEthParams), { network: settings.network, phrase })),
266
167
  AVAX: new xchainAvax.Client(Object.assign(Object.assign({}, xchainAvax.defaultAvaxParams), { network: settings.network, phrase })),
267
168
  BSC: new xchainBsc.Client(Object.assign(Object.assign({}, xchainBsc.defaultBscParams), { network: settings.network, phrase })),
268
- MAYA: new xchainMayachain.Client(settings),
269
169
  };
270
170
  this.clients.BCH.setNetwork(settings.network);
271
171
  this.clients.BCH.setPhrase(settings.phrase, 0);
@@ -275,9 +175,8 @@ class Wallet {
275
175
  this.clients.DOGE.setPhrase(settings.phrase, 0);
276
176
  this.clients.LTC.setNetwork(settings.network);
277
177
  this.clients.LTC.setPhrase(settings.phrase, 0);
278
- this.ethHelper = new EthHelper(this.clients.ETH, this.thorchainQuery.thorchainCache);
279
178
  this.evmHelpers = {
280
- // ETH: new EvmHelper(this.clients.ETH, this.thorchainQuery.thorchainCache),
179
+ ETH: new EvmHelper(this.clients.ETH, this.thorchainQuery.thorchainCache),
281
180
  BSC: new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache),
282
181
  AVAX: new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache),
283
182
  };
@@ -348,31 +247,10 @@ class Wallet {
348
247
  // if input == synth return errors.
349
248
  if (swap.input.asset.synth)
350
249
  return errors;
351
- if (swap.input.asset.chain === xchainEthereum.ETHChain) {
352
- if (xchainUtil.eqAsset(swap.input.asset, xchainEthereum.AssetETH) !== true) {
353
- const isApprovedResult = yield this.ethHelper.isTCRouterApprovedToSpend(swap.input.asset, swap.input.baseAmount, swap.walletIndex);
354
- console.log(isApprovedResult);
355
- if (!isApprovedResult) {
356
- errors.push('TC router has not been approved to spend this amount');
357
- }
358
- }
359
- }
360
- else if (swap.input.asset.chain === xchainAvax.AVAXChain) {
361
- if (xchainUtil.eqAsset(swap.input.asset, xchainAvax.AssetAVAX) !== true) {
362
- const isApprovedResult = yield this.evmHelpers[`AVAX`].isTCRouterApprovedToSpend(swap.input.asset, swap.input.baseAmount, swap.walletIndex);
363
- console.log(isApprovedResult);
364
- if (!isApprovedResult) {
365
- errors.push('TC router has not been approved to spend this amount');
366
- }
367
- }
368
- }
369
- else if (swap.input.asset.chain === xchainBsc.BSCChain) {
370
- if (xchainUtil.eqAsset(swap.input.asset, xchainBsc.AssetBSC) !== true) {
371
- const isApprovedResult = yield this.evmHelpers[`BSC`].isTCRouterApprovedToSpend(swap.input.asset, swap.input.baseAmount, swap.walletIndex);
372
- console.log(isApprovedResult);
373
- if (!isApprovedResult) {
374
- errors.push('TC router has not been approved to spend this amount');
375
- }
250
+ if (this.isERC20Asset(swap.input.asset)) {
251
+ const isApprovedResult = yield this.evmHelpers[swap.input.asset.chain].isTCRouterApprovedToSpend(swap.input.asset, swap.input.baseAmount, swap.walletIndex);
252
+ if (!isApprovedResult) {
253
+ errors.push('TC router has not been approved to spend this amount');
376
254
  }
377
255
  }
378
256
  return errors;
@@ -411,7 +289,7 @@ class Wallet {
411
289
  const inbound = (yield this.thorchainQuery.thorchainCache.getInboundDetails())[swap.input.asset.chain];
412
290
  if (!(inbound === null || inbound === void 0 ? void 0 : inbound.address))
413
291
  throw Error(`no asgard address found for ${swap.input.asset.chain}`);
414
- if (swap.input.asset.chain === xchainEthereum.ETHChain) {
292
+ if (this.isEVMChain(swap.input.asset)) {
415
293
  const params = {
416
294
  walletIndex: 0,
417
295
  asset: swap.input.asset,
@@ -419,41 +297,7 @@ class Wallet {
419
297
  feeOption: swap.feeOption || xchainClient.FeeOption.Fast,
420
298
  memo: swap.memo,
421
299
  };
422
- const hash = yield this.ethHelper.sendDeposit(params);
423
- return { hash, url: client.getExplorerTxUrl(hash) };
424
- }
425
- else if (swap.input.asset.chain === xchainAvax.AVAXChain) {
426
- const params = {
427
- walletIndex: 0,
428
- asset: swap.input.asset,
429
- amount: swap.input.baseAmount,
430
- feeOption: swap.feeOption || xchainClient.FeeOption.Fast,
431
- memo: swap.memo,
432
- };
433
- const hash = yield this.evmHelpers['AVAX'].sendDeposit(params);
434
- return { hash, url: client.getExplorerTxUrl(hash) };
435
- }
436
- else if (swap.input.asset.chain === xchainBsc.BSCChain) {
437
- const params = {
438
- walletIndex: 0,
439
- asset: swap.input.asset,
440
- amount: swap.input.baseAmount,
441
- feeOption: swap.feeOption || xchainClient.FeeOption.Fast,
442
- memo: swap.memo,
443
- };
444
- const hash = yield this.evmHelpers['BSC'].sendDeposit(params);
445
- return { hash, url: client.getExplorerTxUrl(hash) };
446
- }
447
- else if (swap.input.asset.chain === xchainMayachain.MAYAChain) {
448
- // add mayachain
449
- const params = {
450
- walletIndex: 0,
451
- asset: swap.input.asset,
452
- amount: swap.input.baseAmount,
453
- recipient: inbound.address,
454
- memo: swap.memo,
455
- };
456
- const hash = yield client.transfer(params);
300
+ const hash = yield this.evmHelpers[swap.input.asset.chain].sendDeposit(params);
457
301
  return { hash, url: client.getExplorerTxUrl(hash) };
458
302
  }
459
303
  else {
@@ -553,7 +397,7 @@ class Wallet {
553
397
  addSavers(assetAmount, memo, toAddress) {
554
398
  return __awaiter(this, void 0, void 0, function* () {
555
399
  const assetClient = this.clients[assetAmount.asset.chain];
556
- if (assetAmount.asset.chain === xchainEthereum.ETHChain) {
400
+ if (this.isEVMChain(assetAmount.asset)) {
557
401
  const addParams = {
558
402
  wallIndex: 0,
559
403
  asset: assetAmount.asset,
@@ -561,30 +405,7 @@ class Wallet {
561
405
  feeOption: xchainClient.FeeOption.Fast,
562
406
  memo: memo,
563
407
  };
564
- const hash = yield this.ethHelper.sendDeposit(addParams);
565
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
566
- }
567
- else if (assetAmount.asset.chain === xchainAvax.AVAXChain) {
568
- const addParams = {
569
- wallIndex: 0,
570
- asset: assetAmount.asset,
571
- amount: assetAmount.baseAmount,
572
- feeOption: xchainClient.FeeOption.Fast,
573
- memo: memo,
574
- };
575
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
576
- const hash = yield evmHelper.sendDeposit(addParams);
577
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
578
- }
579
- else if (assetAmount.asset.chain === xchainBsc.BSCChain) {
580
- const addParams = {
581
- wallIndex: 0,
582
- asset: assetAmount.asset,
583
- amount: assetAmount.baseAmount,
584
- feeOption: xchainClient.FeeOption.Fast,
585
- memo: memo,
586
- };
587
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
408
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
588
409
  const hash = yield evmHelper.sendDeposit(addParams);
589
410
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
590
411
  }
@@ -614,49 +435,26 @@ class Wallet {
614
435
  * @param waitTimeSeconds - expected wait for the transaction to be processed
615
436
  * @returns
616
437
  */
617
- withdrawSavers(dustAssetAmount, memo, toAddress) {
438
+ withdrawSavers(assetAmount, memo, toAddress) {
618
439
  return __awaiter(this, void 0, void 0, function* () {
619
- const assetClient = this.clients[dustAssetAmount.asset.chain];
620
- if (dustAssetAmount.asset.chain === xchainEthereum.ETHChain) {
621
- const addParams = {
622
- wallIndex: 0,
623
- asset: dustAssetAmount.asset,
624
- amount: dustAssetAmount.baseAmount,
625
- feeOption: xchainClient.FeeOption.Fast,
626
- memo: memo,
627
- };
628
- const hash = yield this.ethHelper.sendDeposit(addParams);
629
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
630
- }
631
- else if (dustAssetAmount.asset.chain === xchainAvax.AVAXChain) {
632
- const addParams = {
633
- wallIndex: 0,
634
- asset: dustAssetAmount.asset,
635
- amount: dustAssetAmount.baseAmount,
636
- feeOption: xchainClient.FeeOption.Fast,
637
- memo: memo,
638
- };
639
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
640
- const hash = yield evmHelper.sendDeposit(addParams);
641
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
642
- }
643
- else if (dustAssetAmount.asset.chain === xchainBsc.BSCChain) {
440
+ const assetClient = this.clients[assetAmount.asset.chain];
441
+ if (this.isEVMChain(assetAmount.asset)) {
644
442
  const addParams = {
645
443
  wallIndex: 0,
646
- asset: dustAssetAmount.asset,
647
- amount: dustAssetAmount.baseAmount,
444
+ asset: assetAmount.asset,
445
+ amount: assetAmount.baseAmount,
648
446
  feeOption: xchainClient.FeeOption.Fast,
649
447
  memo: memo,
650
448
  };
651
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
449
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
652
450
  const hash = yield evmHelper.sendDeposit(addParams);
653
451
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
654
452
  }
655
453
  else {
656
454
  const addParams = {
657
455
  wallIndex: 0,
658
- asset: dustAssetAmount.asset,
659
- amount: dustAssetAmount.baseAmount,
456
+ asset: assetAmount.asset,
457
+ amount: assetAmount.baseAmount,
660
458
  recipient: toAddress,
661
459
  memo: memo,
662
460
  };
@@ -674,7 +472,7 @@ class Wallet {
674
472
  loanOpen(params) {
675
473
  return __awaiter(this, void 0, void 0, function* () {
676
474
  const assetClient = this.clients[params.amount.asset.chain];
677
- if (params.amount.asset.chain === xchainEthereum.ETHChain) {
475
+ if (this.isEVMChain(params.amount.asset)) {
678
476
  const addParams = {
679
477
  wallIndex: 0,
680
478
  asset: params.amount.asset,
@@ -682,30 +480,7 @@ class Wallet {
682
480
  feeOption: xchainClient.FeeOption.Fast,
683
481
  memo: params.memo,
684
482
  };
685
- const hash = yield this.ethHelper.sendDeposit(addParams);
686
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
687
- }
688
- else if (params.amount.asset.chain === xchainAvax.AVAXChain) {
689
- const addParams = {
690
- wallIndex: 0,
691
- asset: params.amount.asset,
692
- amount: params.amount.baseAmount,
693
- feeOption: xchainClient.FeeOption.Fast,
694
- memo: params.memo,
695
- };
696
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
697
- const hash = yield evmHelper.sendDeposit(addParams);
698
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
699
- }
700
- else if (params.amount.asset.chain === xchainBsc.BSCChain) {
701
- const addParams = {
702
- wallIndex: 0,
703
- asset: params.amount.asset,
704
- amount: params.amount.baseAmount,
705
- feeOption: xchainClient.FeeOption.Fast,
706
- memo: params.memo,
707
- };
708
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
483
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
709
484
  const hash = yield evmHelper.sendDeposit(addParams);
710
485
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
711
486
  }
@@ -731,7 +506,7 @@ class Wallet {
731
506
  loanClose(params) {
732
507
  return __awaiter(this, void 0, void 0, function* () {
733
508
  const assetClient = this.clients[params.amount.asset.chain];
734
- if (params.amount.asset.chain === xchainEthereum.ETHChain) {
509
+ if (this.isEVMChain(params.amount.asset)) {
735
510
  const addParams = {
736
511
  wallIndex: 0,
737
512
  asset: params.amount.asset,
@@ -739,30 +514,7 @@ class Wallet {
739
514
  feeOption: xchainClient.FeeOption.Fast,
740
515
  memo: params.memo,
741
516
  };
742
- const hash = yield this.ethHelper.sendDeposit(addParams);
743
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
744
- }
745
- else if (params.amount.asset.chain === xchainAvax.AVAXChain) {
746
- const addParams = {
747
- wallIndex: 0,
748
- asset: params.amount.asset,
749
- amount: params.amount.baseAmount,
750
- feeOption: xchainClient.FeeOption.Fast,
751
- memo: params.memo,
752
- };
753
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
754
- const hash = yield evmHelper.sendDeposit(addParams);
755
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
756
- }
757
- else if (params.amount.asset.chain === xchainBsc.BSCChain) {
758
- const addParams = {
759
- wallIndex: 0,
760
- asset: params.amount.asset,
761
- amount: params.amount.baseAmount,
762
- feeOption: xchainClient.FeeOption.Fast,
763
- memo: params.memo,
764
- };
765
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
517
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
766
518
  const hash = yield evmHelper.sendDeposit(addParams);
767
519
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
768
520
  }
@@ -796,30 +548,7 @@ class Wallet {
796
548
  */
797
549
  addAssetLP(params, constructedMemo, assetClient, inboundAsgard) {
798
550
  return __awaiter(this, void 0, void 0, function* () {
799
- if (params.asset.asset.chain === xchainEthereum.ETHChain) {
800
- const addParams = {
801
- wallIndex: 0,
802
- asset: params.asset.asset,
803
- amount: params.asset.baseAmount,
804
- feeOption: xchainClient.FeeOption.Fast,
805
- memo: constructedMemo,
806
- };
807
- const hash = yield this.ethHelper.sendDeposit(addParams);
808
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
809
- }
810
- else if (params.asset.asset.chain === xchainAvax.AVAXChain) {
811
- const addParams = {
812
- wallIndex: 0,
813
- asset: params.asset.asset,
814
- amount: params.asset.baseAmount,
815
- feeOption: xchainClient.FeeOption.Fast,
816
- memo: constructedMemo,
817
- };
818
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
819
- const hash = yield evmHelper.sendDeposit(addParams);
820
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
821
- }
822
- else if (params.asset.asset.chain === xchainBsc.BSCChain) {
551
+ if (this.isEVMChain(params.asset.asset)) {
823
552
  const addParams = {
824
553
  wallIndex: 0,
825
554
  asset: params.asset.asset,
@@ -827,7 +556,7 @@ class Wallet {
827
556
  feeOption: xchainClient.FeeOption.Fast,
828
557
  memo: constructedMemo,
829
558
  };
830
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
559
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
831
560
  const hash = yield evmHelper.sendDeposit(addParams);
832
561
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
833
562
  }
@@ -861,7 +590,7 @@ class Wallet {
861
590
  */
862
591
  withdrawAssetLP(params, constructedMemo, assetClient, inboundAsgard) {
863
592
  return __awaiter(this, void 0, void 0, function* () {
864
- if (params.assetFee.asset.chain === xchainEthereum.ETHChain) {
593
+ if (this.isEVMChain(params.assetFee.asset)) {
865
594
  const withdrawParams = {
866
595
  wallIndex: 0,
867
596
  asset: params.assetFee.asset,
@@ -869,31 +598,7 @@ class Wallet {
869
598
  feeOption: xchainClient.FeeOption.Fast,
870
599
  memo: constructedMemo,
871
600
  };
872
- // console.log(withdrawParams.amount.amount().toNumber())
873
- const hash = yield this.ethHelper.sendDeposit(withdrawParams);
874
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
875
- }
876
- else if (params.assetFee.asset.chain === xchainAvax.AVAXChain) {
877
- const withdrawParams = {
878
- wallIndex: 0,
879
- asset: params.assetFee.asset,
880
- amount: params.assetFee.baseAmount,
881
- feeOption: xchainClient.FeeOption.Fast,
882
- memo: constructedMemo,
883
- };
884
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
885
- const hash = yield evmHelper.sendDeposit(withdrawParams);
886
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
887
- }
888
- else if (params.assetFee.asset.chain === xchainBsc.BSCChain) {
889
- const withdrawParams = {
890
- wallIndex: 0,
891
- asset: params.assetFee.asset,
892
- amount: params.assetFee.baseAmount,
893
- feeOption: xchainClient.FeeOption.Fast,
894
- memo: constructedMemo,
895
- };
896
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
601
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
897
602
  const hash = yield evmHelper.sendDeposit(withdrawParams);
898
603
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
899
604
  }
@@ -952,6 +657,14 @@ class Wallet {
952
657
  return { hash, url: thorchainClient.getExplorerTxUrl(hash) };
953
658
  });
954
659
  }
660
+ isERC20Asset(asset) {
661
+ const isGasAsset = ['ETH', 'BSC', 'AVAX'].includes(asset.symbol);
662
+ return this.isEVMChain(asset) && !isGasAsset;
663
+ }
664
+ isEVMChain(asset) {
665
+ const isEvmChain = ['ETH', 'BSC', 'AVAX'].includes(asset.chain);
666
+ return isEvmChain;
667
+ }
955
668
  }
956
669
 
957
670
  const defaultQuery = new xchainThorchainQuery.ThorchainQuery();
@@ -1,2 +1 @@
1
1
  export * from './evm-helper';
2
- export * from './eth-helper';
package/lib/wallet.d.ts CHANGED
@@ -10,7 +10,6 @@ export type NodeUrls = Record<Network, string>;
10
10
  export declare class Wallet {
11
11
  private thorchainQuery;
12
12
  clients: Record<string, XChainClient>;
13
- private ethHelper;
14
13
  evmHelpers: Record<string, EvmHelper>;
15
14
  /**
16
15
  * Contructor to create a Wallet
@@ -79,7 +78,7 @@ export declare class Wallet {
79
78
  * @param waitTimeSeconds - expected wait for the transaction to be processed
80
79
  * @returns
81
80
  */
82
- withdrawSavers(dustAssetAmount: CryptoAmount, memo: string, toAddress: Address): Promise<TxSubmitted>;
81
+ withdrawSavers(assetAmount: CryptoAmount, memo: string, toAddress: Address): Promise<TxSubmitted>;
83
82
  loanOpen(params: LoanOpenParams): Promise<TxSubmitted>;
84
83
  loanClose(params: LoanCloseParams): Promise<TxSubmitted>;
85
84
  /** Function handles liquidity add for all non rune assets
@@ -116,4 +115,6 @@ export declare class Wallet {
116
115
  * @returns - tx object
117
116
  */
118
117
  private withdrawRuneLP;
118
+ private isERC20Asset;
119
+ private isEVMChain;
119
120
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xchainjs/xchain-thorchain-amm",
3
- "version": "0.5.3",
3
+ "version": "0.5.4",
4
4
  "description": "module that exposes estimating & swappping cryptocurrency assets on thorchain",
5
5
  "keywords": [
6
6
  "THORChain",
@@ -43,16 +43,16 @@
43
43
  "@xchainjs/xchain-bsc": "^0.2.2",
44
44
  "@xchainjs/xchain-binance": "^5.7.1",
45
45
  "@xchainjs/xchain-bitcoin": "^0.22.1",
46
- "@xchainjs/xchain-bitcoincash": "^0.16.1",
46
+ "@xchainjs/xchain-bitcoincash": "^0.16.2",
47
47
  "@xchainjs/xchain-client": "^0.14.1",
48
48
  "@xchainjs/xchain-cosmos": "^0.21.1",
49
49
  "@xchainjs/xchain-crypto": "^0.3.0",
50
50
  "@xchainjs/xchain-doge": "^0.6.1",
51
- "@xchainjs/xchain-ethereum": "^0.28.2",
51
+ "@xchainjs/xchain-ethereum": "^0.29.0",
52
52
  "@xchainjs/xchain-evm": "^0.2.2",
53
53
  "@xchainjs/xchain-litecoin": "^0.12.1",
54
54
  "@xchainjs/xchain-midgard": "^0.5.0",
55
- "@xchainjs/xchain-mayachain": "^0.2.1",
55
+ "@xchainjs/xchain-mayachain": "^0.2.2",
56
56
  "@xchainjs/xchain-thorchain": "^0.28.1",
57
57
  "@xchainjs/xchain-thorchain-query": "^0.4.3",
58
58
  "@xchainjs/xchain-thornode": "^0.3.2",
@@ -79,16 +79,16 @@
79
79
  "@xchainjs/xchain-bsc": "^0.2.2",
80
80
  "@xchainjs/xchain-binance": "^5.7.1",
81
81
  "@xchainjs/xchain-bitcoin": "^0.22.1",
82
- "@xchainjs/xchain-bitcoincash": "^0.16.1",
82
+ "@xchainjs/xchain-bitcoincash": "^0.16.2",
83
83
  "@xchainjs/xchain-client": "^0.14.1",
84
84
  "@xchainjs/xchain-cosmos": "^0.21.1",
85
85
  "@xchainjs/xchain-crypto": "^0.3.0",
86
86
  "@xchainjs/xchain-doge": "^0.6.1",
87
- "@xchainjs/xchain-ethereum": "^0.28.2",
87
+ "@xchainjs/xchain-ethereum": "^0.29.0",
88
88
  "@xchainjs/xchain-evm": "^0.2.2",
89
89
  "@xchainjs/xchain-litecoin": "^0.12.1",
90
90
  "@xchainjs/xchain-midgard": "^0.5.0",
91
- "@xchainjs/xchain-mayachain": "^0.2.1",
91
+ "@xchainjs/xchain-mayachain": "^0.2.2",
92
92
  "@xchainjs/xchain-thorchain": "^0.28.1",
93
93
  "@xchainjs/xchain-thorchain-query": "^0.4.3",
94
94
  "@xchainjs/xchain-thornode": "^0.3.2",
@@ -1,25 +0,0 @@
1
- import { TxHash, XChainClient } from '@xchainjs/xchain-client';
2
- import { ThorchainCache } from '@xchainjs/xchain-thorchain-query';
3
- import { Asset, BaseAmount } from '@xchainjs/xchain-util';
4
- import { ethers } from 'ethers';
5
- import { DepositParams } from '../types';
6
- export declare class EthHelper {
7
- private ethClient;
8
- private client;
9
- private thorchainCache;
10
- constructor(client: XChainClient, thorchainCache: ThorchainCache);
11
- /**
12
- * Transaction to THORChain inbound address.
13
- *
14
- * @param {DepositParams} params The transaction options.
15
- * @returns {TxHash} The transaction hash.
16
- *
17
- * @throws {"halted chain"} Thrown if chain is halted.
18
- * @throws {"halted trading"} Thrown if trading is halted.
19
- * @throws {"amount is not approved"} Thrown if the amount is not allowed to spend
20
- * @throws {"router address is not defined"} Thrown if router address is not defined
21
- */
22
- sendDeposit(params: DepositParams): Promise<TxHash>;
23
- isTCRouterApprovedToSpend(asset: Asset, amount: BaseAmount, walletIndex?: number): Promise<boolean>;
24
- approveTCRouterToSpend(asset: Asset, amount?: ethers.BigNumber, walletIndex?: number): Promise<ethers.providers.TransactionResponse>;
25
- }