Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Commit

Permalink
feat: add getdefaultpair to zrxswapper (#91)
Browse files Browse the repository at this point in the history
* Add getDefaultPair to zrxSwapper

* Check for chain type

* trigger build

* trigger build
  • Loading branch information
toshiSat authored Oct 4, 2021
1 parent 26b788d commit d1cf1be
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/swapper/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export interface Swapper {
*/
getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>

/**
* Get the default pair of the swapper
*/
getDefaultPair(): Partial<Asset>[]

/**
* Get the minimum and maximum trade value of the sellAsset and buyAsset
* @param input
Expand Down
2 changes: 1 addition & 1 deletion packages/swapper/src/manager/SwapperManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class SwapperManager<T extends SwapperType> {
return this
}

async getQuote(swapperType: T, quoteParams: GetQuoteInput): Promise<Quote | undefined> {
async getBestQuote(swapperType: T, quoteParams: GetQuoteInput): Promise<Quote | undefined> {
const swapper = this.getSwapper(swapperType)
return swapper.getQuote(quoteParams)
}
Expand Down
4 changes: 4 additions & 0 deletions packages/swapper/src/swappers/thorchain/ThorchainSwapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ export class ThorchainSwapper implements Swapper {
async executeQuote(): Promise<ExecQuoteOutput> {
throw new Error('ThorchainSwapper: executeQuote unimplemented')
}

getDefaultPair(): Partial<Asset>[] {
throw new Error('Method not implemented.')
}
}
22 changes: 21 additions & 1 deletion packages/swapper/src/swappers/zrx/ZrxSwapper.test.ts
Original file line number Diff line number Diff line change
@@ -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()
}))
Expand Down Expand Up @@ -40,6 +45,7 @@ const setupQuote = () => {

describe('ZrxSwapper', () => {
const input = <GetQuoteInput>{}
const quote = <Quote>{}
const wallet = <HDWallet>{}
const web3 = <Web3>{}
const adapterManager = <ChainAdapterManager>{}
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions packages/swapper/src/swappers/zrx/ZrxSwapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ export class ZrxSwapper implements Swapper {
return availableAssets.length === 2
}

getDefaultPair(): Partial<Asset>[] {
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<ExecQuoteOutput> {
return executeQuote(this.deps, args)
}
Expand Down

0 comments on commit d1cf1be

Please sign in to comment.