@shapeshiftoss/swapper 5.1.1 → 5.1.4

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/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ## ShapeShift Swapper
1
+ # @shapeshiftoss/swapper
2
2
 
3
3
  ## Getting Started
4
4
 
@@ -10,16 +10,16 @@ yarn add @shapeshiftoss/swapper
10
10
 
11
11
  ### Swapper CLI
12
12
  - Copy the `sample.env` to `.env`
13
- ```
13
+ ```sh
14
14
  cp sample.env .env
15
15
  ```
16
16
 
17
17
  - Usage:
18
- ```
18
+ ```sh
19
19
  $ swapcli [sellSymbol] [buySymbol] [sellAmount](denominated in sell asset, not wei)
20
20
  ```
21
21
  ie:
22
- ```
22
+ ```sh
23
23
  $ swapcli ETH USDC 0.001
24
24
  ```
25
25
 
package/dist/api.d.ts CHANGED
@@ -79,9 +79,11 @@ export interface TradeQuote<C extends SupportedChainIds> extends TradeBase<C> {
79
79
  maximum: string;
80
80
  }
81
81
  export interface Trade<C extends SupportedChainIds> extends TradeBase<C> {
82
+ receiveAddress: string;
83
+ }
84
+ export interface ZrxTrade<C extends SupportedChainIds> extends Trade<C> {
82
85
  txData: string;
83
86
  depositAddress: string;
84
- receiveAddress: string;
85
87
  }
86
88
  export declare type ExecuteTradeInput<C extends SupportedChainIds> = {
87
89
  trade: Trade<C>;
@@ -9,7 +9,7 @@ export declare class TestSwapper implements Swapper {
9
9
  supportAssets: string[];
10
10
  getType(): SwapperType;
11
11
  constructor();
12
- getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
12
+ getUsdRate(input: Pick<Asset, 'symbol' | 'assetId'>): Promise<string>;
13
13
  getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
14
14
  approvalNeeded(): Promise<ApprovalNeededOutput>;
15
15
  approveInfinite(): Promise<string>;
@@ -3,7 +3,7 @@ import { ApprovalNeededOutput, Asset, ExecQuoteOutput, GetMinMaxInput, MinMaxOut
3
3
  import { Swapper, Trade, TradeQuote } from '../../api';
4
4
  export declare class ThorchainSwapper implements Swapper {
5
5
  getType(): SwapperType;
6
- getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
6
+ getUsdRate(input: Pick<Asset, 'symbol' | 'assetId'>): Promise<string>;
7
7
  getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
8
8
  approvalNeeded(): Promise<ApprovalNeededOutput>;
9
9
  approveInfinite(): Promise<string>;
@@ -10,6 +10,7 @@ const helpers_1 = require("../utils/helpers/helpers");
10
10
  async function ZrxApprovalNeeded({ adapterManager, web3 }, { quote, wallet }) {
11
11
  var _a, _b;
12
12
  const { sellAsset } = quote;
13
+ const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(sellAsset.assetId);
13
14
  try {
14
15
  if (sellAsset.chainId !== 'eip155:1') {
15
16
  throw new api_1.SwapError('[ZrxApprovalNeeded] - sellAsset chainId is not supported', {
@@ -25,8 +26,8 @@ async function ZrxApprovalNeeded({ adapterManager, web3 }, { quote, wallet }) {
25
26
  const adapter = await adapterManager.byChainId(sellAsset.chainId);
26
27
  const bip44Params = adapter.buildBIP44Params({ accountNumber });
27
28
  const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
28
- if (!quote.sellAsset.tokenId || !quote.allowanceContract) {
29
- throw new api_1.SwapError('[ZrxApprovalNeeded] - tokenId and allowanceTarget are required', {
29
+ if (!quote.allowanceContract) {
30
+ throw new api_1.SwapError('[ZrxApprovalNeeded] - allowanceTarget is required', {
30
31
  code: api_1.SwapErrorTypes.VALIDATION_FAILED,
31
32
  details: { chainId: sellAsset.chainId }
32
33
  });
@@ -34,7 +35,7 @@ async function ZrxApprovalNeeded({ adapterManager, web3 }, { quote, wallet }) {
34
35
  const allowanceResult = await (0, helpers_1.getERC20Allowance)({
35
36
  web3,
36
37
  erc20AllowanceAbi: erc20Allowance_abi_1.erc20AllowanceAbi,
37
- tokenId: quote.sellAsset.tokenId,
38
+ sellAssetErc20Address,
38
39
  spenderAddress: quote.allowanceContract,
39
40
  ownerAddress: receiveAddress
40
41
  });
@@ -2,7 +2,7 @@ import { AssetId } from '@shapeshiftoss/caip';
2
2
  import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
3
3
  import { ApprovalNeededOutput, Asset, ExecQuoteOutput, GetMinMaxInput, MinMaxOutput, SupportedChainIds, SwapperType } from '@shapeshiftoss/types';
4
4
  import Web3 from 'web3';
5
- import { ApprovalNeededInput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, Trade, TradeQuote } from '../../api';
5
+ import { ApprovalNeededInput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, TradeQuote, ZrxTrade } from '../../api';
6
6
  export declare type ZrxSwapperDeps = {
7
7
  adapterManager: ChainAdapterManager;
8
8
  web3: Web3;
@@ -12,9 +12,9 @@ export declare class ZrxSwapper implements Swapper {
12
12
  deps: ZrxSwapperDeps;
13
13
  constructor(deps: ZrxSwapperDeps);
14
14
  getType(): SwapperType;
15
- buildTrade(args: BuildTradeInput): Promise<Trade<SupportedChainIds>>;
15
+ buildTrade(args: BuildTradeInput): Promise<ZrxTrade<SupportedChainIds>>;
16
16
  getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<SupportedChainIds>>;
17
- getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
17
+ getUsdRate(input: Pick<Asset, 'symbol' | 'assetId'>): Promise<string>;
18
18
  getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
19
19
  executeTrade(args: ExecuteTradeInput<SupportedChainIds>): Promise<ExecQuoteOutput>;
20
20
  approvalNeeded(args: ApprovalNeededInput<SupportedChainIds>): Promise<ApprovalNeededOutput>;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getZrxTradeQuote = void 0;
4
+ const caip_1 = require("@shapeshiftoss/caip");
4
5
  const api_1 = require("../../../api");
5
6
  const getZrxMinMax_1 = require("../getZrxMinMax/getZrxMinMax");
6
7
  const bignumber_1 = require("../utils/bignumber");
@@ -17,12 +18,14 @@ async function getZrxTradeQuote(input) {
17
18
  details: { buyAssetChainId: buyAsset.chainId, sellAssetChainId: sellAsset.chainId }
18
19
  });
19
20
  }
21
+ const { assetReference: sellAssetErc20Address, assetNamespace: sellAssetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
22
+ const { assetReference: buyAssetErc20Address, assetNamespace: buyAssetNamespace } = (0, caip_1.fromAssetId)(buyAsset.assetId);
20
23
  const useSellAmount = !!sellAmount;
21
- const buyToken = buyAsset.tokenId || buyAsset.symbol;
22
- const sellToken = sellAsset.tokenId || sellAsset.symbol;
24
+ const buyToken = buyAssetNamespace === 'erc20' ? buyAssetErc20Address : buyAsset.symbol;
25
+ const sellToken = sellAssetNamespace === 'erc20' ? sellAssetErc20Address : sellAsset.symbol;
23
26
  const { minimum, maximum } = await (0, getZrxMinMax_1.getZrxMinMax)(sellAsset, buyAsset);
24
27
  const minQuoteSellAmount = (0, bignumber_1.bnOrZero)(minimum).times((0, bignumber_1.bn)(10).exponentiatedBy(sellAsset.precision));
25
- const normalizedSellAmount = (0, helpers_1.normalizeAmount)((0, bignumber_1.bnOrZero)(sellAmount).eq(0) ? minQuoteSellAmount.toString() : sellAmount);
28
+ const normalizedSellAmount = (0, helpers_1.normalizeAmount)((0, bignumber_1.bnOrZero)(sellAmount).eq(0) ? minQuoteSellAmount : sellAmount);
26
29
  /**
27
30
  * /swap/v1/price
28
31
  * params: {
@@ -53,7 +56,7 @@ async function getZrxTradeQuote(input) {
53
56
  chainSpecific: {
54
57
  estimatedGas: estimatedGas.toString(),
55
58
  gasPrice: data.gasPrice,
56
- approvalFee: sellAsset.tokenId &&
59
+ approvalFee: sellAssetErc20Address &&
57
60
  (0, bignumber_1.bnOrZero)(constants_1.APPROVAL_GAS_LIMIT).multipliedBy((0, bignumber_1.bnOrZero)(data.gasPrice)).toString()
58
61
  }
59
62
  },
@@ -16,7 +16,7 @@ export declare type GetAllowanceRequiredArgs = {
16
16
  export declare type GetERC20AllowanceArgs = {
17
17
  erc20AllowanceAbi: AbiItem[];
18
18
  web3: Web3;
19
- tokenId: string;
19
+ sellAssetErc20Address: string;
20
20
  ownerAddress: string;
21
21
  spenderAddress: string;
22
22
  };
@@ -35,9 +35,9 @@ declare type GrantAllowanceArgs = {
35
35
  * ETH or ERC20, we still keep 7 decimal places.
36
36
  * @param amount
37
37
  */
38
- export declare const normalizeAmount: (amount: string | undefined) => string | undefined;
39
- export declare const getERC20Allowance: ({ erc20AllowanceAbi, web3, tokenId, ownerAddress, spenderAddress }: GetERC20AllowanceArgs) => Promise<any>;
38
+ export declare const normalizeAmount: (amount: string | number | BigNumber) => string;
39
+ export declare const getERC20Allowance: ({ erc20AllowanceAbi, web3, sellAssetErc20Address, ownerAddress, spenderAddress }: GetERC20AllowanceArgs) => Promise<any>;
40
40
  export declare const getAllowanceRequired: ({ receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi }: GetAllowanceRequiredArgs) => Promise<BigNumber>;
41
- export declare const getUsdRate: (input: Pick<Asset, 'symbol' | 'tokenId'>) => Promise<string>;
41
+ export declare const getUsdRate: (input: Pick<Asset, 'symbol' | 'assetId'>) => Promise<string>;
42
42
  export declare const grantAllowance: ({ quote, wallet, adapterManager, erc20Abi, web3 }: GrantAllowanceArgs) => Promise<string>;
43
43
  export {};
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.grantAllowance = exports.getUsdRate = exports.getAllowanceRequired = exports.getERC20Allowance = exports.normalizeAmount = void 0;
4
+ const caip_1 = require("@shapeshiftoss/caip");
4
5
  const web3_utils_1 = require("web3-utils");
5
6
  const api_1 = require("../../../../api");
6
7
  const bignumber_1 = require("../bignumber");
@@ -14,13 +15,11 @@ const zrxService_1 = require("../zrxService");
14
15
  * @param amount
15
16
  */
16
17
  const normalizeAmount = (amount) => {
17
- if (!amount)
18
- return;
19
18
  return (0, bignumber_1.bnOrZero)(amount).toNumber().toLocaleString('fullwide', { useGrouping: false });
20
19
  };
21
20
  exports.normalizeAmount = normalizeAmount;
22
- const getERC20Allowance = async ({ erc20AllowanceAbi, web3, tokenId, ownerAddress, spenderAddress }) => {
23
- const erc20Contract = new web3.eth.Contract(erc20AllowanceAbi, tokenId);
21
+ const getERC20Allowance = async ({ erc20AllowanceAbi, web3, sellAssetErc20Address, ownerAddress, spenderAddress }) => {
22
+ const erc20Contract = new web3.eth.Contract(erc20AllowanceAbi, sellAssetErc20Address);
24
23
  return erc20Contract.methods.allowance(ownerAddress, spenderAddress).call();
25
24
  };
26
25
  exports.getERC20Allowance = getERC20Allowance;
@@ -31,18 +30,13 @@ const getAllowanceRequired = async ({ receiveAddress, allowanceContract, sellAss
31
30
  }
32
31
  const ownerAddress = receiveAddress;
33
32
  const spenderAddress = allowanceContract;
34
- const tokenId = sellAsset.tokenId;
35
- if (!tokenId) {
36
- throw new api_1.SwapError('[getAllowanceRequired] - sellAsset.tokenId is required', {
37
- code: api_1.SwapErrorTypes.VALIDATION_FAILED
38
- });
39
- }
33
+ const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(sellAsset.assetId);
40
34
  const allowanceOnChain = await (0, exports.getERC20Allowance)({
41
35
  web3,
42
36
  erc20AllowanceAbi,
43
37
  ownerAddress,
44
38
  spenderAddress,
45
- tokenId
39
+ sellAssetErc20Address
46
40
  });
47
41
  if (allowanceOnChain === '0')
48
42
  return (0, bignumber_1.bnOrZero)(sellAmount);
@@ -66,16 +60,17 @@ const getAllowanceRequired = async ({ receiveAddress, allowanceContract, sellAss
66
60
  };
67
61
  exports.getAllowanceRequired = getAllowanceRequired;
68
62
  const getUsdRate = async (input) => {
69
- const { symbol, tokenId } = input;
63
+ const { symbol, assetId } = input;
64
+ const { assetReference: erc20Address, assetNamespace } = (0, caip_1.fromAssetId)(assetId);
70
65
  try {
71
66
  const USDC_CONTRACT_ADDRESS = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
72
- if ((tokenId === null || tokenId === void 0 ? void 0 : tokenId.toLowerCase()) === USDC_CONTRACT_ADDRESS)
67
+ if ((erc20Address === null || erc20Address === void 0 ? void 0 : erc20Address.toLowerCase()) === USDC_CONTRACT_ADDRESS)
73
68
  return '1'; // Will break if comparing against usdc
74
69
  const rateResponse = await zrxService_1.zrxService.get('/swap/v1/price', {
75
70
  params: {
76
71
  buyToken: USDC_CONTRACT_ADDRESS,
77
72
  buyAmount: '1000000000',
78
- sellToken: tokenId || symbol
73
+ sellToken: assetNamespace === 'erc20' ? erc20Address : symbol
79
74
  }
80
75
  });
81
76
  const price = (0, bignumber_1.bnOrZero)(rateResponse.data.price);
@@ -98,13 +93,9 @@ exports.getUsdRate = getUsdRate;
98
93
  const grantAllowance = async ({ quote, wallet, adapterManager, erc20Abi, web3 }) => {
99
94
  var _a, _b, _c, _d, _e;
100
95
  try {
101
- if (!quote.sellAsset.tokenId) {
102
- throw new api_1.SwapError('[grantAllowance] - sellAsset.tokenId is required', {
103
- code: api_1.SwapErrorTypes.VALIDATION_FAILED
104
- });
105
- }
96
+ const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(quote.sellAsset.assetId);
106
97
  const adapter = await adapterManager.byChainId('eip155:1');
107
- const erc20Contract = new web3.eth.Contract(erc20Abi, quote.sellAsset.tokenId);
98
+ const erc20Contract = new web3.eth.Contract(erc20Abi, sellAssetErc20Address);
108
99
  const approveTx = erc20Contract.methods
109
100
  .approve(quote.allowanceContract, quote.sellAmount)
110
101
  .encodeABI();
@@ -112,11 +103,11 @@ const grantAllowance = async ({ quote, wallet, adapterManager, erc20Abi, web3 })
112
103
  const bip44Params = adapter.buildBIP44Params({ accountNumber });
113
104
  const { txToSign } = await adapter.buildSendTransaction({
114
105
  wallet,
115
- to: quote.sellAsset.tokenId,
106
+ to: sellAssetErc20Address,
116
107
  bip44Params,
117
108
  value: '0',
118
109
  chainSpecific: {
119
- erc20ContractAddress: quote.sellAsset.tokenId,
110
+ erc20ContractAddress: sellAssetErc20Address,
120
111
  gasPrice: (0, web3_utils_1.numberToHex)(((_b = (_a = quote.feeData) === null || _a === void 0 ? void 0 : _a.chainSpecific) === null || _b === void 0 ? void 0 : _b.gasPrice) || 0),
121
112
  gasLimit: (0, web3_utils_1.numberToHex)(((_d = (_c = quote.feeData) === null || _c === void 0 ? void 0 : _c.chainSpecific) === null || _d === void 0 ? void 0 : _d.estimatedGas) || 0)
122
113
  }
@@ -1,4 +1,4 @@
1
1
  import { SupportedChainIds } from '@shapeshiftoss/types';
2
- import { BuildTradeInput, Trade } from '../../..';
2
+ import { BuildTradeInput, ZrxTrade } from '../../..';
3
3
  import { ZrxSwapperDeps } from '../ZrxSwapper';
4
- export declare function zrxBuildTrade({ adapterManager, web3 }: ZrxSwapperDeps, input: BuildTradeInput): Promise<Trade<SupportedChainIds>>;
4
+ export declare function zrxBuildTrade({ adapterManager, web3 }: ZrxSwapperDeps, input: BuildTradeInput): Promise<ZrxTrade<SupportedChainIds>>;
@@ -24,6 +24,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.zrxBuildTrade = void 0;
27
+ const caip_1 = require("@shapeshiftoss/caip");
27
28
  const rax = __importStar(require("retry-axios"));
28
29
  const __1 = require("../../..");
29
30
  const erc20Allowance_abi_1 = require("../utils/abi/erc20Allowance-abi");
@@ -36,8 +37,10 @@ async function zrxBuildTrade({ adapterManager, web3 }, input) {
36
37
  var _a, _b, _c;
37
38
  const { sellAsset, buyAsset, sellAmount, slippage, sellAssetAccountId, buyAssetAccountId, wallet } = input;
38
39
  try {
39
- const buyToken = buyAsset.tokenId || buyAsset.symbol;
40
- const sellToken = sellAsset.tokenId || sellAsset.symbol;
40
+ const { assetReference: buyAssetErc20Address, assetNamespace: buyAssetNamespace } = (0, caip_1.fromAssetId)(buyAsset.assetId);
41
+ const { assetReference: sellAssetErc20Address, assetNamespace: sellAssetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
42
+ const buyToken = buyAssetNamespace === 'erc20' ? buyAssetErc20Address : buyAsset.symbol;
43
+ const sellToken = sellAssetNamespace === 'erc20' ? sellAssetErc20Address : sellAsset.symbol;
41
44
  if (buyAsset.chainId !== 'eip155:1') {
42
45
  throw new __1.SwapError('[ZrxBuildTrade] - buyAsset must be on chainId eip155:1', {
43
46
  code: __1.SwapErrorTypes.VALIDATION_FAILED,
@@ -77,7 +80,7 @@ async function zrxBuildTrade({ adapterManager, web3 }, input) {
77
80
  params: {
78
81
  buyToken,
79
82
  sellToken,
80
- sellAmount: (0, helpers_1.normalizeAmount)(sellAmount === null || sellAmount === void 0 ? void 0 : sellAmount.toString()),
83
+ sellAmount: (0, helpers_1.normalizeAmount)(sellAmount),
81
84
  takerAddress: receiveAddress,
82
85
  slippagePercentage,
83
86
  skipValidation: false,
@@ -6,7 +6,8 @@ const api_1 = require("../../../api");
6
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
- const { sellAsset } = trade;
9
+ const zrxTrade = trade;
10
+ const { sellAsset } = zrxTrade;
10
11
  try {
11
12
  // value is 0 for erc20s
12
13
  const value = sellAsset.assetId === 'eip155:1/slip44:60' ? trade.sellAmount : '0';
@@ -17,7 +18,7 @@ async function zrxExecuteTrade({ adapterManager }, { trade, wallet }) {
17
18
  const buildTxResponse = await adapter.buildSendTransaction({
18
19
  value,
19
20
  wallet,
20
- to: trade.depositAddress,
21
+ to: zrxTrade.depositAddress,
21
22
  chainSpecific: {
22
23
  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),
23
24
  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)
@@ -25,7 +26,7 @@ async function zrxExecuteTrade({ adapterManager }, { trade, wallet }) {
25
26
  bip44Params
26
27
  });
27
28
  const { txToSign } = buildTxResponse;
28
- const txWithQuoteData = { ...txToSign, data: (_e = trade.txData) !== null && _e !== void 0 ? _e : '' };
29
+ const txWithQuoteData = { ...txToSign, data: (_e = zrxTrade.txData) !== null && _e !== void 0 ? _e : '' };
29
30
  if (wallet.supportsOfflineSigning()) {
30
31
  const signedTx = await adapter.signTransaction({ txToSign: txWithQuoteData, wallet });
31
32
  const txid = await adapter.broadcastTransaction(signedTx);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "5.1.1",
3
+ "version": "5.1.4",
4
4
  "description": "TODO: description",
5
5
  "repository": "https://github.com/shapeshift/lib",
6
6
  "license": "MIT",