Skip to content

Commit

Permalink
feat: Add formatV4UniversalFarmToSerializedFarm
Browse files Browse the repository at this point in the history
  • Loading branch information
ChefMomota committed Nov 1, 2024
1 parent 5b02483 commit 9a17803
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/farms/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ export enum Protocol {
V3 = 'v3',
STABLE = 'stable',
V4BIN = 'v4bin',
V4CLAMM = 'v4cl',
}

export type FarmBaseConfig = {
Expand Down Expand Up @@ -348,11 +349,16 @@ export type UniversalFarmConfigV3 = {
feeAmount: FeeAmount
} & FarmBaseConfig

export type UniversalFarmConfigV4 = {
pid: number
protocol: Protocol.V4CLAMM | Protocol.V4BIN
poolId: Address
} & FarmBaseConfig
/**
* minimal pool info for a farm
*/
export type UniversalFarmConfig = Prettify<
UniversalFarmConfigV2 | UniversalFarmConfigStableSwap | UniversalFarmConfigV3
UniversalFarmConfigV2 | UniversalFarmConfigStableSwap | UniversalFarmConfigV3 | UniversalFarmConfigV4
>

// only v2/ss farms have bCakeWrapperAddress
Expand Down
22 changes: 22 additions & 0 deletions packages/farms/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { getStableSwapPools } from '@pancakeswap/stable-swap-sdk'
import {
ComputedFarmConfigV3,
FarmV3Data,
Protocol,
SerializedClassicFarmConfig,
SerializedFarmConfig,
SerializedStableFarmConfig,
UniversalFarmConfig,
UniversalFarmConfigStableSwap,
UniversalFarmConfigV2,
UniversalFarmConfigV3,
UniversalFarmConfigV4,
} from './types'

export function isActiveV3Farm(farm: FarmV3Data, poolLength: number) {
Expand All @@ -24,6 +26,8 @@ type LegacyClassicFarmConfig = Omit<SerializedClassicFarmConfig, 'pid'> & { chai
pid?: number
}
type LegacyV3FarmConfig = ComputedFarmConfigV3 & { chainId: ChainId; version: 2 | 3 }
type LegacyV4FarmConfig = Omit<ComputedFarmConfigV3, 'feeAmount'> & { chainId: ChainId; version: 4 }

export function formatUniversalFarmToSerializedFarm(farms: UniversalFarmConfig[]): Array<LegacyFarmConfig> {
return farms
.map((farm) => {
Expand All @@ -34,6 +38,9 @@ export function formatUniversalFarmToSerializedFarm(farms: UniversalFarmConfig[]
return formatV2UniversalFarmToSerializedFarm(farm as UniversalFarmConfigV2)
case 'v3':
return formatV3UniversalFarmToSerializedFarm(farm as UniversalFarmConfigV3)
case Protocol.V4BIN:
case Protocol.V4CLAMM:
return formatV4UniversalFarmToSerializedFarm(farm as UniversalFarmConfigV4)
default:
return undefined
}
Expand Down Expand Up @@ -100,3 +107,18 @@ const formatV3UniversalFarmToSerializedFarm = (farm: UniversalFarmConfigV3): Leg
version: 3,
}
}

const formatV4UniversalFarmToSerializedFarm = (farm: UniversalFarmConfigV4): LegacyV4FarmConfig => {
const { chainId, pid, lpAddress, token0, token1 } = farm
return {
pid,
lpAddress,
lpSymbol: `${token0.symbol}-${token1.symbol} LP`,
token0,
token1,
token: token0,
quoteToken: token1,
chainId,
version: 4,
}
}

0 comments on commit 9a17803

Please sign in to comment.