Skip to content

Commit

Permalink
fix: mismatch gauge in cms and sc side (#10929)
Browse files Browse the repository at this point in the history
The old alg using sc side data as the source, this cause some data in
cms not reflect in google sheets.
1. The original algorithm using address as condition to match, now I
modified to gid.
2. CMS side data is used as SSOT.

<!-- start pr-codex -->

---

## PR-Codex overview
This PR focuses on enhancing the `GaugeInfo` type structure, introducing
new tests for gauge retrieval, and refining the logic for fetching and
merging gauge configurations.

### Detailed summary
- Added `gid` property to `GaugeInfo` type.
- Included an import for `abortcontroller-polyfill.ts`.
- Updated test exclusions in `vitest.config.ts`.
- Created tests for gauge retrieval in `gauges.test.ts`.
- Modified `fetchAllGauges` to use `gid` instead of `pid`.
- Introduced `fetchGaugesSC` function for fetching smart contract
gauges.

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

<!-- end pr-codex -->
  • Loading branch information
chef-ryan authored Nov 8, 2024
1 parent f2a3249 commit ad8519f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 46 deletions.
22 changes: 22 additions & 0 deletions apps/web/src/__tests__/gagues/gauges.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ChainId } from '@pancakeswap/chains'
import { getAllGauges } from '@pancakeswap/gauges'
import { getViemClients } from 'utils/viem'
import { describe, it } from 'vitest'

describe('get all guages', () => {
it('contract gaguges and cms gauges should be match', async () => {
const gauges = await getAllGauges(
getViemClients({
chainId: ChainId.BSC,
}),
{
testnet: false,
inCap: false,
bothCap: false,
killed: true,
},
)
const notMatched = gauges.find((x) => !x.pairAddress)
expect(notMatched).toBeUndefined()
}, 300000)
})
2 changes: 1 addition & 1 deletion apps/web/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ export default defineConfig({
setupFiles: ['./vitest.setup.js'],
environment: 'happy-dom',
globals: true,
exclude: ['src/config/__tests__', 'node_modules'],
exclude: ['src/config/__tests__', 'src/__tests__/gagues', 'node_modules'],
},
})
1 change: 1 addition & 0 deletions apps/web/vitest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom/extend-expect'
import { vi } from 'vitest'
import './src/utils/abortcontroller-polyfill.ts'

global.setImmediate = vi.useRealTimers

Expand Down
28 changes: 13 additions & 15 deletions packages/gauges/src/fetchAllGauges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,17 @@ export const fetchAllGauges = async (
...options,
})) as ContractFunctionReturnType<typeof gaugesVotingABI, AbiStateMutability, 'gauges'>[]

return response.reduce((prev, curr) => {
const [pid, masterChef, chainId, pairAddress, boostMultiplier, maxVoteCap] = curr
return [
...prev,
{
pid: Number(pid),
hash: getGaugeHash(pairAddress, Number(chainId)),
pairAddress,
masterChef,
chainId: Number(chainId),
boostMultiplier: Number(boostMultiplier),
maxVoteCap: Number(maxVoteCap),
},
]
}, [] as GaugeInfo[])
return response.map((x, i) => {
const [pid, masterChef, chainId, pairAddress, boostMultiplier, maxVoteCap] = x
return {
gid: i,
pid: Number(pid),
hash: getGaugeHash(pairAddress, Number(chainId)),
pairAddress,
masterChef,
chainId: Number(chainId),
boostMultiplier: Number(boostMultiplier),
maxVoteCap: Number(maxVoteCap),
} as GaugeInfo
})
}
49 changes: 19 additions & 30 deletions packages/gauges/src/getAllGauges.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import keyBy from 'lodash/keyBy'
import { PublicClient } from 'viem'
import { getGauges } from './constants/config/getGauges'
import { CONFIG_TESTNET } from './constants/config/testnet'
import { fetchAllGauges } from './fetchAllGauges'
import { fetchAllKilledGauges } from './fetchAllKilledGauges'
import { fetchAllGaugesVoting } from './fetchGaugeVoting'
import { Gauge, GaugeConfig, GaugeInfoConfig } from './types'
import { Gauge, GaugeInfoConfig } from './types'

export type getAllGaugesOptions = {
testnet?: boolean
Expand All @@ -25,37 +26,16 @@ export const getAllGauges = async (
},
): Promise<Gauge[]> => {
const { testnet, inCap, bothCap, killed, blockNumber } = options
const gaugesConfig = testnet ? CONFIG_TESTNET : await getGauges()
const presets = gaugesConfig.sort((a, b) => (a.gid < b.gid ? -1 : 1))
const gaugesCMS = testnet ? CONFIG_TESTNET : await getGauges()
gaugesCMS.sort((a, b) => (a.gid < b.gid ? -1 : 1))
const gaugesSC = await fetchGaugesSC(client, killed, blockNumber)
const gaugesSCMap = keyBy(gaugesSC, 'gid')

const allGaugeInfos = await fetchAllGauges(client, {
blockNumber,
const allGaugeInfoConfigs = gaugesCMS.map((config) => {
const correspondingSC = gaugesSCMap[config.gid]
const mergedConfig: GaugeInfoConfig = { ...config, ...correspondingSC }
return mergedConfig
})
let allActiveGaugeInfos = allGaugeInfos

allActiveGaugeInfos = await fetchAllKilledGauges(client, allGaugeInfos, { blockNumber })

if (!killed) allActiveGaugeInfos = allActiveGaugeInfos.filter((gauge) => !gauge.killed)

const allGaugeInfoConfigs = allActiveGaugeInfos.reduce((prev, gauge) => {
const filters = presets.filter((p) => p.address === gauge.pairAddress && Number(p.chainId) === gauge.chainId)
let preset: GaugeConfig

if (!filters.length) return prev
if (filters.length > 1) {
preset = filters[filters.length - 1]
} else {
preset = filters[0]
}

return [
...prev,
{
...preset,
...gauge,
},
]
}, [] as GaugeInfoConfig[])

if (!bothCap) {
const allGaugesVoting = await fetchAllGaugesVoting(client, allGaugeInfoConfigs, inCap, options)
Expand All @@ -79,3 +59,12 @@ export const getAllGauges = async (
]
}, [] as Gauge[])
}

async function fetchGaugesSC(client: PublicClient, killed?: boolean, blockNumber?: bigint) {
let gaugesSC = await fetchAllGauges(client, {
blockNumber,
})
gaugesSC = await fetchAllKilledGauges(client, gaugesSC, { blockNumber })
if (!killed) gaugesSC = gaugesSC.filter((gauge) => !gauge.killed)
return gaugesSC
}
1 change: 1 addition & 0 deletions packages/gauges/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface GaugeVeCakePoolConfig extends GaugeBaseConfig {
export type GaugeConfig = GaugeV2Config | GaugeStableSwapConfig | GaugeV3Config | GaugeALMConfig | GaugeVeCakePoolConfig

export type GaugeInfo = {
gid: number
hash: Hash
pairAddress: Address
masterChef: Address
Expand Down

0 comments on commit ad8519f

Please sign in to comment.