@shapeshiftoss/swapper 9.1.0 → 9.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/api.d.ts CHANGED
@@ -85,7 +85,6 @@ declare type GetBtcTradeQuoteInput = CommonTradeInput & {
85
85
  };
86
86
  export declare type GetTradeQuoteInput = GetBtcTradeQuoteInput | GetEvmTradeQuoteInput | GetCosmosSdkTradeQuoteInput;
87
87
  export declare type BuildTradeInput = GetTradeQuoteInput & {
88
- buyAssetAccountNumber: number;
89
88
  slippage?: string;
90
89
  wallet: HDWallet;
91
90
  };
@@ -137,7 +137,6 @@ const main = async () => {
137
137
  sellAmount: sellAmountBase,
138
138
  sellAsset,
139
139
  sellAssetAccountNumber: 0,
140
- buyAssetAccountNumber: 0,
141
140
  receiveAddress: '0xc770eefad204b5180df6a14ee197d99d808ee52d'
142
141
  });
143
142
  const txid = await swapper.executeTrade({ trade, wallet });
@@ -1,9 +1,5 @@
1
1
  import { ChainId } from '@shapeshiftoss/caip';
2
2
  import { Trade } from '../../api';
3
- export declare type CowSwapPriceResponse = {
4
- amount: string;
5
- token: string;
6
- };
7
3
  export declare type CowSwapQuoteResponse = {
8
4
  quote: {
9
5
  sellToken: string;
@@ -1,6 +1,6 @@
1
1
  export declare const MAX_ALLOWANCE = "100000000000000000000000000";
2
2
  export declare const MAX_COWSWAP_TRADE = "100000000000000000000000000";
3
- export declare const MIN_COWSWAP_VALUE_USD = 10;
3
+ export declare const MIN_COWSWAP_VALUE_USD = 20;
4
4
  export declare const DEFAULT_SOURCE: {
5
5
  name: string;
6
6
  proportion: string;
@@ -11,6 +11,7 @@ export declare const COW_SWAP_VAULT_RELAYER_ADDRESS = "0xc92e8bdf79f0507f65a392b
11
11
  export declare const COW_SWAP_SETTLEMENT_ADDRESS = "0x9008D19f58AAbD9eD0D60971565AA8510560ab41";
12
12
  export declare const WETH_ASSET_ID = "eip155:1/erc20:0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
13
13
  export declare const ORDER_KIND_SELL = "sell";
14
+ export declare const ORDER_KIND_BUY = "buy";
14
15
  export declare const SIGNING_SCHEME = "ethsign";
15
16
  export declare const ERC20_TOKEN_BALANCE = "erc20";
16
17
  export declare const ORDER_STATUS_FULFILLED = "fulfilled";
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ORDER_STATUS_FULFILLED = exports.ERC20_TOKEN_BALANCE = exports.SIGNING_SCHEME = exports.ORDER_KIND_SELL = exports.WETH_ASSET_ID = exports.COW_SWAP_SETTLEMENT_ADDRESS = exports.COW_SWAP_VAULT_RELAYER_ADDRESS = exports.DEFAULT_APP_DATA = exports.DEFAULT_ADDRESS = exports.DEFAULT_SOURCE = exports.MIN_COWSWAP_VALUE_USD = exports.MAX_COWSWAP_TRADE = exports.MAX_ALLOWANCE = void 0;
3
+ exports.ORDER_STATUS_FULFILLED = exports.ERC20_TOKEN_BALANCE = exports.SIGNING_SCHEME = exports.ORDER_KIND_BUY = exports.ORDER_KIND_SELL = exports.WETH_ASSET_ID = exports.COW_SWAP_SETTLEMENT_ADDRESS = exports.COW_SWAP_VAULT_RELAYER_ADDRESS = exports.DEFAULT_APP_DATA = exports.DEFAULT_ADDRESS = exports.DEFAULT_SOURCE = exports.MIN_COWSWAP_VALUE_USD = exports.MAX_COWSWAP_TRADE = exports.MAX_ALLOWANCE = void 0;
4
4
  const constants_1 = require("@ethersproject/constants");
5
5
  exports.MAX_ALLOWANCE = '100000000000000000000000000';
6
6
  exports.MAX_COWSWAP_TRADE = '100000000000000000000000000';
7
- exports.MIN_COWSWAP_VALUE_USD = 10;
7
+ exports.MIN_COWSWAP_VALUE_USD = 20;
8
8
  exports.DEFAULT_SOURCE = [{ name: 'CowSwap', proportion: '1' }];
9
9
  exports.DEFAULT_ADDRESS = constants_1.AddressZero;
10
10
  exports.DEFAULT_APP_DATA = constants_1.HashZero;
@@ -12,6 +12,7 @@ exports.COW_SWAP_VAULT_RELAYER_ADDRESS = '0xc92e8bdf79f0507f65a392b0ab4667716bfe
12
12
  exports.COW_SWAP_SETTLEMENT_ADDRESS = '0x9008D19f58AAbD9eD0D60971565AA8510560ab41';
13
13
  exports.WETH_ASSET_ID = 'eip155:1/erc20:0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
14
14
  exports.ORDER_KIND_SELL = 'sell';
15
+ exports.ORDER_KIND_BUY = 'buy';
15
16
  exports.SIGNING_SCHEME = 'ethsign';
16
17
  exports.ERC20_TOKEN_BALANCE = 'erc20';
17
18
  exports.ORDER_STATUS_FULFILLED = 'fulfilled';
@@ -23,19 +23,24 @@ export declare type CowSwapOrder = {
23
23
  sellTokenBalance: string;
24
24
  buyTokenBalance: string;
25
25
  };
26
- export declare type CowSwapQuoteApiInput = {
26
+ export declare type CowSwapQuoteApiInputBase = {
27
27
  appData: string;
28
28
  buyToken: string;
29
29
  from: string;
30
30
  kind: string;
31
31
  partiallyFillable: boolean;
32
32
  receiver: string;
33
- sellAmountBeforeFee: string;
34
33
  sellToken: string;
35
34
  validTo: number;
36
35
  };
37
- export declare const getUsdRate: ({ apiUrl }: CowSwapperDeps, input: Asset) => Promise<string>;
36
+ export declare type CowSwapSellQuoteApiInput = CowSwapQuoteApiInputBase & {
37
+ sellAmountBeforeFee: string;
38
+ };
39
+ export declare type CowSwapBuyQuoteApiInput = CowSwapQuoteApiInputBase & {
40
+ buyAmountAfterFee: string;
41
+ };
38
42
  export declare const getNowPlusThirtyMinutesTimestamp: () => number;
43
+ export declare const getUsdRate: ({ apiUrl }: CowSwapperDeps, input: Asset) => Promise<string>;
39
44
  export declare const hashTypedData: (domain: TypedDataDomain, types: TypedDataTypes, data: Record<string, unknown>) => string;
40
45
  /**
41
46
  * Compute the 32-byte signing hash for the specified order.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.domain = exports.hashOrder = exports.hashTypedData = exports.getNowPlusThirtyMinutesTimestamp = exports.getUsdRate = exports.ORDER_TYPE_FIELDS = void 0;
3
+ exports.domain = exports.hashOrder = exports.hashTypedData = exports.getUsdRate = exports.getNowPlusThirtyMinutesTimestamp = exports.ORDER_TYPE_FIELDS = void 0;
4
4
  const asset_service_1 = require("@shapeshiftoss/asset-service");
5
5
  const caip_1 = require("@shapeshiftoss/caip");
6
6
  const ethers_1 = require("ethers");
@@ -24,6 +24,12 @@ exports.ORDER_TYPE_FIELDS = [
24
24
  { name: 'sellTokenBalance', type: 'string' },
25
25
  { name: 'buyTokenBalance', type: 'string' }
26
26
  ];
27
+ const getNowPlusThirtyMinutesTimestamp = () => {
28
+ const ts = new Date();
29
+ ts.setMinutes(ts.getMinutes() + 30);
30
+ return Math.round(ts.getTime() / 1000);
31
+ };
32
+ exports.getNowPlusThirtyMinutesTimestamp = getNowPlusThirtyMinutesTimestamp;
27
33
  const getUsdRate = async ({ apiUrl }, input) => {
28
34
  // Replacing ETH by WETH specifically for CowSwap in order to get an usd rate when called with ETH as feeAsset
29
35
  const asset = input.assetId !== caip_1.ethAssetId ? input : new asset_service_1.AssetService().getAll()[constants_1.WETH_ASSET_ID];
@@ -37,20 +43,46 @@ const getUsdRate = async ({ apiUrl }, input) => {
37
43
  if (erc20Address === USDC_CONTRACT_ADDRESS) {
38
44
  return '1';
39
45
  }
46
+ // rate is imprecise for low $ values, hence asking for $1000
40
47
  const buyAmountInDollars = 1000;
41
- const buyAmount = (0, bignumber_1.bn)(buyAmountInDollars).times((0, bignumber_1.bn)(10).exponentiatedBy(USDC_ASSET_PRECISION));
48
+ const buyAmount = (0, bignumber_1.bn)(buyAmountInDollars)
49
+ .times((0, bignumber_1.bn)(10).exponentiatedBy(USDC_ASSET_PRECISION))
50
+ .toString();
42
51
  try {
43
- // rate is imprecise for low $ values, hence asking for $1000
44
- // cowSwap api used : markets/{baseToken}-{quoteToken}/{kind}/{amount}
45
- // It returns the estimated amount in quoteToken for either buying or selling amount of baseToken.
46
- const rateResponse = await cowService_1.cowService.get(`${apiUrl}/v1/markets/${USDC_CONTRACT_ADDRESS}-${erc20Address}/buy/${buyAmount}`);
47
- const tokenAmount = (0, bignumber_1.bnOrZero)(rateResponse.data.amount).div((0, bignumber_1.bn)(10).exponentiatedBy(asset.precision));
48
- if (!tokenAmount.gt(0))
49
- throw new api_1.SwapError('[getUsdRate] - Failed to get token amount', {
52
+ const apiInput = {
53
+ sellToken: erc20Address,
54
+ buyToken: USDC_CONTRACT_ADDRESS,
55
+ receiver: constants_1.DEFAULT_ADDRESS,
56
+ validTo: (0, exports.getNowPlusThirtyMinutesTimestamp)(),
57
+ appData: constants_1.DEFAULT_APP_DATA,
58
+ partiallyFillable: false,
59
+ from: constants_1.DEFAULT_ADDRESS,
60
+ kind: constants_1.ORDER_KIND_BUY,
61
+ buyAmountAfterFee: buyAmount
62
+ };
63
+ /**
64
+ * /v1/quote
65
+ * params: {
66
+ * sellToken: contract address of token to sell
67
+ * buyToken: contractAddress of token to buy
68
+ * receiver: receiver address can be defaulted to "0x0000000000000000000000000000000000000000"
69
+ * validTo: time duration during which quote is valid (eg : 1654851610 as timestamp)
70
+ * appData: appData for the CowSwap quote that can be used later, can be defaulted to "0x0000000000000000000000000000000000000000000000000000000000000000"
71
+ * partiallyFillable: false
72
+ * from: sender address can be defaulted to "0x0000000000000000000000000000000000000000"
73
+ * kind: "sell" or "buy"
74
+ * sellAmountBeforeFee / buyAmountAfterFee: amount in base unit
75
+ * }
76
+ */
77
+ const quoteResponse = await cowService_1.cowService.post(`${apiUrl}/v1/quote/`, apiInput);
78
+ const { data: { quote } } = quoteResponse;
79
+ const sellCryptoAmount = (0, bignumber_1.bn)(quote.sellAmount).div((0, bignumber_1.bn)(10).exponentiatedBy(asset.precision));
80
+ if (!sellCryptoAmount.gt(0))
81
+ throw new api_1.SwapError('[getUsdRate] - Failed to get sell token amount', {
50
82
  code: api_1.SwapErrorTypes.RESPONSE_ERROR
51
83
  });
52
- // dividing $1000 by amount of token received
53
- return (0, bignumber_1.bn)(buyAmountInDollars).dividedBy(tokenAmount).toString();
84
+ // dividing $1000 by amount of sell token received
85
+ return (0, bignumber_1.bn)(buyAmountInDollars).div(sellCryptoAmount).toString();
54
86
  }
55
87
  catch (e) {
56
88
  if (e instanceof api_1.SwapError)
@@ -62,12 +94,6 @@ const getUsdRate = async ({ apiUrl }, input) => {
62
94
  }
63
95
  };
64
96
  exports.getUsdRate = getUsdRate;
65
- const getNowPlusThirtyMinutesTimestamp = () => {
66
- const ts = new Date();
67
- ts.setMinutes(ts.getMinutes() + 30);
68
- return Math.round(ts.getTime() / 1000);
69
- };
70
- exports.getNowPlusThirtyMinutesTimestamp = getNowPlusThirtyMinutesTimestamp;
71
97
  const hashTypedData = (domain, types, data) => {
72
98
  return ethers_1.ethers.utils._TypedDataEncoder.hash(domain, types, data);
73
99
  };
@@ -20,7 +20,6 @@ export declare const setupBuildTrade: () => {
20
20
  } & {
21
21
  chainId: import("../../../api").EvmSupportedChainIds;
22
22
  } & {
23
- buyAssetAccountNumber: number;
24
23
  slippage?: string | undefined;
25
24
  wallet: HDWallet;
26
25
  };
@@ -41,7 +41,6 @@ const setupBuildTrade = () => {
41
41
  buyAsset,
42
42
  sendMax: false,
43
43
  sellAssetAccountNumber: 0,
44
- buyAssetAccountNumber: 0,
45
44
  sellAsset,
46
45
  wallet: {},
47
46
  receiveAddress: ''
@@ -18,7 +18,6 @@ export declare const setupExecuteTrade: () => {
18
18
  sellAsset: Asset;
19
19
  sendMax: boolean;
20
20
  sellAssetAccountNumber: number;
21
- buyAssetAccountNumber: number;
22
21
  txData: string;
23
22
  depositAddress: string;
24
23
  receiveAddress: string;
@@ -34,7 +34,6 @@ const setupExecuteTrade = () => {
34
34
  sellAsset,
35
35
  sendMax: false,
36
36
  sellAssetAccountNumber: 0,
37
- buyAssetAccountNumber: 0,
38
37
  txData: '0x0',
39
38
  depositAddress: '0x0',
40
39
  receiveAddress: '0x0',
@@ -37,7 +37,7 @@ 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;
40
- const { sellAsset, buyAsset, sellAmount, slippage, sellAssetAccountNumber, buyAssetAccountNumber, wallet } = input;
40
+ const { sellAsset, buyAsset, sellAmount, slippage, sellAssetAccountNumber, receiveAddress } = input;
41
41
  try {
42
42
  const { assetReference: buyAssetErc20Address, assetNamespace: buyAssetNamespace } = (0, caip_1.fromAssetId)(buyAsset.assetId);
43
43
  const { assetReference: sellAssetErc20Address, assetNamespace: sellAssetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
@@ -50,8 +50,6 @@ async function zrxBuildTrade({ adapter, web3 }, input) {
50
50
  details: { chainId: sellAsset.chainId }
51
51
  });
52
52
  }
53
- const bip44Params = adapter.buildBIP44Params({ accountNumber: Number(buyAssetAccountNumber) });
54
- const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
55
53
  const slippagePercentage = slippage ? (0, bignumber_1.bnOrZero)(slippage).div(100).toString() : constants_1.DEFAULT_SLIPPAGE;
56
54
  const baseUrl = (0, helpers_2.baseUrlFromChainId)(buyAsset.chainId);
57
55
  const zrxService = (0, zrxService_1.zrxServiceFactory)(baseUrl);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "9.1.0",
3
+ "version": "9.2.1",
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",
@@ -30,23 +30,23 @@
30
30
  "web3": "1.7.4"
31
31
  },
32
32
  "peerDependencies": {
33
- "@shapeshiftoss/asset-service": "^7.0.0",
34
- "@shapeshiftoss/caip": "^6.2.0",
35
- "@shapeshiftoss/chain-adapters": "^7.2.0",
33
+ "@shapeshiftoss/asset-service": "^7.1.0",
34
+ "@shapeshiftoss/caip": "^6.7.0",
35
+ "@shapeshiftoss/chain-adapters": "^7.13.0",
36
36
  "@shapeshiftoss/errors": "^1.1.2",
37
37
  "@shapeshiftoss/hdwallet-core": "^1.27.0",
38
- "@shapeshiftoss/types": "^8.0.0"
38
+ "@shapeshiftoss/types": "^8.1.0"
39
39
  },
40
40
  "devDependencies": {
41
- "@shapeshiftoss/asset-service": "^7.0.0",
42
- "@shapeshiftoss/caip": "^6.2.0",
43
- "@shapeshiftoss/chain-adapters": "^7.2.0",
41
+ "@shapeshiftoss/asset-service": "^7.1.0",
42
+ "@shapeshiftoss/caip": "^6.7.0",
43
+ "@shapeshiftoss/chain-adapters": "^7.13.0",
44
44
  "@shapeshiftoss/errors": "^1.1.2",
45
45
  "@shapeshiftoss/hdwallet-core": "^1.27.0",
46
- "@shapeshiftoss/types": "^8.0.0",
46
+ "@shapeshiftoss/types": "^8.1.0",
47
47
  "@types/readline-sync": "^1.4.4",
48
48
  "readline-sync": "^1.4.10",
49
49
  "web3-utils": "1.7.4",
50
- "@shapeshiftoss/unchained-client": "^9.0.1"
50
+ "@shapeshiftoss/unchained-client": "^9.5.0"
51
51
  }
52
52
  }