Skip to content

Commit

Permalink
feat: add calc gauges voting sc (#8495)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChefJerry authored Dec 1, 2023
1 parent 363e4cf commit 418c8f7
Show file tree
Hide file tree
Showing 20 changed files with 242 additions and 556 deletions.
486 changes: 0 additions & 486 deletions apps/web/src/config/abi/gaugesVoting.ts

This file was deleted.

7 changes: 3 additions & 4 deletions apps/web/src/config/constants/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChainId } from '@pancakeswap/chains'
import { masterChefV3Addresses } from '@pancakeswap/farms'
import { GAUGES_ADDRESS, GAUGES_CALC_ADDRESS } from '@pancakeswap/gauges'
import { ICAKE } from '@pancakeswap/ifos'
import { CAKE_VAULT } from '@pancakeswap/pools'
import { V3_QUOTER_ADDRESSES } from '@pancakeswap/smart-router/evm'
Expand Down Expand Up @@ -265,10 +266,8 @@ export default {
[ChainId.BSC]: '0x5692DB8177a81A6c6afc8084C2976C9933EC1bAB',
[ChainId.BSC_TESTNET]: '0x279957513FC505F8Cb16f4b6783D170C9BEcE322',
},
gaugesVoting: {
[ChainId.BSC]: '0xf81953dC234cdEf1D6D0d3ef61b232C6bCbF9aeF',
[ChainId.BSC_TESTNET]: '0x357b01894b21787B41A8FA4DCaFE92293470FaD9',
},
gaugesVoting: GAUGES_ADDRESS,
gaugesVotingCalc: GAUGES_CALC_ADDRESS,
revenueSharingVeCake: {
[ChainId.BSC]: '0x9cac9745731d1Cf2B483f257745A512f0938DD01',
[ChainId.BSC_TESTNET]: '0x482a401D57C9892D6d6BD6A4A976CfDDeD83BF11',
Expand Down
33 changes: 21 additions & 12 deletions apps/web/src/hooks/useContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@ import addresses from 'config/constants/contracts'
import { useMemo } from 'react'
import { getMulticallAddress, getPredictionsV1Address, getZapAddress } from 'utils/addressHelpers'
import {
getAffiliateProgramContract,
getAnniversaryAchievementContract,
getBCakeFarmBoosterContract,
getBCakeFarmBoosterProxyFactoryContract,
getBCakeFarmBoosterV3Contract,
getPositionManagerWrapperContract,
getPositionManagerAdapterContract,
getBCakeProxyContract,
getBunnyFactoryContract,
getCakeFlexibleSideVaultV2Contract,
getCakePredictionsContract,
getCakeVaultV2Contract,
getCalcGaugesVotingContract,
getChainlinkOracleContract,
getContract,
getCrossFarmingProxyContract,
getFarmAuctionContract,
getFixedStakingContract,
getGaugesVotingContract,
getIfoCreditAddressContract,
getLotteryV2Contract,
getMasterChefContract,
Expand All @@ -31,37 +34,35 @@ import {
getNftSaleContract,
getNonBscVaultContract,
getPointCenterIfoContract,
getPositionManagerAdapterContract,
getPositionManagerWrapperContract,
getPotteryDrawContract,
getPotteryVaultContract,
getPredictionsV1Contract,
getPredictionsV2Contract,
getProfileContract,
getRevenueSharingCakePoolContract,
getRevenueSharingPoolContract,
getRevenueSharingPoolGatewayContract,
getRevenueSharingVeCakeContract,
getSidContract,
getStableSwapNativeHelperContract,
getTradingCompetitionContractEaster,
getTradingCompetitionContractFanToken,
getTradingCompetitionContractMoD,
getTradingCompetitionContractMobox,
getTradingRewardContract,
getTradingRewardTopTradesContract,
getUnsContract,
getAffiliateProgramContract,
getV3AirdropContract,
getV3MigratorContract,
getTradingRewardTopTradesContract,
getVCakeContract,
getRevenueSharingPoolContract,
getAnniversaryAchievementContract,
getFixedStakingContract,
getVeCakeContract,
getGaugesVotingContract,
getRevenueSharingPoolGatewayContract,
getRevenueSharingVeCakeContract,
getRevenueSharingCakePoolContract,
} from 'utils/contractHelpers'

import { WNATIVE, pancakePairV2ABI } from '@pancakeswap/sdk'
import { ChainId } from '@pancakeswap/chains'
import { ifoV7ABI } from '@pancakeswap/ifos'
import { WNATIVE, pancakePairV2ABI } from '@pancakeswap/sdk'
import { CAKE } from '@pancakeswap/tokens'
import { nonfungiblePositionManagerABI } from '@pancakeswap/v3-sdk'
import { multicallABI } from 'config/abi/Multicall'
Expand Down Expand Up @@ -488,6 +489,14 @@ export const useGaugesVotingContract = () => {
return useMemo(() => getGaugesVotingContract(signer ?? undefined, chainId), [chainId, signer])
}

export const useCalcGaugesVotingContract = () => {
const { chainId } = useActiveChainId()

const { data: signer } = useWalletClient()

return useMemo(() => getCalcGaugesVotingContract(signer ?? undefined, chainId), [chainId, signer])
}

export const useRevenueSharingCakePoolContract = () => {
const { chainId } = useActiveChainId()
const { data: signer } = useWalletClient()
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/pages/api/gauges/getAllGauges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const handler: NextApiHandler = async (req, res) => {
const queryParsed = qs.parse(queryString)
const testnet = Boolean(queryParsed.testnet ?? false)
const inCap = Boolean(queryParsed.inCap ?? true)
const bothCap = Boolean(queryParsed.bothCap ?? false)

try {
const gauges = await getAllGauges(
Expand All @@ -21,6 +22,7 @@ const handler: NextApiHandler = async (req, res) => {
{
testnet,
inCap,
bothCap,
},
)

Expand All @@ -31,7 +33,7 @@ const handler: NextApiHandler = async (req, res) => {
})
} catch (err) {
return res.status(500).json({
error: err,
error: JSON.parse(stringify(err)),
})
}
}
Expand Down
7 changes: 6 additions & 1 deletion apps/web/src/pages/api/gauges/getAllGaugesCSV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getAllGauges } from '@pancakeswap/gauges'
import { NextApiHandler } from 'next'
import qs from 'qs'
import { getViemClients } from 'utils/viem.server'
import { stringify } from 'viem'

const MAX_CACHE_SECONDS = 60 * 5

Expand All @@ -14,6 +15,8 @@ const keys = [
'hash',
'chainId',
'weight',
'inCapWeight',
'notInCapWeight',
'boostMultiplier',
'maxVoteCap',
'type',
Expand All @@ -27,6 +30,7 @@ const handler: NextApiHandler = async (req, res) => {

const testnet = Boolean(queryParsed.testnet ?? false)
const inCap = Boolean(queryParsed.inCap ?? true)
const bothCap = Boolean(queryParsed.bothCap ?? false)

try {
const gauges = await getAllGauges(
Expand All @@ -36,6 +40,7 @@ const handler: NextApiHandler = async (req, res) => {
{
testnet,
inCap,
bothCap,
},
)

Expand All @@ -51,7 +56,7 @@ const handler: NextApiHandler = async (req, res) => {
return res.status(200).send(data.join('\n'))
} catch (err) {
return res.status(500).json({
error: err,
error: JSON.parse(stringify(err)),
})
}
}
Expand Down
2 changes: 0 additions & 2 deletions apps/web/src/state/vecake/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,3 @@ export const cakeLockAmountAtom = atom<string>('0')
export const cakeLockWeeksAtom = atom<string>('26')
export const cakeLockTxHashAtom = atom<`0x${string}` | ''>('')
export const cakeLockApprovedAtom = atom<boolean>(false)

export const gaugesInCapAtom = atom<boolean>(true)
4 changes: 4 additions & 0 deletions apps/web/src/utils/addressHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ export const getGaugesVotingAddress = (chainId?: number) => {
return getAddressFromMap(addresses.gaugesVoting, chainId)
}

export const getCalcGaugesVotingAddress = (chainId?: number) => {
return getAddressFromMap(addresses.gaugesVotingCalc, chainId)
}

export const getRevenueSharingCakePoolAddress = (chainId?: number) => {
return getAddressFromMap(addresses.revenueSharingCakePool, chainId)
}
Expand Down
12 changes: 11 additions & 1 deletion apps/web/src/utils/contractHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getBunnyFactoryAddress,
getCakeFlexibleSideVaultAddress,
getCakeVaultAddress,
getCalcGaugesVotingAddress,
getCrossFarmingReceiverAddress,
getCrossFarmingSenderAddress,
getFarmAuctionAddress,
Expand Down Expand Up @@ -54,6 +55,7 @@ import { predictionsV1ABI } from 'config/abi/predictionsV1'
import { stableSwapNativeHelperABI } from 'config/abi/stableSwapNativeHelper'

import { ChainId } from '@pancakeswap/chains'
import { calcGaugesVotingABI, gaugesVotingABI } from '@pancakeswap/gauges'
import { getIfoCreditAddressContract as getIfoCreditAddressContract_ } from '@pancakeswap/ifos'
import { cakeFlexibleSideVaultV2ABI, cakeVaultV2ABI } from '@pancakeswap/pools'
import { positionManagerAdapterABI, positionManagerWrapperABI } from '@pancakeswap/position-managers'
Expand All @@ -71,7 +73,6 @@ import { chainlinkOracleABI } from 'config/abi/chainlinkOracle'
import { crossFarmingReceiverABI } from 'config/abi/crossFarmingReceiver'
import { farmAuctionABI } from 'config/abi/farmAuction'
import { fixedStakingABI } from 'config/abi/fixedStaking'
import { gaugesVotingABI } from 'config/abi/gaugesVoting'
import { lotteryV2ABI } from 'config/abi/lotteryV2'
import { lpTokenABI } from 'config/abi/lpTokenAbi'
import { masterChefV2ABI } from 'config/abi/masterchefV2'
Expand Down Expand Up @@ -474,6 +475,15 @@ export const getGaugesVotingContract = (signer?: WalletClient, chainId?: number)
})
}

export const getCalcGaugesVotingContract = (signer?: WalletClient, chainId?: number) => {
return getContract({
abi: calcGaugesVotingABI,
address: getCalcGaugesVotingAddress(chainId) ?? getCalcGaugesVotingAddress(ChainId.BSC),
signer,
chainId,
})
}

export const getRevenueSharingCakePoolContract = (signer?: WalletClient, chainId?: number) => {
return getContract({
abi: revenueSharingPoolProxyABI,
Expand Down
9 changes: 2 additions & 7 deletions apps/web/src/views/GaugesVoting/hooks/useGauges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { ChainId } from '@pancakeswap/chains'
import { Gauge } from '@pancakeswap/gauges'
import { useQuery } from '@tanstack/react-query'
import { useActiveChainId } from 'hooks/useActiveChainId'
import { useAtomValue } from 'jotai'
import { gaugesInCapAtom } from 'state/vecake/atoms'

type Response = {
data: Gauge[]
Expand All @@ -12,14 +10,11 @@ type Response = {

export const useGauges = () => {
const { chainId } = useActiveChainId()
const inCap = useAtomValue(gaugesInCapAtom)

const { data, isLoading } = useQuery(
['gaugesVoting', chainId, inCap],
['gaugesVoting', chainId],
async (): Promise<Gauge[]> => {
const response = await fetch(
`/api/gauges/getAllGauges?inCap=${inCap}&testnet=${chainId === ChainId.BSC_TESTNET ? 1 : ''}`,
)
const response = await fetch(`/api/gauges/getAllGauges?testnet=${chainId === ChainId.BSC_TESTNET ? 1 : ''}`)
if (response.ok) {
const result = (await response.json()) as Response

Expand Down
11 changes: 4 additions & 7 deletions apps/web/src/views/GaugesVoting/hooks/useGaugesTotalWeight.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { useQuery } from '@tanstack/react-query'
import { useGaugesVotingContract } from 'hooks/useContract'
import { useAtomValue } from 'jotai'
import { gaugesInCapAtom } from 'state/vecake/atoms'
import { useCalcGaugesVotingContract } from 'hooks/useContract'

export const useGaugesTotalWeight = () => {
const gaugesVotingContract = useGaugesVotingContract()
const inCap = useAtomValue(gaugesInCapAtom)
const gaugesVotingContract = useCalcGaugesVotingContract()

const { data } = useQuery(['gaugesTotalWeight', gaugesVotingContract.address, inCap], async () => {
return gaugesVotingContract.read.getTotalWeight([inCap])
const { data } = useQuery(['gaugesTotalWeight', gaugesVotingContract.address], async () => {
return gaugesVotingContract.read.getTotalWeight([true])
})

return data ?? 0n
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/views/GaugesVoting/hooks/useUserVoteGauges.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { gaugesVotingABI } from '@pancakeswap/gauges'
import { useQuery } from '@tanstack/react-query'
import { gaugesVotingABI } from 'config/abi/gaugesVoting'
import useAccountActiveChain from 'hooks/useAccountActiveChain'
import { useGaugesVotingContract } from 'hooks/useContract'
import { useMemo } from 'react'
Expand Down
115 changes: 115 additions & 0 deletions packages/gauges/src/abis/calcGaugesVoting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
export const calcGaugesVotingABI = [
{
inputs: [],
name: 'gaugeVotingAddress',
outputs: [{ internalType: 'address', name: '', type: 'address' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{ internalType: 'address', name: '_gaugeAddr', type: 'address' },
{ internalType: 'uint256', name: '_chainId', type: 'uint256' },
{ internalType: 'bool', name: '_inCap', type: 'bool' },
],
name: 'getGaugeRelativeWeight',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{ internalType: 'uint256', name: '_gaugeId', type: 'uint256' },
{ internalType: 'bool', name: '_inCap', type: 'bool' },
],
name: 'getGaugeRelativeWeightById',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{ internalType: 'address', name: '_gaugeAddr', type: 'address' },
{ internalType: 'uint256', name: '_chainId', type: 'uint256' },
{ internalType: 'bool', name: '_inCap', type: 'bool' },
],
name: 'getGaugeWeight',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [{ internalType: 'uint256', name: '_gaugeId', type: 'uint256' }],
name: 'getGaugeWeightDetails',
outputs: [
{ internalType: 'uint256', name: 'gaugeWeight', type: 'uint256' },
{ internalType: 'uint256', name: 'gaugeTotalWeight', type: 'uint256' },
{ internalType: 'uint256', name: 'gaugeRawPercent', type: 'uint256' },
{ internalType: 'uint256', name: 'gaugeCappedPercent', type: 'uint256' },
{ internalType: 'uint256', name: 'gaugeInCapWeight', type: 'uint256' },
{ internalType: 'uint256', name: 'gaugeTotalFinalWeights', type: 'uint256' },
{ internalType: 'uint256', name: 'gaugeFinalPercent', type: 'uint256' },
],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{ internalType: 'address', name: '_gaugeAddr', type: 'address' },
{ internalType: 'uint256', name: '_chainId', type: 'uint256' },
{ internalType: 'bool', name: '_inCap', type: 'bool' },
{ internalType: 'uint256', name: '_gaugeTotalWeight', type: 'uint256' },
{ internalType: 'uint256', name: '_gaugeTotalCappedPercent', type: 'uint256' },
{ internalType: 'uint256', name: '_gaugeTotalFinalWeights', type: 'uint256' },
],
name: 'getGaugeWeightMass',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{ internalType: 'uint256', name: '_gaugeId', type: 'uint256' },
{ internalType: 'bool', name: '_inCap', type: 'bool' },
],
name: 'getGaugeWeightbyId',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'getRawTotalGaugeWeight',
outputs: [{ internalType: 'uint256', name: 'gaugeTotalWeight', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'getTotalCappedPercent',
outputs: [{ internalType: 'uint256', name: 'gaugeTotalCappedPercent', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'getTotalFinalWeights',
outputs: [{ internalType: 'uint256', name: 'gaugeTotalFinalWeights', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [{ internalType: 'bool', name: '_inCap', type: 'bool' }],
name: 'getTotalWeight',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [{ internalType: 'bool', name: '_inCap', type: 'bool' }],
name: 'massGetGaugeWeight',
outputs: [{ internalType: 'uint256[]', name: 'result', type: 'uint256[]' }],
stateMutability: 'view',
type: 'function',
},
] as const
Loading

1 comment on commit 418c8f7

@vercel
Copy link

@vercel vercel bot commented on 418c8f7 Dec 1, 2023

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.