@shapeshiftoss/swapper 1.12.0 → 1.15.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/dist/api.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { CAIP19 } from '@shapeshiftoss/caip';
1
2
  import { HDWallet } from '@shapeshiftoss/hdwallet-core';
2
3
  import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, Asset, BuildQuoteTxInput, ChainTypes, ExecQuoteInput, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, Quote, SendMaxAmountInput, SwapperType } from '@shapeshiftoss/types';
3
4
  export declare class SwapError extends Error {
@@ -28,7 +29,7 @@ export interface Swapper {
28
29
  /**
29
30
  * Get the default pair of the swapper
30
31
  */
31
- getDefaultPair(): Pick<Asset, 'chain' | 'symbol' | 'name'>[];
32
+ getDefaultPair(): [CAIP19, CAIP19];
32
33
  /**
33
34
  * Get the minimum and maximum trade value of the sellAsset and buyAsset
34
35
  */
@@ -1,3 +1,4 @@
1
+ import { CAIP19 } from '@shapeshiftoss/caip';
1
2
  import { ApprovalNeededOutput, Asset, ChainTypes, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, Quote, SwapperType } from '@shapeshiftoss/types';
2
3
  import { Swapper } from '../../api';
3
4
  export declare class ThorchainSwapper implements Swapper {
@@ -9,7 +10,7 @@ export declare class ThorchainSwapper implements Swapper {
9
10
  getAvailableAssets(assets: Asset[]): Asset[];
10
11
  canTradePair(sellAsset: Asset, buyAsset: Asset): boolean;
11
12
  executeQuote(): Promise<ExecQuoteOutput>;
12
- getDefaultPair(): Pick<Asset, 'chain' | 'symbol' | 'name'>[];
13
+ getDefaultPair(): [CAIP19, CAIP19];
13
14
  approvalNeeded(): Promise<ApprovalNeededOutput>;
14
15
  approveInfinite(): Promise<string>;
15
16
  getSendMaxAmount(): Promise<string>;
@@ -18,8 +18,8 @@ async function ZrxApprovalNeeded({ adapterManager, web3 }, { quote, wallet }) {
18
18
  }
19
19
  const accountNumber = quote.sellAssetAccountId ? Number(quote.sellAssetAccountId) : 0;
20
20
  const adapter = adapterManager.byChain(sellAsset.chain);
21
- const bip32Params = adapter.buildBIP32Params({ accountNumber });
22
- const receiveAddress = await adapter.getAddress({ wallet, bip32Params });
21
+ const bip44Params = adapter.buildBIP44Params({ accountNumber });
22
+ const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
23
23
  /**
24
24
  * /swap/v1/quote
25
25
  * params: {
@@ -10,10 +10,10 @@ const helpers_1 = require("../utils/helpers/helpers");
10
10
  const zrxService_1 = require("../utils/zrxService");
11
11
  async function ZrxApproveInfinite({ adapterManager, web3 }, { quote, wallet }) {
12
12
  const adapter = adapterManager.byChain(types_1.ChainTypes.Ethereum);
13
- const bip32Params = adapter.buildBIP32Params({
13
+ const bip44Params = adapter.buildBIP44Params({
14
14
  accountNumber: (0, bignumber_1.bnOrZero)(quote.sellAssetAccountId).toNumber()
15
15
  }); // TODO: Add account number
16
- const receiveAddress = await adapter.getAddress({ wallet, bip32Params });
16
+ const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
17
17
  /**
18
18
  * /swap/v1/quote
19
19
  * params: {
@@ -53,8 +53,8 @@ async function ZrxBuildQuoteTx({ adapterManager, web3 }, { input, wallet }) {
53
53
  throw new __1.SwapError(`ZrxSwapper:ZrxBuildQuoteTx buyAsset must be on chain [${types_1.ChainTypes.Ethereum}]`);
54
54
  }
55
55
  const adapter = adapterManager.byChain(buyAsset.chain);
56
- const bip32Params = adapter.buildBIP32Params({ accountNumber: Number(buyAssetAccountId) });
57
- const receiveAddress = await adapter.getAddress({ wallet, bip32Params });
56
+ const bip44Params = adapter.buildBIP44Params({ accountNumber: Number(buyAssetAccountId) });
57
+ const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
58
58
  if (new bignumber_js_1.default(slippage || 0).gt(constants_1.MAX_SLIPPAGE)) {
59
59
  throw new __1.SwapError(`ZrxSwapper:ZrxBuildQuoteTx slippage value of ${slippage} is greater than max slippage value of ${constants_1.MAX_SLIPPAGE}`);
60
60
  }
@@ -24,7 +24,7 @@ async function ZrxExecuteQuote({ adapterManager }, { quote, wallet }) {
24
24
  // value is 0 for erc20s
25
25
  const value = sellAsset.symbol === 'ETH' ? quote.sellAmount : '0';
26
26
  const adapter = adapterManager.byChain(sellAsset.chain);
27
- const bip32Params = adapter.buildBIP32Params({
27
+ const bip44Params = adapter.buildBIP44Params({
28
28
  accountNumber: Number(quote.sellAssetAccountId)
29
29
  });
30
30
  let buildTxResponse, signedTx, txid;
@@ -37,7 +37,7 @@ async function ZrxExecuteQuote({ adapterManager }, { quote, wallet }) {
37
37
  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),
38
38
  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)
39
39
  },
40
- bip32Params
40
+ bip44Params
41
41
  });
42
42
  }
43
43
  catch (error) {
@@ -1,3 +1,4 @@
1
+ import { CAIP19 } from '@shapeshiftoss/caip';
1
2
  import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
2
3
  import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, Asset, BuildQuoteTxInput, ChainTypes, ExecQuoteInput, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, Quote, SendMaxAmountInput, SwapperType } from '@shapeshiftoss/types';
3
4
  import Web3 from 'web3';
@@ -20,7 +21,7 @@ export declare class ZrxSwapper implements Swapper {
20
21
  getMinMax(input: GetQuoteInput): Promise<MinMaxOutput>;
21
22
  getAvailableAssets(assets: Asset[]): Asset[];
22
23
  canTradePair(sellAsset: Asset, buyAsset: Asset): boolean;
23
- getDefaultPair(): Pick<Asset, 'chain' | 'symbol' | 'name'>[];
24
+ getDefaultPair(): [CAIP19, CAIP19];
24
25
  executeQuote(args: ExecQuoteInput<ChainTypes, SwapperType>): Promise<ExecQuoteOutput>;
25
26
  approvalNeeded(args: ApprovalNeededInput<ChainTypes, SwapperType>): Promise<ApprovalNeededOutput>;
26
27
  approveInfinite(args: ApproveInfiniteInput<ChainTypes, SwapperType>): Promise<string>;
@@ -44,8 +44,8 @@ class ZrxSwapper {
44
44
  return availableAssets.length === 2;
45
45
  }
46
46
  getDefaultPair() {
47
- const ETH = { name: 'Ethereum', chain: types_1.ChainTypes.Ethereum, symbol: 'ETH' };
48
- const FOX = { name: 'Fox', chain: types_1.ChainTypes.Ethereum, symbol: 'FOX' };
47
+ const ETH = 'eip155:1/slip44:60';
48
+ const FOX = 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d';
49
49
  return [ETH, FOX];
50
50
  }
51
51
  async executeQuote(args) {
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.getZrxSendMaxAmount = void 0;
7
+ const caip_1 = require("@shapeshiftoss/caip");
7
8
  const types_1 = require("@shapeshiftoss/types");
8
9
  const bignumber_js_1 = __importDefault(require("bignumber.js"));
9
10
  const api_1 = require("../../../api");
@@ -12,10 +13,10 @@ const constants_1 = require("../utils/constants");
12
13
  async function getZrxSendMaxAmount({ adapterManager }, { wallet, quote, sellAssetAccountId, feeEstimateKey = types_1.chainAdapters.FeeDataKey.Average }) {
13
14
  var _a, _b, _c;
14
15
  const adapter = adapterManager.byChain(types_1.ChainTypes.Ethereum);
15
- const bip32Params = adapter.buildBIP32Params({
16
+ const bip44Params = adapter.buildBIP44Params({
16
17
  accountNumber: (0, bignumber_1.bnOrZero)(sellAssetAccountId).toNumber()
17
18
  });
18
- const ethAddress = await adapter.getAddress({ wallet, bip32Params });
19
+ const ethAddress = await adapter.getAddress({ wallet, bip44Params });
19
20
  const account = await adapter.getAccount(ethAddress);
20
21
  if (!quote.sellAsset) {
21
22
  throw new api_1.SwapError('quote.sellAsset is required');
@@ -23,7 +24,9 @@ async function getZrxSendMaxAmount({ adapterManager }, { wallet, quote, sellAsse
23
24
  const tokenId = (_a = quote.sellAsset) === null || _a === void 0 ? void 0 : _a.tokenId;
24
25
  let balance;
25
26
  if (tokenId) {
26
- balance = (_c = (_b = account.chainSpecific.tokens) === null || _b === void 0 ? void 0 : _b.find((token) => token.contract.toLowerCase() === tokenId.toLowerCase())) === null || _c === void 0 ? void 0 : _c.balance;
27
+ balance = (_c = (_b = account.chainSpecific.tokens) === null || _b === void 0 ? void 0 : _b.find((token) => {
28
+ return caip_1.caip19.fromCAIP19(token.caip19).tokenId === tokenId.toLowerCase();
29
+ })) === null || _c === void 0 ? void 0 : _c.balance;
27
30
  }
28
31
  else {
29
32
  balance = account.balance;
@@ -81,7 +81,7 @@ const grantAllowance = async ({ quote, wallet, adapter, erc20Abi, web3 }) => {
81
81
  const approveTx = erc20Contract.methods
82
82
  .approve(quote.allowanceContract, quote.sellAmount)
83
83
  .encodeABI();
84
- const bip32Params = adapter.buildBIP32Params({
84
+ const bip44Params = adapter.buildBIP44Params({
85
85
  accountNumber: Number(quote.sellAssetAccountId) || 0
86
86
  });
87
87
  let grantAllowanceTxToSign, signedTx, broadcastedTxId;
@@ -89,7 +89,7 @@ const grantAllowance = async ({ quote, wallet, adapter, erc20Abi, web3 }) => {
89
89
  const { txToSign } = await adapter.buildSendTransaction({
90
90
  wallet,
91
91
  to: quote.sellAsset.tokenId,
92
- bip32Params,
92
+ bip44Params,
93
93
  value: '0',
94
94
  chainSpecific: {
95
95
  erc20ContractAddress: quote.sellAsset.tokenId,
@@ -4,6 +4,7 @@ exports.FOX = exports.WETH = exports.BTC = void 0;
4
4
  const types_1 = require("@shapeshiftoss/types");
5
5
  exports.BTC = {
6
6
  caip19: 'bip122:000000000019d6689c085ae165831e93/slip44:0',
7
+ caip2: 'bip122:000000000019d6689c085ae165831e93',
7
8
  name: 'bitcoin',
8
9
  chain: types_1.ChainTypes.Bitcoin,
9
10
  dataSource: types_1.AssetDataSource.CoinGecko,
@@ -23,6 +24,7 @@ exports.BTC = {
23
24
  };
24
25
  exports.WETH = {
25
26
  caip19: 'eip155:1/erc20:0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
27
+ caip2: 'eip155:1',
26
28
  name: 'WETH',
27
29
  chain: types_1.ChainTypes.Ethereum,
28
30
  dataSource: types_1.AssetDataSource.CoinGecko,
@@ -43,6 +45,7 @@ exports.WETH = {
43
45
  };
44
46
  exports.FOX = {
45
47
  caip19: 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d',
48
+ caip2: 'eip155:1',
46
49
  name: 'FOX',
47
50
  chain: types_1.ChainTypes.Ethereum,
48
51
  dataSource: types_1.AssetDataSource.CoinGecko,
@@ -2,7 +2,7 @@
2
2
  import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
3
3
  import Web3 from 'web3';
4
4
  export declare const chainAdapterMockFuncs: {
5
- buildBIP32Params: jest.Mock<{
5
+ buildBIP44Params: jest.Mock<{
6
6
  purpose: number;
7
7
  coinType: number;
8
8
  accountNumber: number;
@@ -12,11 +12,16 @@ export declare const chainAdapterMockFuncs: {
12
12
  }>, []>;
13
13
  signTransaction: jest.Mock<Promise<string>, []>;
14
14
  broadcastTransaction: jest.Mock<Promise<string>, []>;
15
+ getInfo: jest.Mock<Promise<{
16
+ data: {
17
+ network: string;
18
+ };
19
+ }>, []>;
15
20
  getAddress: jest.Mock<Promise<string>, []>;
16
21
  getAccount: jest.Mock<Promise<{
17
22
  chainSpecific: {
18
23
  tokens: {
19
- contract: string;
24
+ caip19: string;
20
25
  balance: string;
21
26
  }[];
22
27
  };
@@ -9,13 +9,14 @@ const types_1 = require("@shapeshiftoss/types");
9
9
  const web3_1 = __importDefault(require("web3"));
10
10
  jest.mock('@shapeshiftoss/chain-adapters');
11
11
  exports.chainAdapterMockFuncs = {
12
- buildBIP32Params: jest.fn(() => ({ purpose: 44, coinType: 60, accountNumber: 0 })),
12
+ buildBIP44Params: jest.fn(() => ({ purpose: 44, coinType: 60, accountNumber: 0 })),
13
13
  buildSendTransaction: jest.fn(() => Promise.resolve({ txToSign: {} })),
14
14
  signTransaction: jest.fn(() => Promise.resolve('signedTx')),
15
15
  broadcastTransaction: jest.fn(() => Promise.resolve('broadcastedTx')),
16
+ getInfo: jest.fn(() => Promise.resolve({ data: { network: 'mainnet' } })),
16
17
  getAddress: jest.fn(() => Promise.resolve('address')),
17
18
  getAccount: jest.fn(() => Promise.resolve({
18
- chainSpecific: { tokens: [{ contract: 'contractAddress', balance: '1000000' }] }
19
+ chainSpecific: { tokens: [{ caip19: 'eip155:1/erc20:contractAddress', balance: '1000000' }] }
19
20
  })),
20
21
  getFeeData: jest.fn(() => Promise.resolve({
21
22
  [types_1.chainAdapters.FeeDataKey.Average]: { txFee: '10000' }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "1.12.0",
3
+ "version": "1.15.0",
4
4
  "description": "TODO: description",
5
5
  "repository": "https://github.com/shapeshift/lib",
6
6
  "license": "MIT",
@@ -28,12 +28,14 @@
28
28
  },
29
29
  "peerDependencies": {
30
30
  "@shapeshiftoss/asset-service": "^1.6.1",
31
+ "@shapeshiftoss/caip": "^1.6.1",
31
32
  "@shapeshiftoss/chain-adapters": "^1.15.3",
32
33
  "@shapeshiftoss/hdwallet-core": "^1.18.2",
33
34
  "@shapeshiftoss/types": "^1.13.1"
34
35
  },
35
36
  "devDependencies": {
36
37
  "@shapeshiftoss/asset-service": "^1.6.1",
38
+ "@shapeshiftoss/caip": "^1.6.1",
37
39
  "@shapeshiftoss/chain-adapters": "^1.16.2",
38
40
  "@shapeshiftoss/hdwallet-core": "^1.18.2",
39
41
  "@shapeshiftoss/types": "^1.13.1",