@shapeshiftoss/swapper 7.2.0 → 7.3.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.
@@ -1,6 +1,6 @@
1
1
  import { AssetId, ChainId } from '@shapeshiftoss/caip';
2
2
  import type { Asset } from '@shapeshiftoss/types';
3
- import { ApprovalNeededOutput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
3
+ import { ApprovalNeededInput, ApprovalNeededOutput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
4
4
  import { ThorchainSwapperDeps } from './types';
5
5
  export declare class ThorchainSwapper implements Swapper<ChainId> {
6
6
  private swapSupportedChainIds;
@@ -10,7 +10,7 @@ export declare class ThorchainSwapper implements Swapper<ChainId> {
10
10
  initialize(): Promise<void>;
11
11
  getType(): SwapperType;
12
12
  getUsdRate(input: Pick<Asset, 'symbol' | 'assetId'>): Promise<string>;
13
- approvalNeeded(): Promise<ApprovalNeededOutput>;
13
+ approvalNeeded(input: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
14
14
  approveInfinite(): Promise<string>;
15
15
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
16
16
  filterAssetIdsBySellable(): AssetId[];
@@ -4,6 +4,7 @@ exports.ThorchainSwapper = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
5
  const api_1 = require("../../api");
6
6
  const getTradeQuote_1 = require("./getThorTradeQuote/getTradeQuote");
7
+ const thorTradeApprovalNeeded_1 = require("./thorTradeApprovalNeeded/thorTradeApprovalNeeded");
7
8
  const getUsdRate_1 = require("./utils/getUsdRate/getUsdRate");
8
9
  const thorService_1 = require("./utils/thorService");
9
10
  class ThorchainSwapper {
@@ -40,8 +41,8 @@ class ThorchainSwapper {
40
41
  getUsdRate(input) {
41
42
  return (0, getUsdRate_1.getUsdRate)({ deps: this.deps, input });
42
43
  }
43
- async approvalNeeded() {
44
- throw new Error('ThorchainSwapper: approvalNeeded unimplemented');
44
+ async approvalNeeded(input) {
45
+ return (0, thorTradeApprovalNeeded_1.thorTradeApprovalNeeded)({ deps: this.deps, input });
45
46
  }
46
47
  async approveInfinite() {
47
48
  throw new Error('ThorchainSwapper: approveInfinite unimplemented');
@@ -22,7 +22,7 @@ const getThorTradeQuote = async ({ deps, input }) => {
22
22
  const sellAssetChainId = (0, caip_1.toChainId)({ chainReference, chainNamespace });
23
23
  const adapter = deps.adapterManager.get(sellAssetChainId);
24
24
  if (!adapter)
25
- throw new api_1.SwapError(`[getThorTxInfo] - No chain adapter found for ${sellAssetChainId}.`, {
25
+ throw new api_1.SwapError(`[getThorTradeQuote] - No chain adapter found for ${sellAssetChainId}.`, {
26
26
  code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
27
27
  details: { sellAssetChainId }
28
28
  });
@@ -51,7 +51,7 @@ const getThorTradeQuote = async ({ deps, input }) => {
51
51
  sellAssetReference: sellAssetErc20Address
52
52
  });
53
53
  default:
54
- throw new api_1.SwapError('[getThorTxInfo] - Asset chainId is not supported.', {
54
+ throw new api_1.SwapError('[getThorTradeQuote] - Asset chainId is not supported.', {
55
55
  code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
56
56
  details: { sellAssetChainId }
57
57
  });
@@ -61,7 +61,7 @@ const getThorTradeQuote = async ({ deps, input }) => {
61
61
  const buyAssetId = buyAsset.assetId;
62
62
  const feeAssetId = (0, caip_1.getFeeAssetIdFromAssetId)(buyAssetId);
63
63
  if (!feeAssetId)
64
- throw new api_1.SwapError(`[getThorTxInfo] - No feeAssetId found for ${buyAssetId}.`, {
64
+ throw new api_1.SwapError(`[getThorTradeQuote] - No feeAssetId found for ${buyAssetId}.`, {
65
65
  code: api_1.SwapErrorTypes.VALIDATION_FAILED,
66
66
  details: { buyAssetId }
67
67
  });
@@ -0,0 +1,6 @@
1
+ import { ApprovalNeededInput, ApprovalNeededOutput } from '../../../api';
2
+ import { ThorchainSwapperDeps } from '../types';
3
+ export declare const thorTradeApprovalNeeded: ({ deps, input }: {
4
+ deps: ThorchainSwapperDeps;
5
+ input: ApprovalNeededInput<'eip155:1'>;
6
+ }) => Promise<ApprovalNeededOutput>;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.thorTradeApprovalNeeded = void 0;
4
+ const caip_1 = require("@shapeshiftoss/caip");
5
+ const api_1 = require("../../../api");
6
+ const erc20Allowance_abi_1 = require("../../utils/abi/erc20Allowance-abi");
7
+ const bignumber_1 = require("../../utils/bignumber");
8
+ const helpers_1 = require("../../utils/helpers/helpers");
9
+ const thorTradeApprovalNeeded = async ({ deps, input }) => {
10
+ var _a;
11
+ try {
12
+ const { quote, wallet } = input;
13
+ const { sellAsset } = quote;
14
+ const { adapterManager, web3 } = deps;
15
+ const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(sellAsset.assetId);
16
+ const { chainNamespace } = (0, caip_1.fromChainId)(sellAsset.chainId);
17
+ if (chainNamespace !== 'eip155') {
18
+ throw new api_1.SwapError('[thorTradeApprovalNeeded] - sellAsset chain namespace is not supported', {
19
+ code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
20
+ details: { chainNamespace }
21
+ });
22
+ }
23
+ // No approval needed for selling a fee asset
24
+ if (sellAsset.assetId === (0, caip_1.getFeeAssetIdFromAssetId)(sellAsset.assetId)) {
25
+ return { approvalNeeded: false };
26
+ }
27
+ const accountNumber = quote.sellAssetAccountNumber;
28
+ const adapter = adapterManager.get(sellAsset.chainId);
29
+ if (!adapter)
30
+ throw new api_1.SwapError(`[thorTradeApprovalNeeded] - no chain adapter found for chain Id: ${sellAsset.chainId}`, {
31
+ code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
32
+ details: { chainId: sellAsset.chainId }
33
+ });
34
+ const bip44Params = adapter.buildBIP44Params({ accountNumber });
35
+ const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
36
+ if (!quote.allowanceContract) {
37
+ throw new api_1.SwapError('[thorTradeApprovalNeeded] - allowanceTarget is required', {
38
+ code: api_1.SwapErrorTypes.VALIDATION_FAILED,
39
+ details: { chainId: sellAsset.chainId }
40
+ });
41
+ }
42
+ const allowanceResult = await (0, helpers_1.getERC20Allowance)({
43
+ web3,
44
+ erc20AllowanceAbi: erc20Allowance_abi_1.erc20AllowanceAbi,
45
+ sellAssetErc20Address,
46
+ spenderAddress: quote.allowanceContract,
47
+ ownerAddress: receiveAddress
48
+ });
49
+ const allowanceOnChain = (0, bignumber_1.bnOrZero)(allowanceResult);
50
+ if (!((_a = quote.feeData.chainSpecific) === null || _a === void 0 ? void 0 : _a.gasPrice))
51
+ throw new api_1.SwapError('[thorTradeApprovalNeeded] - no gas price with quote', {
52
+ code: api_1.SwapErrorTypes.RESPONSE_ERROR,
53
+ details: { feeData: quote.feeData }
54
+ });
55
+ return {
56
+ approvalNeeded: allowanceOnChain.lte((0, bignumber_1.bnOrZero)(quote.sellAmount))
57
+ };
58
+ }
59
+ catch (e) {
60
+ if (e instanceof api_1.SwapError)
61
+ throw e;
62
+ throw new api_1.SwapError('[thorTradeApprovalNeeded]', {
63
+ cause: e,
64
+ code: api_1.SwapErrorTypes.CHECK_APPROVAL_FAILED
65
+ });
66
+ }
67
+ };
68
+ exports.thorTradeApprovalNeeded = thorTradeApprovalNeeded;
@@ -1,4 +1,5 @@
1
1
  import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
2
+ import Web3 from 'web3';
2
3
  export declare type PoolResponse = {
3
4
  asset: string;
4
5
  assetDepth: string;
@@ -24,4 +25,5 @@ export declare type InboundResponse = {
24
25
  export declare type ThorchainSwapperDeps = {
25
26
  midgardUrl: string;
26
27
  adapterManager: ChainAdapterManager;
28
+ web3: Web3;
27
29
  };
@@ -0,0 +1,2 @@
1
+ import { ThorchainSwapperDeps } from '../../types';
2
+ export declare const setupThorswapDeps: () => ThorchainSwapperDeps;
@@ -0,0 +1,32 @@
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.setupThorswapDeps = void 0;
7
+ const web3_1 = __importDefault(require("web3"));
8
+ jest.mock('web3');
9
+ const setupThorswapDeps = () => {
10
+ const feeData = {
11
+ fast: {
12
+ txFee: '1',
13
+ chainSpecific: { approvalFee: '1', gasLimit: '1', gasPrice: '1' },
14
+ tradeFee: '2'
15
+ }
16
+ };
17
+ const adapterManager = new Map([
18
+ [
19
+ 'eip155:1',
20
+ {
21
+ buildBIP44Params: jest.fn(() => ({ purpose: 44, coinType: 60, accountNumber: 0 })),
22
+ getAddress: jest.fn(() => Promise.resolve('0xthisIsMyAddress')),
23
+ getFeeData: jest.fn(() => feeData)
24
+ }
25
+ ]
26
+ ]);
27
+ const midgardUrl = 'localhost:3000';
28
+ const ethNodeUrl = 'http://localhost:1000';
29
+ const web3Provider = new web3_1.default.providers.HttpProvider(ethNodeUrl);
30
+ return { adapterManager, midgardUrl, web3: new web3_1.default(web3Provider) };
31
+ };
32
+ exports.setupThorswapDeps = setupThorswapDeps;
@@ -31,6 +31,7 @@ const chain_adapters_1 = require("@shapeshiftoss/chain-adapters");
31
31
  const unchained = __importStar(require("@shapeshiftoss/unchained-client"));
32
32
  const web3_1 = __importDefault(require("web3"));
33
33
  jest.mock('@shapeshiftoss/chain-adapters');
34
+ jest.mock('web3');
34
35
  const setupDeps = () => {
35
36
  const ethChainAdapter = new chain_adapters_1.ethereum.ChainAdapter({
36
37
  providers: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "7.2.0",
3
+ "version": "7.3.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",
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "peerDependencies": {
33
33
  "@shapeshiftoss/asset-service": "^5.0.0",
34
- "@shapeshiftoss/caip": "^5.3.0",
34
+ "@shapeshiftoss/caip": "^6.0.1",
35
35
  "@shapeshiftoss/chain-adapters": "^6.0.1",
36
36
  "@shapeshiftoss/errors": "^1.1.1",
37
37
  "@shapeshiftoss/hdwallet-core": "^1.23.0",
@@ -39,13 +39,14 @@
39
39
  },
40
40
  "devDependencies": {
41
41
  "@shapeshiftoss/asset-service": "^5.0.0",
42
- "@shapeshiftoss/caip": "^5.3.0",
42
+ "@shapeshiftoss/caip": "^6.0.1",
43
43
  "@shapeshiftoss/chain-adapters": "^6.0.1",
44
44
  "@shapeshiftoss/errors": "^1.1.1",
45
45
  "@shapeshiftoss/hdwallet-core": "^1.23.0",
46
46
  "@shapeshiftoss/types": "^5.0.0",
47
47
  "@types/readline-sync": "^1.4.4",
48
48
  "readline-sync": "^1.4.10",
49
- "web3-utils": "^1.7.3"
49
+ "web3-utils": "^1.7.3",
50
+ "@shapeshiftoss/unchained-client": "^9.0.0"
50
51
  }
51
52
  }