@shapeshiftoss/swapper 6.3.0 → 7.2.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.
Files changed (51) hide show
  1. package/dist/api.d.ts +25 -27
  2. package/dist/api.js +1 -0
  3. package/dist/manager/SwapperManager.d.ts +6 -5
  4. package/dist/swappercli.js +34 -17
  5. package/dist/swappers/cow/CowApprovalNeeded/CowApprovalNeeded.d.ts +3 -0
  6. package/dist/swappers/cow/CowApprovalNeeded/CowApprovalNeeded.js +42 -0
  7. package/dist/swappers/cow/CowApproveInfinite/CowApproveInfinite.d.ts +3 -0
  8. package/dist/swappers/cow/CowApproveInfinite/CowApproveInfinite.js +31 -0
  9. package/dist/swappers/cow/CowSwapper.d.ts +11 -7
  10. package/dist/swappers/cow/CowSwapper.js +13 -8
  11. package/dist/swappers/cow/getCowSwapMinMax/getCowSwapMinMax.d.ts +4 -0
  12. package/dist/swappers/cow/getCowSwapMinMax/getCowSwapMinMax.js +28 -0
  13. package/dist/swappers/cow/utils/blacklist.d.ts +1 -0
  14. package/dist/swappers/cow/utils/blacklist.js +9 -0
  15. package/dist/swappers/cow/utils/constants.d.ts +2 -0
  16. package/dist/swappers/cow/utils/constants.js +4 -0
  17. package/dist/swappers/index.d.ts +1 -0
  18. package/dist/swappers/index.js +1 -0
  19. package/dist/swappers/test/TestSwapper.d.ts +5 -5
  20. package/dist/swappers/thorchain/ThorchainSwapper.d.ts +6 -6
  21. package/dist/swappers/thorchain/ThorchainSwapper.js +6 -3
  22. package/dist/swappers/thorchain/getThorTradeQuote/getTradeQuote.d.ts +2 -2
  23. package/dist/swappers/thorchain/getThorTradeQuote/getTradeQuote.js +9 -5
  24. package/dist/swappers/thorchain/utils/txFeeHelpers/ethTxFees/getEthTxFees.js +7 -1
  25. package/dist/swappers/{zrx/utils → utils}/abi/erc20-abi.d.ts +0 -0
  26. package/dist/swappers/{zrx/utils → utils}/abi/erc20-abi.js +0 -0
  27. package/dist/swappers/{zrx/utils → utils}/abi/erc20Allowance-abi.d.ts +0 -0
  28. package/dist/swappers/{zrx/utils → utils}/abi/erc20Allowance-abi.js +0 -0
  29. package/dist/swappers/utils/helpers/helpers.d.ts +33 -0
  30. package/dist/swappers/utils/helpers/helpers.js +98 -1
  31. package/dist/swappers/utils/test-data/assets.js +0 -13
  32. package/dist/swappers/utils/test-data/setupDeps.d.ts +6 -0
  33. package/dist/swappers/utils/test-data/setupDeps.js +48 -0
  34. package/dist/swappers/utils/test-data/setupSwapQuote.d.ts +8 -0
  35. package/dist/swappers/utils/test-data/setupSwapQuote.js +30 -0
  36. package/dist/swappers/zrx/ZrxApprovalNeeded/ZrxApprovalNeeded.d.ts +1 -1
  37. package/dist/swappers/zrx/ZrxApprovalNeeded/ZrxApprovalNeeded.js +3 -4
  38. package/dist/swappers/zrx/ZrxApproveInfinite/ZrxApproveInfinite.d.ts +1 -2
  39. package/dist/swappers/zrx/ZrxApproveInfinite/ZrxApproveInfinite.js +4 -4
  40. package/dist/swappers/zrx/ZrxSwapper.d.ts +7 -7
  41. package/dist/swappers/zrx/utils/helpers/helpers.d.ts +1 -33
  42. package/dist/swappers/zrx/utils/helpers/helpers.js +1 -97
  43. package/dist/swappers/zrx/utils/test-data/{setupSwapQuote.d.ts → setupZrxSwapQuote.d.ts} +0 -7
  44. package/dist/swappers/zrx/utils/test-data/{setupSwapQuote.js → setupZrxSwapQuote.js} +1 -27
  45. package/dist/swappers/zrx/zrxBuildTrade/zrxBuildTrade.d.ts +1 -2
  46. package/dist/swappers/zrx/zrxBuildTrade/zrxBuildTrade.js +10 -12
  47. package/dist/swappers/zrx/zrxExecuteTrade/zrxExecuteTrade.d.ts +1 -1
  48. package/dist/swappers/zrx/zrxExecuteTrade/zrxExecuteTrade.js +1 -2
  49. package/package.json +10 -10
  50. package/dist/swappers/zrx/utils/test-data/setupZrxDeps.d.ts +0 -32
  51. package/dist/swappers/zrx/utils/test-data/setupZrxDeps.js +0 -46
package/dist/api.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { AssetId } from '@shapeshiftoss/caip';
1
+ import { AssetId, ChainId } from '@shapeshiftoss/caip';
2
2
  import { BTCSignTx, ETHSignTx, HDWallet } from '@shapeshiftoss/hdwallet-core';
3
- import { Asset, ChainSpecific, SupportedChainIds } from '@shapeshiftoss/types';
3
+ import { Asset, ChainSpecific, KnownChainIds } from '@shapeshiftoss/types';
4
4
  export declare const SwapError: {
5
5
  new <V extends {
6
6
  cause?: unknown;
@@ -23,10 +23,7 @@ export declare const SwapError: {
23
23
  prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
24
24
  stackTraceLimit: number;
25
25
  };
26
- export declare type SupportedAssetInput = {
27
- assetIds: AssetId[];
28
- };
29
- declare type ChainSpecificQuoteFeeData<T1> = ChainSpecific<T1, {
26
+ declare type ChainSpecificQuoteFeeData<T extends ChainId> = ChainSpecific<T, {
30
27
  'eip155:1': {
31
28
  estimatedGas?: string;
32
29
  gasPrice?: string;
@@ -38,10 +35,10 @@ declare type ChainSpecificQuoteFeeData<T1> = ChainSpecific<T1, {
38
35
  satsPerByte: string;
39
36
  };
40
37
  }>;
41
- export declare type QuoteFeeData<T1 extends SupportedChainIds> = {
38
+ export declare type QuoteFeeData<T extends ChainId> = {
42
39
  fee: string;
43
40
  tradeFee: string;
44
- } & ChainSpecificQuoteFeeData<T1>;
41
+ } & ChainSpecificQuoteFeeData<T>;
45
42
  export declare type ByPairInput = {
46
43
  sellAssetId: AssetId;
47
44
  buyAssetId: AssetId;
@@ -67,7 +64,7 @@ export declare type BuildTradeInput = CommonTradeInput & {
67
64
  slippage?: string;
68
65
  wallet: HDWallet;
69
66
  };
70
- interface TradeBase<C extends SupportedChainIds> {
67
+ interface TradeBase<C extends ChainId> {
71
68
  buyAmount: string;
72
69
  sellAmount: string;
73
70
  feeData: QuoteFeeData<C>;
@@ -77,39 +74,39 @@ interface TradeBase<C extends SupportedChainIds> {
77
74
  sellAsset: Asset;
78
75
  sellAssetAccountNumber: number;
79
76
  }
80
- export interface TradeQuote<C extends SupportedChainIds> extends TradeBase<C> {
77
+ export interface TradeQuote<C extends ChainId> extends TradeBase<C> {
81
78
  allowanceContract: string;
82
79
  minimum: string;
83
80
  maximum: string;
84
81
  }
85
- export interface Trade<C extends SupportedChainIds> extends TradeBase<C> {
82
+ export interface Trade<C extends ChainId> extends TradeBase<C> {
86
83
  receiveAddress: string;
87
84
  }
88
- export interface ZrxTrade<C extends SupportedChainIds> extends Trade<C> {
85
+ export interface ZrxTrade extends Trade<'eip155:1'> {
89
86
  txData: string;
90
87
  depositAddress: string;
91
88
  }
92
- export interface BtcThorTrade<C extends SupportedChainIds> extends Trade<C> {
93
- chainId: 'bip122:000000000019d6689c085ae165831e93';
89
+ export interface BtcThorTrade<C extends ChainId> extends Trade<C> {
90
+ chainId: KnownChainIds.BitcoinMainnet;
94
91
  txData: BTCSignTx;
95
92
  }
96
- export interface EthThorTrade<C extends SupportedChainIds> extends Trade<C> {
97
- chainId: 'eip155:1';
93
+ export interface EthThorTrade<C extends ChainId> extends Trade<C> {
94
+ chainId: KnownChainIds.EthereumMainnet;
98
95
  txData: ETHSignTx;
99
96
  }
100
- export declare type ThorTrade<C extends SupportedChainIds> = BtcThorTrade<C> | EthThorTrade<C>;
101
- export declare type ExecuteTradeInput<C extends SupportedChainIds> = {
97
+ export declare type ThorTrade<C extends ChainId> = BtcThorTrade<C> | EthThorTrade<C>;
98
+ export declare type ExecuteTradeInput<C extends ChainId> = {
102
99
  trade: Trade<C>;
103
100
  wallet: HDWallet;
104
101
  };
105
102
  export declare type TradeResult = {
106
103
  tradeId: string;
107
104
  };
108
- export declare type ApproveInfiniteInput<C extends SupportedChainIds> = {
105
+ export declare type ApproveInfiniteInput<C extends ChainId> = {
109
106
  quote: TradeQuote<C>;
110
107
  wallet: HDWallet;
111
108
  };
112
- export declare type ApprovalNeededInput<C extends SupportedChainIds> = {
109
+ export declare type ApprovalNeededInput<C extends ChainId> = {
113
110
  quote: TradeQuote<C>;
114
111
  wallet: HDWallet;
115
112
  };
@@ -154,12 +151,13 @@ export declare enum SwapErrorTypes {
154
151
  UNSUPPORTED_PAIR = "UNSUPPORTED_PAIR",
155
152
  USD_RATE_FAILED = "USD_RATE_FAILED",
156
153
  UNSUPPORTED_CHAIN = "UNSUPPORTED_CHAIN",
154
+ UNSUPPORTED_NAMESPACE = "UNSUPPORTED_NAMESPACE",
157
155
  VALIDATION_FAILED = "VALIDATION_FAILED",
158
156
  MAKE_MEMO_FAILED = "MAKE_MEMO_FAILED",
159
157
  PRICE_RATIO_FAILED = "PRICE_RATIO_FAILED",
160
158
  POOL_NOT_FOUND = "POOL_NOT_FOUND"
161
159
  }
162
- export interface Swapper {
160
+ export interface Swapper<T extends ChainId, TxType = unknown> {
163
161
  /** perform any necessary async initialization */
164
162
  initialize(): Promise<void>;
165
163
  /** Returns the swapper type */
@@ -167,11 +165,11 @@ export interface Swapper {
167
165
  /**
168
166
  * Get builds a trade with definitive rate & txData that can be executed with executeTrade
169
167
  **/
170
- buildTrade(args: BuildTradeInput): Promise<Trade<SupportedChainIds>>;
168
+ buildTrade(args: BuildTradeInput): Promise<Trade<T>>;
171
169
  /**
172
170
  * Get a trade quote
173
171
  */
174
- getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<SupportedChainIds>>;
172
+ getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<T>>;
175
173
  /**
176
174
  * Get the usd rate from either the assets symbol or tokenId
177
175
  */
@@ -179,15 +177,15 @@ export interface Swapper {
179
177
  /**
180
178
  * Execute a trade built with buildTrade by signing and broadcasting
181
179
  */
182
- executeTrade(args: ExecuteTradeInput<SupportedChainIds>): Promise<TradeResult>;
180
+ executeTrade(args: ExecuteTradeInput<T>): Promise<TradeResult>;
183
181
  /**
184
182
  * Get a boolean if a quote needs approval
185
183
  */
186
- approvalNeeded(args: ApprovalNeededInput<SupportedChainIds>): Promise<ApprovalNeededOutput>;
184
+ approvalNeeded(args: ApprovalNeededInput<T>): Promise<ApprovalNeededOutput>;
187
185
  /**
188
186
  * Get the txid of an approve infinite transaction
189
187
  */
190
- approveInfinite(args: ApproveInfiniteInput<SupportedChainIds>): Promise<string>;
188
+ approveInfinite(args: ApproveInfiniteInput<T>): Promise<string>;
191
189
  /**
192
190
  * Get supported buyAssetId's by sellAssetId
193
191
  */
@@ -196,6 +194,6 @@ export interface Swapper {
196
194
  * Get supported sell assetIds
197
195
  */
198
196
  filterAssetIdsBySellable(assetIds: AssetId[]): AssetId[];
199
- getTradeTxs(tradeResult: TradeResult): Promise<TradeTxs>;
197
+ getTradeTxs(tradeResult: TradeResult): Promise<TxType>;
200
198
  }
201
199
  export {};
package/dist/api.js CHANGED
@@ -28,6 +28,7 @@ var SwapErrorTypes;
28
28
  SwapErrorTypes["UNSUPPORTED_PAIR"] = "UNSUPPORTED_PAIR";
29
29
  SwapErrorTypes["USD_RATE_FAILED"] = "USD_RATE_FAILED";
30
30
  SwapErrorTypes["UNSUPPORTED_CHAIN"] = "UNSUPPORTED_CHAIN";
31
+ SwapErrorTypes["UNSUPPORTED_NAMESPACE"] = "UNSUPPORTED_NAMESPACE";
31
32
  SwapErrorTypes["VALIDATION_FAILED"] = "VALIDATION_FAILED";
32
33
  SwapErrorTypes["MAKE_MEMO_FAILED"] = "MAKE_MEMO_FAILED";
33
34
  SwapErrorTypes["PRICE_RATIO_FAILED"] = "PRICE_RATIO_FAILED";
@@ -1,7 +1,8 @@
1
+ import { ChainId } from '@shapeshiftoss/caip';
1
2
  import { BuyAssetBySellIdInput, ByPairInput, SupportedSellAssetsInput, Swapper } from '..';
2
3
  import { SwapperType } from '../api';
3
4
  export declare class SwapperManager {
4
- swappers: Map<SwapperType, Swapper>;
5
+ swappers: Map<SwapperType, Swapper<ChainId>>;
5
6
  constructor();
6
7
  /**
7
8
  *
@@ -9,27 +10,27 @@ export declare class SwapperManager {
9
10
  * @param swapperInstance swapper instance {Swapper}
10
11
  * @returns {SwapperManager}
11
12
  */
12
- addSwapper(swapperType: SwapperType, swapperInstance: Swapper): this;
13
+ addSwapper(swapperType: SwapperType, swapperInstance: Swapper<ChainId>): this;
13
14
  /**
14
15
  *
15
16
  * @param swapperType swapper type {SwapperType|string}
16
17
  * @returns {Swapper}
17
18
  * @deprecated this will be removed, currently used in swapper tests
18
19
  */
19
- getSwapper(swapperType: SwapperType): Swapper;
20
+ getSwapper(swapperType: SwapperType): Swapper<ChainId>;
20
21
  /**
21
22
  *
22
23
  * @param swapperType swapper type {SwapperType|string}
23
24
  * @returns {SwapperManager}
24
25
  */
25
26
  removeSwapper(swapperType: SwapperType): this;
26
- getBestSwapper(args: ByPairInput): Promise<Swapper | undefined>;
27
+ getBestSwapper(args: ByPairInput): Promise<Swapper<ChainId> | undefined>;
27
28
  /**
28
29
  *
29
30
  * @param pair type {GetQuoteInput}
30
31
  * @returns {SwapperType}
31
32
  */
32
- getSwappersByPair(pair: ByPairInput): Swapper[];
33
+ getSwappersByPair(pair: ByPairInput): Swapper<ChainId>[];
33
34
  getSupportedBuyAssetIdsFromSellId(args: BuyAssetBySellIdInput): import("@shapeshiftoss/caip").AssetId[];
34
35
  getSupportedSellableAssetIds(args: SupportedSellAssetsInput): import("@shapeshiftoss/caip").AssetId[];
35
36
  }
@@ -1,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
@@ -6,14 +29,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
29
  const asset_service_1 = require("@shapeshiftoss/asset-service");
7
30
  const chain_adapters_1 = require("@shapeshiftoss/chain-adapters");
8
31
  const hdwallet_native_1 = require("@shapeshiftoss/hdwallet-native");
9
- const types_1 = require("@shapeshiftoss/types");
32
+ const unchained = __importStar(require("@shapeshiftoss/unchained-client"));
10
33
  const bignumber_js_1 = __importDefault(require("bignumber.js"));
11
34
  const dotenv_1 = __importDefault(require("dotenv"));
12
35
  const readline_sync_1 = __importDefault(require("readline-sync"));
13
36
  const web3_1 = __importDefault(require("web3"));
14
37
  const api_1 = require("./api");
15
38
  const SwapperManager_1 = require("./manager/SwapperManager");
16
- const ThorchainSwapper_1 = require("./swappers/thorchain/ThorchainSwapper");
17
39
  const ZrxSwapper_1 = require("./swappers/zrx/ZrxSwapper");
18
40
  dotenv_1.default.config();
19
41
  const { UNCHAINED_HTTP_API = 'http://localhost:31300', UNCHAINED_WS_API = 'wss://localhost:31300', ETH_NODE_URL = 'http://localhost:3000', DEVICE_ID = 'device123', MNEMONIC = 'all all all all all all all all all all all all' } = process.env;
@@ -62,26 +84,21 @@ const main = async () => {
62
84
  }
63
85
  // Swapper Deps
64
86
  const wallet = await getWallet();
65
- const unchainedUrls = {
66
- [types_1.ChainTypes.Ethereum]: {
67
- httpUrl: UNCHAINED_HTTP_API,
68
- wsUrl: UNCHAINED_WS_API
69
- }
70
- };
71
- const adapterManager = new chain_adapters_1.ChainAdapterManager(unchainedUrls);
87
+ const ethChainAdapter = new chain_adapters_1.ethereum.ChainAdapter({
88
+ providers: {
89
+ ws: new unchained.ws.Client(UNCHAINED_WS_API),
90
+ http: new unchained.ethereum.V1Api(new unchained.ethereum.Configuration({
91
+ basePath: UNCHAINED_HTTP_API
92
+ }))
93
+ },
94
+ rpcUrl: 'https://mainnet.infura.io/v3/d734c7eebcdf400185d7eb67322a7e57'
95
+ });
72
96
  const web3Provider = new web3_1.default.providers.HttpProvider(ETH_NODE_URL);
73
97
  const web3 = new web3_1.default(web3Provider);
74
- const zrxSwapperDeps = { wallet, adapterManager, web3 };
75
- const thorchainSwapperDeps = {
76
- midgardUrl: 'https://midgard.thorchain.info/v2',
77
- adapterManager
78
- };
98
+ const zrxSwapperDeps = { wallet, adapter: ethChainAdapter, web3 };
79
99
  const manager = new SwapperManager_1.SwapperManager();
80
100
  const zrxSwapper = new ZrxSwapper_1.ZrxSwapper(zrxSwapperDeps);
81
- const thorchainSwapper = new ThorchainSwapper_1.ThorchainSwapper(thorchainSwapperDeps);
82
- await thorchainSwapper.initialize();
83
101
  manager.addSwapper(api_1.SwapperType.Zrx, zrxSwapper);
84
- manager.addSwapper(api_1.SwapperType.Thorchain, thorchainSwapper);
85
102
  const swapper = await manager.getBestSwapper({
86
103
  sellAssetId: 'eip155:1/slip44:60',
87
104
  buyAssetId: 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d'
@@ -0,0 +1,3 @@
1
+ import { ApprovalNeededInput, ApprovalNeededOutput } from '../../../api';
2
+ import { CowSwapperDeps } from '../CowSwapper';
3
+ export declare function CowApprovalNeeded({ adapter, web3 }: CowSwapperDeps, { quote, wallet }: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CowApprovalNeeded = void 0;
4
+ const caip_1 = require("@shapeshiftoss/caip");
5
+ const api_1 = require("../../../api");
6
+ const erc20Allowance_abi_1 = require("../../utils/abi/erc20Allowance-abi");
7
+ const bignumber_1 = require("../../utils/bignumber");
8
+ const helpers_1 = require("../../utils/helpers/helpers");
9
+ const COW_SWAP_VAULT_RELAYER_ADDRESS = '0xc92e8bdf79f0507f65a392b0ab4667716bfe0110';
10
+ async function CowApprovalNeeded({ adapter, web3 }, { quote, wallet }) {
11
+ const { sellAsset } = quote;
12
+ const { assetReference: sellAssetErc20Address, assetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
13
+ try {
14
+ if (assetNamespace !== 'erc20') {
15
+ throw new api_1.SwapError('[CowApprovalNeeded] - unsupported asset namespace', {
16
+ code: api_1.SwapErrorTypes.UNSUPPORTED_NAMESPACE,
17
+ details: { assetNamespace }
18
+ });
19
+ }
20
+ const receiveAddress = await adapter.getAddress({ wallet });
21
+ const allowanceResult = await (0, helpers_1.getERC20Allowance)({
22
+ web3,
23
+ erc20AllowanceAbi: erc20Allowance_abi_1.erc20AllowanceAbi,
24
+ sellAssetErc20Address,
25
+ spenderAddress: COW_SWAP_VAULT_RELAYER_ADDRESS,
26
+ ownerAddress: receiveAddress
27
+ });
28
+ const allowanceOnChain = (0, bignumber_1.bnOrZero)(allowanceResult);
29
+ return {
30
+ approvalNeeded: allowanceOnChain.lte((0, bignumber_1.bnOrZero)(quote.sellAmount))
31
+ };
32
+ }
33
+ catch (e) {
34
+ if (e instanceof api_1.SwapError)
35
+ throw e;
36
+ throw new api_1.SwapError('[CowApprovalNeeded]', {
37
+ cause: e,
38
+ code: api_1.SwapErrorTypes.CHECK_APPROVAL_FAILED
39
+ });
40
+ }
41
+ }
42
+ exports.CowApprovalNeeded = CowApprovalNeeded;
@@ -0,0 +1,3 @@
1
+ import { ApproveInfiniteInput } from '../../../api';
2
+ import { CowSwapperDeps } from '../CowSwapper';
3
+ export declare function CowApproveInfinite({ adapter, web3 }: CowSwapperDeps, { quote, wallet }: ApproveInfiniteInput<'eip155:1'>): Promise<string>;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CowApproveInfinite = void 0;
4
+ const api_1 = require("../../../api");
5
+ const erc20_abi_1 = require("../../utils/abi/erc20-abi");
6
+ const helpers_1 = require("../../utils/helpers/helpers");
7
+ const constants_1 = require("../utils/constants");
8
+ async function CowApproveInfinite({ adapter, web3 }, { quote, wallet }) {
9
+ try {
10
+ const allowanceGrantRequired = await (0, helpers_1.grantAllowance)({
11
+ quote: {
12
+ ...quote,
13
+ sellAmount: constants_1.MAX_ALLOWANCE
14
+ },
15
+ wallet,
16
+ adapter,
17
+ erc20Abi: erc20_abi_1.erc20Abi,
18
+ web3
19
+ });
20
+ return allowanceGrantRequired;
21
+ }
22
+ catch (e) {
23
+ if (e instanceof api_1.SwapError)
24
+ throw e;
25
+ throw new api_1.SwapError('[CowApproveInfinite]', {
26
+ cause: e,
27
+ code: api_1.SwapErrorTypes.APPROVE_INFINITE_FAILED
28
+ });
29
+ }
30
+ }
31
+ exports.CowApproveInfinite = CowApproveInfinite;
@@ -1,21 +1,25 @@
1
1
  import { AssetId } from '@shapeshiftoss/caip';
2
- import { Asset, SupportedChainIds } from '@shapeshiftoss/types';
2
+ import { ethereum } from '@shapeshiftoss/chain-adapters';
3
+ import { Asset } from '@shapeshiftoss/types';
4
+ import Web3 from 'web3';
3
5
  import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
4
6
  export declare type CowSwapperDeps = {
5
7
  apiUrl: string;
8
+ adapter: ethereum.ChainAdapter;
9
+ web3: Web3;
6
10
  };
7
- export declare class CowSwapper implements Swapper {
11
+ export declare class CowSwapper implements Swapper<'eip155:1'> {
8
12
  static swapperName: string;
9
13
  deps: CowSwapperDeps;
10
14
  constructor(deps: CowSwapperDeps);
11
15
  initialize(): Promise<void>;
12
16
  getType(): SwapperType;
13
- buildTrade(args: BuildTradeInput): Promise<Trade<SupportedChainIds>>;
14
- getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<SupportedChainIds>>;
17
+ buildTrade(args: BuildTradeInput): Promise<Trade<'eip155:1'>>;
18
+ getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<'eip155:1'>>;
15
19
  getUsdRate(input: Asset): Promise<string>;
16
- executeTrade(args: ExecuteTradeInput<SupportedChainIds>): Promise<TradeResult>;
17
- approvalNeeded(args: ApprovalNeededInput<SupportedChainIds>): Promise<ApprovalNeededOutput>;
18
- approveInfinite(args: ApproveInfiniteInput<SupportedChainIds>): Promise<string>;
20
+ executeTrade(args: ExecuteTradeInput<'eip155:1'>): Promise<TradeResult>;
21
+ approvalNeeded(args: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
22
+ approveInfinite(args: ApproveInfiniteInput<'eip155:1'>): Promise<string>;
19
23
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
20
24
  filterAssetIdsBySellable(assetIds: AssetId[]): AssetId[];
21
25
  getTradeTxs(): Promise<TradeTxs>;
@@ -2,6 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CowSwapper = void 0;
4
4
  const api_1 = require("../../api");
5
+ const CowApprovalNeeded_1 = require("./CowApprovalNeeded/CowApprovalNeeded");
6
+ const CowApproveInfinite_1 = require("./CowApproveInfinite/CowApproveInfinite");
7
+ const blacklist_1 = require("./utils/blacklist");
5
8
  const helpers_1 = require("./utils/helpers/helpers");
6
9
  class CowSwapper {
7
10
  constructor(deps) {
@@ -28,20 +31,22 @@ class CowSwapper {
28
31
  throw new Error('CowSwapper: executeTrade unimplemented');
29
32
  }
30
33
  async approvalNeeded(args) {
31
- console.info(args);
32
- throw new Error('CowSwapper: approvalNeeded unimplemented');
34
+ return (0, CowApprovalNeeded_1.CowApprovalNeeded)(this.deps, args);
33
35
  }
34
36
  async approveInfinite(args) {
35
- console.info(args);
36
- throw new Error('CowSwapper: approveInfinite unimplemented');
37
+ return (0, CowApproveInfinite_1.CowApproveInfinite)(this.deps, args);
37
38
  }
38
39
  filterBuyAssetsBySellAssetId(args) {
39
- console.info(args);
40
- return [];
40
+ const { assetIds = [], sellAssetId } = args;
41
+ if (!(sellAssetId === null || sellAssetId === void 0 ? void 0 : sellAssetId.startsWith('eip155:1/erc20')) ||
42
+ blacklist_1.COWSWAP_UNSUPPORTED_ASSETS.includes(sellAssetId))
43
+ return [];
44
+ return assetIds.filter((id) => id !== sellAssetId &&
45
+ id.startsWith('eip155:1/erc20') &&
46
+ !blacklist_1.COWSWAP_UNSUPPORTED_ASSETS.includes(id));
41
47
  }
42
48
  filterAssetIdsBySellable(assetIds) {
43
- console.info(assetIds);
44
- return [];
49
+ return assetIds.filter((id) => id.startsWith('eip155:1/erc20') && !blacklist_1.COWSWAP_UNSUPPORTED_ASSETS.includes(id));
45
50
  }
46
51
  async getTradeTxs() {
47
52
  throw new Error('CowSwapper: executeTrade unimplemented');
@@ -0,0 +1,4 @@
1
+ import { Asset } from '@shapeshiftoss/types';
2
+ import { MinMaxOutput } from '../../../api';
3
+ import { CowSwapperDeps } from '../CowSwapper';
4
+ export declare const getCowSwapMinMax: (deps: CowSwapperDeps, sellAsset: Asset, buyAsset: Asset) => Promise<MinMaxOutput>;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCowSwapMinMax = void 0;
4
+ const api_1 = require("../../../api");
5
+ const bignumber_1 = require("../../utils/bignumber");
6
+ const constants_1 = require("../utils/constants");
7
+ const helpers_1 = require("../utils/helpers/helpers");
8
+ const getCowSwapMinMax = async (deps, sellAsset, buyAsset) => {
9
+ try {
10
+ if (!sellAsset.assetId.startsWith('eip155:1/erc20') ||
11
+ !buyAsset.assetId.startsWith('eip155:1/erc20')) {
12
+ throw new api_1.SwapError('[getCowSwapMinMax]', { code: api_1.SwapErrorTypes.UNSUPPORTED_PAIR });
13
+ }
14
+ const usdRate = await (0, helpers_1.getUsdRate)(deps, sellAsset);
15
+ const minimum = (0, bignumber_1.bn)(1).dividedBy((0, bignumber_1.bnOrZero)(usdRate)).toString(); // $1 worth of the sell token.
16
+ const maximum = constants_1.MAX_COWSWAP_TRADE; // Arbitrarily large value. 10e+28 here.
17
+ return {
18
+ minimum,
19
+ maximum
20
+ };
21
+ }
22
+ catch (e) {
23
+ if (e instanceof api_1.SwapError)
24
+ throw e;
25
+ throw new api_1.SwapError('[getCowSwapMinMax]', { cause: e, code: api_1.SwapErrorTypes.MIN_MAX_FAILED });
26
+ }
27
+ };
28
+ exports.getCowSwapMinMax = getCowSwapMinMax;
@@ -0,0 +1 @@
1
+ export declare const COWSWAP_UNSUPPORTED_ASSETS: readonly string[];
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.COWSWAP_UNSUPPORTED_ASSETS = void 0;
4
+ // CowSwap doesnt have an easily accessible master assets list.
5
+ // We assume all erc20's are supported and remove these explicitely unsupported assets
6
+ exports.COWSWAP_UNSUPPORTED_ASSETS = Object.freeze([
7
+ // Foxy token unsupported by cowswap
8
+ 'eip155:1/erc20:0xdc49108ce5c57bc3408c3a5e95f3d864ec386ed3'
9
+ ]);
@@ -0,0 +1,2 @@
1
+ export declare const MAX_ALLOWANCE = "100000000000000000000000000";
2
+ export declare const MAX_COWSWAP_TRADE = "100000000000000000000000000";
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MAX_COWSWAP_TRADE = void 0;
4
+ exports.MAX_COWSWAP_TRADE = '100000000000000000000000000';
@@ -1,3 +1,4 @@
1
1
  export * from './zrx/ZrxSwapper';
2
2
  export * from './thorchain/ThorchainSwapper';
3
3
  export * from './test/TestSwapper';
4
+ export * from './cow/CowSwapper';
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./zrx/ZrxSwapper"), exports);
18
18
  __exportStar(require("./thorchain/ThorchainSwapper"), exports);
19
19
  __exportStar(require("./test/TestSwapper"), exports);
20
+ __exportStar(require("./cow/CowSwapper"), exports);
@@ -1,11 +1,11 @@
1
- import { AssetId } from '@shapeshiftoss/caip';
2
- import { Asset, SupportedChainIds } from '@shapeshiftoss/types';
1
+ import { AssetId, ChainId } from '@shapeshiftoss/caip';
2
+ import { Asset } from '@shapeshiftoss/types';
3
3
  import { ApprovalNeededOutput, BuyAssetBySellIdInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
4
4
  /**
5
5
  * Playground for testing different scenarios of multiple swappers in the manager.
6
6
  * Meant for local testing only
7
7
  */
8
- export declare class TestSwapper implements Swapper {
8
+ export declare class TestSwapper implements Swapper<ChainId> {
9
9
  supportAssets: string[];
10
10
  initialize(): Promise<void>;
11
11
  getType(): SwapperType;
@@ -15,8 +15,8 @@ export declare class TestSwapper implements Swapper {
15
15
  approveInfinite(): Promise<string>;
16
16
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
17
17
  filterAssetIdsBySellable(): AssetId[];
18
- buildTrade(): Promise<Trade<SupportedChainIds>>;
19
- getTradeQuote(): Promise<TradeQuote<SupportedChainIds>>;
18
+ buildTrade(): Promise<Trade<ChainId>>;
19
+ getTradeQuote(): Promise<TradeQuote<ChainId>>;
20
20
  executeTrade(): Promise<TradeResult>;
21
21
  getTradeTxs(): Promise<TradeTxs>;
22
22
  }
@@ -1,8 +1,8 @@
1
- import { AssetId } from '@shapeshiftoss/caip';
2
- import { Asset, SupportedChainIds } from '@shapeshiftoss/types';
1
+ import { AssetId, ChainId } from '@shapeshiftoss/caip';
2
+ import type { Asset } from '@shapeshiftoss/types';
3
3
  import { ApprovalNeededOutput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
4
4
  import { ThorchainSwapperDeps } from './types';
5
- export declare class ThorchainSwapper implements Swapper {
5
+ export declare class ThorchainSwapper implements Swapper<ChainId> {
6
6
  private swapSupportedChainIds;
7
7
  private supportedAssetIds;
8
8
  deps: ThorchainSwapperDeps;
@@ -14,8 +14,8 @@ export declare class ThorchainSwapper implements Swapper {
14
14
  approveInfinite(): Promise<string>;
15
15
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
16
16
  filterAssetIdsBySellable(): AssetId[];
17
- buildTrade(): Promise<Trade<SupportedChainIds>>;
18
- getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<SupportedChainIds>>;
19
- executeTrade(args: ExecuteTradeInput<SupportedChainIds>): Promise<TradeResult>;
17
+ buildTrade(): Promise<Trade<ChainId>>;
18
+ getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<ChainId>>;
19
+ executeTrade(args: ExecuteTradeInput<ChainId>): Promise<TradeResult>;
20
20
  getTradeTxs(): Promise<TradeTxs>;
21
21
  }
@@ -61,10 +61,13 @@ class ThorchainSwapper {
61
61
  }
62
62
  async executeTrade(args) {
63
63
  const { trade, wallet } = args;
64
- const adapter = await this.deps.adapterManager.byChainId(trade.sellAsset.chainId);
65
- if (trade.sellAsset.chainId === 'eip155:1') {
64
+ const adapter = this.deps.adapterManager.get(trade.sellAsset.chainId);
65
+ if (adapter && trade.sellAsset.chainId === 'eip155:1') {
66
66
  const thorTradeEth = trade;
67
- const signedTx = await adapter.signTransaction({ txToSign: thorTradeEth.txData, wallet });
67
+ const signedTx = await adapter.signTransaction({
68
+ txToSign: thorTradeEth.txData,
69
+ wallet
70
+ });
68
71
  const txid = await adapter.broadcastTransaction(signedTx);
69
72
  return { tradeId: txid };
70
73
  }
@@ -1,7 +1,7 @@
1
- import { SupportedChainIds } from '@shapeshiftoss/types';
1
+ import { ChainId } from '@shapeshiftoss/caip';
2
2
  import { GetTradeQuoteInput, TradeQuote } from '../../../api';
3
3
  import { ThorchainSwapperDeps } from '../types';
4
4
  export declare const getThorTradeQuote: ({ deps, input }: {
5
5
  deps: ThorchainSwapperDeps;
6
6
  input: GetTradeQuoteInput;
7
- }) => Promise<TradeQuote<SupportedChainIds>>;
7
+ }) => Promise<TradeQuote<ChainId>>;
@@ -4,7 +4,6 @@ exports.getThorTradeQuote = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
5
  const api_1 = require("../../../api");
6
6
  const bignumber_1 = require("../../utils/bignumber");
7
- const bignumber_2 = require("../../utils/bignumber");
8
7
  const constants_1 = require("../../utils/constants");
9
8
  const helpers_1 = require("../../utils/helpers/helpers");
10
9
  const constants_2 = require("../utils/constants");
@@ -21,7 +20,12 @@ const getThorTradeQuote = async ({ deps, input }) => {
21
20
  const { assetReference: sellAssetErc20Address, chainReference, chainNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
22
21
  const isErc20Trade = sellAssetErc20Address.startsWith('0x');
23
22
  const sellAssetChainId = (0, caip_1.toChainId)({ chainReference, chainNamespace });
24
- const adapter = deps.adapterManager.byChainId(sellAssetChainId);
23
+ const adapter = deps.adapterManager.get(sellAssetChainId);
24
+ if (!adapter)
25
+ throw new api_1.SwapError(`[getThorTxInfo] - No chain adapter found for ${sellAssetChainId}.`, {
26
+ code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
27
+ details: { sellAssetChainId }
28
+ });
25
29
  const bip44Params = adapter.buildBIP44Params({ accountNumber: Number(sellAssetAccountNumber) });
26
30
  const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
27
31
  const { data, router } = await (0, getThorTxData_1.getThorTxInfo)({
@@ -63,9 +67,9 @@ const getThorTradeQuote = async ({ deps, input }) => {
63
67
  });
64
68
  const feeAssetRatio = await (0, getPriceRatio_1.getPriceRatio)(deps, { sellAssetId, buyAssetId: feeAssetId });
65
69
  const priceRatio = await (0, getPriceRatio_1.getPriceRatio)(deps, { sellAssetId, buyAssetId });
66
- const rate = (0, bignumber_2.bnOrZero)(1).div(priceRatio).toString();
67
- const buyAmount = (0, helpers_1.normalizeAmount)((0, bignumber_2.bnOrZero)(sellAmount).times(rate));
68
- const sellAssetTradeFee = (0, bignumber_2.bnOrZero)(feeData.tradeFee).times((0, bignumber_2.bnOrZero)(feeAssetRatio));
70
+ const rate = (0, bignumber_1.bnOrZero)(1).div(priceRatio).toString();
71
+ const buyAmount = (0, helpers_1.normalizeAmount)((0, bignumber_1.bnOrZero)(sellAmount).times(rate));
72
+ const sellAssetTradeFee = (0, bignumber_1.bnOrZero)(feeData.tradeFee).times((0, bignumber_1.bnOrZero)(feeAssetRatio));
69
73
  // padding minimum by 1.5 the trade fee to avoid thorchain "not enough to cover fee" errors.
70
74
  const minimum = (0, bignumber_1.fromBaseUnit)(sellAssetTradeFee.times(1.5).toString(), sellAsset.precision);
71
75
  return {