diff --git a/src/connectors/pancakeswap/pancakeswap.ts b/src/connectors/pancakeswap/pancakeswap.ts index 47e202be87..cfcb260fda 100644 --- a/src/connectors/pancakeswap/pancakeswap.ts +++ b/src/connectors/pancakeswap/pancakeswap.ts @@ -187,33 +187,11 @@ export class PancakeSwap implements Uniswapish { amount: BigNumber, _allowedSlippage?: string ): Promise { - const nativeTokenAmount: CurrencyAmount = - CurrencyAmount.fromRawAmount(baseToken, amount.toString()); - logger.info( `Fetching pair data for ${quoteToken.address}-${baseToken.address}.` ); - const quoteProvider = SmartRouter.createQuoteProvider({ - // @ts-ignore - onChainProvider: () => this.createPublicClient(), - }); - const pools = await this.getPools(baseToken, quoteToken); - - const trade = await SmartRouter.getBestTrade( - nativeTokenAmount, - quoteToken, - TradeType.EXACT_OUTPUT, - { - gasPriceWei: () => this.createPublicClient().getGasPrice(), - maxHops: this._maximumHops, - maxSplits: 1, - poolProvider: SmartRouter.createStaticPoolProvider(pools), - quoteProvider, - quoterOptimization: true, - allowedPoolTypes: [PoolType.V2, PoolType.V3, PoolType.STABLE], - } - ); + const trade = await this.getBestTrade(baseToken, quoteToken, amount, TradeType.EXACT_OUTPUT); if (!trade) { throw new UniswapishPriceError( @@ -252,33 +230,11 @@ export class PancakeSwap implements Uniswapish { amount: BigNumber, _allowedSlippage?: string ): Promise { - const nativeTokenAmount: CurrencyAmount = - CurrencyAmount.fromRawAmount(baseToken, amount.toString()); - logger.info( `Fetching pair data for ${baseToken.address}-${quoteToken.address}.` ); - const quoteProvider = SmartRouter.createQuoteProvider({ - // @ts-ignore - onChainProvider: () => this.createPublicClient(), - }); - const pools = await this.getPools(baseToken, quoteToken); - - const trade = await SmartRouter.getBestTrade( - nativeTokenAmount, - quoteToken, - TradeType.EXACT_INPUT, - { - gasPriceWei: () => this.createPublicClient().getGasPrice(), - maxHops: this._maximumHops, - maxSplits: 1, - poolProvider: SmartRouter.createStaticPoolProvider(pools), - quoteProvider, - quoterOptimization: true, - allowedPoolTypes: [PoolType.V2, PoolType.V3, PoolType.STABLE], - } - ); + const trade = await this.getBestTrade(baseToken, quoteToken, amount, TradeType.EXACT_INPUT); if (!trade) { throw new UniswapishPriceError( @@ -433,6 +389,34 @@ export class PancakeSwap implements Uniswapish { return fulfilledPools.flat(); } + async getBestTrade(baseToken: Token, quoteToken: Token, amount: BigNumber, tradeType: TradeType): Promise | null> { + const baseTokenAmount: CurrencyAmount = + CurrencyAmount.fromRawAmount(baseToken, amount.toString()); + + const quoteProvider = SmartRouter.createQuoteProvider({ + // @ts-ignore + onChainProvider: () => this.createPublicClient(), + }); + const pools = await this.getPools(baseToken, quoteToken); + + const trade = await SmartRouter.getBestTrade( + baseTokenAmount, + quoteToken, + tradeType, + { + gasPriceWei: () => this.createPublicClient().getGasPrice(), + maxHops: this._maximumHops, + maxSplits: 1, + poolProvider: SmartRouter.createStaticPoolProvider(pools), + quoteProvider, + quoterOptimization: true, + allowedPoolTypes: [PoolType.V2, PoolType.V3, PoolType.STABLE], + } + ); + + return trade; + } + private createPublicClient(): PublicClient { const transportUrl = this.bsc.rpcUrl; diff --git a/test/connectors/pancakeswap/pancakeswap.test.ts b/test/connectors/pancakeswap/pancakeswap.test.ts index d9a18ecd15..342443a1ad 100644 --- a/test/connectors/pancakeswap/pancakeswap.test.ts +++ b/test/connectors/pancakeswap/pancakeswap.test.ts @@ -1,14 +1,9 @@ jest.useFakeTimers(); import { - Fetcher, - Pair, Percent, - Route, Token, CurrencyAmount, - Trade, TradeType, - Currency, } from '@pancakeswap/sdk'; import { BigNumber } from 'ethers'; import { BinanceSmartChain } from '../../../src/chains/binance-smart-chain/binance-smart-chain'; @@ -49,51 +44,53 @@ afterAll(async () => { await bsc.close(); }); -const patchFetchPairData = () => { - patch(Fetcher, 'fetchPairData', () => { - return new Pair( - CurrencyAmount.fromRawAmount(WBNB, '2000000000000000000'), - CurrencyAmount.fromRawAmount(DAI, '1000000000000000000') - ); - }); -}; - -const patchTrade = (key: string, error?: Error) => { - patch(Trade, key, () => { - if (error) return []; - const WBNB_DAI = new Pair( - CurrencyAmount.fromRawAmount(WBNB, '2000000000000000000'), - CurrencyAmount.fromRawAmount(DAI, '1000000000000000000') - ); - const DAI_TO_WBNB = new Route([WBNB_DAI], DAI, WBNB); - return [ - new Trade( - DAI_TO_WBNB, - CurrencyAmount.fromRawAmount(DAI, '1000000000000000'), - TradeType.EXACT_INPUT - ), - ]; - }); -}; +// const patchFetchPairData = () => { +// patch(Fetcher, 'fetchPairData', () => { +// return new Pair( +// CurrencyAmount.fromRawAmount(WBNB, '2000000000000000000'), +// CurrencyAmount.fromRawAmount(DAI, '1000000000000000000') +// ); +// }); +// }; + +// const patchTrade = (key: string, error?: Error) => { +// patch(Trade, key, () => { +// if (error) return []; +// const WBNB_DAI = new Pair( +// CurrencyAmount.fromRawAmount(WBNB, '2000000000000000000'), +// CurrencyAmount.fromRawAmount(DAI, '1000000000000000000') +// ); +// const DAI_TO_WBNB = new Route([WBNB_DAI], DAI, WBNB); +// return [ +// new Trade( +// DAI_TO_WBNB, +// CurrencyAmount.fromRawAmount(DAI, '1000000000000000'), +// TradeType.EXACT_INPUT +// ), +// ]; +// }); +// }; describe('verify PancakeSwap estimateSellTrade', () => { it('Should return an ExpectedTrade when available', async () => { - patchFetchPairData(); - patchTrade('bestTradeExactIn'); + patch(pancakeswap, "getBestTrade", async () => { + return { tradeType: TradeType.EXACT_INPUT, inputAmount: CurrencyAmount.fromRawAmount(WBNB, "10000"), outputAmount: CurrencyAmount.fromRawAmount(DAI, "100000"), routes: [] }; + }) const expectedTrade = await pancakeswap.estimateSellTrade( WBNB, DAI, BigNumber.from(1) ); - expect(expectedTrade).toHaveProperty('trade'); expect(expectedTrade).toHaveProperty('expectedAmount'); }); it('Should throw an error if no pair is available', async () => { - patchFetchPairData(); - patchTrade('bestTradeExactIn', new Error('error getting trade')); + + patch(pancakeswap, "getBestTrade", async () => { + return null; + }) await expect(async () => { await pancakeswap.estimateSellTrade(WBNB, DAI, BigNumber.from(1)); @@ -103,8 +100,9 @@ describe('verify PancakeSwap estimateSellTrade', () => { describe('verify PancakeSwap estimateBuyTrade', () => { it('Should return an ExpectedTrade when available', async () => { - patchFetchPairData(); - patchTrade('bestTradeExactOut'); + patch(pancakeswap, "getBestTrade", async () => { + return { tradeType: TradeType.EXACT_OUTPUT, inputAmount: CurrencyAmount.fromRawAmount(WBNB, "10000"), outputAmount: CurrencyAmount.fromRawAmount(DAI, "100000"), routes: [] }; + }) const expectedTrade = await pancakeswap.estimateBuyTrade( WBNB, @@ -116,8 +114,9 @@ describe('verify PancakeSwap estimateBuyTrade', () => { }); it('Should return an error if no pair is available', async () => { - patchFetchPairData(); - patchTrade('bestTradeExactOut', new Error('error getting trade')); + patch(pancakeswap, "getBestTrade", async () => { + return null; + }) await expect(async () => { await pancakeswap.estimateBuyTrade(WBNB, DAI, BigNumber.from(1));