@xchainjs/xchain-thorchain-amm 0.5.2 → 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,22 +401,40 @@ class Wallet {
557
401
  feeOption: FeeOption.Fast,
558
402
  memo: memo,
559
403
  };
560
- const hash = yield this.ethHelper.sendDeposit(addParams);
404
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
405
+ const hash = yield evmHelper.sendDeposit(addParams);
561
406
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
562
407
  }
563
- else if (assetAmount.asset.chain === AVAXChain) {
408
+ else {
564
409
  const addParams = {
565
410
  wallIndex: 0,
566
411
  asset: assetAmount.asset,
567
412
  amount: assetAmount.baseAmount,
568
- feeOption: FeeOption.Fast,
413
+ recipient: toAddress,
569
414
  memo: memo,
570
415
  };
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) };
416
+ try {
417
+ const hash = yield assetClient.transfer(addParams);
418
+ return { hash, url: assetClient.getExplorerTxUrl(hash) };
419
+ }
420
+ catch (err) {
421
+ const hash = JSON.stringify(err);
422
+ return { hash, url: assetClient.getExplorerAddressUrl(assetClient.getAddress()) };
423
+ }
574
424
  }
575
- else if (assetAmount.asset.chain === BSCChain) {
425
+ });
426
+ }
427
+ /**
428
+ *
429
+ * @param assetAmount - amount to withdraw
430
+ * @param memo - memo required
431
+ * @param waitTimeSeconds - expected wait for the transaction to be processed
432
+ * @returns
433
+ */
434
+ withdrawSavers(assetAmount, memo, toAddress) {
435
+ return __awaiter(this, void 0, void 0, function* () {
436
+ const assetClient = this.clients[assetAmount.asset.chain];
437
+ if (this.isEVMChain(assetAmount.asset)) {
576
438
  const addParams = {
577
439
  wallIndex: 0,
578
440
  asset: assetAmount.asset,
@@ -580,7 +442,7 @@ class Wallet {
580
442
  feeOption: FeeOption.Fast,
581
443
  memo: memo,
582
444
  };
583
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
445
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
584
446
  const hash = yield evmHelper.sendDeposit(addParams);
585
447
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
586
448
  }
@@ -603,58 +465,62 @@ class Wallet {
603
465
  }
604
466
  });
605
467
  }
606
- /**
607
- *
608
- * @param assetAmount - amount to withdraw
609
- * @param memo - memo required
610
- * @param waitTimeSeconds - expected wait for the transaction to be processed
611
- * @returns
612
- */
613
- withdrawSavers(dustAssetAmount, memo, toAddress) {
468
+ loanOpen(params) {
614
469
  return __awaiter(this, void 0, void 0, function* () {
615
- const assetClient = this.clients[dustAssetAmount.asset.chain];
616
- if (dustAssetAmount.asset.chain === ETHChain) {
470
+ const assetClient = this.clients[params.amount.asset.chain];
471
+ if (this.isEVMChain(params.amount.asset)) {
617
472
  const addParams = {
618
473
  wallIndex: 0,
619
- asset: dustAssetAmount.asset,
620
- amount: dustAssetAmount.baseAmount,
474
+ asset: params.amount.asset,
475
+ amount: params.amount.baseAmount,
621
476
  feeOption: FeeOption.Fast,
622
- memo: memo,
477
+ memo: params.memo,
623
478
  };
624
- const hash = yield this.ethHelper.sendDeposit(addParams);
479
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
480
+ const hash = yield evmHelper.sendDeposit(addParams);
625
481
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
626
482
  }
627
- else if (dustAssetAmount.asset.chain === AVAXChain) {
483
+ else {
628
484
  const addParams = {
629
485
  wallIndex: 0,
630
- asset: dustAssetAmount.asset,
631
- amount: dustAssetAmount.baseAmount,
632
- feeOption: FeeOption.Fast,
633
- memo: memo,
486
+ asset: params.amount.asset,
487
+ amount: params.amount.baseAmount,
488
+ recipient: params.toAddress,
489
+ memo: params.memo,
634
490
  };
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) };
491
+ try {
492
+ const hash = yield assetClient.transfer(addParams);
493
+ return { hash, url: assetClient.getExplorerTxUrl(hash) };
494
+ }
495
+ catch (err) {
496
+ const hash = JSON.stringify(err);
497
+ return { hash, url: assetClient.getExplorerAddressUrl(assetClient.getAddress()) };
498
+ }
638
499
  }
639
- else if (dustAssetAmount.asset.chain === BSCChain) {
500
+ });
501
+ }
502
+ loanClose(params) {
503
+ return __awaiter(this, void 0, void 0, function* () {
504
+ const assetClient = this.clients[params.amount.asset.chain];
505
+ if (this.isEVMChain(params.amount.asset)) {
640
506
  const addParams = {
641
507
  wallIndex: 0,
642
- asset: dustAssetAmount.asset,
643
- amount: dustAssetAmount.baseAmount,
508
+ asset: params.amount.asset,
509
+ amount: params.amount.baseAmount,
644
510
  feeOption: FeeOption.Fast,
645
- memo: memo,
511
+ memo: params.memo,
646
512
  };
647
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
513
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
648
514
  const hash = yield evmHelper.sendDeposit(addParams);
649
515
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
650
516
  }
651
517
  else {
652
518
  const addParams = {
653
519
  wallIndex: 0,
654
- asset: dustAssetAmount.asset,
655
- amount: dustAssetAmount.baseAmount,
656
- recipient: toAddress,
657
- memo: memo,
520
+ asset: params.amount.asset,
521
+ amount: params.amount.baseAmount,
522
+ recipient: params.toAddress,
523
+ memo: params.memo,
658
524
  };
659
525
  try {
660
526
  const hash = yield assetClient.transfer(addParams);
@@ -678,30 +544,7 @@ class Wallet {
678
544
  */
679
545
  addAssetLP(params, constructedMemo, assetClient, inboundAsgard) {
680
546
  return __awaiter(this, void 0, void 0, function* () {
681
- if (params.asset.asset.chain === ETHChain) {
682
- const addParams = {
683
- wallIndex: 0,
684
- asset: params.asset.asset,
685
- amount: params.asset.baseAmount,
686
- feeOption: FeeOption.Fast,
687
- memo: constructedMemo,
688
- };
689
- const hash = yield this.ethHelper.sendDeposit(addParams);
690
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
691
- }
692
- else if (params.asset.asset.chain === AVAXChain) {
693
- const addParams = {
694
- wallIndex: 0,
695
- asset: params.asset.asset,
696
- amount: params.asset.baseAmount,
697
- feeOption: FeeOption.Fast,
698
- memo: constructedMemo,
699
- };
700
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
701
- const hash = yield evmHelper.sendDeposit(addParams);
702
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
703
- }
704
- else if (params.asset.asset.chain === BSCChain) {
547
+ if (this.isEVMChain(params.asset.asset)) {
705
548
  const addParams = {
706
549
  wallIndex: 0,
707
550
  asset: params.asset.asset,
@@ -709,7 +552,7 @@ class Wallet {
709
552
  feeOption: FeeOption.Fast,
710
553
  memo: constructedMemo,
711
554
  };
712
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
555
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
713
556
  const hash = yield evmHelper.sendDeposit(addParams);
714
557
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
715
558
  }
@@ -743,7 +586,7 @@ class Wallet {
743
586
  */
744
587
  withdrawAssetLP(params, constructedMemo, assetClient, inboundAsgard) {
745
588
  return __awaiter(this, void 0, void 0, function* () {
746
- if (params.assetFee.asset.chain === ETHChain) {
589
+ if (this.isEVMChain(params.assetFee.asset)) {
747
590
  const withdrawParams = {
748
591
  wallIndex: 0,
749
592
  asset: params.assetFee.asset,
@@ -751,31 +594,7 @@ class Wallet {
751
594
  feeOption: FeeOption.Fast,
752
595
  memo: constructedMemo,
753
596
  };
754
- // console.log(withdrawParams.amount.amount().toNumber())
755
- const hash = yield this.ethHelper.sendDeposit(withdrawParams);
756
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
757
- }
758
- else if (params.assetFee.asset.chain === AVAXChain) {
759
- const withdrawParams = {
760
- wallIndex: 0,
761
- asset: params.assetFee.asset,
762
- amount: params.assetFee.baseAmount,
763
- feeOption: FeeOption.Fast,
764
- memo: constructedMemo,
765
- };
766
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
767
- const hash = yield evmHelper.sendDeposit(withdrawParams);
768
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
769
- }
770
- else if (params.assetFee.asset.chain === BSCChain) {
771
- const withdrawParams = {
772
- wallIndex: 0,
773
- asset: params.assetFee.asset,
774
- amount: params.assetFee.baseAmount,
775
- feeOption: FeeOption.Fast,
776
- memo: constructedMemo,
777
- };
778
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
597
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
779
598
  const hash = yield evmHelper.sendDeposit(withdrawParams);
780
599
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
781
600
  }
@@ -834,6 +653,14 @@ class Wallet {
834
653
  return { hash, url: thorchainClient.getExplorerTxUrl(hash) };
835
654
  });
836
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
+ }
837
664
  }
838
665
 
839
666
  const defaultQuery = new ThorchainQuery();
@@ -1033,6 +860,62 @@ class ThorchainAMM {
1033
860
  return yield wallet.withdrawSavers(withdrawEstimate.dustAmount, withdrawEstimate.memo, withdrawEstimate.toAddress);
1034
861
  });
1035
862
  }
863
+ /**
864
+ *
865
+ * @param loanOpenParams
866
+ * @returns
867
+ */
868
+ getLoanQuoteOpen(loanOpenParams) {
869
+ return __awaiter(this, void 0, void 0, function* () {
870
+ return yield this.thorchainQuery.getLoanQuoteOpen(loanOpenParams);
871
+ });
872
+ }
873
+ /**
874
+ *
875
+ * @param loanCloseParams
876
+ * @returns
877
+ */
878
+ getLoanQuoteClose(loanCloseParams) {
879
+ return __awaiter(this, void 0, void 0, function* () {
880
+ return yield this.thorchainQuery.getLoanQuoteClose(loanCloseParams);
881
+ });
882
+ }
883
+ /**
884
+ *
885
+ * @param wallet - wallet needed to execute transaction
886
+ * @param loanOpenParams - params needed to open the loan
887
+ * @returns - submitted tx
888
+ */
889
+ addLoan(wallet, loanOpenParams) {
890
+ return __awaiter(this, void 0, void 0, function* () {
891
+ const loanOpen = yield this.thorchainQuery.getLoanQuoteOpen(loanOpenParams);
892
+ if (loanOpen.errors.length > 0)
893
+ throw Error(`${loanOpen.errors}`);
894
+ return yield wallet.loanOpen({
895
+ memo: `${loanOpen.memo}`,
896
+ amount: loanOpenParams.amount,
897
+ toAddress: loanOpen.inboundAddress,
898
+ });
899
+ });
900
+ }
901
+ /**
902
+ *
903
+ * @param wallet - wallet to execute the transaction
904
+ * @param loanCloseParams - params needed for withdrawing the loan
905
+ * @returns
906
+ */
907
+ withdrawLoan(wallet, loanCloseParams) {
908
+ return __awaiter(this, void 0, void 0, function* () {
909
+ const withdrawLoan = yield this.thorchainQuery.getLoanQuoteClose(loanCloseParams);
910
+ if (withdrawLoan.errors.length > 0)
911
+ throw Error(`${withdrawLoan.errors}`);
912
+ return yield wallet.loanClose({
913
+ memo: `${withdrawLoan.memo}`,
914
+ amount: loanCloseParams.amount,
915
+ toAddress: withdrawLoan.inboundAddress,
916
+ });
917
+ });
918
+ }
1036
919
  }
1037
920
 
1038
921
  export { ThorchainAMM, Wallet };
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,22 +405,40 @@ class Wallet {
561
405
  feeOption: xchainClient.FeeOption.Fast,
562
406
  memo: memo,
563
407
  };
564
- const hash = yield this.ethHelper.sendDeposit(addParams);
408
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
409
+ const hash = yield evmHelper.sendDeposit(addParams);
565
410
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
566
411
  }
567
- else if (assetAmount.asset.chain === xchainAvax.AVAXChain) {
412
+ else {
568
413
  const addParams = {
569
414
  wallIndex: 0,
570
415
  asset: assetAmount.asset,
571
416
  amount: assetAmount.baseAmount,
572
- feeOption: xchainClient.FeeOption.Fast,
417
+ recipient: toAddress,
573
418
  memo: memo,
574
419
  };
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) };
420
+ try {
421
+ const hash = yield assetClient.transfer(addParams);
422
+ return { hash, url: assetClient.getExplorerTxUrl(hash) };
423
+ }
424
+ catch (err) {
425
+ const hash = JSON.stringify(err);
426
+ return { hash, url: assetClient.getExplorerAddressUrl(assetClient.getAddress()) };
427
+ }
578
428
  }
579
- else if (assetAmount.asset.chain === xchainBsc.BSCChain) {
429
+ });
430
+ }
431
+ /**
432
+ *
433
+ * @param assetAmount - amount to withdraw
434
+ * @param memo - memo required
435
+ * @param waitTimeSeconds - expected wait for the transaction to be processed
436
+ * @returns
437
+ */
438
+ withdrawSavers(assetAmount, memo, toAddress) {
439
+ return __awaiter(this, void 0, void 0, function* () {
440
+ const assetClient = this.clients[assetAmount.asset.chain];
441
+ if (this.isEVMChain(assetAmount.asset)) {
580
442
  const addParams = {
581
443
  wallIndex: 0,
582
444
  asset: assetAmount.asset,
@@ -584,7 +446,7 @@ class Wallet {
584
446
  feeOption: xchainClient.FeeOption.Fast,
585
447
  memo: memo,
586
448
  };
587
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
449
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
588
450
  const hash = yield evmHelper.sendDeposit(addParams);
589
451
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
590
452
  }
@@ -607,58 +469,62 @@ class Wallet {
607
469
  }
608
470
  });
609
471
  }
610
- /**
611
- *
612
- * @param assetAmount - amount to withdraw
613
- * @param memo - memo required
614
- * @param waitTimeSeconds - expected wait for the transaction to be processed
615
- * @returns
616
- */
617
- withdrawSavers(dustAssetAmount, memo, toAddress) {
472
+ loanOpen(params) {
618
473
  return __awaiter(this, void 0, void 0, function* () {
619
- const assetClient = this.clients[dustAssetAmount.asset.chain];
620
- if (dustAssetAmount.asset.chain === xchainEthereum.ETHChain) {
474
+ const assetClient = this.clients[params.amount.asset.chain];
475
+ if (this.isEVMChain(params.amount.asset)) {
621
476
  const addParams = {
622
477
  wallIndex: 0,
623
- asset: dustAssetAmount.asset,
624
- amount: dustAssetAmount.baseAmount,
478
+ asset: params.amount.asset,
479
+ amount: params.amount.baseAmount,
625
480
  feeOption: xchainClient.FeeOption.Fast,
626
- memo: memo,
481
+ memo: params.memo,
627
482
  };
628
- const hash = yield this.ethHelper.sendDeposit(addParams);
483
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
484
+ const hash = yield evmHelper.sendDeposit(addParams);
629
485
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
630
486
  }
631
- else if (dustAssetAmount.asset.chain === xchainAvax.AVAXChain) {
487
+ else {
632
488
  const addParams = {
633
489
  wallIndex: 0,
634
- asset: dustAssetAmount.asset,
635
- amount: dustAssetAmount.baseAmount,
636
- feeOption: xchainClient.FeeOption.Fast,
637
- memo: memo,
490
+ asset: params.amount.asset,
491
+ amount: params.amount.baseAmount,
492
+ recipient: params.toAddress,
493
+ memo: params.memo,
638
494
  };
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) };
495
+ try {
496
+ const hash = yield assetClient.transfer(addParams);
497
+ return { hash, url: assetClient.getExplorerTxUrl(hash) };
498
+ }
499
+ catch (err) {
500
+ const hash = JSON.stringify(err);
501
+ return { hash, url: assetClient.getExplorerAddressUrl(assetClient.getAddress()) };
502
+ }
642
503
  }
643
- else if (dustAssetAmount.asset.chain === xchainBsc.BSCChain) {
504
+ });
505
+ }
506
+ loanClose(params) {
507
+ return __awaiter(this, void 0, void 0, function* () {
508
+ const assetClient = this.clients[params.amount.asset.chain];
509
+ if (this.isEVMChain(params.amount.asset)) {
644
510
  const addParams = {
645
511
  wallIndex: 0,
646
- asset: dustAssetAmount.asset,
647
- amount: dustAssetAmount.baseAmount,
512
+ asset: params.amount.asset,
513
+ amount: params.amount.baseAmount,
648
514
  feeOption: xchainClient.FeeOption.Fast,
649
- memo: memo,
515
+ memo: params.memo,
650
516
  };
651
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
517
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
652
518
  const hash = yield evmHelper.sendDeposit(addParams);
653
519
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
654
520
  }
655
521
  else {
656
522
  const addParams = {
657
523
  wallIndex: 0,
658
- asset: dustAssetAmount.asset,
659
- amount: dustAssetAmount.baseAmount,
660
- recipient: toAddress,
661
- memo: memo,
524
+ asset: params.amount.asset,
525
+ amount: params.amount.baseAmount,
526
+ recipient: params.toAddress,
527
+ memo: params.memo,
662
528
  };
663
529
  try {
664
530
  const hash = yield assetClient.transfer(addParams);
@@ -682,30 +548,7 @@ class Wallet {
682
548
  */
683
549
  addAssetLP(params, constructedMemo, assetClient, inboundAsgard) {
684
550
  return __awaiter(this, void 0, void 0, function* () {
685
- if (params.asset.asset.chain === xchainEthereum.ETHChain) {
686
- const addParams = {
687
- wallIndex: 0,
688
- asset: params.asset.asset,
689
- amount: params.asset.baseAmount,
690
- feeOption: xchainClient.FeeOption.Fast,
691
- memo: constructedMemo,
692
- };
693
- const hash = yield this.ethHelper.sendDeposit(addParams);
694
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
695
- }
696
- else if (params.asset.asset.chain === xchainAvax.AVAXChain) {
697
- const addParams = {
698
- wallIndex: 0,
699
- asset: params.asset.asset,
700
- amount: params.asset.baseAmount,
701
- feeOption: xchainClient.FeeOption.Fast,
702
- memo: constructedMemo,
703
- };
704
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
705
- const hash = yield evmHelper.sendDeposit(addParams);
706
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
707
- }
708
- else if (params.asset.asset.chain === xchainBsc.BSCChain) {
551
+ if (this.isEVMChain(params.asset.asset)) {
709
552
  const addParams = {
710
553
  wallIndex: 0,
711
554
  asset: params.asset.asset,
@@ -713,7 +556,7 @@ class Wallet {
713
556
  feeOption: xchainClient.FeeOption.Fast,
714
557
  memo: constructedMemo,
715
558
  };
716
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
559
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
717
560
  const hash = yield evmHelper.sendDeposit(addParams);
718
561
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
719
562
  }
@@ -747,7 +590,7 @@ class Wallet {
747
590
  */
748
591
  withdrawAssetLP(params, constructedMemo, assetClient, inboundAsgard) {
749
592
  return __awaiter(this, void 0, void 0, function* () {
750
- if (params.assetFee.asset.chain === xchainEthereum.ETHChain) {
593
+ if (this.isEVMChain(params.assetFee.asset)) {
751
594
  const withdrawParams = {
752
595
  wallIndex: 0,
753
596
  asset: params.assetFee.asset,
@@ -755,31 +598,7 @@ class Wallet {
755
598
  feeOption: xchainClient.FeeOption.Fast,
756
599
  memo: constructedMemo,
757
600
  };
758
- // console.log(withdrawParams.amount.amount().toNumber())
759
- const hash = yield this.ethHelper.sendDeposit(withdrawParams);
760
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
761
- }
762
- else if (params.assetFee.asset.chain === xchainAvax.AVAXChain) {
763
- const withdrawParams = {
764
- wallIndex: 0,
765
- asset: params.assetFee.asset,
766
- amount: params.assetFee.baseAmount,
767
- feeOption: xchainClient.FeeOption.Fast,
768
- memo: constructedMemo,
769
- };
770
- const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
771
- const hash = yield evmHelper.sendDeposit(withdrawParams);
772
- return { hash, url: assetClient.getExplorerTxUrl(hash) };
773
- }
774
- else if (params.assetFee.asset.chain === xchainBsc.BSCChain) {
775
- const withdrawParams = {
776
- wallIndex: 0,
777
- asset: params.assetFee.asset,
778
- amount: params.assetFee.baseAmount,
779
- feeOption: xchainClient.FeeOption.Fast,
780
- memo: constructedMemo,
781
- };
782
- const evmHelper = new EvmHelper(this.clients.BSC, this.thorchainQuery.thorchainCache);
601
+ const evmHelper = new EvmHelper(assetClient, this.thorchainQuery.thorchainCache);
783
602
  const hash = yield evmHelper.sendDeposit(withdrawParams);
784
603
  return { hash, url: assetClient.getExplorerTxUrl(hash) };
785
604
  }
@@ -838,6 +657,14 @@ class Wallet {
838
657
  return { hash, url: thorchainClient.getExplorerTxUrl(hash) };
839
658
  });
840
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
+ }
841
668
  }
842
669
 
843
670
  const defaultQuery = new xchainThorchainQuery.ThorchainQuery();
@@ -1037,6 +864,62 @@ class ThorchainAMM {
1037
864
  return yield wallet.withdrawSavers(withdrawEstimate.dustAmount, withdrawEstimate.memo, withdrawEstimate.toAddress);
1038
865
  });
1039
866
  }
867
+ /**
868
+ *
869
+ * @param loanOpenParams
870
+ * @returns
871
+ */
872
+ getLoanQuoteOpen(loanOpenParams) {
873
+ return __awaiter(this, void 0, void 0, function* () {
874
+ return yield this.thorchainQuery.getLoanQuoteOpen(loanOpenParams);
875
+ });
876
+ }
877
+ /**
878
+ *
879
+ * @param loanCloseParams
880
+ * @returns
881
+ */
882
+ getLoanQuoteClose(loanCloseParams) {
883
+ return __awaiter(this, void 0, void 0, function* () {
884
+ return yield this.thorchainQuery.getLoanQuoteClose(loanCloseParams);
885
+ });
886
+ }
887
+ /**
888
+ *
889
+ * @param wallet - wallet needed to execute transaction
890
+ * @param loanOpenParams - params needed to open the loan
891
+ * @returns - submitted tx
892
+ */
893
+ addLoan(wallet, loanOpenParams) {
894
+ return __awaiter(this, void 0, void 0, function* () {
895
+ const loanOpen = yield this.thorchainQuery.getLoanQuoteOpen(loanOpenParams);
896
+ if (loanOpen.errors.length > 0)
897
+ throw Error(`${loanOpen.errors}`);
898
+ return yield wallet.loanOpen({
899
+ memo: `${loanOpen.memo}`,
900
+ amount: loanOpenParams.amount,
901
+ toAddress: loanOpen.inboundAddress,
902
+ });
903
+ });
904
+ }
905
+ /**
906
+ *
907
+ * @param wallet - wallet to execute the transaction
908
+ * @param loanCloseParams - params needed for withdrawing the loan
909
+ * @returns
910
+ */
911
+ withdrawLoan(wallet, loanCloseParams) {
912
+ return __awaiter(this, void 0, void 0, function* () {
913
+ const withdrawLoan = yield this.thorchainQuery.getLoanQuoteClose(loanCloseParams);
914
+ if (withdrawLoan.errors.length > 0)
915
+ throw Error(`${withdrawLoan.errors}`);
916
+ return yield wallet.loanClose({
917
+ memo: `${withdrawLoan.memo}`,
918
+ amount: loanCloseParams.amount,
919
+ toAddress: withdrawLoan.inboundAddress,
920
+ });
921
+ });
922
+ }
1040
923
  }
1041
924
 
1042
925
  exports.ThorchainAMM = ThorchainAMM;
@@ -1,4 +1,4 @@
1
- import { AddliquidityPosition, CryptoAmount, EstimateAddLP, EstimateAddSaver, EstimateWithdrawLP, EstimateWithdrawSaver, QuoteSwapParams, SaversPosition, SaversWithdraw, ThorchainQuery, TxDetails, WithdrawLiquidityPosition, getSaver } from '@xchainjs/xchain-thorchain-query';
1
+ import { AddliquidityPosition, CryptoAmount, EstimateAddLP, EstimateAddSaver, EstimateWithdrawLP, EstimateWithdrawSaver, LoanCloseParams, LoanCloseQuote, LoanOpenParams, LoanOpenQuote, QuoteSwapParams, SaversPosition, SaversWithdraw, ThorchainQuery, TxDetails, WithdrawLiquidityPosition, getSaver } from '@xchainjs/xchain-thorchain-query';
2
2
  import { TxSubmitted } from './types';
3
3
  import { Wallet } from './wallet';
4
4
  export type AmmEstimateSwapParams = QuoteSwapParams & {
@@ -94,4 +94,30 @@ export declare class ThorchainAMM {
94
94
  * @returns
95
95
  */
96
96
  withdrawSaver(wallet: Wallet, withdrawParams: SaversWithdraw): Promise<TxSubmitted>;
97
+ /**
98
+ *
99
+ * @param loanOpenParams
100
+ * @returns
101
+ */
102
+ getLoanQuoteOpen(loanOpenParams: LoanOpenParams): Promise<LoanOpenQuote>;
103
+ /**
104
+ *
105
+ * @param loanCloseParams
106
+ * @returns
107
+ */
108
+ getLoanQuoteClose(loanCloseParams: LoanCloseParams): Promise<LoanCloseQuote>;
109
+ /**
110
+ *
111
+ * @param wallet - wallet needed to execute transaction
112
+ * @param loanOpenParams - params needed to open the loan
113
+ * @returns - submitted tx
114
+ */
115
+ addLoan(wallet: Wallet, loanOpenParams: LoanOpenParams): Promise<TxSubmitted>;
116
+ /**
117
+ *
118
+ * @param wallet - wallet to execute the transaction
119
+ * @param loanCloseParams - params needed for withdrawing the loan
120
+ * @returns
121
+ */
122
+ withdrawLoan(wallet: Wallet, loanCloseParams: LoanCloseParams): Promise<TxSubmitted>;
97
123
  }
package/lib/types.d.ts CHANGED
@@ -46,3 +46,13 @@ export type DepositParams = {
46
46
  feeOption: FeeOption;
47
47
  memo: string;
48
48
  };
49
+ export type LoanOpenParams = {
50
+ memo: string;
51
+ amount: CryptoAmount;
52
+ toAddress: Address;
53
+ };
54
+ export type LoanCloseParams = {
55
+ memo: string;
56
+ amount: CryptoAmount;
57
+ toAddress: Address;
58
+ };
@@ -1,2 +1 @@
1
1
  export * from './evm-helper';
2
- export * from './eth-helper';
package/lib/wallet.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Network, XChainClient } from '@xchainjs/xchain-client';
2
2
  import { CryptoAmount, ThorchainQuery } from '@xchainjs/xchain-thorchain-query';
3
3
  import { Address } from '@xchainjs/xchain-util';
4
- import { AddLiquidity, AllBalances, ExecuteSwap, TxSubmitted, WithdrawLiquidity } from './types';
4
+ import { AddLiquidity, AllBalances, ExecuteSwap, LoanCloseParams, LoanOpenParams, TxSubmitted, WithdrawLiquidity } from './types';
5
5
  import { EvmHelper } from './utils/evm-helper';
6
6
  export type NodeUrls = Record<Network, string>;
7
7
  /**
@@ -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,9 @@ 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>;
82
+ loanOpen(params: LoanOpenParams): Promise<TxSubmitted>;
83
+ loanClose(params: LoanCloseParams): Promise<TxSubmitted>;
83
84
  /** Function handles liquidity add for all non rune assets
84
85
  *
85
86
  * @param params - parameters for add liquidity
@@ -114,4 +115,6 @@ export declare class Wallet {
114
115
  * @returns - tx object
115
116
  */
116
117
  private withdrawRuneLP;
118
+ private isERC20Asset;
119
+ private isEVMChain;
117
120
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xchainjs/xchain-thorchain-amm",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
4
4
  "description": "module that exposes estimating & swappping cryptocurrency assets on thorchain",
5
5
  "keywords": [
6
6
  "THORChain",
@@ -43,19 +43,19 @@
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
- "@xchainjs/xchain-thorchain-query": "^0.4.2",
58
- "@xchainjs/xchain-thornode": "^0.3.1",
57
+ "@xchainjs/xchain-thorchain-query": "^0.4.3",
58
+ "@xchainjs/xchain-thornode": "^0.3.2",
59
59
  "@xchainjs/xchain-util": "^0.13.0",
60
60
  "@xchainjs/xchain-utxo-providers": "^0.2.1",
61
61
  "axios": "^1.3.6",
@@ -79,19 +79,19 @@
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
- "@xchainjs/xchain-thorchain-query": "^0.4.2",
94
- "@xchainjs/xchain-thornode": "^0.3.1",
93
+ "@xchainjs/xchain-thorchain-query": "^0.4.3",
94
+ "@xchainjs/xchain-thornode": "^0.3.2",
95
95
  "@xchainjs/xchain-util": "^0.13.0",
96
96
  "@xchainjs/xchain-utxo-providers": "^0.2.1",
97
97
  "axios": "^1.3.6",
@@ -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
- }