Skip to content

Commit

Permalink
feat: gauge config test (#8841)
Browse files Browse the repository at this point in the history
<!--
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 updating dependencies and making changes related to
gauges in the codebase.

### Detailed summary
- Updated dependencies for `@pancakeswap/sdk` and `@types/lodash`.
- Modified token addresses and types in
`packages/gauges/src/constants/config/prod.ts`.
- Added a new function `filterKilledGauges` in
`packages/gauges/src/fetchAllKilledGauges.ts`.
- Added new tests in `packages/gauges/src/tests/config.test.ts`.
- Updated the ABI definition in
`packages/gauges/src/abis/gaugesVoting.ts`.

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

<!-- end pr-codex -->
  • Loading branch information
ChefJerry authored Jan 16, 2024
1 parent 1faa163 commit 533887e
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 16 deletions.
3 changes: 3 additions & 0 deletions packages/gauges/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"./package.json": "./package.json"
},
"devDependencies": {
"@pancakeswap/sdk": "workspace:^",
"@types/lodash": "^4.14.202",
"lodash": "^4.17.21",
"tsup": "^6.7.0"
},
"dependencies": {
Expand Down
151 changes: 139 additions & 12 deletions packages/gauges/src/abis/gaugesVoting.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
export const gaugesVotingABI = [
{
inputs: [
{ internalType: 'address', name: '_token', type: 'address' },
{ internalType: 'address', name: '_votingEscrow', type: 'address' },
],
inputs: [{ internalType: 'address', name: '_votingEscrow', type: 'address' }],
stateMutability: 'nonpayable',
type: 'constructor',
},
Expand All @@ -16,6 +13,46 @@ export const gaugesVotingABI = [
name: 'AddType',
type: 'event',
},
{
anonymous: false,
inputs: [
{ indexed: true, internalType: 'address', name: 'sender', type: 'address' },
{ indexed: false, internalType: 'uint256', name: 'allocation', type: 'uint256' },
],
name: 'AdminAllocationChanged',
type: 'event',
},
{
anonymous: false,
inputs: [
{ indexed: true, internalType: 'address', name: 'sender', type: 'address' },
{ indexed: false, internalType: 'uint256', name: 'period', type: 'uint256' },
],
name: 'AdminOnlyPeriodUpdated',
type: 'event',
},
{
anonymous: false,
inputs: [
{ indexed: true, internalType: 'address', name: 'sender', type: 'address' },
{ indexed: true, internalType: 'address', name: 'gauage_addr', type: 'address' },
{ indexed: false, internalType: 'uint256', name: 'chainId', type: 'uint256' },
{ indexed: false, internalType: 'bytes32', name: 'hash', type: 'bytes32' },
],
name: 'GaugeKilled',
type: 'event',
},
{
anonymous: false,
inputs: [
{ indexed: true, internalType: 'address', name: 'sender', type: 'address' },
{ indexed: true, internalType: 'address', name: 'gauage_addr', type: 'address' },
{ indexed: false, internalType: 'uint256', name: 'chainId', type: 'uint256' },
{ indexed: false, internalType: 'bytes32', name: 'hash', type: 'bytes32' },
],
name: 'GaugeUnKilled',
type: 'event',
},
{
anonymous: false,
inputs: [
Expand Down Expand Up @@ -109,10 +146,33 @@ export const gaugesVotingABI = [
name: 'VoteForGaugeFromAdmin',
type: 'event',
},
{
anonymous: false,
inputs: [
{ indexed: true, internalType: 'address', name: 'sender', type: 'address' },
{ indexed: false, internalType: 'uint256', name: 'delay', type: 'uint256' },
],
name: 'WeightVoteDelayChanged',
type: 'event',
},
{
inputs: [],
name: 'ADMIN_VOTE_PERIOD',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'WEIGHT_VOTE_DELAY',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{ internalType: 'address', name: 'gauge_addr', type: 'address' },
{ internalType: 'uint256', name: '_type', type: 'uint256' },
{ internalType: 'uint256', name: 'gauge_type', type: 'uint256' },
{ internalType: 'uint256', name: '_weight', type: 'uint256' },
{ internalType: 'uint256', name: '_pid', type: 'uint256' },
{ internalType: 'address', name: '_masterChef', type: 'address' },
Expand All @@ -135,6 +195,27 @@ export const gaugesVotingABI = [
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [],
name: 'adminAllocation',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
name: 'adminSlopes',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [{ internalType: 'uint256', name: '_numerator', type: 'uint256' }],
name: 'changeAdminAllocation',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{ internalType: 'address', name: 'gauge_addr', type: 'address' },
Expand All @@ -156,6 +237,13 @@ export const gaugesVotingABI = [
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [{ internalType: 'uint256', name: '_delay', type: 'uint256' }],
name: 'changeWeightVoteDelay',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
{ inputs: [], name: 'checkpoint', outputs: [], stateMutability: 'nonpayable', type: 'function' },
{
inputs: [
Expand Down Expand Up @@ -191,6 +279,13 @@ export const gaugesVotingABI = [
stateMutability: 'view',
type: 'function',
},
{
inputs: [{ internalType: 'bytes32', name: '', type: 'bytes32' }],
name: 'gaugeIsKilled_',
outputs: [{ internalType: 'bool', name: '', type: 'bool' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [{ internalType: 'bytes32', name: '', type: 'bytes32' }],
name: 'gaugeLastScheduled',
Expand Down Expand Up @@ -357,6 +452,16 @@ export const gaugesVotingABI = [
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{ internalType: 'address', name: '_gauge_addr', type: 'address' },
{ internalType: 'uint256', name: '_chainId', type: 'uint256' },
],
name: 'killGauge',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{ internalType: 'address', name: '', type: 'address' },
Expand All @@ -382,13 +487,6 @@ export const gaugesVotingABI = [
type: 'function',
},
{ inputs: [], name: 'renounceOwnership', outputs: [], stateMutability: 'nonpayable', type: 'function' },
{
inputs: [],
name: 'token',
outputs: [{ internalType: 'address', name: '', type: 'address' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'totalLastScheduled',
Expand All @@ -403,6 +501,23 @@ export const gaugesVotingABI = [
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{ internalType: 'address', name: '_gauge_addr', type: 'address' },
{ internalType: 'uint256', name: '_chainId', type: 'uint256' },
],
name: 'unkillGauge',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [{ internalType: 'uint256', name: '_newAdminOnlyPeriod', type: 'uint256' }],
name: 'updateAdminOnlyPeriod',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{ internalType: 'address', name: 'gauge_addr', type: 'address' },
Expand Down Expand Up @@ -455,6 +570,18 @@ export const gaugesVotingABI = [
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{ internalType: 'address[]', name: '_gauge_addrs', type: 'address[]' },
{ internalType: 'uint256[]', name: '_admin_weights', type: 'uint256[]' },
{ internalType: 'uint256[]', name: '_ends', type: 'uint256[]' },
{ internalType: 'uint256[]', name: '_chainIds', type: 'uint256[]' },
],
name: 'voteFromAdminBulk',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [{ internalType: 'address', name: '', type: 'address' }],
name: 'voteUserPower',
Expand Down
6 changes: 3 additions & 3 deletions packages/gauges/src/constants/config/prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ export const CONFIG_PROD: GaugeConfig[] = [
chainId: ChainId.BSC,
type: GaugeType.V3,
token0Address: bscTokens.tusd.address,
token1Address: bscTokens.bnb.address,
token1Address: bscTokens.busd.address,
feeTier: FeeAmount.LOWEST,
},
{
Expand Down Expand Up @@ -811,7 +811,7 @@ export const CONFIG_PROD: GaugeConfig[] = [
pairName: 'TRX-USDT',
chainId: ChainId.BSC,
type: GaugeType.V3,
token0Address: bscTokens.trx.address,
token0Address: bscTokens.trxv2.address,
token1Address: bscTokens.usdt.address,
feeTier: FeeAmount.MEDIUM,
},
Expand Down Expand Up @@ -2393,7 +2393,7 @@ export const CONFIG_PROD: GaugeConfig[] = [
pairName: 'RDP-BNB',
address: '0xc9B415b8331e1Fb0d2f3442Ac8413E279304090f',
chainId: ChainId.BSC,
type: GaugeType.V3,
type: GaugeType.V2,
token0Address: bscTokens.rdp.address,
token1Address: bscTokens.wbnb.address,
feeTier: FeeAmount.MEDIUM,
Expand Down
27 changes: 27 additions & 0 deletions packages/gauges/src/fetchAllKilledGauges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ContractFunctionConfig, ContractFunctionResult, Hex, MulticallContracts, PublicClient } from 'viem'
import { gaugesVotingABI } from './abis'
import { getContract } from './contract'
import { GaugeInfo } from './types'

export const filterKilledGauges = async (client: PublicClient, gauges: GaugeInfo[]): Promise<GaugeInfo[]> => {
const contract = getContract(client)

const multicalls: MulticallContracts<ContractFunctionConfig<typeof gaugesVotingABI, 'gaugeIsKilled_'>[]> = []

for (let i = 0; i < gauges.length; i++) {
multicalls.push({
...contract,
functionName: 'gaugeIsKilled_',
args: [gauges[i].hash as Hex],
} as const)
}

const response = (await client.multicall({
contracts: multicalls,
allowFailure: false,
})) as ContractFunctionResult<typeof gaugesVotingABI, 'gaugeIsKilled_'>[]

return gauges.filter((_, index) => {
return !response[index]
})
}
4 changes: 3 additions & 1 deletion packages/gauges/src/getAllGauges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PublicClient } from 'viem'
import { CONFIG_PROD } from './constants/config/prod'
import { CONFIG_TESTNET } from './constants/config/testnet'
import { fetchAllGauges } from './fetchAllGauges'
import { filterKilledGauges } from './fetchAllKilledGauges'
import { fetchAllGaugesVoting } from './fetchGaugeVoting'
import { Gauge, GaugeConfig, GaugeInfoConfig } from './types'

Expand All @@ -23,7 +24,8 @@ export const getAllGauges = async (
const presets = testnet ? CONFIG_TESTNET : CONFIG_PROD

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

Expand Down
45 changes: 45 additions & 0 deletions packages/gauges/src/tests/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { chainNames } from '@pancakeswap/chains'
import { FACTORY_ADDRESS_MAP, Token, computePairAddress } from '@pancakeswap/sdk'
import { DEPLOYER_ADDRESSES, computePoolAddress } from '@pancakeswap/v3-sdk'
import groupBy from 'lodash/groupBy'
import { describe, expect, it } from 'vitest'
import { CONFIG_PROD } from '../constants/config/prod'
import { GaugeType } from '../types'

describe('Gauges Config', async () => {
const chainName = chainNames[1]

const gidGroups = groupBy(CONFIG_PROD, 'gid')
Object.entries(gidGroups).forEach(([gid, gauge]) => {
it(`${chainName} gauges with gid #${gid} should be unique`, () => {
expect(gauge.length).toBe(1)
})
})

CONFIG_PROD.forEach((gauge) => {
it(`${chainName} gid #${gauge.gid} tokens chainId-lpAddress-feeTier should be matched`, () => {
if (gauge.type === GaugeType.V3) {
const tokenA = new Token(gauge.chainId, gauge.token0Address, 18, '')
const tokenB = new Token(gauge.chainId, gauge.token1Address, 18, '')
const computedAddress = computePoolAddress({
deployerAddress: DEPLOYER_ADDRESSES[gauge.chainId],
tokenA,
tokenB,
fee: gauge.feeTier,
})
expect(computedAddress).toBe(gauge.address)
}

if (gauge.type === GaugeType.V2) {
const tokenA = new Token(gauge.chainId, gauge.token0Address, 18, '')
const tokenB = new Token(gauge.chainId, gauge.token1Address, 18, '')
const computedAddress = computePairAddress({
factoryAddress: FACTORY_ADDRESS_MAP[gauge.chainId],
tokenA,
tokenB,
})
expect(gauge.address).toBe(computedAddress)
}
})
})
})
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 533887e

@vercel
Copy link

@vercel vercel bot commented on 533887e Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.