@shapeshiftoss/swapper 9.4.0 → 9.5.0

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/dist/api.d.ts CHANGED
@@ -42,6 +42,9 @@ declare type ChainSpecificQuoteFeeData<T extends ChainId> = ChainSpecific<T, {
42
42
  byteCount: string;
43
43
  satsPerByte: string;
44
44
  };
45
+ [KnownChainIds.CosmosMainnet]: {
46
+ estimatedGas: string;
47
+ };
45
48
  }>;
46
49
  export declare type QuoteFeeData<T extends ChainId> = {
47
50
  fee: string;
@@ -15,7 +15,8 @@ class ThorchainSwapper {
15
15
  this.name = 'Thorchain';
16
16
  this.sellSupportedChainIds = {
17
17
  [types_1.KnownChainIds.EthereumMainnet]: true,
18
- [types_1.KnownChainIds.BitcoinMainnet]: true
18
+ [types_1.KnownChainIds.BitcoinMainnet]: true,
19
+ [types_1.KnownChainIds.CosmosMainnet]: true
19
20
  };
20
21
  this.buySupportedChainIds = {
21
22
  [types_1.KnownChainIds.EthereumMainnet]: true,
@@ -106,6 +107,14 @@ class ThorchainSwapper {
106
107
  const txid = await adapter.broadcastTransaction(signedTx);
107
108
  return { tradeId: txid };
108
109
  }
110
+ else if (trade.sellAsset.chainId === types_1.KnownChainIds.CosmosMainnet) {
111
+ const signedTx = await adapter.signTransaction({
112
+ txToSign: trade.txData,
113
+ wallet
114
+ });
115
+ const txid = await adapter.broadcastTransaction(signedTx);
116
+ return { tradeId: txid };
117
+ }
109
118
  else {
110
119
  throw new api_1.SwapError('[executeTrade]: unsupported trade', {
111
120
  code: api_1.SwapErrorTypes.SIGN_AND_BROADCAST_FAILED,
@@ -6,6 +6,7 @@ const api_1 = require("../../../api");
6
6
  const constants_1 = require("../../utils/constants");
7
7
  const getTradeQuote_1 = require("../getThorTradeQuote/getTradeQuote");
8
8
  const getThorTxData_1 = require("../utils/bitcoin/utils/getThorTxData");
9
+ const cosmosTxData_1 = require("../utils/cosmos/cosmosTxData");
9
10
  const makeTradeTx_1 = require("../utils/ethereum/makeTradeTx");
10
11
  const buildTrade = async ({ deps, input }) => {
11
12
  var _a, _b, _c, _d;
@@ -75,6 +76,26 @@ const buildTrade = async ({ deps, input }) => {
75
76
  txData: buildTxResponse.txToSign
76
77
  };
77
78
  }
79
+ else if (input.chainId === types_1.KnownChainIds.CosmosMainnet) {
80
+ const txData = await (0, cosmosTxData_1.cosmosTxData)({
81
+ deps,
82
+ sellAdapter: sellAdapter,
83
+ sellAmount,
84
+ sellAsset,
85
+ slippageTolerance,
86
+ chainId: input.chainId,
87
+ buyAsset,
88
+ wallet,
89
+ destinationAddress,
90
+ quote: quote
91
+ });
92
+ return {
93
+ chainId: types_1.KnownChainIds.CosmosMainnet,
94
+ ...quote,
95
+ receiveAddress: destinationAddress,
96
+ txData
97
+ };
98
+ }
78
99
  else {
79
100
  throw new api_1.SwapError('[buildTrade]: unsupported chain id', {
80
101
  code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED,
@@ -21,8 +21,8 @@ const getThorTradeQuote = async ({ deps, input }) => {
21
21
  });
22
22
  try {
23
23
  const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(sellAsset.assetId);
24
- const adapter = deps.adapterManager.get(chainId);
25
- if (!adapter)
24
+ const sellAdapter = deps.adapterManager.get(chainId);
25
+ if (!sellAdapter)
26
26
  throw new api_1.SwapError(`[getThorTradeQuote] - No chain adapter found for ${chainId}.`, {
27
27
  code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
28
28
  details: { chainId }
@@ -97,6 +97,19 @@ const getThorTradeQuote = async ({ deps, input }) => {
97
97
  feeData
98
98
  };
99
99
  })();
100
+ case types_1.KnownChainIds.CosmosMainnet:
101
+ return (async () => {
102
+ const feeData = await sellAdapter.getFeeData({});
103
+ return {
104
+ ...commonQuoteFields,
105
+ allowanceContract: '0x0',
106
+ feeData: {
107
+ fee: feeData.fast.txFee,
108
+ tradeFee,
109
+ chainSpecific: { estimatedGas: feeData.fast.chainSpecific.gasLimit }
110
+ }
111
+ };
112
+ })();
100
113
  default:
101
114
  throw new api_1.SwapError('[getThorTradeQuote] - Asset chainId is not supported.', {
102
115
  code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
@@ -1,6 +1,6 @@
1
1
  import { ChainId } from '@shapeshiftoss/caip';
2
2
  import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
3
- import { BTCSignTx, ETHSignTx } from '@shapeshiftoss/hdwallet-core';
3
+ import { BTCSignTx, CosmosSignTx, ETHSignTx } from '@shapeshiftoss/hdwallet-core';
4
4
  import { KnownChainIds } from '@shapeshiftoss/types';
5
5
  import type Web3 from 'web3';
6
6
  import { Trade } from '../../api';
@@ -39,4 +39,8 @@ export interface EthThorTrade<C extends ChainId> extends Trade<C> {
39
39
  chainId: KnownChainIds.EthereumMainnet;
40
40
  txData: ETHSignTx;
41
41
  }
42
- export declare type ThorTrade<C extends ChainId> = BtcThorTrade<C> | EthThorTrade<C>;
42
+ export interface CosmosThorTrade<C extends ChainId> extends Trade<C> {
43
+ chainId: KnownChainIds.CosmosMainnet;
44
+ txData: CosmosSignTx;
45
+ }
46
+ export declare type ThorTrade<C extends ChainId> = BtcThorTrade<C> | EthThorTrade<C> | CosmosThorTrade<C>;
@@ -3,9 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getThorTxInfo = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
5
  const api_1 = require("../../../../../api");
6
- const bignumber_1 = require("../../../../utils/bignumber");
7
- const constants_1 = require("../../constants");
8
- const getTradeRate_1 = require("../../getTradeRate/getTradeRate");
6
+ const getLimit_1 = require("../../getLimit/getLimit");
9
7
  const makeSwapMemo_1 = require("../../makeSwapMemo/makeSwapMemo");
10
8
  const thorService_1 = require("../../thorService");
11
9
  const getThorTxInfo = async ({ deps, sellAsset, buyAsset, sellAmount, slippageTolerance, destinationAddress, wallet, bip44Params, accountType, tradeFee }) => {
@@ -18,20 +16,16 @@ const getThorTxInfo = async ({ deps, sellAsset, buyAsset, sellAmount, slippageTo
18
16
  code: api_1.SwapErrorTypes.RESPONSE_ERROR,
19
17
  details: { inboundAddresses }
20
18
  });
21
- const tradeRate = await (0, getTradeRate_1.getTradeRate)(sellAsset, buyAsset.assetId, sellAmount, deps);
22
- const expectedBuyAmountPrecision8 = (0, bignumber_1.toBaseUnit)((0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(sellAmount).times(tradeRate), sellAsset.precision), constants_1.THORCHAIN_FIXED_PRECISION);
23
- const isValidSlippageRange = (0, bignumber_1.bnOrZero)(slippageTolerance).gte(0) && (0, bignumber_1.bnOrZero)(slippageTolerance).lte(1);
24
- if ((0, bignumber_1.bnOrZero)(expectedBuyAmountPrecision8).lt(0) || !isValidSlippageRange)
25
- throw new api_1.SwapError('[getThorTxInfo]: bad expected buy amount or bad slippage tolerance', {
26
- code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED,
27
- details: { expectedBuyAmountPrecision8, slippageTolerance }
28
- });
29
- const tradeFeePrecision8 = (0, bignumber_1.toBaseUnit)((0, bignumber_1.bnOrZero)(tradeFee), constants_1.THORCHAIN_FIXED_PRECISION);
30
- const limit = (0, bignumber_1.bnOrZero)(expectedBuyAmountPrecision8)
31
- .times((0, bignumber_1.bn)(1).minus(slippageTolerance))
32
- .minus((0, bignumber_1.bnOrZero)(tradeFeePrecision8))
33
- .decimalPlaces(0)
34
- .toString();
19
+ const limit = await (0, getLimit_1.getLimit)({
20
+ buyAssetId: buyAsset.assetId,
21
+ destinationAddress,
22
+ sellAmount,
23
+ sellAsset,
24
+ buyAsset,
25
+ slippageTolerance,
26
+ deps,
27
+ tradeFee
28
+ });
35
29
  const memo = (0, makeSwapMemo_1.makeSwapMemo)({
36
30
  buyAssetId: buyAsset.assetId,
37
31
  destinationAddress,
@@ -0,0 +1,19 @@
1
+ import { Asset } from '@shapeshiftoss/asset-service';
2
+ import { ChainId } from '@shapeshiftoss/caip';
3
+ import { ChainAdapter } from '@shapeshiftoss/chain-adapters';
4
+ import { HDWallet } from '@shapeshiftoss/hdwallet-core';
5
+ import { KnownChainIds } from '@shapeshiftoss/types';
6
+ import { TradeQuote } from '../../../../api';
7
+ import { ThorchainSwapperDeps } from '../../types';
8
+ export declare const cosmosTxData: (input: {
9
+ destinationAddress: string;
10
+ deps: ThorchainSwapperDeps;
11
+ sellAmount: string;
12
+ sellAsset: Asset;
13
+ buyAsset: Asset;
14
+ slippageTolerance: string;
15
+ wallet: HDWallet;
16
+ quote: TradeQuote<KnownChainIds.CosmosMainnet>;
17
+ chainId: ChainId;
18
+ sellAdapter: ChainAdapter<KnownChainIds.CosmosMainnet>;
19
+ }) => Promise<import("@shapeshiftoss/hdwallet-core").CosmosSignTx>;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cosmosTxData = void 0;
4
+ const api_1 = require("../../../../api");
5
+ const getLimit_1 = require("../getLimit/getLimit");
6
+ const makeSwapMemo_1 = require("../makeSwapMemo/makeSwapMemo");
7
+ const thorService_1 = require("../thorService");
8
+ const cosmosTxData = async (input) => {
9
+ const { deps, destinationAddress, sellAmount, sellAsset, buyAsset, slippageTolerance, quote, wallet, sellAdapter } = input;
10
+ const { data: inboundAddresses } = await thorService_1.thorService.get(`${deps.midgardUrl}/thorchain/inbound_addresses`);
11
+ const atomInboundAddresses = inboundAddresses.find((inbound) => inbound.chain === 'GAIA');
12
+ const vault = atomInboundAddresses === null || atomInboundAddresses === void 0 ? void 0 : atomInboundAddresses.address;
13
+ if (!vault)
14
+ throw new api_1.SwapError('[buildTrade]: no vault for chain', {
15
+ code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED,
16
+ fn: 'buildTrade',
17
+ details: { chainId: input.chainId }
18
+ });
19
+ const limit = await (0, getLimit_1.getLimit)({
20
+ buyAssetId: buyAsset.assetId,
21
+ destinationAddress,
22
+ sellAmount,
23
+ sellAsset,
24
+ buyAsset,
25
+ slippageTolerance,
26
+ deps,
27
+ tradeFee: quote.feeData.tradeFee
28
+ });
29
+ const memo = (0, makeSwapMemo_1.makeSwapMemo)({
30
+ buyAssetId: buyAsset.assetId,
31
+ destinationAddress,
32
+ limit
33
+ });
34
+ const buildTxResponse = await sellAdapter.buildSendTransaction({
35
+ value: sellAmount,
36
+ wallet,
37
+ to: vault,
38
+ memo,
39
+ chainSpecific: {
40
+ gas: quote.feeData.chainSpecific.estimatedGas,
41
+ fee: quote.feeData.fee
42
+ }
43
+ });
44
+ return buildTxResponse.txToSign;
45
+ };
46
+ exports.cosmosTxData = cosmosTxData;
@@ -3,9 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getThorTxInfo = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
5
  const api_1 = require("../../../../../api");
6
- const bignumber_1 = require("../../../../utils/bignumber");
7
- const getTradeRate_1 = require("../../../utils/getTradeRate/getTradeRate");
8
- const constants_1 = require("../../constants");
6
+ const getLimit_1 = require("../../getLimit/getLimit");
9
7
  const makeSwapMemo_1 = require("../../makeSwapMemo/makeSwapMemo");
10
8
  const thorService_1 = require("../../thorService");
11
9
  const routerCalldata_1 = require("../routerCalldata");
@@ -22,20 +20,16 @@ const getThorTxInfo = async ({ deps, sellAsset, buyAsset, sellAmount, slippageTo
22
20
  code: api_1.SwapErrorTypes.RESPONSE_ERROR,
23
21
  details: { inboundAddresses }
24
22
  });
25
- const tradeRate = await (0, getTradeRate_1.getTradeRate)(sellAsset, buyAsset.assetId, sellAmount, deps);
26
- const expectedBuyAmountPrecision8 = (0, bignumber_1.toBaseUnit)((0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(sellAmount).times(tradeRate), sellAsset.precision), constants_1.THORCHAIN_FIXED_PRECISION);
27
- const tradeFeePrecision8 = (0, bignumber_1.toBaseUnit)((0, bignumber_1.bnOrZero)(tradeFee), constants_1.THORCHAIN_FIXED_PRECISION);
28
- const isValidSlippageRange = (0, bignumber_1.bnOrZero)(slippageTolerance).gte(0) && (0, bignumber_1.bnOrZero)(slippageTolerance).lte(1);
29
- if ((0, bignumber_1.bnOrZero)(expectedBuyAmountPrecision8).lt(0) || !isValidSlippageRange)
30
- throw new api_1.SwapError('[makeTradeTx]: bad expected buy amount or bad slippage tolerance', {
31
- code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED,
32
- details: { expectedBuyAmountPrecision8, slippageTolerance }
33
- });
34
- const limit = (0, bignumber_1.bnOrZero)(expectedBuyAmountPrecision8)
35
- .times((0, bignumber_1.bn)(1).minus(slippageTolerance))
36
- .minus((0, bignumber_1.bnOrZero)(tradeFeePrecision8))
37
- .decimalPlaces(0)
38
- .toString();
23
+ const limit = await (0, getLimit_1.getLimit)({
24
+ buyAssetId: buyAsset.assetId,
25
+ destinationAddress,
26
+ sellAmount,
27
+ sellAsset,
28
+ buyAsset,
29
+ slippageTolerance,
30
+ deps,
31
+ tradeFee
32
+ });
39
33
  const memo = (0, makeSwapMemo_1.makeSwapMemo)({
40
34
  buyAssetId: buyAsset.assetId,
41
35
  destinationAddress,
@@ -0,0 +1,12 @@
1
+ import { Asset } from '@shapeshiftoss/asset-service';
2
+ import { ThorchainSwapperDeps } from '../../types';
3
+ export declare const getLimit: ({ sellAsset, buyAsset, sellAmount, deps, slippageTolerance, tradeFee }: {
4
+ buyAssetId: string;
5
+ destinationAddress: string;
6
+ sellAsset: Asset;
7
+ buyAsset: Asset;
8
+ sellAmount: string;
9
+ deps: ThorchainSwapperDeps;
10
+ slippageTolerance: string;
11
+ tradeFee: string;
12
+ }) => Promise<string>;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getLimit = void 0;
4
+ const api_1 = require("../../../../api");
5
+ const bignumber_1 = require("../../../utils/bignumber");
6
+ const constants_1 = require("../constants");
7
+ const getTradeRate_1 = require("../getTradeRate/getTradeRate");
8
+ const getLimit = async ({ sellAsset, buyAsset, sellAmount, deps, slippageTolerance, tradeFee }) => {
9
+ const tradeRate = await (0, getTradeRate_1.getTradeRate)(sellAsset, buyAsset.assetId, sellAmount, deps);
10
+ const expectedBuyAmountPrecision8 = (0, bignumber_1.toBaseUnit)((0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(sellAmount).times(tradeRate), sellAsset.precision), constants_1.THORCHAIN_FIXED_PRECISION);
11
+ const isValidSlippageRange = (0, bignumber_1.bnOrZero)(slippageTolerance).gte(0) && (0, bignumber_1.bnOrZero)(slippageTolerance).lte(1);
12
+ if ((0, bignumber_1.bnOrZero)(expectedBuyAmountPrecision8).lt(0) || !isValidSlippageRange)
13
+ throw new api_1.SwapError('[getThorTxInfo]: bad expected buy amount or bad slippage tolerance', {
14
+ code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED,
15
+ details: { expectedBuyAmountPrecision8, slippageTolerance }
16
+ });
17
+ const tradeFeePrecision8 = (0, bignumber_1.toBaseUnit)((0, bignumber_1.bnOrZero)(tradeFee), constants_1.THORCHAIN_FIXED_PRECISION);
18
+ return (0, bignumber_1.bnOrZero)(expectedBuyAmountPrecision8)
19
+ .times((0, bignumber_1.bn)(1).minus(slippageTolerance))
20
+ .minus((0, bignumber_1.bnOrZero)(tradeFeePrecision8))
21
+ .decimalPlaces(0)
22
+ .toString();
23
+ };
24
+ exports.getLimit = getLimit;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "9.4.0",
3
+ "version": "9.5.0",
4
4
  "description": "An interface and implementations to trade coins via various protocols",
5
5
  "repository": "https://github.com/shapeshift/lib",
6
6
  "license": "MIT",