|
| 1 | +import { Injectable } from '@nestjs/common'; |
| 2 | +import { BigNumber, ethers } from 'ethers'; |
| 3 | +import { DEX_ADDRESSES } from '../addresses'; |
| 4 | +import { |
| 5 | + DexAdapter, |
| 6 | + ExactInputSingleParams, |
| 7 | + QuoteSingleParams, |
| 8 | +} from './dex-adapter'; |
| 9 | +import { |
| 10 | + UNISWAP_V3_FACTORY_ABI, |
| 11 | + UNISWAP_V3_QUOTER_V2_ABI, |
| 12 | + UNISWAP_V3_ROUTER_ABI, |
| 13 | +} from '../abis'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Pancake v3 is a Uniswap v3 fork; ABIs are compatible. Addresses must be set in DEX_ADDRESSES.pancakeV3. |
| 17 | + */ |
| 18 | +@Injectable() |
| 19 | +export class PancakeV3Adapter implements DexAdapter { |
| 20 | + readonly id = 'pancakeV3' as const; |
| 21 | + |
| 22 | + supportsChain(chainId: number): boolean { |
| 23 | + return !!DEX_ADDRESSES.pancakeV3[chainId]; |
| 24 | + } |
| 25 | + |
| 26 | + getAddresses(chainId: number) { |
| 27 | + const a = DEX_ADDRESSES.pancakeV3[chainId]; |
| 28 | + if (!a) throw new Error(`PancakeV3 not configured for chain ${chainId}`); |
| 29 | + return a; |
| 30 | + } |
| 31 | + |
| 32 | + async getPool( |
| 33 | + provider: ethers.providers.Provider, |
| 34 | + chainId: number, |
| 35 | + tokenIn: string, |
| 36 | + tokenOut: string, |
| 37 | + fee: number, |
| 38 | + ) { |
| 39 | + const { factory } = this.getAddresses(chainId); |
| 40 | + const c = new ethers.Contract(factory, UNISWAP_V3_FACTORY_ABI, provider); |
| 41 | + return await c.getPool(tokenIn, tokenOut, fee); |
| 42 | + } |
| 43 | + |
| 44 | + async quoteExactInputSingle( |
| 45 | + provider: ethers.providers.Provider, |
| 46 | + chainId: number, |
| 47 | + p: QuoteSingleParams, |
| 48 | + ) { |
| 49 | + const { quoterV2 } = this.getAddresses(chainId); |
| 50 | + const q = new ethers.Contract(quoterV2, UNISWAP_V3_QUOTER_V2_ABI, provider); |
| 51 | + const res = await q.callStatic.quoteExactInputSingle({ |
| 52 | + tokenIn: p.tokenIn, |
| 53 | + tokenOut: p.tokenOut, |
| 54 | + amountIn: p.amountIn, |
| 55 | + fee: p.fee, |
| 56 | + sqrtPriceLimitX96: p.sqrtPriceLimitX96 ?? 0, |
| 57 | + }); |
| 58 | + return { amountOut: res[0] as BigNumber }; |
| 59 | + } |
| 60 | + |
| 61 | + async estimateGasExactInputSingle( |
| 62 | + signer: ethers.Signer, |
| 63 | + chainId: number, |
| 64 | + p: ExactInputSingleParams, |
| 65 | + ) { |
| 66 | + const { router } = this.getAddresses(chainId); |
| 67 | + const r = new ethers.Contract(router, UNISWAP_V3_ROUTER_ABI, signer); |
| 68 | + return await r.estimateGas.exactInputSingle({ |
| 69 | + tokenIn: p.tokenIn, |
| 70 | + tokenOut: p.tokenOut, |
| 71 | + fee: p.fee, |
| 72 | + recipient: p.recipient, |
| 73 | + deadline: p.deadline, |
| 74 | + amountIn: p.amountIn, |
| 75 | + amountOutMinimum: p.amountOutMinimum, |
| 76 | + sqrtPriceLimitX96: p.sqrtPriceLimitX96 ?? 0, |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + async exactInputSingle( |
| 81 | + signer: ethers.Signer, |
| 82 | + chainId: number, |
| 83 | + p: ExactInputSingleParams, |
| 84 | + ) { |
| 85 | + const { router } = this.getAddresses(chainId); |
| 86 | + const r = new ethers.Contract(router, UNISWAP_V3_ROUTER_ABI, signer); |
| 87 | + const tx = await r.exactInputSingle({ |
| 88 | + tokenIn: p.tokenIn, |
| 89 | + tokenOut: p.tokenOut, |
| 90 | + fee: p.fee, |
| 91 | + recipient: p.recipient, |
| 92 | + deadline: p.deadline, |
| 93 | + amountIn: p.amountIn, |
| 94 | + amountOutMinimum: p.amountOutMinimum, |
| 95 | + sqrtPriceLimitX96: p.sqrtPriceLimitX96 ?? 0, |
| 96 | + }); |
| 97 | + const rcpt = await tx.wait(); |
| 98 | + return rcpt; |
| 99 | + } |
| 100 | +} |
0 commit comments