@shapeshiftoss/swapper 7.9.1 → 8.0.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 (41) hide show
  1. package/README.md +4 -4
  2. package/dist/api.d.ts +20 -23
  3. package/dist/api.js +3 -1
  4. package/dist/manager/SwapperManager.d.ts +1 -2
  5. package/dist/manager/SwapperManager.js +3 -3
  6. package/dist/swappercli.js +4 -4
  7. package/dist/swappers/cow/CowSwapper.d.ts +7 -7
  8. package/dist/swappers/cow/cowApprovalNeeded/cowApprovalNeeded.d.ts +2 -1
  9. package/dist/swappers/cow/cowApproveInfinite/cowApproveInfinite.d.ts +2 -1
  10. package/dist/swappers/cow/getCowSwapTradeQuote/getCowSwapTradeQuote.d.ts +2 -1
  11. package/dist/swappers/thorchain/ThorchainSwapper.d.ts +3 -2
  12. package/dist/swappers/thorchain/ThorchainSwapper.js +2 -1
  13. package/dist/swappers/thorchain/thorTradeApprovalNeeded/thorTradeApprovalNeeded.d.ts +2 -1
  14. package/dist/swappers/thorchain/thorTradeApproveInfinite/thorTradeApproveInfinite.d.ts +2 -1
  15. package/dist/swappers/thorchain/types.d.ts +14 -1
  16. package/dist/swappers/thorchain/utils/txFeeHelpers/btcTxFees/getBtcTxFees.d.ts +2 -2
  17. package/dist/swappers/thorchain/utils/txFeeHelpers/ethTxFees/getEthTxFees.d.ts +2 -2
  18. package/dist/swappers/utils/helpers/helpers.d.ts +7 -7
  19. package/dist/swappers/utils/helpers/helpers.js +2 -2
  20. package/dist/swappers/utils/test-data/setupDeps.js +2 -0
  21. package/dist/swappers/utils/test-data/setupSwapQuote.d.ts +3 -3
  22. package/dist/swappers/utils/test-data/setupSwapQuote.js +2 -1
  23. package/dist/swappers/zrx/ZrxSwapper.d.ts +11 -15
  24. package/dist/swappers/zrx/ZrxSwapper.js +15 -4
  25. package/dist/swappers/zrx/getZrxTradeQuote/getZrxTradeQuote.d.ts +2 -2
  26. package/dist/swappers/zrx/getZrxTradeQuote/getZrxTradeQuote.js +7 -4
  27. package/dist/swappers/zrx/types.d.ts +15 -1
  28. package/dist/swappers/zrx/utils/helpers/helpers.d.ts +4 -0
  29. package/dist/swappers/zrx/utils/helpers/helpers.js +46 -3
  30. package/dist/swappers/zrx/utils/test-data/setupZrxSwapQuote.d.ts +9 -2
  31. package/dist/swappers/zrx/utils/test-data/setupZrxSwapQuote.js +2 -1
  32. package/dist/swappers/zrx/utils/zrxService.d.ts +2 -1
  33. package/dist/swappers/zrx/utils/zrxService.js +3 -3
  34. package/dist/swappers/zrx/zrxApprovalNeeded/zrxApprovalNeeded.d.ts +3 -3
  35. package/dist/swappers/zrx/zrxApprovalNeeded/zrxApprovalNeeded.js +1 -1
  36. package/dist/swappers/zrx/zrxApproveInfinite/zrxApproveInfinite.d.ts +3 -3
  37. package/dist/swappers/zrx/zrxBuildTrade/zrxBuildTrade.d.ts +3 -3
  38. package/dist/swappers/zrx/zrxBuildTrade/zrxBuildTrade.js +14 -10
  39. package/dist/swappers/zrx/zrxExecuteTrade/zrxExecuteTrade.d.ts +3 -3
  40. package/dist/swappers/zrx/zrxExecuteTrade/zrxExecuteTrade.js +5 -5
  41. package/package.json +1 -1
package/README.md CHANGED
@@ -31,16 +31,16 @@ import { SwapperManager, SwapperType, ZrxSwapper } from '@shapeshiftoss/swapper'
31
31
  // in code
32
32
  const manager = new SwapperManager<MyCustomSwapperTypes>()
33
33
 
34
- // Add a swapper to the manager, you can add your own if they follow the `Swapper` api spec
34
+ // Add a swapper to the manager, you can add your own if it follows the `Swapper` API spec
35
35
  manager
36
- .addSwapper(SwapperType.Zrx, new ZrxSwapper())
37
- .addSwapper(SwapperType.Thorchain, new ThorchainSwapper())
36
+ .addSwapper(new ZrxSwapper())
37
+ .addSwapper(new ThorchainSwapper())
38
38
 
39
39
  // Get a swapper from the manager
40
40
  const swapper = manager.getSwapper(SwapperType.Zrx)
41
41
 
42
42
  // Remove a swapper from the manager
43
- manager.removeSwapper(SwapperType.Zrx)
43
+ manager.removeSwapper(SwapperType.ZrxEthereum)
44
44
  ```
45
45
 
46
46
  ### Working with the manager
package/dist/api.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { AssetId, ChainId } from '@shapeshiftoss/caip';
2
- import { BTCSignTx, ETHSignTx, HDWallet } from '@shapeshiftoss/hdwallet-core';
2
+ import { avalanche, ethereum } from '@shapeshiftoss/chain-adapters';
3
+ import { HDWallet } from '@shapeshiftoss/hdwallet-core';
3
4
  import { Asset, BIP44Params, ChainSpecific, KnownChainIds, UtxoAccountType } from '@shapeshiftoss/types';
4
5
  export declare const SwapError: {
5
6
  new <V extends {
@@ -24,13 +25,19 @@ export declare const SwapError: {
24
25
  stackTraceLimit: number;
25
26
  };
26
27
  declare type ChainSpecificQuoteFeeData<T extends ChainId> = ChainSpecific<T, {
27
- 'eip155:1': {
28
+ [KnownChainIds.EthereumMainnet]: {
28
29
  estimatedGas?: string;
29
30
  gasPrice?: string;
30
31
  approvalFee?: string;
31
32
  totalFee?: string;
32
33
  };
33
- 'bip122:000000000019d6689c085ae165831e93': {
34
+ [KnownChainIds.AvalancheMainnet]: {
35
+ estimatedGas?: string;
36
+ gasPrice?: string;
37
+ approvalFee?: string;
38
+ totalFee?: string;
39
+ };
40
+ [KnownChainIds.BitcoinMainnet]: {
34
41
  byteCount: string;
35
42
  satsPerByte: string;
36
43
  };
@@ -50,7 +57,7 @@ export declare type BuyAssetBySellIdInput = {
50
57
  export declare type SupportedSellAssetsInput = {
51
58
  assetIds: AssetId[];
52
59
  };
53
- export declare type CommonTradeInput = {
60
+ declare type CommonTradeInput = {
54
61
  sellAsset: Asset;
55
62
  buyAsset: Asset;
56
63
  sellAmount: string;
@@ -58,16 +65,18 @@ export declare type CommonTradeInput = {
58
65
  sellAssetAccountNumber: number;
59
66
  wallet?: HDWallet;
60
67
  };
61
- export declare type GetEthTradeQuoteInput = CommonTradeInput & {
62
- chainId: 'eip155:1';
68
+ export declare type EvmSupportedChainIds = KnownChainIds.EthereumMainnet | KnownChainIds.AvalancheMainnet;
69
+ export declare type EvmSupportedChainAdapters = ethereum.ChainAdapter | avalanche.ChainAdapter;
70
+ export declare type GetEvmTradeQuoteInput = CommonTradeInput & {
71
+ chainId: EvmSupportedChainIds;
63
72
  };
64
- export declare type GetBtcTradeQuoteInput = CommonTradeInput & {
65
- chainId: 'bip122:000000000019d6689c085ae165831e93';
73
+ declare type GetBtcTradeQuoteInput = CommonTradeInput & {
74
+ chainId: KnownChainIds.BitcoinMainnet;
66
75
  accountType: UtxoAccountType;
67
76
  bip44Params: BIP44Params;
68
77
  wallet: HDWallet;
69
78
  };
70
- export declare type GetTradeQuoteInput = GetEthTradeQuoteInput | GetBtcTradeQuoteInput;
79
+ export declare type GetTradeQuoteInput = GetBtcTradeQuoteInput | GetEvmTradeQuoteInput;
71
80
  export declare type BuildTradeInput = GetTradeQuoteInput & {
72
81
  buyAssetAccountNumber: number;
73
82
  slippage?: string;
@@ -91,19 +100,6 @@ export interface TradeQuote<C extends ChainId> extends TradeBase<C> {
91
100
  export interface Trade<C extends ChainId> extends TradeBase<C> {
92
101
  receiveAddress: string;
93
102
  }
94
- export interface ZrxTrade extends Trade<'eip155:1'> {
95
- txData: string;
96
- depositAddress: string;
97
- }
98
- export interface BtcThorTrade<C extends ChainId> extends Trade<C> {
99
- chainId: KnownChainIds.BitcoinMainnet;
100
- txData: BTCSignTx;
101
- }
102
- export interface EthThorTrade<C extends ChainId> extends Trade<C> {
103
- chainId: KnownChainIds.EthereumMainnet;
104
- txData: ETHSignTx;
105
- }
106
- export declare type ThorTrade<C extends ChainId> = BtcThorTrade<C> | EthThorTrade<C>;
107
103
  export declare type ExecuteTradeInput<C extends ChainId> = {
108
104
  trade: Trade<C>;
109
105
  wallet: HDWallet;
@@ -131,7 +127,8 @@ export declare type ApprovalNeededOutput = {
131
127
  approvalNeeded: boolean;
132
128
  };
133
129
  export declare enum SwapperType {
134
- Zrx = "0x",
130
+ ZrxEthereum = "0xEthereum",
131
+ ZrxAvalanche = "0xAvalanche",
135
132
  Thorchain = "Thorchain",
136
133
  CowSwap = "CowSwap",
137
134
  Test = "Test"
package/dist/api.js CHANGED
@@ -2,10 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SwapErrorTypes = exports.SwapperType = exports.SwapError = void 0;
4
4
  const errors_1 = require("@shapeshiftoss/errors");
5
+ const types_1 = require("@shapeshiftoss/types");
5
6
  exports.SwapError = (0, errors_1.createErrorClass)('SwapError');
6
7
  var SwapperType;
7
8
  (function (SwapperType) {
8
- SwapperType["Zrx"] = "0x";
9
+ SwapperType["ZrxEthereum"] = "0xEthereum";
10
+ SwapperType["ZrxAvalanche"] = "0xAvalanche";
9
11
  SwapperType["Thorchain"] = "Thorchain";
10
12
  SwapperType["CowSwap"] = "CowSwap";
11
13
  SwapperType["Test"] = "Test";
@@ -6,11 +6,10 @@ export declare class SwapperManager {
6
6
  constructor();
7
7
  /**
8
8
  *
9
- * @param swapperType swapper type {SwapperType|string}
10
9
  * @param swapperInstance swapper instance {Swapper}
11
10
  * @returns {SwapperManager}
12
11
  */
13
- addSwapper(swapperType: SwapperType, swapperInstance: Swapper<ChainId>): this;
12
+ addSwapper(swapperInstance: Swapper<ChainId>): this;
14
13
  /**
15
14
  *
16
15
  * @param swapperType swapper type {SwapperType|string}
@@ -19,18 +19,18 @@ class SwapperManager {
19
19
  }
20
20
  /**
21
21
  *
22
- * @param swapperType swapper type {SwapperType|string}
23
22
  * @param swapperInstance swapper instance {Swapper}
24
23
  * @returns {SwapperManager}
25
24
  */
26
- addSwapper(swapperType, swapperInstance) {
25
+ addSwapper(swapperInstance) {
26
+ validateSwapper(swapperInstance);
27
+ const swapperType = swapperInstance.getType();
27
28
  const swapper = this.swappers.get(swapperType);
28
29
  if (swapper)
29
30
  throw new api_1.SwapError('[addSwapper] - swapper already exists', {
30
31
  code: api_1.SwapErrorTypes.MANAGER_ERROR,
31
32
  details: { swapperType }
32
33
  });
33
- validateSwapper(swapperInstance);
34
34
  this.swappers.set(swapperType, swapperInstance);
35
35
  return this;
36
36
  }
@@ -29,12 +29,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  const asset_service_1 = require("@shapeshiftoss/asset-service");
30
30
  const chain_adapters_1 = require("@shapeshiftoss/chain-adapters");
31
31
  const hdwallet_native_1 = require("@shapeshiftoss/hdwallet-native");
32
+ const types_1 = require("@shapeshiftoss/types");
32
33
  const unchained = __importStar(require("@shapeshiftoss/unchained-client"));
33
34
  const bignumber_js_1 = __importDefault(require("bignumber.js"));
34
35
  const dotenv_1 = __importDefault(require("dotenv"));
35
36
  const readline_sync_1 = __importDefault(require("readline-sync"));
36
37
  const web3_1 = __importDefault(require("web3"));
37
- const api_1 = require("./api");
38
38
  const SwapperManager_1 = require("./manager/SwapperManager");
39
39
  const ZrxSwapper_1 = require("./swappers/zrx/ZrxSwapper");
40
40
  dotenv_1.default.config();
@@ -98,7 +98,7 @@ const main = async () => {
98
98
  const zrxSwapperDeps = { wallet, adapter: ethChainAdapter, web3 };
99
99
  const manager = new SwapperManager_1.SwapperManager();
100
100
  const zrxSwapper = new ZrxSwapper_1.ZrxSwapper(zrxSwapperDeps);
101
- manager.addSwapper(api_1.SwapperType.Zrx, zrxSwapper);
101
+ manager.addSwapper(zrxSwapper);
102
102
  const swapper = await manager.getBestSwapper({
103
103
  sellAssetId: 'eip155:1/slip44:60',
104
104
  buyAssetId: 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d'
@@ -109,7 +109,7 @@ const main = async () => {
109
109
  let quote;
110
110
  try {
111
111
  quote = await swapper.getTradeQuote({
112
- chainId: 'eip155:1',
112
+ chainId: types_1.KnownChainIds.EthereumMainnet,
113
113
  sellAsset,
114
114
  buyAsset,
115
115
  sellAmount: sellAmountBase,
@@ -128,7 +128,7 @@ const main = async () => {
128
128
  const answer = readline_sync_1.default.question(`Swap ${sellAmount} ${sellAsset.symbol} for ${buyAmount} ${buyAsset.symbol} on ${swapper.getType()}? (y/n): `);
129
129
  if (answer === 'y') {
130
130
  const trade = await swapper.buildTrade({
131
- chainId: 'eip155:1',
131
+ chainId: types_1.KnownChainIds.EthereumMainnet,
132
132
  wallet,
133
133
  buyAsset,
134
134
  sendMax: false,
@@ -1,6 +1,6 @@
1
1
  import { AssetId } from '@shapeshiftoss/caip';
2
2
  import { ethereum } from '@shapeshiftoss/chain-adapters';
3
- import { Asset } from '@shapeshiftoss/types';
3
+ import { Asset, KnownChainIds } from '@shapeshiftoss/types';
4
4
  import Web3 from 'web3';
5
5
  import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
6
6
  /**
@@ -13,17 +13,17 @@ export declare type CowSwapperDeps = {
13
13
  web3: Web3;
14
14
  feeAsset: Asset;
15
15
  };
16
- export declare class CowSwapper implements Swapper<'eip155:1'> {
16
+ export declare class CowSwapper implements Swapper<KnownChainIds.EthereumMainnet> {
17
17
  static swapperName: string;
18
18
  deps: CowSwapperDeps;
19
19
  constructor(deps: CowSwapperDeps);
20
20
  getType(): SwapperType;
21
- buildTrade(args: BuildTradeInput): Promise<Trade<'eip155:1'>>;
22
- getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<'eip155:1'>>;
21
+ buildTrade(args: BuildTradeInput): Promise<Trade<KnownChainIds.EthereumMainnet>>;
22
+ getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<KnownChainIds.EthereumMainnet>>;
23
23
  getUsdRate(input: Asset): Promise<string>;
24
- executeTrade(args: ExecuteTradeInput<'eip155:1'>): Promise<TradeResult>;
25
- approvalNeeded(args: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
26
- approveInfinite(args: ApproveInfiniteInput<'eip155:1'>): Promise<string>;
24
+ executeTrade(args: ExecuteTradeInput<KnownChainIds.EthereumMainnet>): Promise<TradeResult>;
25
+ approvalNeeded(args: ApprovalNeededInput<KnownChainIds.EthereumMainnet>): Promise<ApprovalNeededOutput>;
26
+ approveInfinite(args: ApproveInfiniteInput<KnownChainIds.EthereumMainnet>): Promise<string>;
27
27
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
28
28
  filterAssetIdsBySellable(assetIds: AssetId[]): AssetId[];
29
29
  getTradeTxs(): Promise<TradeTxs>;
@@ -1,3 +1,4 @@
1
+ import { KnownChainIds } from '@shapeshiftoss/types';
1
2
  import { ApprovalNeededInput, ApprovalNeededOutput } from '../../../api';
2
3
  import { CowSwapperDeps } from '../CowSwapper';
3
- export declare function cowApprovalNeeded({ adapter, web3 }: CowSwapperDeps, { quote, wallet }: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
4
+ export declare function cowApprovalNeeded({ adapter, web3 }: CowSwapperDeps, { quote, wallet }: ApprovalNeededInput<KnownChainIds.EthereumMainnet>): Promise<ApprovalNeededOutput>;
@@ -1,3 +1,4 @@
1
+ import { KnownChainIds } from '@shapeshiftoss/types';
1
2
  import { ApproveInfiniteInput } from '../../../api';
2
3
  import { CowSwapperDeps } from '../CowSwapper';
3
- export declare function cowApproveInfinite({ adapter, web3 }: CowSwapperDeps, { quote, wallet }: ApproveInfiniteInput<'eip155:1'>): Promise<string>;
4
+ export declare function cowApproveInfinite({ adapter, web3 }: CowSwapperDeps, { quote, wallet }: ApproveInfiniteInput<KnownChainIds.EthereumMainnet>): Promise<string>;
@@ -1,3 +1,4 @@
1
+ import { KnownChainIds } from '@shapeshiftoss/types';
1
2
  import { GetTradeQuoteInput, TradeQuote } from '../../../api';
2
3
  import { CowSwapperDeps } from '../CowSwapper';
3
4
  export declare type CowSwapQuoteApiInput = {
@@ -11,4 +12,4 @@ export declare type CowSwapQuoteApiInput = {
11
12
  sellToken: string;
12
13
  validTo: number;
13
14
  };
14
- export declare function getCowSwapTradeQuote(deps: CowSwapperDeps, input: GetTradeQuoteInput): Promise<TradeQuote<'eip155:1'>>;
15
+ export declare function getCowSwapTradeQuote(deps: CowSwapperDeps, input: GetTradeQuoteInput): Promise<TradeQuote<KnownChainIds.EthereumMainnet>>;
@@ -1,5 +1,6 @@
1
1
  import { AssetId, ChainId } from '@shapeshiftoss/caip';
2
2
  import type { Asset } from '@shapeshiftoss/types';
3
+ import { KnownChainIds } from '@shapeshiftoss/types';
3
4
  import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
4
5
  import { ThorchainSwapperDeps } from './types';
5
6
  export declare class ThorchainSwapper implements Swapper<ChainId> {
@@ -10,8 +11,8 @@ export declare class ThorchainSwapper implements Swapper<ChainId> {
10
11
  initialize(): Promise<void>;
11
12
  getType(): SwapperType;
12
13
  getUsdRate(input: Pick<Asset, 'symbol' | 'assetId'>): Promise<string>;
13
- approvalNeeded(input: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
14
- approveInfinite(input: ApproveInfiniteInput<'eip155:1'>): Promise<string>;
14
+ approvalNeeded(input: ApprovalNeededInput<KnownChainIds.EthereumMainnet>): Promise<ApprovalNeededOutput>;
15
+ approveInfinite(input: ApproveInfiniteInput<KnownChainIds.EthereumMainnet>): Promise<string>;
15
16
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
16
17
  filterAssetIdsBySellable(): AssetId[];
17
18
  buildTrade(): Promise<Trade<ChainId>>;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ThorchainSwapper = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
+ const types_1 = require("@shapeshiftoss/types");
5
6
  const api_1 = require("../../api");
6
7
  const getTradeQuote_1 = require("./getThorTradeQuote/getTradeQuote");
7
8
  const thorTradeApprovalNeeded_1 = require("./thorTradeApprovalNeeded/thorTradeApprovalNeeded");
@@ -64,7 +65,7 @@ class ThorchainSwapper {
64
65
  async executeTrade(args) {
65
66
  const { trade, wallet } = args;
66
67
  const adapter = this.deps.adapterManager.get(trade.sellAsset.chainId);
67
- if (adapter && trade.sellAsset.chainId === 'eip155:1') {
68
+ if (adapter && trade.sellAsset.chainId === types_1.KnownChainIds.EthereumMainnet) {
68
69
  const thorTradeEth = trade;
69
70
  const signedTx = await adapter.signTransaction({
70
71
  txToSign: thorTradeEth.txData,
@@ -1,6 +1,7 @@
1
+ import { KnownChainIds } from '@shapeshiftoss/types';
1
2
  import { ApprovalNeededInput, ApprovalNeededOutput } from '../../../api';
2
3
  import { ThorchainSwapperDeps } from '../types';
3
4
  export declare const thorTradeApprovalNeeded: ({ deps, input }: {
4
5
  deps: ThorchainSwapperDeps;
5
- input: ApprovalNeededInput<'eip155:1'>;
6
+ input: ApprovalNeededInput<KnownChainIds.EthereumMainnet>;
6
7
  }) => Promise<ApprovalNeededOutput>;
@@ -1,6 +1,7 @@
1
+ import { KnownChainIds } from '@shapeshiftoss/types';
1
2
  import { ApproveInfiniteInput } from '../../../api';
2
3
  import { ThorchainSwapperDeps } from '../types';
3
4
  export declare const thorTradeApproveInfinite: ({ deps, input }: {
4
5
  deps: ThorchainSwapperDeps;
5
- input: ApproveInfiniteInput<'eip155:1'>;
6
+ input: ApproveInfiniteInput<KnownChainIds.EthereumMainnet>;
6
7
  }) => Promise<string>;
@@ -1,5 +1,9 @@
1
+ import { ChainId } from '@shapeshiftoss/caip';
1
2
  import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
2
- import Web3 from 'web3';
3
+ import { BTCSignTx, ETHSignTx } from '@shapeshiftoss/hdwallet-core';
4
+ import { KnownChainIds } from '@shapeshiftoss/types';
5
+ import type Web3 from 'web3';
6
+ import { Trade } from '../../api';
3
7
  export declare type PoolResponse = {
4
8
  asset: string;
5
9
  assetDepth: string;
@@ -27,3 +31,12 @@ export declare type ThorchainSwapperDeps = {
27
31
  adapterManager: ChainAdapterManager;
28
32
  web3: Web3;
29
33
  };
34
+ export interface BtcThorTrade<C extends ChainId> extends Trade<C> {
35
+ chainId: KnownChainIds.BitcoinMainnet;
36
+ txData: BTCSignTx;
37
+ }
38
+ export interface EthThorTrade<C extends ChainId> extends Trade<C> {
39
+ chainId: KnownChainIds.EthereumMainnet;
40
+ txData: ETHSignTx;
41
+ }
42
+ export declare type ThorTrade<C extends ChainId> = BtcThorTrade<C> | EthThorTrade<C>;
@@ -1,5 +1,5 @@
1
1
  import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
2
- import { Asset } from '@shapeshiftoss/types';
2
+ import { Asset, KnownChainIds } from '@shapeshiftoss/types';
3
3
  import { QuoteFeeData } from '../../../../../api';
4
4
  import { ThorchainSwapperDeps } from '../../../types';
5
5
  export declare const getBtcTxFees: ({ deps, opReturnData, vault, buyAsset, sellAmount, adapterManager, pubkey }: {
@@ -10,4 +10,4 @@ export declare const getBtcTxFees: ({ deps, opReturnData, vault, buyAsset, sellA
10
10
  sellAmount: string;
11
11
  adapterManager: ChainAdapterManager;
12
12
  pubkey: string;
13
- }) => Promise<QuoteFeeData<'bip122:000000000019d6689c085ae165831e93'>>;
13
+ }) => Promise<QuoteFeeData<KnownChainIds.BitcoinMainnet>>;
@@ -1,6 +1,6 @@
1
1
  import { AssetReference } from '@shapeshiftoss/caip';
2
2
  import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
3
- import { Asset } from '@shapeshiftoss/types';
3
+ import { Asset, KnownChainIds } from '@shapeshiftoss/types';
4
4
  import { QuoteFeeData } from '../../../../../api';
5
5
  import { ThorchainSwapperDeps } from '../../../types';
6
6
  export declare const getEthTxFees: ({ deps, data, router, buyAsset, sellAmount, adapterManager, receiveAddress, sellAssetReference }: {
@@ -12,4 +12,4 @@ export declare const getEthTxFees: ({ deps, data, router, buyAsset, sellAmount,
12
12
  sellAssetReference: AssetReference | string;
13
13
  adapterManager: ChainAdapterManager;
14
14
  receiveAddress: string;
15
- }) => Promise<QuoteFeeData<'eip155:1'>>;
15
+ }) => Promise<QuoteFeeData<KnownChainIds.EthereumMainnet>>;
@@ -1,12 +1,12 @@
1
- import { ethereum } from '@shapeshiftoss/chain-adapters';
2
1
  import { HDWallet } from '@shapeshiftoss/hdwallet-core';
3
2
  import { Asset } from '@shapeshiftoss/types';
4
3
  import BigNumber from 'bignumber.js';
5
4
  import Web3 from 'web3';
6
5
  import { AbiItem } from 'web3-utils';
7
- import { TradeQuote } from '../../../api';
6
+ import { EvmSupportedChainAdapters, EvmSupportedChainIds, TradeQuote } from '../../../api';
8
7
  import { BN } from '../bignumber';
9
8
  export declare type GetAllowanceRequiredArgs = {
9
+ adapter: EvmSupportedChainAdapters;
10
10
  receiveAddress: string;
11
11
  allowanceContract: string;
12
12
  sellAsset: Asset;
@@ -21,16 +21,16 @@ export declare type GetERC20AllowanceArgs = {
21
21
  ownerAddress: string;
22
22
  spenderAddress: string;
23
23
  };
24
- declare type GrantAllowanceArgs = {
25
- quote: TradeQuote<'eip155:1'>;
24
+ declare type GrantAllowanceArgs<T extends EvmSupportedChainIds> = {
25
+ quote: TradeQuote<T>;
26
26
  wallet: HDWallet;
27
- adapter: ethereum.ChainAdapter;
27
+ adapter: EvmSupportedChainAdapters;
28
28
  erc20Abi: AbiItem[];
29
29
  web3: Web3;
30
30
  };
31
31
  export declare const getERC20Allowance: ({ erc20AllowanceAbi, web3, sellAssetErc20Address, ownerAddress, spenderAddress }: GetERC20AllowanceArgs) => Promise<any>;
32
- export declare const getAllowanceRequired: ({ receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi }: GetAllowanceRequiredArgs) => Promise<BigNumber>;
33
- export declare const grantAllowance: ({ quote, wallet, adapter, erc20Abi, web3 }: GrantAllowanceArgs) => Promise<string>;
32
+ export declare const getAllowanceRequired: ({ adapter, receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi }: GetAllowanceRequiredArgs) => Promise<BigNumber>;
33
+ export declare const grantAllowance: <T extends EvmSupportedChainIds>({ quote, wallet, adapter, erc20Abi, web3 }: GrantAllowanceArgs<T>) => Promise<string>;
34
34
  /**
35
35
  * This function keeps 17 significant digits, so even if we try to trade 1 Billion of an
36
36
  * ETH or ERC20, we still keep 7 decimal places.
@@ -10,9 +10,9 @@ const getERC20Allowance = async ({ erc20AllowanceAbi, web3, sellAssetErc20Addres
10
10
  return erc20Contract.methods.allowance(ownerAddress, spenderAddress).call();
11
11
  };
12
12
  exports.getERC20Allowance = getERC20Allowance;
13
- const getAllowanceRequired = async ({ receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi }) => {
13
+ const getAllowanceRequired = async ({ adapter, receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi }) => {
14
14
  try {
15
- if (sellAsset.assetId === 'eip155:1/slip44:60') {
15
+ if (sellAsset.assetId === adapter.getFeeAssetId()) {
16
16
  return (0, bignumber_1.bn)(0);
17
17
  }
18
18
  const ownerAddress = receiveAddress;
@@ -27,6 +27,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.setupDeps = void 0;
30
+ const caip_1 = require("@shapeshiftoss/caip");
30
31
  const chain_adapters_1 = require("@shapeshiftoss/chain-adapters");
31
32
  const unchained = __importStar(require("@shapeshiftoss/unchained-client"));
32
33
  const web3_1 = __importDefault(require("web3"));
@@ -43,6 +44,7 @@ const setupDeps = () => {
43
44
  },
44
45
  rpcUrl: 'https://mainnet.infura.io/v3/d734c7eebcdf400185d7eb67322a7e57'
45
46
  });
47
+ ethChainAdapter.getFeeAssetId = () => caip_1.ethAssetId;
46
48
  const ethNodeUrl = 'http://localhost:1000';
47
49
  const web3Provider = new web3_1.default.providers.HttpProvider(ethNodeUrl);
48
50
  return { web3: new web3_1.default(web3Provider), adapter: ethChainAdapter, feeAsset: assets_1.WETH };
@@ -1,8 +1,8 @@
1
- import { Asset } from '@shapeshiftoss/types';
1
+ import { Asset, KnownChainIds } from '@shapeshiftoss/types';
2
2
  import { TradeQuote } from '../../../api';
3
3
  export declare const setupQuote: () => {
4
- quoteInput: import("../../../api").GetEthTradeQuoteInput;
5
- tradeQuote: TradeQuote<"eip155:1">;
4
+ quoteInput: import("../../../api").GetEvmTradeQuoteInput;
5
+ tradeQuote: TradeQuote<KnownChainIds.EthereumMainnet>;
6
6
  buyAsset: Asset;
7
7
  sellAsset: Asset;
8
8
  };
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.setupQuote = void 0;
4
+ const types_1 = require("@shapeshiftoss/types");
4
5
  const assets_1 = require("./assets");
5
6
  const setupQuote = () => {
6
7
  const sellAsset = { ...assets_1.FOX };
@@ -19,7 +20,7 @@ const setupQuote = () => {
19
20
  sources: []
20
21
  };
21
22
  const quoteInput = {
22
- chainId: 'eip155:1',
23
+ chainId: types_1.KnownChainIds.EthereumMainnet,
23
24
  sellAmount: '1000000000000000000',
24
25
  sellAsset,
25
26
  buyAsset,
@@ -1,23 +1,19 @@
1
- import { AssetId } from '@shapeshiftoss/caip';
2
- import { ethereum } from '@shapeshiftoss/chain-adapters';
1
+ import { AssetId, ChainId } from '@shapeshiftoss/caip';
3
2
  import { Asset } from '@shapeshiftoss/types';
4
- import Web3 from 'web3';
5
- import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, TradeQuote, TradeResult, TradeTxs, ZrxTrade } from '../../api';
6
- export declare type ZrxSwapperDeps = {
7
- adapter: ethereum.ChainAdapter;
8
- web3: Web3;
9
- };
10
- export declare class ZrxSwapper implements Swapper<'eip155:1'> {
3
+ import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, EvmSupportedChainIds, GetEvmTradeQuoteInput, Swapper, SwapperType, TradeQuote, TradeResult, TradeTxs } from '../../api';
4
+ import { ZrxExecuteTradeInput, ZrxSwapperDeps, ZrxTrade } from './types';
5
+ export declare class ZrxSwapper<T extends EvmSupportedChainIds> implements Swapper<T> {
11
6
  static swapperName: string;
12
7
  deps: ZrxSwapperDeps;
8
+ chainId: ChainId;
13
9
  constructor(deps: ZrxSwapperDeps);
14
- getType(): SwapperType;
15
- buildTrade(args: BuildTradeInput): Promise<ZrxTrade>;
16
- getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<'eip155:1'>>;
10
+ getType(): SwapperType.ZrxEthereum | SwapperType.ZrxAvalanche;
11
+ buildTrade(args: BuildTradeInput): Promise<ZrxTrade<T>>;
12
+ getTradeQuote(input: GetEvmTradeQuoteInput): Promise<TradeQuote<T>>;
17
13
  getUsdRate(input: Asset): Promise<string>;
18
- executeTrade(args: ExecuteTradeInput<'eip155:1'>): Promise<TradeResult>;
19
- approvalNeeded(args: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
20
- approveInfinite(args: ApproveInfiniteInput<'eip155:1'>): Promise<string>;
14
+ executeTrade(args: ZrxExecuteTradeInput<T>): Promise<TradeResult>;
15
+ approvalNeeded(args: ApprovalNeededInput<T>): Promise<ApprovalNeededOutput>;
16
+ approveInfinite(args: ApproveInfiniteInput<T>): Promise<string>;
21
17
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
22
18
  filterAssetIdsBySellable(assetIds?: AssetId[]): AssetId[];
23
19
  getTradeTxs(tradeResult: TradeResult): Promise<TradeTxs>;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ZrxSwapper = void 0;
4
+ const types_1 = require("@shapeshiftoss/types");
4
5
  const api_1 = require("../../api");
5
6
  const getZrxTradeQuote_1 = require("./getZrxTradeQuote/getZrxTradeQuote");
6
7
  const blacklist_1 = require("./utils/blacklist");
@@ -12,9 +13,19 @@ const zrxExecuteTrade_1 = require("./zrxExecuteTrade/zrxExecuteTrade");
12
13
  class ZrxSwapper {
13
14
  constructor(deps) {
14
15
  this.deps = deps;
16
+ this.chainId = deps.adapter.getChainId();
15
17
  }
16
18
  getType() {
17
- return api_1.SwapperType.Zrx;
19
+ switch (this.chainId) {
20
+ case types_1.KnownChainIds.EthereumMainnet:
21
+ return api_1.SwapperType.ZrxEthereum;
22
+ case types_1.KnownChainIds.AvalancheMainnet:
23
+ return api_1.SwapperType.ZrxAvalanche;
24
+ default:
25
+ throw new api_1.SwapError('[getType]', {
26
+ code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN
27
+ });
28
+ }
18
29
  }
19
30
  async buildTrade(args) {
20
31
  return (0, zrxBuildTrade_1.zrxBuildTrade)(this.deps, args);
@@ -36,12 +47,12 @@ class ZrxSwapper {
36
47
  }
37
48
  filterBuyAssetsBySellAssetId(args) {
38
49
  const { assetIds = [], sellAssetId } = args;
39
- return assetIds.filter((id) => id.startsWith('eip155:1') &&
40
- (sellAssetId === null || sellAssetId === void 0 ? void 0 : sellAssetId.startsWith('eip155:1')) &&
50
+ return assetIds.filter((id) => id.startsWith(this.chainId) &&
51
+ (sellAssetId === null || sellAssetId === void 0 ? void 0 : sellAssetId.startsWith(this.chainId)) &&
41
52
  !blacklist_1.UNSUPPORTED_ASSETS.includes(id));
42
53
  }
43
54
  filterAssetIdsBySellable(assetIds = []) {
44
- return assetIds.filter((id) => id.startsWith('eip155:1') && !blacklist_1.UNSUPPORTED_ASSETS.includes(id));
55
+ return assetIds.filter((id) => id.startsWith(this.chainId) && !blacklist_1.UNSUPPORTED_ASSETS.includes(id));
45
56
  }
46
57
  async getTradeTxs(tradeResult) {
47
58
  return {
@@ -1,2 +1,2 @@
1
- import { GetTradeQuoteInput, TradeQuote } from '../../../api';
2
- export declare function getZrxTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<'eip155:1'>>;
1
+ import { EvmSupportedChainIds, GetEvmTradeQuoteInput, TradeQuote } from '../../../api';
2
+ export declare function getZrxTradeQuote<T extends EvmSupportedChainIds>(input: GetEvmTradeQuoteInput): Promise<TradeQuote<T>>;
@@ -8,13 +8,14 @@ const constants_1 = require("../../utils/constants");
8
8
  const helpers_1 = require("../../utils/helpers/helpers");
9
9
  const getZrxMinMax_1 = require("../getZrxMinMax/getZrxMinMax");
10
10
  const constants_2 = require("../utils/constants");
11
+ const helpers_2 = require("../utils/helpers/helpers");
11
12
  const zrxService_1 = require("../utils/zrxService");
12
13
  async function getZrxTradeQuote(input) {
13
14
  var _a;
14
15
  try {
15
16
  const { sellAsset, buyAsset, sellAmount, sellAssetAccountNumber } = input;
16
- if (buyAsset.chainId !== 'eip155:1' || sellAsset.chainId !== 'eip155:1') {
17
- throw new api_1.SwapError('[getZrxTradeQuote] - Both assets need to be on the Ethereum chain to use Zrx', {
17
+ if (buyAsset.chainId !== input.chainId || sellAsset.chainId !== input.chainId) {
18
+ throw new api_1.SwapError('[getZrxTradeQuote] - Both assets need to be on the same supported EVM chain to use Zrx', {
18
19
  code: api_1.SwapErrorTypes.UNSUPPORTED_PAIR,
19
20
  details: { buyAssetChainId: buyAsset.chainId, sellAssetChainId: sellAsset.chainId }
20
21
  });
@@ -27,16 +28,18 @@ async function getZrxTradeQuote(input) {
27
28
  const { minimum, maximum } = await (0, getZrxMinMax_1.getZrxMinMax)(sellAsset, buyAsset);
28
29
  const minQuoteSellAmount = (0, bignumber_1.bnOrZero)(minimum).times((0, bignumber_1.bn)(10).exponentiatedBy(sellAsset.precision));
29
30
  const normalizedSellAmount = (0, helpers_1.normalizeAmount)((0, bignumber_1.bnOrZero)(sellAmount).eq(0) ? minQuoteSellAmount : sellAmount);
31
+ const baseUrl = (0, helpers_2.baseUrlFromChainId)(buyAsset.chainId);
32
+ const zrxService = (0, zrxService_1.zrxServiceFactory)(baseUrl);
30
33
  /**
31
34
  * /swap/v1/price
32
35
  * params: {
33
36
  * sellToken: contract address (or symbol) of token to sell
34
37
  * buyToken: contractAddress (or symbol) of token to buy
35
38
  * sellAmount?: integer string value of the smallest increment of the sell token
36
- * buyAmount?: integer string value of the smallest incremtent of the buy token
39
+ * buyAmount?: integer string value of the smallest increment of the buy token
37
40
  * }
38
41
  */
39
- const quoteResponse = await zrxService_1.zrxService.get('/swap/v1/price', {
42
+ const quoteResponse = await zrxService.get('/swap/v1/price', {
40
43
  params: {
41
44
  sellToken,
42
45
  buyToken,
@@ -1,4 +1,6 @@
1
- import { SwapSource } from '../../api';
1
+ import { HDWallet } from '@shapeshiftoss/hdwallet-core';
2
+ import type Web3 from 'web3';
3
+ import { EvmSupportedChainAdapters, EvmSupportedChainIds, SwapSource, Trade } from '../../api';
2
4
  export declare type ZrxCommonResponse = {
3
5
  price: string;
4
6
  gasPrice: string;
@@ -15,3 +17,15 @@ export declare type ZrxQuoteResponse = ZrxCommonResponse & {
15
17
  data: string;
16
18
  gas: string;
17
19
  };
20
+ export interface ZrxTrade<T extends EvmSupportedChainIds> extends Trade<T> {
21
+ txData: string;
22
+ depositAddress: string;
23
+ }
24
+ export declare type ZrxExecuteTradeInput<T extends EvmSupportedChainIds> = {
25
+ trade: ZrxTrade<T>;
26
+ wallet: HDWallet;
27
+ };
28
+ export declare type ZrxSwapperDeps = {
29
+ adapter: EvmSupportedChainAdapters;
30
+ web3: Web3;
31
+ };
@@ -1,2 +1,6 @@
1
+ import { AssetId } from '@shapeshiftoss/caip';
1
2
  import { Asset } from '@shapeshiftoss/types';
3
+ export declare const baseUrlFromChainId: (chainId: string) => string;
4
+ export declare const usdcContractFromChainId: (chainId: string) => string;
5
+ export declare const isNativeEvmAsset: (assetId: AssetId) => boolean;
2
6
  export declare const getUsdRate: (asset: Asset) => Promise<string>;
@@ -1,18 +1,61 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getUsdRate = void 0;
3
+ exports.getUsdRate = exports.isNativeEvmAsset = exports.usdcContractFromChainId = exports.baseUrlFromChainId = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
+ const types_1 = require("@shapeshiftoss/types");
5
6
  const api_1 = require("../../../../api");
6
7
  const bignumber_1 = require("../../../utils/bignumber");
7
8
  const zrxService_1 = require("../zrxService");
9
+ const baseUrlFromChainId = (chainId) => {
10
+ switch (chainId) {
11
+ case types_1.KnownChainIds.EthereumMainnet: {
12
+ return 'https://api.0x.org/';
13
+ }
14
+ case types_1.KnownChainIds.AvalancheMainnet:
15
+ return 'https://avalanche.api.0x.org/';
16
+ default:
17
+ throw new api_1.SwapError(`baseUrlFromChainId] - Unsupported chainId: ${chainId}`, {
18
+ code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN
19
+ });
20
+ }
21
+ };
22
+ exports.baseUrlFromChainId = baseUrlFromChainId;
23
+ const usdcContractFromChainId = (chainId) => {
24
+ switch (chainId) {
25
+ case types_1.KnownChainIds.EthereumMainnet: {
26
+ return '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
27
+ }
28
+ case types_1.KnownChainIds.AvalancheMainnet:
29
+ return '0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e';
30
+ default:
31
+ throw new api_1.SwapError(`usdcContractFromChainId] - Unsupported chainId: ${chainId}`, {
32
+ code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN
33
+ });
34
+ }
35
+ };
36
+ exports.usdcContractFromChainId = usdcContractFromChainId;
37
+ const isNativeEvmAsset = (assetId) => {
38
+ const { chainId } = (0, caip_1.fromAssetId)(assetId);
39
+ switch (chainId) {
40
+ case types_1.KnownChainIds.EthereumMainnet:
41
+ return assetId === caip_1.ethAssetId;
42
+ case types_1.KnownChainIds.AvalancheMainnet:
43
+ return assetId === caip_1.avalancheAssetId;
44
+ default:
45
+ return false;
46
+ }
47
+ };
48
+ exports.isNativeEvmAsset = isNativeEvmAsset;
8
49
  const getUsdRate = async (asset) => {
9
50
  const { assetReference: erc20Address, assetNamespace } = (0, caip_1.fromAssetId)(asset.assetId);
10
51
  const { symbol } = asset;
11
52
  try {
12
- const USDC_CONTRACT_ADDRESS = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
53
+ const USDC_CONTRACT_ADDRESS = (0, exports.usdcContractFromChainId)(asset.chainId);
13
54
  if ((erc20Address === null || erc20Address === void 0 ? void 0 : erc20Address.toLowerCase()) === USDC_CONTRACT_ADDRESS)
14
55
  return '1'; // Will break if comparing against usdc
15
- const rateResponse = await zrxService_1.zrxService.get('/swap/v1/price', {
56
+ const baseUrl = (0, exports.baseUrlFromChainId)(asset.chainId);
57
+ const zrxService = (0, zrxService_1.zrxServiceFactory)(baseUrl);
58
+ const rateResponse = await zrxService.get('/swap/v1/price', {
16
59
  params: {
17
60
  buyToken: USDC_CONTRACT_ADDRESS,
18
61
  buyAmount: '1000000000',
@@ -7,8 +7,15 @@ export declare const setupZrxTradeQuoteResponse: () => {
7
7
  sellAsset: Asset;
8
8
  };
9
9
  export declare const setupBuildTrade: () => {
10
- buildTradeInput: import("../../../../api").CommonTradeInput & {
11
- chainId: "eip155:1";
10
+ buildTradeInput: {
11
+ sellAsset: Asset;
12
+ buyAsset: Asset;
13
+ sellAmount: string;
14
+ sendMax: boolean;
15
+ sellAssetAccountNumber: number;
16
+ wallet?: HDWallet | undefined;
17
+ } & {
18
+ chainId: import("../../../../api").EvmSupportedChainIds;
12
19
  } & {
13
20
  buyAssetAccountNumber: number;
14
21
  slippage?: string | undefined;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.setupExecuteTrade = exports.setupBuildTrade = exports.setupZrxTradeQuoteResponse = void 0;
4
+ const types_1 = require("@shapeshiftoss/types");
4
5
  const assets_1 = require("../../../utils/test-data/assets");
5
6
  const setupZrxTradeQuoteResponse = () => {
6
7
  const sellAsset = { ...assets_1.FOX };
@@ -23,7 +24,7 @@ const setupBuildTrade = () => {
23
24
  const sellAsset = { ...assets_1.FOX };
24
25
  const buyAsset = { ...assets_1.WETH };
25
26
  const buildTradeInput = {
26
- chainId: 'eip155:1',
27
+ chainId: types_1.KnownChainIds.EthereumMainnet,
27
28
  sellAmount: '1000000000000000000',
28
29
  buyAsset,
29
30
  sendMax: false,
@@ -1 +1,2 @@
1
- export declare const zrxService: import("axios").AxiosInstance;
1
+ import { AxiosInstance } from 'axios';
2
+ export declare const zrxServiceFactory: (baseUrl: string) => AxiosInstance;
@@ -3,14 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.zrxService = void 0;
6
+ exports.zrxServiceFactory = void 0;
7
7
  const axios_1 = __importDefault(require("axios"));
8
8
  const axiosConfig = {
9
- baseURL: 'https://api.0x.org/',
10
9
  timeout: 10000,
11
10
  headers: {
12
11
  Accept: 'application/json',
13
12
  'Content-Type': 'application/json'
14
13
  }
15
14
  };
16
- exports.zrxService = axios_1.default.create(axiosConfig);
15
+ const zrxServiceFactory = (baseUrl) => axios_1.default.create({ ...axiosConfig, baseURL: baseUrl });
16
+ exports.zrxServiceFactory = zrxServiceFactory;
@@ -1,3 +1,3 @@
1
- import { ApprovalNeededInput, ApprovalNeededOutput } from '../../../api';
2
- import { ZrxSwapperDeps } from '../ZrxSwapper';
3
- export declare function zrxApprovalNeeded({ adapter, web3 }: ZrxSwapperDeps, { quote, wallet }: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
1
+ import { ApprovalNeededInput, ApprovalNeededOutput, EvmSupportedChainIds } from '../../../api';
2
+ import { ZrxSwapperDeps } from '../types';
3
+ export declare function zrxApprovalNeeded<T extends EvmSupportedChainIds>({ adapter, web3 }: ZrxSwapperDeps, { quote, wallet }: ApprovalNeededInput<T>): Promise<ApprovalNeededOutput>;
@@ -11,7 +11,7 @@ async function zrxApprovalNeeded({ adapter, web3 }, { quote, wallet }) {
11
11
  const { sellAsset } = quote;
12
12
  const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(sellAsset.assetId);
13
13
  try {
14
- if (sellAsset.chainId !== 'eip155:1') {
14
+ if (sellAsset.chainId !== adapter.getChainId()) {
15
15
  throw new api_1.SwapError('[zrxApprovalNeeded] - sellAsset chainId is not supported', {
16
16
  code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
17
17
  details: { chainId: sellAsset.chainId }
@@ -1,3 +1,3 @@
1
- import { ApproveInfiniteInput } from '../../../api';
2
- import { ZrxSwapperDeps } from '../ZrxSwapper';
3
- export declare function zrxApproveInfinite({ adapter, web3 }: ZrxSwapperDeps, { quote, wallet }: ApproveInfiniteInput<'eip155:1'>): Promise<string>;
1
+ import { ApproveInfiniteInput, EvmSupportedChainIds } from '../../../api';
2
+ import { ZrxSwapperDeps } from '../types';
3
+ export declare function zrxApproveInfinite<T extends EvmSupportedChainIds>({ adapter, web3 }: ZrxSwapperDeps, { quote, wallet }: ApproveInfiniteInput<T>): Promise<string>;
@@ -1,3 +1,3 @@
1
- import { BuildTradeInput, ZrxTrade } from '../../..';
2
- import { ZrxSwapperDeps } from '../ZrxSwapper';
3
- export declare function zrxBuildTrade({ adapter, web3 }: ZrxSwapperDeps, input: BuildTradeInput): Promise<ZrxTrade>;
1
+ import { BuildTradeInput, EvmSupportedChainIds } from '../../../api';
2
+ import { ZrxSwapperDeps, ZrxTrade } from '../types';
3
+ export declare function zrxBuildTrade<T extends EvmSupportedChainIds>({ adapter, web3 }: ZrxSwapperDeps, input: BuildTradeInput): Promise<ZrxTrade<T>>;
@@ -26,14 +26,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.zrxBuildTrade = void 0;
27
27
  const caip_1 = require("@shapeshiftoss/caip");
28
28
  const rax = __importStar(require("retry-axios"));
29
- const __1 = require("../../..");
29
+ const api_1 = require("../../../api");
30
30
  const erc20Allowance_abi_1 = require("../../utils/abi/erc20Allowance-abi");
31
31
  const bignumber_1 = require("../../utils/bignumber");
32
32
  const constants_1 = require("../../utils/constants");
33
33
  const helpers_1 = require("../../utils/helpers/helpers");
34
- const helpers_2 = require("../../utils/helpers/helpers");
35
34
  const applyAxiosRetry_1 = require("../utils/applyAxiosRetry");
36
35
  const constants_2 = require("../utils/constants");
36
+ const helpers_2 = require("../utils/helpers/helpers");
37
37
  const zrxService_1 = require("../utils/zrxService");
38
38
  async function zrxBuildTrade({ adapter, web3 }, input) {
39
39
  var _a, _b, _c;
@@ -43,15 +43,18 @@ async function zrxBuildTrade({ adapter, web3 }, input) {
43
43
  const { assetReference: sellAssetErc20Address, assetNamespace: sellAssetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
44
44
  const buyToken = buyAssetNamespace === 'erc20' ? buyAssetErc20Address : buyAsset.symbol;
45
45
  const sellToken = sellAssetNamespace === 'erc20' ? sellAssetErc20Address : sellAsset.symbol;
46
- if (buyAsset.chainId !== 'eip155:1') {
47
- throw new __1.SwapError('[zrxBuildTrade] - buyAsset must be on chainId eip155:1', {
48
- code: __1.SwapErrorTypes.VALIDATION_FAILED,
46
+ const adapterChainId = adapter.getChainId();
47
+ if (buyAsset.chainId !== adapterChainId) {
48
+ throw new api_1.SwapError(`[zrxBuildTrade] - buyAsset must be on chainId ${adapterChainId}`, {
49
+ code: api_1.SwapErrorTypes.VALIDATION_FAILED,
49
50
  details: { chainId: sellAsset.chainId }
50
51
  });
51
52
  }
52
53
  const bip44Params = adapter.buildBIP44Params({ accountNumber: Number(buyAssetAccountNumber) });
53
54
  const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
54
55
  const slippagePercentage = slippage ? (0, bignumber_1.bnOrZero)(slippage).div(100).toString() : constants_1.DEFAULT_SLIPPAGE;
56
+ const baseUrl = (0, helpers_2.baseUrlFromChainId)(buyAsset.chainId);
57
+ const zrxService = (0, zrxService_1.zrxServiceFactory)(baseUrl);
55
58
  /**
56
59
  * /swap/v1/quote
57
60
  * params: {
@@ -60,7 +63,7 @@ async function zrxBuildTrade({ adapter, web3 }, input) {
60
63
  * sellAmount?: integer string value of the smallest increment of the sell token
61
64
  * }
62
65
  */
63
- const zrxRetry = (0, applyAxiosRetry_1.applyAxiosRetry)(zrxService_1.zrxService, {
66
+ const zrxRetry = (0, applyAxiosRetry_1.applyAxiosRetry)(zrxService, {
64
67
  statusCodesToRetry: [[400, 400]],
65
68
  shouldRetry: (err) => {
66
69
  var _a, _b, _c, _d;
@@ -81,7 +84,7 @@ async function zrxBuildTrade({ adapter, web3 }, input) {
81
84
  params: {
82
85
  buyToken,
83
86
  sellToken,
84
- sellAmount: (0, helpers_2.normalizeAmount)(sellAmount),
87
+ sellAmount: (0, helpers_1.normalizeAmount)(sellAmount),
85
88
  takerAddress: receiveAddress,
86
89
  slippagePercentage,
87
90
  skipValidation: false,
@@ -111,6 +114,7 @@ async function zrxBuildTrade({ adapter, web3 }, input) {
111
114
  sources: ((_a = data.sources) === null || _a === void 0 ? void 0 : _a.filter((s) => parseFloat(s.proportion) > 0)) || constants_2.DEFAULT_SOURCE
112
115
  };
113
116
  const allowanceRequired = await (0, helpers_1.getAllowanceRequired)({
117
+ adapter,
114
118
  sellAsset,
115
119
  allowanceContract: data.allowanceTarget,
116
120
  receiveAddress,
@@ -131,10 +135,10 @@ async function zrxBuildTrade({ adapter, web3 }, input) {
131
135
  return trade;
132
136
  }
133
137
  catch (e) {
134
- if (e instanceof __1.SwapError)
138
+ if (e instanceof api_1.SwapError)
135
139
  throw e;
136
- throw new __1.SwapError('[zrxBuildTrade]', {
137
- code: __1.SwapErrorTypes.BUILD_TRADE_FAILED
140
+ throw new api_1.SwapError('[zrxBuildTrade]', {
141
+ code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED
138
142
  });
139
143
  }
140
144
  }
@@ -1,3 +1,3 @@
1
- import { ExecuteTradeInput, TradeResult } from '../../../api';
2
- import { ZrxSwapperDeps } from '../ZrxSwapper';
3
- export declare function zrxExecuteTrade({ adapter }: ZrxSwapperDeps, { trade, wallet }: ExecuteTradeInput<'eip155:1'>): Promise<TradeResult>;
1
+ import { EvmSupportedChainIds, TradeResult } from '../../../api';
2
+ import { ZrxExecuteTradeInput, ZrxSwapperDeps } from '../types';
3
+ export declare function zrxExecuteTrade<T extends EvmSupportedChainIds>({ adapter }: ZrxSwapperDeps, { trade, wallet }: ZrxExecuteTradeInput<T>): Promise<TradeResult>;
@@ -3,20 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.zrxExecuteTrade = void 0;
4
4
  const web3_utils_1 = require("web3-utils");
5
5
  const api_1 = require("../../../api");
6
+ const helpers_1 = require("../utils/helpers/helpers");
6
7
  async function zrxExecuteTrade({ adapter }, { trade, wallet }) {
7
8
  var _a, _b, _c, _d, _e, _f;
8
- const zrxTrade = trade;
9
- const { sellAsset } = zrxTrade;
9
+ const { sellAsset } = trade;
10
10
  try {
11
11
  // value is 0 for erc20s
12
- const value = sellAsset.assetId === 'eip155:1/slip44:60' ? trade.sellAmount : '0';
12
+ const value = (0, helpers_1.isNativeEvmAsset)(sellAsset.assetId) ? trade.sellAmount : '0';
13
13
  const bip44Params = adapter.buildBIP44Params({
14
14
  accountNumber: trade.sellAssetAccountNumber
15
15
  });
16
16
  const buildTxResponse = await adapter.buildSendTransaction({
17
17
  value,
18
18
  wallet,
19
- to: zrxTrade.depositAddress,
19
+ to: trade.depositAddress,
20
20
  chainSpecific: {
21
21
  gasPrice: (0, web3_utils_1.numberToHex)(((_b = (_a = trade.feeData) === null || _a === void 0 ? void 0 : _a.chainSpecific) === null || _b === void 0 ? void 0 : _b.gasPrice) || 0),
22
22
  gasLimit: (0, web3_utils_1.numberToHex)(((_d = (_c = trade.feeData) === null || _c === void 0 ? void 0 : _c.chainSpecific) === null || _d === void 0 ? void 0 : _d.estimatedGas) || 0)
@@ -24,7 +24,7 @@ async function zrxExecuteTrade({ adapter }, { trade, wallet }) {
24
24
  bip44Params
25
25
  });
26
26
  const { txToSign } = buildTxResponse;
27
- const txWithQuoteData = { ...txToSign, data: (_e = zrxTrade.txData) !== null && _e !== void 0 ? _e : '' };
27
+ const txWithQuoteData = { ...txToSign, data: (_e = trade.txData) !== null && _e !== void 0 ? _e : '' };
28
28
  if (wallet.supportsOfflineSigning()) {
29
29
  const signedTx = await adapter.signTransaction({ txToSign: txWithQuoteData, wallet });
30
30
  const txid = await adapter.broadcastTransaction(signedTx);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "7.9.1",
3
+ "version": "8.0.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",