@shapeshiftoss/swapper 6.2.0 → 7.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 (44) hide show
  1. package/dist/api.d.ts +28 -30
  2. package/dist/manager/SwapperManager.d.ts +8 -7
  3. package/dist/swappercli.js +38 -20
  4. package/dist/swappers/cow/CowSwapper.d.ts +8 -8
  5. package/dist/swappers/test/TestSwapper.d.ts +5 -5
  6. package/dist/swappers/thorchain/ThorchainSwapper.d.ts +7 -7
  7. package/dist/swappers/thorchain/ThorchainSwapper.js +9 -5
  8. package/dist/swappers/thorchain/getThorTradeQuote/getTradeQuote.d.ts +7 -0
  9. package/dist/swappers/thorchain/getThorTradeQuote/getTradeQuote.js +98 -0
  10. package/dist/swappers/thorchain/utils/constants.d.ts +1 -0
  11. package/dist/swappers/thorchain/utils/constants.js +4 -0
  12. package/dist/swappers/thorchain/utils/ethereum/makeTradeTx.d.ts +0 -3
  13. package/dist/swappers/thorchain/utils/ethereum/makeTradeTx.js +9 -50
  14. package/dist/swappers/thorchain/utils/ethereum/utils/getThorTxData.d.ts +15 -0
  15. package/dist/swappers/thorchain/utils/ethereum/utils/getThorTxData.js +55 -0
  16. package/dist/swappers/thorchain/utils/getTradeRate/getTradeRate.js +4 -4
  17. package/dist/swappers/thorchain/utils/txFeeHelpers/ethTxFees/getEthTxFees.d.ts +15 -0
  18. package/dist/swappers/thorchain/utils/txFeeHelpers/ethTxFees/getEthTxFees.js +44 -0
  19. package/dist/swappers/utils/bignumber.d.ts +2 -0
  20. package/dist/swappers/utils/bignumber.js +14 -1
  21. package/dist/swappers/utils/constants.d.ts +2 -0
  22. package/dist/swappers/utils/constants.js +5 -0
  23. package/dist/swappers/utils/helpers/helpers.d.ts +7 -0
  24. package/dist/swappers/utils/helpers/helpers.js +13 -0
  25. package/dist/swappers/utils/test-data/assets.js +0 -13
  26. package/dist/swappers/zrx/ZrxApprovalNeeded/ZrxApprovalNeeded.d.ts +1 -1
  27. package/dist/swappers/zrx/ZrxApprovalNeeded/ZrxApprovalNeeded.js +2 -3
  28. package/dist/swappers/zrx/ZrxApproveInfinite/ZrxApproveInfinite.d.ts +1 -2
  29. package/dist/swappers/zrx/ZrxApproveInfinite/ZrxApproveInfinite.js +2 -2
  30. package/dist/swappers/zrx/ZrxSwapper.d.ts +7 -7
  31. package/dist/swappers/zrx/getZrxTradeQuote/getZrxTradeQuote.js +6 -5
  32. package/dist/swappers/zrx/utils/constants.d.ts +0 -4
  33. package/dist/swappers/zrx/utils/constants.js +1 -5
  34. package/dist/swappers/zrx/utils/helpers/helpers.d.ts +7 -16
  35. package/dist/swappers/zrx/utils/helpers/helpers.js +3 -16
  36. package/dist/swappers/zrx/utils/test-data/setupSwapQuote.d.ts +3 -3
  37. package/dist/swappers/zrx/utils/test-data/setupSwapQuote.js +5 -5
  38. package/dist/swappers/zrx/utils/test-data/setupZrxDeps.d.ts +3 -29
  39. package/dist/swappers/zrx/utils/test-data/setupZrxDeps.js +34 -32
  40. package/dist/swappers/zrx/zrxBuildTrade/zrxBuildTrade.d.ts +1 -2
  41. package/dist/swappers/zrx/zrxBuildTrade/zrxBuildTrade.js +11 -10
  42. package/dist/swappers/zrx/zrxExecuteTrade/zrxExecuteTrade.d.ts +1 -1
  43. package/dist/swappers/zrx/zrxExecuteTrade/zrxExecuteTrade.js +2 -4
  44. package/package.json +10 -10
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getThorTxInfo = void 0;
4
+ const caip_1 = require("@shapeshiftoss/caip");
5
+ const api_1 = require("../../../../../api");
6
+ const bignumber_1 = require("../../../../utils/bignumber");
7
+ const getPriceRatio_1 = require("../../getPriceRatio/getPriceRatio");
8
+ const makeSwapMemo_1 = require("../../makeSwapMemo/makeSwapMemo");
9
+ const thorService_1 = require("../../thorService");
10
+ const routerCalldata_1 = require("../routerCalldata");
11
+ const getThorTxInfo = async ({ deps, sellAsset, buyAsset, sellAmount, slippageTolerance, destinationAddress, isErc20Trade }) => {
12
+ try {
13
+ const { assetReference } = (0, caip_1.fromAssetId)(sellAsset.assetId);
14
+ const { data: inboundAddresses } = await thorService_1.thorService.get(`${deps.midgardUrl}/thorchain/inbound_addresses`);
15
+ const ethInboundAddresses = inboundAddresses.find((inbound) => inbound.chain === 'ETH');
16
+ const vault = ethInboundAddresses === null || ethInboundAddresses === void 0 ? void 0 : ethInboundAddresses.address;
17
+ const router = ethInboundAddresses === null || ethInboundAddresses === void 0 ? void 0 : ethInboundAddresses.router;
18
+ if (!vault || !router)
19
+ throw new api_1.SwapError(`[getPriceRatio]: router or vault found for ETH`, {
20
+ code: api_1.SwapErrorTypes.RESPONSE_ERROR,
21
+ details: { inboundAddresses }
22
+ });
23
+ const priceRatio = await (0, getPriceRatio_1.getPriceRatio)(deps, {
24
+ buyAssetId: buyAsset.assetId,
25
+ sellAssetId: sellAsset.assetId
26
+ });
27
+ const expectedBuyAmount = (0, bignumber_1.toBaseUnit)((0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(sellAmount).dividedBy(priceRatio), sellAsset.precision), buyAsset.precision);
28
+ if (!(0, bignumber_1.bnOrZero)(expectedBuyAmount).gte(0) ||
29
+ !((0, bignumber_1.bnOrZero)(slippageTolerance).gte(0) && (0, bignumber_1.bnOrZero)(slippageTolerance).lte(1)))
30
+ throw new api_1.SwapError('[makeTradeTx]: bad expected buy amount or bad slippage tolerance', {
31
+ code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED
32
+ });
33
+ const limit = (0, bignumber_1.bnOrZero)(expectedBuyAmount)
34
+ .times((0, bignumber_1.bn)(1).minus(slippageTolerance))
35
+ .decimalPlaces(0)
36
+ .toString();
37
+ const memo = (0, makeSwapMemo_1.makeSwapMemo)({
38
+ buyAssetId: buyAsset.assetId,
39
+ destinationAddress,
40
+ limit
41
+ });
42
+ const data = await (0, routerCalldata_1.deposit)(router, vault, isErc20Trade ? assetReference : '0x0000000000000000000000000000000000000000', sellAmount, memo);
43
+ return {
44
+ data,
45
+ memo,
46
+ router
47
+ };
48
+ }
49
+ catch (e) {
50
+ if (e instanceof api_1.SwapError)
51
+ throw e;
52
+ throw new api_1.SwapError('[getThorTxInfo]', { cause: e, code: api_1.SwapErrorTypes.TRADE_QUOTE_FAILED });
53
+ }
54
+ };
55
+ exports.getThorTxInfo = getThorTxInfo;
@@ -4,7 +4,7 @@ exports.getTradeRate = 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 makeTradeTx_1 = require("../ethereum/makeTradeTx");
7
+ const bignumber_2 = require("../../../utils/bignumber");
8
8
  const thorService_1 = require("../thorService");
9
9
  const THOR_PRECISION = 8;
10
10
  const getSwapOutput = (inputAmount, poolData, toRune) => {
@@ -41,7 +41,7 @@ const getTradeRate = async (sellAsset, buyAssetId, sellAmount, deps) => {
41
41
  details: { buyPoolId, sellPoolId }
42
42
  });
43
43
  // All thorchain pool amounts are base 8 regardless of token precision
44
- const sellBaseAmount = (0, bignumber_1.bn)((0, makeTradeTx_1.toBaseUnit)((0, makeTradeTx_1.fromBaseUnit)(sellAmount, sellAsset.precision), THOR_PRECISION));
44
+ const sellBaseAmount = (0, bignumber_1.bn)((0, bignumber_2.toBaseUnit)((0, bignumber_2.fromBaseUnit)(sellAmount, sellAsset.precision), THOR_PRECISION));
45
45
  const sellAssetPoolData = {
46
46
  assetBalance: (0, bignumber_1.bn)(sellPool.assetDepth),
47
47
  runeBalance: (0, bignumber_1.bn)(sellPool.runeDepth)
@@ -51,7 +51,7 @@ const getTradeRate = async (sellAsset, buyAssetId, sellAmount, deps) => {
51
51
  runeBalance: (0, bignumber_1.bn)(buyPool.runeDepth)
52
52
  };
53
53
  const outputAmountBase8 = getDoubleSwapOutput(sellBaseAmount, sellAssetPoolData, buyAssetPoolData);
54
- const outputAmount = (0, makeTradeTx_1.fromBaseUnit)(outputAmountBase8, THOR_PRECISION);
55
- return (0, bignumber_1.bn)(outputAmount).div((0, makeTradeTx_1.fromBaseUnit)(sellAmount, sellAsset.precision)).toString();
54
+ const outputAmount = (0, bignumber_2.fromBaseUnit)(outputAmountBase8, THOR_PRECISION);
55
+ return (0, bignumber_1.bn)(outputAmount).div((0, bignumber_2.fromBaseUnit)(sellAmount, sellAsset.precision)).toString();
56
56
  };
57
57
  exports.getTradeRate = getTradeRate;
@@ -0,0 +1,15 @@
1
+ import { AssetReference } from '@shapeshiftoss/caip';
2
+ import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
3
+ import { Asset } from '@shapeshiftoss/types';
4
+ import { QuoteFeeData } from '../../../../../api';
5
+ import { ThorchainSwapperDeps } from '../../../types';
6
+ export declare const getEthTxFees: ({ deps, data, router, buyAsset, sellAmount, adapterManager, receiveAddress, sellAssetReference }: {
7
+ deps: ThorchainSwapperDeps;
8
+ data: string;
9
+ router: string;
10
+ buyAsset: Asset;
11
+ sellAmount: string;
12
+ sellAssetReference: AssetReference | string;
13
+ adapterManager: ChainAdapterManager;
14
+ receiveAddress: string;
15
+ }) => Promise<QuoteFeeData<'eip155:1'>>;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEthTxFees = void 0;
4
+ const types_1 = require("@shapeshiftoss/types");
5
+ const api_1 = require("../../../../../api");
6
+ const bignumber_1 = require("../../../../utils/bignumber");
7
+ const constants_1 = require("../../../../utils/constants");
8
+ const estimateTradeFee_1 = require("../../estimateTradeFee/estimateTradeFee");
9
+ const getEthTxFees = async ({ deps, data, router, buyAsset, sellAmount, adapterManager, receiveAddress, sellAssetReference }) => {
10
+ try {
11
+ const adapter = adapterManager.get(types_1.KnownChainIds.EthereumMainnet);
12
+ if (!adapter)
13
+ throw new api_1.SwapError(`[getThorTxInfo] - No chain adapter found for ${types_1.KnownChainIds.EthereumMainnet}.`, {
14
+ code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
15
+ details: { chainId: types_1.KnownChainIds.EthereumMainnet }
16
+ });
17
+ // TODO(ryankk): add falling back on hard coded gas Limit * new chain adapter method if it fails
18
+ const feeDataOptions = await adapter.getFeeData({
19
+ to: router,
20
+ value: sellAmount,
21
+ chainSpecific: { from: receiveAddress, contractData: data }
22
+ });
23
+ const feeData = feeDataOptions['fast'];
24
+ const tradeFee = await (0, estimateTradeFee_1.estimateTradeFee)(deps, buyAsset.assetId);
25
+ return {
26
+ fee: feeData.txFee,
27
+ chainSpecific: {
28
+ estimatedGas: feeData.chainSpecific.gasLimit,
29
+ gasPrice: feeData.chainSpecific.gasPrice,
30
+ approvalFee: sellAssetReference &&
31
+ (0, bignumber_1.bnOrZero)(constants_1.APPROVAL_GAS_LIMIT)
32
+ .multipliedBy((0, bignumber_1.bnOrZero)(feeData.chainSpecific.gasPrice))
33
+ .toString()
34
+ },
35
+ tradeFee
36
+ };
37
+ }
38
+ catch (e) {
39
+ if (e instanceof api_1.SwapError)
40
+ throw e;
41
+ throw new api_1.SwapError('[getThorTxInfo]', { cause: e, code: api_1.SwapErrorTypes.TRADE_QUOTE_FAILED });
42
+ }
43
+ };
44
+ exports.getEthTxFees = getEthTxFees;
@@ -2,3 +2,5 @@ import BigNumber from 'bignumber.js';
2
2
  export declare type BN = BigNumber;
3
3
  export declare const bn: (n: BigNumber.Value, base?: number) => BN;
4
4
  export declare const bnOrZero: (n: BigNumber.Value | null | undefined) => BN;
5
+ export declare const fromBaseUnit: (value: BigNumber.Value, decimals: number, displayDecimals?: number) => string;
6
+ export declare const toBaseUnit: (amount: BigNumber.Value | undefined, precision: number) => string;
@@ -3,7 +3,7 @@ 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.bnOrZero = exports.bn = void 0;
6
+ exports.toBaseUnit = exports.fromBaseUnit = exports.bnOrZero = exports.bn = void 0;
7
7
  const bignumber_js_1 = __importDefault(require("bignumber.js"));
8
8
  const bn = (n, base = 10) => new bignumber_js_1.default(n, base);
9
9
  exports.bn = bn;
@@ -12,3 +12,16 @@ const bnOrZero = (n) => {
12
12
  return value.isNaN() ? (0, exports.bn)(0) : value;
13
13
  };
14
14
  exports.bnOrZero = bnOrZero;
15
+ const fromBaseUnit = (value, decimals, displayDecimals = 6) => {
16
+ return (0, exports.bnOrZero)(value)
17
+ .div(`1e+${decimals}`)
18
+ .decimalPlaces(displayDecimals, bignumber_js_1.default.ROUND_DOWN)
19
+ .toString();
20
+ };
21
+ exports.fromBaseUnit = fromBaseUnit;
22
+ const toBaseUnit = (amount, precision) => {
23
+ return (0, exports.bnOrZero)(amount)
24
+ .times(new bignumber_js_1.default(10).exponentiatedBy((0, exports.bnOrZero)(precision)))
25
+ .toFixed(0);
26
+ };
27
+ exports.toBaseUnit = toBaseUnit;
@@ -0,0 +1,2 @@
1
+ export declare const APPROVAL_GAS_LIMIT = "100000";
2
+ export declare const DEFAULT_SLIPPAGE = "0.03";
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_SLIPPAGE = exports.APPROVAL_GAS_LIMIT = void 0;
4
+ exports.APPROVAL_GAS_LIMIT = '100000'; // Most approvals are around 40k, we've seen 72k in the wild, so 100000 for safety.
5
+ exports.DEFAULT_SLIPPAGE = '0.03'; // 3%
@@ -0,0 +1,7 @@
1
+ import { BN } from '../bignumber';
2
+ /**
3
+ * This function keeps 17 significant digits, so even if we try to trade 1 Billion of an
4
+ * ETH or ERC20, we still keep 7 decimal places.
5
+ * @param amount
6
+ */
7
+ export declare const normalizeAmount: (amount: string | number | BN) => string;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normalizeAmount = void 0;
4
+ const bignumber_1 = require("../bignumber");
5
+ /**
6
+ * This function keeps 17 significant digits, so even if we try to trade 1 Billion of an
7
+ * ETH or ERC20, we still keep 7 decimal places.
8
+ * @param amount
9
+ */
10
+ const normalizeAmount = (amount) => {
11
+ return (0, bignumber_1.bnOrZero)(amount).toNumber().toLocaleString('fullwide', { useGrouping: false });
12
+ };
13
+ exports.normalizeAmount = normalizeAmount;
@@ -1,13 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UNSUPPORTED = exports.ETH = exports.WBTC = exports.FOX = exports.WETH = exports.BTC = void 0;
4
- const types_1 = require("@shapeshiftoss/types");
5
4
  exports.BTC = {
6
5
  assetId: 'bip122:000000000019d6689c085ae165831e93/slip44:0',
7
6
  chainId: 'bip122:000000000019d6689c085ae165831e93',
8
7
  name: 'bitcoin',
9
- chain: types_1.ChainTypes.Bitcoin,
10
- network: types_1.NetworkTypes.MAINNET,
11
8
  precision: 8,
12
9
  color: '#FFFFFF',
13
10
  icon: 'https://assets.coincap.io/assets/icons/btc@2x.png',
@@ -20,8 +17,6 @@ exports.WETH = {
20
17
  assetId: 'eip155:1/erc20:0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
21
18
  chainId: 'eip155:1',
22
19
  name: 'WETH',
23
- chain: types_1.ChainTypes.Ethereum,
24
- network: types_1.NetworkTypes.MAINNET,
25
20
  precision: 18,
26
21
  color: '#FFFFFF',
27
22
  icon: 'https://assets.coingecko.com/coins/images/2518/thumb/weth.png?1628852295',
@@ -34,8 +29,6 @@ exports.FOX = {
34
29
  assetId: 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d',
35
30
  chainId: 'eip155:1',
36
31
  name: 'FOX',
37
- chain: types_1.ChainTypes.Ethereum,
38
- network: types_1.NetworkTypes.MAINNET,
39
32
  precision: 18,
40
33
  color: '#FFFFFF',
41
34
  icon: 'https://assets.coincap.io/assets/icons/fox@2x.png',
@@ -48,21 +41,17 @@ exports.WBTC = {
48
41
  assetId: 'eip155:1/erc20:0x2260fac5e5542a773aa44fbcfedf7c193bc2c599',
49
42
  chainId: 'eip155:1',
50
43
  color: '#FFFFFF',
51
- chain: types_1.ChainTypes.Ethereum,
52
44
  explorer: 'https://etherscan.io',
53
45
  explorerAddressLink: 'https://etherscan.io/address/',
54
46
  explorerTxLink: 'https://etherscan.io/tx/',
55
47
  icon: 'https://rawcdn.githack.com/trustwallet/assets/master/blockchains/ethereum/assets/0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599/logo.png',
56
48
  name: 'Wrapped Bitcoin',
57
- network: types_1.NetworkTypes.MAINNET,
58
49
  precision: 8,
59
50
  symbol: 'WBTC'
60
51
  };
61
52
  exports.ETH = {
62
53
  assetId: 'eip155:1/slip44:60',
63
54
  chainId: 'eip155:1',
64
- chain: types_1.ChainTypes.Ethereum,
65
- network: types_1.NetworkTypes.MAINNET,
66
55
  symbol: 'ETH',
67
56
  name: 'Ethereum',
68
57
  precision: 18,
@@ -75,8 +64,6 @@ exports.ETH = {
75
64
  exports.UNSUPPORTED = {
76
65
  assetId: 'eip155:1/slip44:420',
77
66
  chainId: 'eip155:1',
78
- chain: types_1.ChainTypes.Ethereum,
79
- network: types_1.NetworkTypes.MAINNET,
80
67
  symbol: 'ETH',
81
68
  name: 'Ethereum',
82
69
  precision: 18,
@@ -1,3 +1,3 @@
1
1
  import { ApprovalNeededInput, ApprovalNeededOutput } from '../../../api';
2
2
  import { ZrxSwapperDeps } from '../ZrxSwapper';
3
- export declare function ZrxApprovalNeeded({ adapterManager, web3 }: ZrxSwapperDeps, { quote, wallet }: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
3
+ export declare function ZrxApprovalNeeded({ adapter, web3 }: ZrxSwapperDeps, { quote, wallet }: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
@@ -6,7 +6,7 @@ const api_1 = require("../../../api");
6
6
  const bignumber_1 = require("../../utils/bignumber");
7
7
  const erc20Allowance_abi_1 = require("../utils/abi/erc20Allowance-abi");
8
8
  const helpers_1 = require("../utils/helpers/helpers");
9
- async function ZrxApprovalNeeded({ adapterManager, web3 }, { quote, wallet }) {
9
+ async function ZrxApprovalNeeded({ adapter, web3 }, { quote, wallet }) {
10
10
  var _a;
11
11
  const { sellAsset } = quote;
12
12
  const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(sellAsset.assetId);
@@ -21,8 +21,7 @@ async function ZrxApprovalNeeded({ adapterManager, web3 }, { quote, wallet }) {
21
21
  if (sellAsset.assetId === (0, caip_1.getFeeAssetIdFromAssetId)(sellAsset.assetId)) {
22
22
  return { approvalNeeded: false };
23
23
  }
24
- const accountNumber = (0, bignumber_1.bnOrZero)(quote.sellAssetAccountId).toNumber();
25
- const adapter = await adapterManager.byChainId(sellAsset.chainId);
24
+ const accountNumber = quote.sellAssetAccountNumber;
26
25
  const bip44Params = adapter.buildBIP44Params({ accountNumber });
27
26
  const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
28
27
  if (!quote.allowanceContract) {
@@ -1,4 +1,3 @@
1
- import { SupportedChainIds } from '@shapeshiftoss/types';
2
1
  import { ApproveInfiniteInput } from '../../../api';
3
2
  import { ZrxSwapperDeps } from '../ZrxSwapper';
4
- export declare function ZrxApproveInfinite({ adapterManager, web3 }: ZrxSwapperDeps, { quote, wallet }: ApproveInfiniteInput<SupportedChainIds>): Promise<string>;
3
+ export declare function ZrxApproveInfinite({ adapter, web3 }: ZrxSwapperDeps, { quote, wallet }: ApproveInfiniteInput<'eip155:1'>): Promise<string>;
@@ -5,7 +5,7 @@ const api_1 = require("../../../api");
5
5
  const erc20_abi_1 = require("../utils/abi/erc20-abi");
6
6
  const constants_1 = require("../utils/constants");
7
7
  const helpers_1 = require("../utils/helpers/helpers");
8
- async function ZrxApproveInfinite({ adapterManager, web3 }, { quote, wallet }) {
8
+ async function ZrxApproveInfinite({ adapter, web3 }, { quote, wallet }) {
9
9
  try {
10
10
  const allowanceGrantRequired = await (0, helpers_1.grantAllowance)({
11
11
  quote: {
@@ -13,7 +13,7 @@ async function ZrxApproveInfinite({ adapterManager, web3 }, { quote, wallet }) {
13
13
  sellAmount: constants_1.MAX_ALLOWANCE
14
14
  },
15
15
  wallet,
16
- adapterManager,
16
+ adapter,
17
17
  erc20Abi: erc20_abi_1.erc20Abi,
18
18
  web3
19
19
  });
@@ -1,24 +1,24 @@
1
1
  import { AssetId } from '@shapeshiftoss/caip';
2
- import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
3
- import { Asset, SupportedChainIds } from '@shapeshiftoss/types';
2
+ import { ethereum } from '@shapeshiftoss/chain-adapters';
3
+ import { Asset } from '@shapeshiftoss/types';
4
4
  import Web3 from 'web3';
5
5
  import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, TradeQuote, TradeResult, TradeTxs, ZrxTrade } from '../../api';
6
6
  export declare type ZrxSwapperDeps = {
7
- adapterManager: ChainAdapterManager;
7
+ adapter: ethereum.ChainAdapter;
8
8
  web3: Web3;
9
9
  };
10
- export declare class ZrxSwapper implements Swapper {
10
+ export declare class ZrxSwapper implements Swapper<'eip155:1'> {
11
11
  static swapperName: string;
12
12
  deps: ZrxSwapperDeps;
13
13
  constructor(deps: ZrxSwapperDeps);
14
14
  initialize(): Promise<void>;
15
15
  getType(): SwapperType;
16
- buildTrade(args: BuildTradeInput): Promise<ZrxTrade<SupportedChainIds>>;
17
- getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<SupportedChainIds>>;
16
+ buildTrade(args: BuildTradeInput): Promise<ZrxTrade>;
17
+ getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<'eip155:1'>>;
18
18
  getUsdRate(input: Asset): Promise<string>;
19
19
  executeTrade(args: ExecuteTradeInput<'eip155:1'>): Promise<TradeResult>;
20
20
  approvalNeeded(args: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
21
- approveInfinite(args: ApproveInfiniteInput<SupportedChainIds>): Promise<string>;
21
+ approveInfinite(args: ApproveInfiniteInput<'eip155:1'>): Promise<string>;
22
22
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
23
23
  filterAssetIdsBySellable(assetIds?: AssetId[]): AssetId[];
24
24
  getTradeTxs(tradeResult: TradeResult): Promise<TradeTxs>;
@@ -4,14 +4,15 @@ exports.getZrxTradeQuote = 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 constants_1 = require("../../utils/constants");
8
+ const helpers_1 = require("../../utils/helpers/helpers");
7
9
  const getZrxMinMax_1 = require("../getZrxMinMax/getZrxMinMax");
8
- const constants_1 = require("../utils/constants");
9
- const helpers_1 = require("../utils/helpers/helpers");
10
+ const constants_2 = require("../utils/constants");
10
11
  const zrxService_1 = require("../utils/zrxService");
11
12
  async function getZrxTradeQuote(input) {
12
13
  var _a;
13
14
  try {
14
- const { sellAsset, buyAsset, sellAmount, sellAssetAccountId } = input;
15
+ const { sellAsset, buyAsset, sellAmount, sellAssetAccountNumber } = input;
15
16
  if (buyAsset.chainId !== 'eip155:1' || sellAsset.chainId !== 'eip155:1') {
16
17
  throw new api_1.SwapError('[getZrxTradeQuote] - Both assets need to be on the Ethereum chain to use Zrx', {
17
18
  code: api_1.SwapErrorTypes.UNSUPPORTED_PAIR,
@@ -61,11 +62,11 @@ async function getZrxTradeQuote(input) {
61
62
  },
62
63
  sellAmount: data.sellAmount,
63
64
  buyAmount: data.buyAmount,
64
- sources: ((_a = data.sources) === null || _a === void 0 ? void 0 : _a.filter((s) => parseFloat(s.proportion) > 0)) || constants_1.DEFAULT_SOURCE,
65
+ sources: ((_a = data.sources) === null || _a === void 0 ? void 0 : _a.filter((s) => parseFloat(s.proportion) > 0)) || constants_2.DEFAULT_SOURCE,
65
66
  allowanceContract: data.allowanceTarget,
66
67
  buyAsset,
67
68
  sellAsset,
68
- sellAssetAccountId
69
+ sellAssetAccountNumber
69
70
  };
70
71
  }
71
72
  catch (e) {
@@ -5,7 +5,6 @@ export declare const FOX_TOKEN_CONTRACT_ADDRESS = "0xc770EEfAd204B5180dF6a14Ee19
5
5
  export declare const WETH_TOKEN_CONTRACT_ADDRESS = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
6
6
  export declare const UNISWAP_WETH_FOX_POOL_ADDRESS = "0x470e8de2ebaef52014a47cb5e6af86884947f08c";
7
7
  export declare const MAX_ALLOWANCE = "100000000000000000000000000";
8
- export declare const APPROVAL_GAS_LIMIT = "100000";
9
8
  export declare const LIQUIDITY_GAS_LIMIT = "350000";
10
9
  export declare const MAX_GAS_PRICE = "1000000000000";
11
10
  export declare const MAX_ZRX_TRADE = "100000000000000000000000000";
@@ -13,8 +12,5 @@ export declare const DEFAULT_SOURCE: {
13
12
  name: string;
14
13
  proportion: string;
15
14
  }[];
16
- export declare const DEFAULT_SLIPPAGE = "0.03";
17
- export declare const MAX_SLIPPAGE = "0.3";
18
- export declare const ETH_FEE_ESTIMATE_PADDING = "1.2";
19
15
  export declare const AFFILIATE_ADDRESS = "0xc770eefad204b5180df6a14ee197d99d808ee52d";
20
16
  export declare const APPROVAL_BUY_AMOUNT = "100000000000000000";
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.APPROVAL_BUY_AMOUNT = exports.AFFILIATE_ADDRESS = exports.ETH_FEE_ESTIMATE_PADDING = exports.MAX_SLIPPAGE = exports.DEFAULT_SLIPPAGE = exports.DEFAULT_SOURCE = exports.MAX_ZRX_TRADE = exports.MAX_GAS_PRICE = exports.LIQUIDITY_GAS_LIMIT = exports.APPROVAL_GAS_LIMIT = exports.MAX_ALLOWANCE = exports.UNISWAP_WETH_FOX_POOL_ADDRESS = exports.WETH_TOKEN_CONTRACT_ADDRESS = exports.FOX_TOKEN_CONTRACT_ADDRESS = exports.ZERO_ADDRESS = exports.UNISWAP_FACTORY_ADDRESS = exports.UNISWAP_ROUTER_ADDRESS = void 0;
3
+ exports.APPROVAL_BUY_AMOUNT = exports.AFFILIATE_ADDRESS = exports.DEFAULT_SOURCE = exports.MAX_ZRX_TRADE = exports.MAX_GAS_PRICE = exports.LIQUIDITY_GAS_LIMIT = exports.MAX_ALLOWANCE = exports.UNISWAP_WETH_FOX_POOL_ADDRESS = exports.WETH_TOKEN_CONTRACT_ADDRESS = exports.FOX_TOKEN_CONTRACT_ADDRESS = exports.ZERO_ADDRESS = exports.UNISWAP_FACTORY_ADDRESS = exports.UNISWAP_ROUTER_ADDRESS = void 0;
4
4
  exports.UNISWAP_ROUTER_ADDRESS = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D';
5
5
  exports.UNISWAP_FACTORY_ADDRESS = '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f';
6
6
  exports.ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
@@ -8,13 +8,9 @@ exports.FOX_TOKEN_CONTRACT_ADDRESS = '0xc770EEfAd204B5180dF6a14Ee197D99d808ee52d
8
8
  exports.WETH_TOKEN_CONTRACT_ADDRESS = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2';
9
9
  exports.UNISWAP_WETH_FOX_POOL_ADDRESS = '0x470e8de2ebaef52014a47cb5e6af86884947f08c';
10
10
  exports.MAX_ALLOWANCE = '100000000000000000000000000';
11
- exports.APPROVAL_GAS_LIMIT = '100000'; // Most approvals are around 40k, we've seen 72k in the wild, so 100000 for safety.
12
11
  exports.LIQUIDITY_GAS_LIMIT = '350000'; // Most Liquidity txs are around 150000
13
12
  exports.MAX_GAS_PRICE = '1000000000000'; // 1000 GWei
14
13
  exports.MAX_ZRX_TRADE = '100000000000000000000000000';
15
14
  exports.DEFAULT_SOURCE = [{ name: '0x', proportion: '1' }];
16
- exports.DEFAULT_SLIPPAGE = '0.03'; // 3%
17
- exports.MAX_SLIPPAGE = '0.3'; // 30%
18
- exports.ETH_FEE_ESTIMATE_PADDING = '1.2'; // Padding the fee estimate by 20% to avoid trade errors.
19
15
  exports.AFFILIATE_ADDRESS = '0xc770eefad204b5180df6a14ee197d99d808ee52d';
20
16
  exports.APPROVAL_BUY_AMOUNT = '100000000000000000'; // A valid buy amount - 0.1 ETH
@@ -1,10 +1,10 @@
1
- import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
1
+ import { ethereum } from '@shapeshiftoss/chain-adapters';
2
2
  import { HDWallet } from '@shapeshiftoss/hdwallet-core';
3
- import { Asset, SupportedChainIds } from '@shapeshiftoss/types';
4
- import BigNumber from 'bignumber.js';
3
+ import { Asset } from '@shapeshiftoss/types';
5
4
  import Web3 from 'web3';
6
5
  import { AbiItem } from 'web3-utils';
7
6
  import { TradeQuote } from '../../../../api';
7
+ import { BN } from '../../../utils/bignumber';
8
8
  export declare type GetAllowanceRequiredArgs = {
9
9
  receiveAddress: string;
10
10
  allowanceContract: string;
@@ -21,23 +21,14 @@ export declare type GetERC20AllowanceArgs = {
21
21
  spenderAddress: string;
22
22
  };
23
23
  declare type GrantAllowanceArgs = {
24
- quote: TradeQuote<SupportedChainIds>;
24
+ quote: TradeQuote<'eip155:1'>;
25
25
  wallet: HDWallet;
26
- adapterManager: ChainAdapterManager;
26
+ adapter: ethereum.ChainAdapter;
27
27
  erc20Abi: AbiItem[];
28
28
  web3: Web3;
29
29
  };
30
- /**
31
- * Very large amounts like those found in ERC20s with a precision of 18 get converted
32
- * to exponential notation ('1.6e+21') in javascript. The 0x api doesn't play well with
33
- * exponential notation so we need to ensure that it is represented as an integer string.
34
- * This function keeps 17 significant digits, so even if we try to trade 1 Billion of an
35
- * ETH or ERC20, we still keep 7 decimal places.
36
- * @param amount
37
- */
38
- export declare const normalizeAmount: (amount: string | number | BigNumber) => string;
39
30
  export declare const getERC20Allowance: ({ erc20AllowanceAbi, web3, sellAssetErc20Address, ownerAddress, spenderAddress }: GetERC20AllowanceArgs) => Promise<any>;
40
- export declare const getAllowanceRequired: ({ receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi }: GetAllowanceRequiredArgs) => Promise<BigNumber>;
31
+ export declare const getAllowanceRequired: ({ receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi }: GetAllowanceRequiredArgs) => Promise<BN>;
41
32
  export declare const getUsdRate: (asset: Asset) => Promise<string>;
42
- export declare const grantAllowance: ({ quote, wallet, adapterManager, erc20Abi, web3 }: GrantAllowanceArgs) => Promise<string>;
33
+ export declare const grantAllowance: ({ quote, wallet, adapter, erc20Abi, web3 }: GrantAllowanceArgs) => Promise<string>;
43
34
  export {};
@@ -1,23 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.grantAllowance = exports.getUsdRate = exports.getAllowanceRequired = exports.getERC20Allowance = exports.normalizeAmount = void 0;
3
+ exports.grantAllowance = exports.getUsdRate = exports.getAllowanceRequired = exports.getERC20Allowance = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
5
  const web3_utils_1 = require("web3-utils");
6
6
  const api_1 = require("../../../../api");
7
7
  const bignumber_1 = require("../../../utils/bignumber");
8
8
  const zrxService_1 = require("../zrxService");
9
- /**
10
- * Very large amounts like those found in ERC20s with a precision of 18 get converted
11
- * to exponential notation ('1.6e+21') in javascript. The 0x api doesn't play well with
12
- * exponential notation so we need to ensure that it is represented as an integer string.
13
- * This function keeps 17 significant digits, so even if we try to trade 1 Billion of an
14
- * ETH or ERC20, we still keep 7 decimal places.
15
- * @param amount
16
- */
17
- const normalizeAmount = (amount) => {
18
- return (0, bignumber_1.bnOrZero)(amount).toNumber().toLocaleString('fullwide', { useGrouping: false });
19
- };
20
- exports.normalizeAmount = normalizeAmount;
21
9
  const getERC20Allowance = async ({ erc20AllowanceAbi, web3, sellAssetErc20Address, ownerAddress, spenderAddress }) => {
22
10
  const erc20Contract = new web3.eth.Contract(erc20AllowanceAbi, sellAssetErc20Address);
23
11
  return erc20Contract.methods.allowance(ownerAddress, spenderAddress).call();
@@ -90,16 +78,15 @@ const getUsdRate = async (asset) => {
90
78
  }
91
79
  };
92
80
  exports.getUsdRate = getUsdRate;
93
- const grantAllowance = async ({ quote, wallet, adapterManager, erc20Abi, web3 }) => {
81
+ const grantAllowance = async ({ quote, wallet, adapter, erc20Abi, web3 }) => {
94
82
  var _a, _b, _c, _d, _e;
95
83
  try {
96
84
  const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(quote.sellAsset.assetId);
97
- const adapter = await adapterManager.byChainId('eip155:1');
98
85
  const erc20Contract = new web3.eth.Contract(erc20Abi, sellAssetErc20Address);
99
86
  const approveTx = erc20Contract.methods
100
87
  .approve(quote.allowanceContract, quote.sellAmount)
101
88
  .encodeABI();
102
- const accountNumber = (0, bignumber_1.bnOrZero)(quote.sellAssetAccountId).toNumber();
89
+ const accountNumber = quote.sellAssetAccountNumber;
103
90
  const bip44Params = adapter.buildBIP44Params({ accountNumber });
104
91
  const { txToSign } = await adapter.buildSendTransaction({
105
92
  wallet,
@@ -21,8 +21,8 @@ export declare const setupBuildTrade: () => {
21
21
  buyAmount: string;
22
22
  buyAsset: Asset;
23
23
  sendMax: boolean;
24
- sellAssetAccountId: string;
25
- buyAssetAccountId: string;
24
+ sellAssetAccountNumber: number;
25
+ buyAssetAccountNumber: number;
26
26
  sellAsset: Asset;
27
27
  };
28
28
  buyAsset: Asset;
@@ -40,7 +40,7 @@ export declare const setupExecuteTrade: () => {
40
40
  buyAsset: Asset;
41
41
  sellAsset: Asset;
42
42
  sendMax: boolean;
43
- sellAssetAccountId: string;
43
+ sellAssetAccountNumber: number;
44
44
  txData: string;
45
45
  depositAddress: string;
46
46
  receiveAddress: string;
@@ -11,7 +11,7 @@ const setupQuote = () => {
11
11
  sellAsset,
12
12
  buyAsset,
13
13
  allowanceContract: 'allowanceContractAddress',
14
- sellAssetAccountId: '0',
14
+ sellAssetAccountNumber: 0,
15
15
  minimum: '0',
16
16
  maximum: '999999999999',
17
17
  feeData: { fee: '0', tradeFee: '0', chainSpecific: {} },
@@ -22,7 +22,7 @@ const setupQuote = () => {
22
22
  sellAmount: '1000000000000000000',
23
23
  sellAsset,
24
24
  buyAsset,
25
- sellAssetAccountId: '0',
25
+ sellAssetAccountNumber: 0,
26
26
  sendMax: false
27
27
  };
28
28
  return { quoteInput, tradeQuote, buyAsset, sellAsset };
@@ -56,8 +56,8 @@ const setupBuildTrade = () => {
56
56
  buyAmount: '',
57
57
  buyAsset,
58
58
  sendMax: false,
59
- sellAssetAccountId: '0',
60
- buyAssetAccountId: '0',
59
+ sellAssetAccountNumber: 0,
60
+ buyAssetAccountNumber: 0,
61
61
  sellAsset
62
62
  };
63
63
  return { buildTradeInput, buyAsset, sellAsset };
@@ -77,7 +77,7 @@ const setupExecuteTrade = () => {
77
77
  buyAsset,
78
78
  sellAsset,
79
79
  sendMax: false,
80
- sellAssetAccountId: '0',
80
+ sellAssetAccountNumber: 0,
81
81
  txData: '0x0',
82
82
  depositAddress: '0x0',
83
83
  receiveAddress: '0x0',
@@ -1,32 +1,6 @@
1
- import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
1
+ import { ethereum } from '@shapeshiftoss/chain-adapters';
2
2
  import Web3 from 'web3';
3
- export declare const chainAdapterMockFuncs: {
4
- buildBIP44Params: jest.Mock<{
5
- purpose: number;
6
- coinType: number;
7
- accountNumber: number;
8
- }, []>;
9
- buildSendTransaction: jest.Mock<Promise<{
10
- txToSign: {};
11
- }>, []>;
12
- signTransaction: jest.Mock<Promise<string>, []>;
13
- broadcastTransaction: jest.Mock<Promise<string>, []>;
14
- getAddress: jest.Mock<Promise<string>, []>;
15
- getAccount: jest.Mock<Promise<{
16
- chainSpecific: {
17
- tokens: {
18
- assetId: string;
19
- balance: string;
20
- }[];
21
- };
22
- }>, []>;
23
- getFeeData: jest.Mock<Promise<{
24
- average: {
25
- txFee: string;
26
- };
27
- }>, []>;
28
- };
29
3
  export declare const setupZrxDeps: () => {
30
- web3Instance: Web3;
31
- adapterManager: ChainAdapterManager;
4
+ web3: Web3;
5
+ adapter: ethereum.ChainAdapter;
32
6
  };