From d1cf1be7c611517068c789d3cfa854aa19573a2b Mon Sep 17 00:00:00 2001 From: Toshi <10103480+toshiSat@users.noreply.github.com> Date: Mon, 4 Oct 2021 12:19:06 -0600 Subject: [PATCH] feat: add getdefaultpair to zrxswapper (#91) * Add getDefaultPair to zrxSwapper * Check for chain type * trigger build * trigger build --- packages/swapper/src/api.ts | 5 +++++ .../swapper/src/manager/SwapperManager.ts | 2 +- .../swappers/thorchain/ThorchainSwapper.ts | 4 ++++ .../src/swappers/zrx/ZrxSwapper.test.ts | 22 ++++++++++++++++++- .../swapper/src/swappers/zrx/ZrxSwapper.ts | 6 +++++ 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/swapper/src/api.ts b/packages/swapper/src/api.ts index dedd55b9b..097811f49 100644 --- a/packages/swapper/src/api.ts +++ b/packages/swapper/src/api.ts @@ -48,6 +48,11 @@ export interface Swapper { */ getUsdRate(input: Pick): Promise + /** + * Get the default pair of the swapper + */ + getDefaultPair(): Partial[] + /** * Get the minimum and maximum trade value of the sellAsset and buyAsset * @param input diff --git a/packages/swapper/src/manager/SwapperManager.ts b/packages/swapper/src/manager/SwapperManager.ts index 9e171da61..542f4051d 100644 --- a/packages/swapper/src/manager/SwapperManager.ts +++ b/packages/swapper/src/manager/SwapperManager.ts @@ -57,7 +57,7 @@ export class SwapperManager { return this } - async getQuote(swapperType: T, quoteParams: GetQuoteInput): Promise { + async getBestQuote(swapperType: T, quoteParams: GetQuoteInput): Promise { const swapper = this.getSwapper(swapperType) return swapper.getQuote(quoteParams) } diff --git a/packages/swapper/src/swappers/thorchain/ThorchainSwapper.ts b/packages/swapper/src/swappers/thorchain/ThorchainSwapper.ts index 441fc2982..46ee674bc 100644 --- a/packages/swapper/src/swappers/thorchain/ThorchainSwapper.ts +++ b/packages/swapper/src/swappers/thorchain/ThorchainSwapper.ts @@ -43,4 +43,8 @@ export class ThorchainSwapper implements Swapper { async executeQuote(): Promise { throw new Error('ThorchainSwapper: executeQuote unimplemented') } + + getDefaultPair(): Partial[] { + throw new Error('Method not implemented.') + } } diff --git a/packages/swapper/src/swappers/zrx/ZrxSwapper.test.ts b/packages/swapper/src/swappers/zrx/ZrxSwapper.test.ts index 571785425..0674971ad 100644 --- a/packages/swapper/src/swappers/zrx/ZrxSwapper.test.ts +++ b/packages/swapper/src/swappers/zrx/ZrxSwapper.test.ts @@ -1,17 +1,22 @@ import Web3 from 'web3' import { HDWallet } from '@shapeshiftoss/hdwallet-core' import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters' -import { GetQuoteInput, SwapperType } from '@shapeshiftoss/types' +import { ChainTypes, GetQuoteInput, SwapperType, Quote } from '@shapeshiftoss/types' import { ZrxSwapper } from '..' import { ZrxError } from '../..' import { DEFAULT_SLIPPAGE } from './utils/constants' import { buildQuoteTx } from '../zrx/buildQuoteTx/buildQuoteTx' +import { executeQuote } from '../zrx/executeQuote/executeQuote' import { getZrxQuote } from './getQuote/getQuote' import { FOX, WETH, BTC } from './utils/test-data/assets' import { getUsdRate } from './utils/helpers/helpers' import { getMinMax } from './getMinMax/getMinMax' jest.mock('./utils/helpers/helpers') +jest.mock('../zrx/executeQuote/executeQuote', () => ({ + executeQuote: jest.fn() +})) + jest.mock('../zrx/buildQuoteTx/buildQuoteTx', () => ({ buildQuoteTx: jest.fn() })) @@ -40,6 +45,7 @@ const setupQuote = () => { describe('ZrxSwapper', () => { const input = {} + const quote = {} const wallet = {} const web3 = {} const adapterManager = {} @@ -82,6 +88,20 @@ describe('ZrxSwapper', () => { await swapper.buildQuoteTx(args) expect(buildQuoteTx).toHaveBeenCalled() }) + it('calls executeQuote on swapper.executeQuote', async () => { + const swapper = new ZrxSwapper(zrxSwapperDeps) + const args = { quote, wallet } + await swapper.executeQuote(args) + expect(executeQuote).toHaveBeenCalled() + }) + it('gets default pair', () => { + const swapper = new ZrxSwapper(zrxSwapperDeps) + const pair = swapper.getDefaultPair() + expect(pair).toHaveLength(2) + pair.forEach((asset) => { + expect(asset.chain).toBe(ChainTypes.Ethereum) + }) + }) it('calls getUsdRate on swapper.getUsdRate', async () => { const swapper = new ZrxSwapper(zrxSwapperDeps) await swapper.getUsdRate(FOX) diff --git a/packages/swapper/src/swappers/zrx/ZrxSwapper.ts b/packages/swapper/src/swappers/zrx/ZrxSwapper.ts index 4b52287ce..ba5a2e8c6 100644 --- a/packages/swapper/src/swappers/zrx/ZrxSwapper.ts +++ b/packages/swapper/src/swappers/zrx/ZrxSwapper.ts @@ -67,6 +67,12 @@ export class ZrxSwapper implements Swapper { return availableAssets.length === 2 } + getDefaultPair(): Partial[] { + const ETH = { name: 'Ethereum', chain: ChainTypes.Ethereum, symbol: 'ETH' } + const USDC = { name: 'USDC', chain: ChainTypes.Ethereum, symbol: 'USDC' } + return [ETH, USDC] + } + async executeQuote(args: ExecQuoteInput): Promise { return executeQuote(this.deps, args) }