-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!-- Before opening a pull request, please read the [contributing guidelines](https://github.com/pancakeswap/pancake-frontend/blob/develop/CONTRIBUTING.md) first --> <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on adding support for stable swap addresses in farms configuration and updating related functions and configurations. ### Detailed summary - Added `stableSwapAddress` field to `UniversalFarmConfigStableSwap` - Updated functions to use `stableSwapAddress` instead of `stableLpAddress` - Added support for fetching stable swap pairs in functions - Modified configuration files to include `stableSwapAddress` field > The following files were skipped due to too many changes: `packages/farms/src/utils.ts` > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
- Loading branch information
Showing
15 changed files
with
195 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ChainId, chainNames, chainNameToChainId } from '@pancakeswap/chains' | ||
import { formatUniversalFarmToSerializedFarm, UNIVERSAL_FARMS } from '@pancakeswap/farms' | ||
import { NextApiHandler } from 'next' | ||
import { stringify } from 'viem' | ||
import { enum as enum_, nativeEnum } from 'zod' | ||
|
||
const allChainNames = Object.values(chainNames) as [string, ...string[]] | ||
|
||
const zChain = nativeEnum(ChainId).or(enum_(allChainNames)) | ||
|
||
const handler: NextApiHandler = async (req, res) => { | ||
const isChainInt = !Number.isNaN(parseInt(req.query.chain as string, 10)) | ||
const chainQuery = isChainInt ? Number(req.query.chain) : req.query.chain | ||
const parsedChain = zChain.safeParse(chainQuery) | ||
|
||
if (!parsedChain.success) { | ||
return res.status(400).json({ error: parsedChain.error }) | ||
} | ||
|
||
const chainId = isChainInt ? Number(parsedChain.data) : Number(chainNameToChainId[parsedChain.data]) | ||
|
||
if (!chainId) { | ||
return res.status(400).json({ error: 'Invalid chain' }) | ||
} | ||
|
||
try { | ||
const farmConfig = UNIVERSAL_FARMS.filter((farm) => farm.chainId === chainId) | ||
const legacyFarmConfig = formatUniversalFarmToSerializedFarm(farmConfig) | ||
// cache for long time, it should revalidate on every deployment | ||
res.setHeader('Cache-Control', `max-age=10800, s-maxage=31536000`) | ||
|
||
return res.status(200).json({ | ||
data: JSON.parse(stringify(legacyFarmConfig)), | ||
lastUpdatedAt: new Date().toISOString, | ||
}) | ||
} catch (error) { | ||
console.error(error) | ||
return res.status(500).json({ error: JSON.parse(stringify(error)) }) | ||
} | ||
} | ||
|
||
export default handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { formatUniversalFarmToSerializedFarm, UNIVERSAL_FARMS, UNIVERSAL_FARMS_WITH_TESTNET } from '@pancakeswap/farms' | ||
import { NextApiHandler } from 'next' | ||
import { stringify } from 'viem' | ||
|
||
const handler: NextApiHandler = async (req, res) => { | ||
const includeTestnet = !!req.query.includeTestnet | ||
|
||
try { | ||
const farmConfig = includeTestnet ? UNIVERSAL_FARMS : UNIVERSAL_FARMS_WITH_TESTNET | ||
const legacyFarmConfig = formatUniversalFarmToSerializedFarm(farmConfig) | ||
// cache for long time, it should revalidate on every deployment | ||
res.setHeader('Cache-Control', `max-age=10800, s-maxage=31536000`) | ||
|
||
return res.status(200).json({ | ||
data: JSON.parse(stringify(legacyFarmConfig)), | ||
lastUpdatedAt: new Date().toISOString, | ||
}) | ||
} catch (error) { | ||
console.error(error) | ||
return res.status(500).json({ error: JSON.parse(stringify(error)) }) | ||
} | ||
} | ||
|
||
export default handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,101 @@ | ||
import { FarmV3Data } from './types' | ||
import { ChainId } from '@pancakeswap/chains' | ||
import { getStableSwapPools } from '@pancakeswap/stable-swap-sdk' | ||
import { isAddressEqual } from 'viem' | ||
import { UNIVERSAL_BCAKEWRAPPER_FARMS } from './farms' | ||
import { | ||
ComputedFarmConfigV3, | ||
FarmV3Data, | ||
SerializedClassicFarmConfig, | ||
SerializedFarmConfig, | ||
SerializedStableFarmConfig, | ||
UniversalFarmConfig, | ||
UniversalFarmConfigStableSwap, | ||
UniversalFarmConfigV2, | ||
UniversalFarmConfigV3, | ||
} from './types' | ||
|
||
export function isActiveV3Farm(farm: FarmV3Data, poolLength: number) { | ||
return farm.pid !== 0 && farm.multiplier !== '0X' && poolLength && poolLength >= farm.pid | ||
} | ||
|
||
type LegacyFarmConfig = SerializedFarmConfig & { chainId: ChainId; version: 2 | 3 } | ||
type LegacyStableFarmConfig = SerializedStableFarmConfig & { chainId: ChainId; version: 2 | 3 } | ||
type LegacyClassicFarmConfig = SerializedClassicFarmConfig & { chainId: ChainId; version: 2 | 3 } | ||
type LegacyV3FarmConfig = ComputedFarmConfigV3 & { chainId: ChainId; version: 2 | 3 } | ||
export function formatUniversalFarmToSerializedFarm(farms: UniversalFarmConfig[]): Array<LegacyFarmConfig> { | ||
return farms | ||
.map((farm) => { | ||
switch (farm.protocol) { | ||
case 'stable': | ||
return formatStableUniversalFarmToSerializedFarm(farm as UniversalFarmConfigStableSwap) | ||
case 'v2': | ||
return formatV2UniversalFarmToSerializedFarm(farm as UniversalFarmConfigV2) | ||
case 'v3': | ||
return formatV3UniversalFarmToSerializedFarm(farm as UniversalFarmConfigV3) | ||
default: | ||
return undefined | ||
} | ||
}) | ||
.filter((farm): farm is LegacyFarmConfig => farm !== undefined) | ||
} | ||
|
||
const formatStableUniversalFarmToSerializedFarm = ( | ||
farm: UniversalFarmConfigStableSwap, | ||
): LegacyStableFarmConfig | undefined => { | ||
const { chainId, lpAddress, pid, token0, token1, stableSwapAddress } = farm | ||
const stablePair = getStableSwapPools(chainId).find((pair) => { | ||
return isAddressEqual(pair.stableSwapAddress, stableSwapAddress) | ||
}) | ||
const bCakeConfig = UNIVERSAL_BCAKEWRAPPER_FARMS.find((config) => { | ||
return chainId === config.chainId && isAddressEqual(config.lpAddress, lpAddress) | ||
}) | ||
|
||
if (!stablePair) { | ||
console.warn(`Could not find stable pair for farm with stableSwapAddress ${stableSwapAddress}`) | ||
return undefined | ||
} | ||
|
||
return { | ||
pid, | ||
lpAddress, | ||
lpSymbol: `${token0.symbol}-${token1.symbol} LP`, | ||
token: token0, | ||
quoteToken: token1, | ||
stableSwapAddress, | ||
stableLpFee: stablePair.stableLpFee, | ||
stableLpFeeRateOfTotalFee: stablePair.stableLpFeeRateOfTotalFee, | ||
infoStableSwapAddress: stablePair.infoStableSwapAddress, | ||
bCakeWrapperAddress: bCakeConfig?.bCakeWrapperAddress, | ||
chainId, | ||
version: 2, | ||
} | ||
} | ||
|
||
const formatV2UniversalFarmToSerializedFarm = (farm: UniversalFarmConfigV2): LegacyClassicFarmConfig => { | ||
const { chainId, pid, lpAddress, token0, token1 } = farm | ||
return { | ||
pid, | ||
lpAddress, | ||
lpSymbol: `${token0.symbol}-${token1.symbol} LP`, | ||
token: token0, | ||
quoteToken: token1, | ||
chainId, | ||
version: 2, | ||
} | ||
} | ||
|
||
const formatV3UniversalFarmToSerializedFarm = (farm: UniversalFarmConfigV3): LegacyV3FarmConfig => { | ||
const { chainId, pid, lpAddress, token0, token1, feeAmount } = farm | ||
return { | ||
pid, | ||
lpAddress, | ||
lpSymbol: `${token0.symbol}-${token1.symbol} LP`, | ||
token0, | ||
token1, | ||
token: token0, | ||
quoteToken: token1, | ||
feeAmount, | ||
chainId, | ||
version: 3, | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.