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

Commit

Permalink
chore: confine swapper type extensions to specific swapper
Browse files Browse the repository at this point in the history
  • Loading branch information
0xApotheosis committed Jul 6, 2022
1 parent 0c5e412 commit 3cda567
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
24 changes: 3 additions & 21 deletions packages/swapper/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { AssetId, ChainId } from '@shapeshiftoss/caip'
import { createErrorClass } from '@shapeshiftoss/errors'
import { BTCSignTx, ETHSignTx, HDWallet } from '@shapeshiftoss/hdwallet-core'
import {
Asset,
BIP44Params,
ChainSpecific,
KnownChainIds,
UtxoAccountType
} from '@shapeshiftoss/types'
import { Asset, ChainSpecific, KnownChainIds } from '@shapeshiftoss/types'

import { ZrxSupportedChainIds } from './swappers/zrx/ZrxSwapper'

Expand Down Expand Up @@ -54,7 +48,8 @@ export type SupportedSellAssetsInput = {
assetIds: AssetId[]
}

type CommonTradeInput = {
export type GetTradeQuoteInput = {
chainId: KnownChainIds
sellAsset: Asset
buyAsset: Asset
sellAmount: string
Expand All @@ -63,19 +58,6 @@ type CommonTradeInput = {
wallet?: HDWallet // TODO remove this in a followup PR
}

type GetEthereumTradeQuoteInput = CommonTradeInput & {
chainId: KnownChainIds.EthereumMainnet | KnownChainIds.AvalancheMainnet
}

type GetBtcTradeQuoteInput = CommonTradeInput & {
chainId: KnownChainIds.BitcoinMainnet
accountType: UtxoAccountType
bip44Params: BIP44Params
wallet: HDWallet
}

export type GetTradeQuoteInput = GetBtcTradeQuoteInput | GetEthereumTradeQuoteInput

export type BuildTradeInput = GetTradeQuoteInput & {
buyAssetAccountNumber: number
slippage?: string
Expand Down
6 changes: 5 additions & 1 deletion packages/swapper/src/swappers/cow/CowSwapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export type CowSwapperDeps = {
feeAsset: Asset // should be WETH asset
}

type GetCowSwapTradeQuoteInput = GetTradeQuoteInput & {
chainId: KnownChainIds.EthereumMainnet
}

export class CowSwapper implements Swapper<KnownChainIds.EthereumMainnet> {
public static swapperName = 'CowSwapper'
deps: CowSwapperDeps
Expand All @@ -56,7 +60,7 @@ export class CowSwapper implements Swapper<KnownChainIds.EthereumMainnet> {
}

async getTradeQuote(
input: GetTradeQuoteInput
input: GetCowSwapTradeQuoteInput
): Promise<TradeQuote<KnownChainIds.EthereumMainnet>> {
return getCowSwapTradeQuote(this.deps, input)
}
Expand Down
19 changes: 16 additions & 3 deletions packages/swapper/src/swappers/thorchain/ThorchainSwapper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { adapters, AssetId, ChainId, fromAssetId } from '@shapeshiftoss/caip'
import { ethereum } from '@shapeshiftoss/chain-adapters'
import type { ETHSignTx } from '@shapeshiftoss/hdwallet-core'
import type { ETHSignTx, HDWallet } from '@shapeshiftoss/hdwallet-core'
import type { Asset } from '@shapeshiftoss/types'
import { KnownChainIds } from '@shapeshiftoss/types'
import { BIP44Params, KnownChainIds, UtxoAccountType } from '@shapeshiftoss/types'

import {
ApprovalNeededInput,
Expand All @@ -28,6 +28,19 @@ import { PoolResponse, ThorchainSwapperDeps } from './types'
import { getUsdRate } from './utils/getUsdRate/getUsdRate'
import { thorService } from './utils/thorService'

type GetEthereumTradeQuoteInput = GetTradeQuoteInput & {
chainId: KnownChainIds.EthereumMainnet
}

type GetBtcTradeQuoteInput = GetTradeQuoteInput & {
chainId: KnownChainIds.BitcoinMainnet
accountType: UtxoAccountType
bip44Params: BIP44Params
wallet: HDWallet
}

type GetThorSwapTradeQuoteInput = GetEthereumTradeQuoteInput | GetBtcTradeQuoteInput

export class ThorchainSwapper implements Swapper<ChainId> {
private swapSupportedChainIds: Record<ChainId, boolean> = {
'eip155:1': true,
Expand Down Expand Up @@ -97,7 +110,7 @@ export class ThorchainSwapper implements Swapper<ChainId> {
throw new Error('ThorchainSwapper: buildTrade unimplemented')
}

async getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<ChainId>> {
async getTradeQuote(input: GetThorSwapTradeQuoteInput): Promise<TradeQuote<ChainId>> {
return getThorTradeQuote({ deps: this.deps, input })
}

Expand Down
6 changes: 5 additions & 1 deletion packages/swapper/src/swappers/zrx/ZrxSwapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export type ZrxSwapperDeps = {

export type ZrxSupportedChainIds = KnownChainIds.EthereumMainnet | KnownChainIds.AvalancheMainnet

type GetZrxSwapTradeQuoteInput = GetTradeQuoteInput & {
chainId: ZrxSupportedChainIds
}

export class ZrxSwapper<T extends ZrxSupportedChainIds> implements Swapper<ZrxSupportedChainIds> {
public static swapperName = 'ZrxSwapper'
deps: ZrxSwapperDeps
Expand All @@ -55,7 +59,7 @@ export class ZrxSwapper<T extends ZrxSupportedChainIds> implements Swapper<ZrxSu
return zrxBuildTrade(this.deps, args)
}

async getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<ZrxSupportedChainIds>> {
async getTradeQuote(input: GetZrxSwapTradeQuoteInput): Promise<TradeQuote<ZrxSupportedChainIds>> {
return getZrxTradeQuote(input)
}

Expand Down

0 comments on commit 3cda567

Please sign in to comment.