@shapeshiftoss/swapper 5.11.0 → 5.12.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.
@@ -1,13 +1,18 @@
1
1
  import { AssetId } from '@shapeshiftoss/caip';
2
2
  import { Asset, SupportedChainIds } from '@shapeshiftoss/types';
3
3
  import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
4
+ export declare type CowSwapperDeps = {
5
+ apiUrl: string;
6
+ };
4
7
  export declare class CowSwapper implements Swapper {
5
8
  static swapperName: string;
9
+ deps: CowSwapperDeps;
10
+ constructor(deps: CowSwapperDeps);
6
11
  initialize(): Promise<void>;
7
12
  getType(): SwapperType;
8
13
  buildTrade(args: BuildTradeInput): Promise<Trade<SupportedChainIds>>;
9
14
  getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<SupportedChainIds>>;
10
- getUsdRate(input: Pick<Asset, 'symbol' | 'assetId'>): Promise<string>;
15
+ getUsdRate(input: Asset): Promise<string>;
11
16
  executeTrade(args: ExecuteTradeInput<SupportedChainIds>): Promise<TradeResult>;
12
17
  approvalNeeded(args: ApprovalNeededInput<SupportedChainIds>): Promise<ApprovalNeededOutput>;
13
18
  approveInfinite(args: ApproveInfiniteInput<SupportedChainIds>): Promise<string>;
@@ -2,7 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CowSwapper = void 0;
4
4
  const api_1 = require("../../api");
5
+ const helpers_1 = require("./utils/helpers/helpers");
5
6
  class CowSwapper {
7
+ constructor(deps) {
8
+ this.deps = deps;
9
+ }
6
10
  // eslint-disable-next-line @typescript-eslint/no-empty-function
7
11
  async initialize() { }
8
12
  getType() {
@@ -16,9 +20,8 @@ class CowSwapper {
16
20
  console.info(input);
17
21
  throw new Error('CowSwapper: getTradeQuote unimplemented');
18
22
  }
19
- getUsdRate(input) {
20
- console.info(input);
21
- throw new Error('CowSwapper: getUsdRate unimplemented');
23
+ async getUsdRate(input) {
24
+ return (0, helpers_1.getUsdRate)(this.deps, input);
22
25
  }
23
26
  async executeTrade(args) {
24
27
  console.info(args);
@@ -0,0 +1,4 @@
1
+ export declare type CowSwapPriceResponse = {
2
+ amount: string;
3
+ token: string;
4
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1 @@
1
+ export declare const cowService: import("axios").AxiosInstance;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.cowService = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const axiosConfig = {
9
+ timeout: 10000,
10
+ headers: {
11
+ Accept: 'application/json',
12
+ 'Content-Type': 'application/json'
13
+ }
14
+ };
15
+ exports.cowService = axios_1.default.create(axiosConfig);
@@ -0,0 +1,3 @@
1
+ import { Asset } from '@shapeshiftoss/types';
2
+ import { CowSwapperDeps } from '../../CowSwapper';
3
+ export declare const getUsdRate: ({ apiUrl }: CowSwapperDeps, input: Asset) => Promise<string>;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getUsdRate = void 0;
4
+ const caip_1 = require("@shapeshiftoss/caip");
5
+ const api_1 = require("../../../../api");
6
+ const bignumber_1 = require("../../../utils/bignumber");
7
+ const cowService_1 = require("../cowService");
8
+ const USDC_CONTRACT_ADDRESS = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
9
+ const USDC_ASSET_PRECISION = 6;
10
+ const getUsdRate = async ({ apiUrl }, input) => {
11
+ const asset = input;
12
+ const { assetReference: erc20Address, assetNamespace } = (0, caip_1.fromAssetId)(asset.assetId);
13
+ if (assetNamespace !== 'erc20') {
14
+ throw new api_1.SwapError('[getUsdRate] - unsupported asset namespace', {
15
+ code: api_1.SwapErrorTypes.USD_RATE_FAILED,
16
+ details: { assetNamespace }
17
+ });
18
+ }
19
+ const buyAmountInDollars = 1000;
20
+ const buyAmount = (0, bignumber_1.bn)(buyAmountInDollars).times((0, bignumber_1.bn)(10).exponentiatedBy(USDC_ASSET_PRECISION));
21
+ try {
22
+ // rate is imprecise for low $ values, hence asking for $1000
23
+ // cowSwap api used : markets/{baseToken}-{quoteToken}/{kind}/{amount}
24
+ // It returns the estimated amount in quoteToken for either buying or selling amount of baseToken.
25
+ const rateResponse = await cowService_1.cowService.get(`${apiUrl}/v1/markets/${USDC_CONTRACT_ADDRESS}-${erc20Address}/buy/${buyAmount}`);
26
+ const tokenAmount = (0, bignumber_1.bnOrZero)(rateResponse.data.amount).div((0, bignumber_1.bn)(10).exponentiatedBy(asset.precision));
27
+ if (!tokenAmount.gt(0))
28
+ throw new api_1.SwapError('[getUsdRate] - Failed to get token amount', {
29
+ code: api_1.SwapErrorTypes.RESPONSE_ERROR
30
+ });
31
+ // dividing $1000 by amount of token received
32
+ return (0, bignumber_1.bn)(buyAmountInDollars).dividedBy(tokenAmount).toString();
33
+ }
34
+ catch (e) {
35
+ if (e instanceof api_1.SwapError)
36
+ throw e;
37
+ throw new api_1.SwapError('[getUsdRate]', {
38
+ cause: e,
39
+ code: api_1.SwapErrorTypes.USD_RATE_FAILED
40
+ });
41
+ }
42
+ };
43
+ exports.getUsdRate = getUsdRate;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.estimateTradeFee = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
5
  const api_1 = require("../../../../api");
6
- const bignumber_1 = require("../../../zrx/utils/bignumber");
6
+ const bignumber_1 = require("../../../utils/bignumber");
7
7
  const thorService_1 = require("../thorService");
8
8
  const gweiGasPrecision = 9;
9
9
  // https://docs.thorchain.org/how-it-works/fees#outbound-fee
@@ -7,7 +7,7 @@ exports.makeTradeTx = exports.toBaseUnit = exports.fromBaseUnit = void 0;
7
7
  const caip_1 = require("@shapeshiftoss/caip");
8
8
  const bignumber_js_1 = __importDefault(require("bignumber.js"));
9
9
  const api_1 = require("../../../../api");
10
- const bignumber_1 = require("../../../zrx/utils/bignumber");
10
+ const bignumber_1 = require("../../../utils/bignumber");
11
11
  const getPriceRatio_1 = require("../getPriceRatio/getPriceRatio");
12
12
  const makeSwapMemo_1 = require("../makeSwapMemo/makeSwapMemo");
13
13
  const thorService_1 = require("../thorService");
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getPriceRatio = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
5
  const api_1 = require("../../../../api");
6
- const bignumber_1 = require("../../../zrx/utils/bignumber");
6
+ const bignumber_1 = require("../../../utils/bignumber");
7
7
  const thorService_1 = require("../thorService");
8
8
  const getPriceRatio = async (deps, input) => {
9
9
  var _a, _b;
File without changes
@@ -2,3 +2,5 @@ import { Asset } from '@shapeshiftoss/types';
2
2
  export declare const BTC: Asset;
3
3
  export declare const WETH: Asset;
4
4
  export declare const FOX: Asset;
5
+ export declare const WBTC: Asset;
6
+ export declare const ETH: Asset;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FOX = exports.WETH = exports.BTC = void 0;
3
+ exports.ETH = exports.WBTC = exports.FOX = exports.WETH = exports.BTC = void 0;
4
4
  const types_1 = require("@shapeshiftoss/types");
5
5
  exports.BTC = {
6
6
  assetId: 'bip122:000000000019d6689c085ae165831e93/slip44:0',
@@ -44,3 +44,31 @@ exports.FOX = {
44
44
  explorerAddressLink: 'https://etherscan.io/address/',
45
45
  symbol: 'FOX'
46
46
  };
47
+ exports.WBTC = {
48
+ assetId: 'eip155:1/erc20:0x2260fac5e5542a773aa44fbcfedf7c193bc2c599',
49
+ chainId: 'eip155:1',
50
+ color: '#FFFFFF',
51
+ chain: types_1.ChainTypes.Ethereum,
52
+ explorer: 'https://etherscan.io',
53
+ explorerAddressLink: 'https://etherscan.io/address/',
54
+ explorerTxLink: 'https://etherscan.io/tx/',
55
+ icon: 'https://rawcdn.githack.com/trustwallet/assets/master/blockchains/ethereum/assets/0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599/logo.png',
56
+ name: 'Wrapped Bitcoin',
57
+ network: types_1.NetworkTypes.MAINNET,
58
+ precision: 8,
59
+ symbol: 'WBTC'
60
+ };
61
+ exports.ETH = {
62
+ assetId: 'eip155:1/slip44:60',
63
+ chainId: 'eip155:1',
64
+ chain: types_1.ChainTypes.Ethereum,
65
+ network: types_1.NetworkTypes.MAINNET,
66
+ symbol: 'ETH',
67
+ name: 'Ethereum',
68
+ precision: 18,
69
+ color: '#FFFFFF',
70
+ icon: 'https://assets.coincap.io/assets/icons/eth@2x.png',
71
+ explorer: 'https://etherscan.io',
72
+ explorerTxLink: 'https://etherscan.io/tx/',
73
+ explorerAddressLink: 'https://etherscan.io/address/'
74
+ };
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ZrxApprovalNeeded = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
5
  const api_1 = require("../../../api");
6
+ const bignumber_1 = require("../../utils/bignumber");
6
7
  const erc20Allowance_abi_1 = require("../utils/abi/erc20Allowance-abi");
7
- const bignumber_1 = require("../utils/bignumber");
8
8
  const helpers_1 = require("../utils/helpers/helpers");
9
9
  async function ZrxApprovalNeeded({ adapterManager, web3 }, { quote, wallet }) {
10
10
  var _a;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getZrxMinMax = void 0;
4
4
  const api_1 = require("../../../api");
5
- const bignumber_1 = require("../utils/bignumber");
5
+ const bignumber_1 = require("../../utils/bignumber");
6
6
  const constants_1 = require("../utils/constants");
7
7
  const helpers_1 = require("../utils/helpers/helpers");
8
8
  const getZrxMinMax = async (sellAsset, buyAsset) => {
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getZrxTradeQuote = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
5
  const api_1 = require("../../../api");
6
+ const bignumber_1 = require("../../utils/bignumber");
6
7
  const getZrxMinMax_1 = require("../getZrxMinMax/getZrxMinMax");
7
- const bignumber_1 = require("../utils/bignumber");
8
8
  const constants_1 = require("../utils/constants");
9
9
  const helpers_1 = require("../utils/helpers/helpers");
10
10
  const zrxService_1 = require("../utils/zrxService");
@@ -4,7 +4,7 @@ exports.grantAllowance = exports.getUsdRate = exports.getAllowanceRequired = exp
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
- const bignumber_1 = require("../bignumber");
7
+ const bignumber_1 = require("../../../utils/bignumber");
8
8
  const zrxService_1 = require("../zrxService");
9
9
  /**
10
10
  * Very large amounts like those found in ERC20s with a precision of 18 get converted
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.setupExecuteTrade = exports.setupBuildTrade = exports.setupZrxTradeQuoteResponse = exports.setupQuote = void 0;
4
- const assets_1 = require("./assets");
4
+ const assets_1 = require("../../../utils/test-data/assets");
5
5
  const setupQuote = () => {
6
6
  const sellAsset = { ...assets_1.FOX };
7
7
  const buyAsset = { ...assets_1.WETH };
@@ -27,9 +27,9 @@ exports.zrxBuildTrade = void 0;
27
27
  const caip_1 = require("@shapeshiftoss/caip");
28
28
  const rax = __importStar(require("retry-axios"));
29
29
  const __1 = require("../../..");
30
+ const bignumber_1 = require("../../utils/bignumber");
30
31
  const erc20Allowance_abi_1 = require("../utils/abi/erc20Allowance-abi");
31
32
  const applyAxiosRetry_1 = require("../utils/applyAxiosRetry");
32
- const bignumber_1 = require("../utils/bignumber");
33
33
  const constants_1 = require("../utils/constants");
34
34
  const helpers_1 = require("../utils/helpers/helpers");
35
35
  const zrxService_1 = require("../utils/zrxService");
@@ -3,7 +3,7 @@ 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 bignumber_1 = require("../utils/bignumber");
6
+ const bignumber_1 = require("../../utils/bignumber");
7
7
  async function zrxExecuteTrade({ adapterManager }, { trade, wallet }) {
8
8
  var _a, _b, _c, _d, _e, _f;
9
9
  const zrxTrade = trade;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "5.11.0",
3
+ "version": "5.12.0",
4
4
  "description": "TODO: description",
5
5
  "repository": "https://github.com/shapeshift/lib",
6
6
  "license": "MIT",