@shapeshiftoss/swapper 5.1.3 → 5.2.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.
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
@@ -122,6 +122,8 @@ export declare enum SwapErrorTypes {
122
122
  VALIDATION_FAILED = "VALIDATION_FAILED"
123
123
  }
124
124
  export interface Swapper {
125
+ /** perform any necessary async initialization */
126
+ initialize(): Promise<void>;
125
127
  /** Returns the swapper type */
126
128
  getType(): SwapperType;
127
129
  /**
@@ -7,9 +7,10 @@ import { BuyAssetBySellIdInput, Swapper, Trade, TradeQuote } from '../../api';
7
7
  */
8
8
  export declare class TestSwapper implements Swapper {
9
9
  supportAssets: string[];
10
+ initialize(): Promise<void>;
10
11
  getType(): SwapperType;
11
12
  constructor();
12
- getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
13
+ getUsdRate(input: Pick<Asset, 'symbol' | 'assetId'>): Promise<string>;
13
14
  getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
14
15
  approvalNeeded(): Promise<ApprovalNeededOutput>;
15
16
  approveInfinite(): Promise<string>;
@@ -14,6 +14,9 @@ class TestSwapper {
14
14
  'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d'
15
15
  ];
16
16
  }
17
+ // noop for test
18
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
19
+ async initialize() { }
17
20
  getType() {
18
21
  return types_1.SwapperType.Test;
19
22
  }
@@ -3,7 +3,8 @@ 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
+ initialize(): Promise<void>;
7
+ getUsdRate(input: Pick<Asset, 'symbol' | 'assetId'>): Promise<string>;
7
8
  getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
8
9
  approvalNeeded(): Promise<ApprovalNeededOutput>;
9
10
  approveInfinite(): Promise<string>;
@@ -6,6 +6,9 @@ class ThorchainSwapper {
6
6
  getType() {
7
7
  return types_1.SwapperType.Thorchain;
8
8
  }
9
+ // TODO populate supported assets from midgard
10
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
11
+ async initialize() { }
9
12
  getUsdRate(input) {
10
13
  console.info(input);
11
14
  throw new Error('ThorchainSwapper: getUsdRate unimplemented');
@@ -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
  });
@@ -11,10 +11,11 @@ export declare class ZrxSwapper implements Swapper {
11
11
  static swapperName: string;
12
12
  deps: ZrxSwapperDeps;
13
13
  constructor(deps: ZrxSwapperDeps);
14
+ initialize(): Promise<void>;
14
15
  getType(): SwapperType;
15
16
  buildTrade(args: BuildTradeInput): Promise<ZrxTrade<SupportedChainIds>>;
16
17
  getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<SupportedChainIds>>;
17
- getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
18
+ getUsdRate(input: Pick<Asset, 'symbol' | 'assetId'>): Promise<string>;
18
19
  getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
19
20
  executeTrade(args: ExecuteTradeInput<SupportedChainIds>): Promise<ExecQuoteOutput>;
20
21
  approvalNeeded(args: ApprovalNeededInput<SupportedChainIds>): Promise<ApprovalNeededOutput>;
@@ -14,6 +14,9 @@ class ZrxSwapper {
14
14
  constructor(deps) {
15
15
  this.deps = deps;
16
16
  }
17
+ // noop for zrx
18
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
19
+ async initialize() { }
17
20
  getType() {
18
21
  return types_1.SwapperType.Zrx;
19
22
  }
@@ -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,9 +18,11 @@ 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
28
  const normalizedSellAmount = (0, helpers_1.normalizeAmount)((0, bignumber_1.bnOrZero)(sellAmount).eq(0) ? minQuoteSellAmount : sellAmount);
@@ -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
  };
@@ -36,8 +36,8 @@ declare type GrantAllowanceArgs = {
36
36
  * @param amount
37
37
  */
38
38
  export declare const normalizeAmount: (amount: string | number | BigNumber) => string;
39
- export declare const getERC20Allowance: ({ erc20AllowanceAbi, web3, tokenId, ownerAddress, spenderAddress }: GetERC20AllowanceArgs) => Promise<any>;
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");
@@ -17,8 +18,8 @@ const normalizeAmount = (amount) => {
17
18
  return (0, bignumber_1.bnOrZero)(amount).toNumber().toLocaleString('fullwide', { useGrouping: false });
18
19
  };
19
20
  exports.normalizeAmount = normalizeAmount;
20
- const getERC20Allowance = async ({ erc20AllowanceAbi, web3, tokenId, ownerAddress, spenderAddress }) => {
21
- 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);
22
23
  return erc20Contract.methods.allowance(ownerAddress, spenderAddress).call();
23
24
  };
24
25
  exports.getERC20Allowance = getERC20Allowance;
@@ -29,18 +30,13 @@ const getAllowanceRequired = async ({ receiveAddress, allowanceContract, sellAss
29
30
  }
30
31
  const ownerAddress = receiveAddress;
31
32
  const spenderAddress = allowanceContract;
32
- const tokenId = sellAsset.tokenId;
33
- if (!tokenId) {
34
- throw new api_1.SwapError('[getAllowanceRequired] - sellAsset.tokenId is required', {
35
- code: api_1.SwapErrorTypes.VALIDATION_FAILED
36
- });
37
- }
33
+ const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(sellAsset.assetId);
38
34
  const allowanceOnChain = await (0, exports.getERC20Allowance)({
39
35
  web3,
40
36
  erc20AllowanceAbi,
41
37
  ownerAddress,
42
38
  spenderAddress,
43
- tokenId
39
+ sellAssetErc20Address
44
40
  });
45
41
  if (allowanceOnChain === '0')
46
42
  return (0, bignumber_1.bnOrZero)(sellAmount);
@@ -64,16 +60,17 @@ const getAllowanceRequired = async ({ receiveAddress, allowanceContract, sellAss
64
60
  };
65
61
  exports.getAllowanceRequired = getAllowanceRequired;
66
62
  const getUsdRate = async (input) => {
67
- const { symbol, tokenId } = input;
63
+ const { symbol, assetId } = input;
64
+ const { assetReference: erc20Address, assetNamespace } = (0, caip_1.fromAssetId)(assetId);
68
65
  try {
69
66
  const USDC_CONTRACT_ADDRESS = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
70
- 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)
71
68
  return '1'; // Will break if comparing against usdc
72
69
  const rateResponse = await zrxService_1.zrxService.get('/swap/v1/price', {
73
70
  params: {
74
71
  buyToken: USDC_CONTRACT_ADDRESS,
75
72
  buyAmount: '1000000000',
76
- sellToken: tokenId || symbol
73
+ sellToken: assetNamespace === 'erc20' ? erc20Address : symbol
77
74
  }
78
75
  });
79
76
  const price = (0, bignumber_1.bnOrZero)(rateResponse.data.price);
@@ -96,13 +93,9 @@ exports.getUsdRate = getUsdRate;
96
93
  const grantAllowance = async ({ quote, wallet, adapterManager, erc20Abi, web3 }) => {
97
94
  var _a, _b, _c, _d, _e;
98
95
  try {
99
- if (!quote.sellAsset.tokenId) {
100
- throw new api_1.SwapError('[grantAllowance] - sellAsset.tokenId is required', {
101
- code: api_1.SwapErrorTypes.VALIDATION_FAILED
102
- });
103
- }
96
+ const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(quote.sellAsset.assetId);
104
97
  const adapter = await adapterManager.byChainId('eip155:1');
105
- const erc20Contract = new web3.eth.Contract(erc20Abi, quote.sellAsset.tokenId);
98
+ const erc20Contract = new web3.eth.Contract(erc20Abi, sellAssetErc20Address);
106
99
  const approveTx = erc20Contract.methods
107
100
  .approve(quote.allowanceContract, quote.sellAmount)
108
101
  .encodeABI();
@@ -110,11 +103,11 @@ const grantAllowance = async ({ quote, wallet, adapterManager, erc20Abi, web3 })
110
103
  const bip44Params = adapter.buildBIP44Params({ accountNumber });
111
104
  const { txToSign } = await adapter.buildSendTransaction({
112
105
  wallet,
113
- to: quote.sellAsset.tokenId,
106
+ to: sellAssetErc20Address,
114
107
  bip44Params,
115
108
  value: '0',
116
109
  chainSpecific: {
117
- erc20ContractAddress: quote.sellAsset.tokenId,
110
+ erc20ContractAddress: sellAssetErc20Address,
118
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),
119
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)
120
113
  }
@@ -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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "5.1.3",
3
+ "version": "5.2.0",
4
4
  "description": "TODO: description",
5
5
  "repository": "https://github.com/shapeshift/lib",
6
6
  "license": "MIT",
@@ -30,19 +30,19 @@
30
30
  "web3": "^1.6.1"
31
31
  },
32
32
  "peerDependencies": {
33
- "@shapeshiftoss/asset-service": "^3.0.0",
34
- "@shapeshiftoss/caip": "^4.0.1",
35
- "@shapeshiftoss/chain-adapters": "^4.0.0",
33
+ "@shapeshiftoss/asset-service": "^4.2.4",
34
+ "@shapeshiftoss/caip": "^5.0.0",
35
+ "@shapeshiftoss/chain-adapters": "^4.3.3",
36
36
  "@shapeshiftoss/hdwallet-core": "1.20.0",
37
- "@shapeshiftoss/types": "^4.0.0",
37
+ "@shapeshiftoss/types": "^4.3.1",
38
38
  "@shapeshiftoss/errors": "^1.1.0"
39
39
  },
40
40
  "devDependencies": {
41
- "@shapeshiftoss/asset-service": "^3.0.0",
42
- "@shapeshiftoss/caip": "^4.0.1",
43
- "@shapeshiftoss/chain-adapters": "^4.0.0",
41
+ "@shapeshiftoss/asset-service": "^4.2.4",
42
+ "@shapeshiftoss/caip": "^5.0.0",
43
+ "@shapeshiftoss/chain-adapters": "^4.3.3",
44
44
  "@shapeshiftoss/hdwallet-core": "^1.20.0",
45
- "@shapeshiftoss/types": "^4.1.0",
45
+ "@shapeshiftoss/types": "^4.3.1",
46
46
  "@shapeshiftoss/errors": "^1.1.0",
47
47
  "@types/readline-sync": "^1.4.4",
48
48
  "readline-sync": "^1.4.10",