Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: evergreen fox farming #7749

Merged
merged 5 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
652 changes: 652 additions & 0 deletions packages/contracts/src/abis/evergreenFarming.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/contracts/src/abis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './arbRetryableTx'
export * from './arbSys'
export * from './iBep20'
export * from './farming'
export * from './evergreenFarming'
export * from './foxy'
export * from './foxyStaking'
export * from './l1ArbitrumGateway'
Expand Down
5 changes: 5 additions & 0 deletions packages/contracts/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type Address, erc20Abi } from 'viem'

import { EVERGREEN_FARMING_ABI } from './abis/evergreenFarming'
import { FARMING_ABI } from './abis/farming'
import { I_UNISWAP_V2_PAIR_ABI } from './abis/iUniswapV2Pair'
import { THORCHAIN_ROUTER_ABI } from './abis/thorchainRouter'
Expand All @@ -19,6 +20,8 @@ export const ETH_FOX_STAKING_V6_CONTRACT = '0xebb1761ad43034fd7faa64d84e5bbd8cb5
export const ETH_FOX_STAKING_V7_CONTRACT = '0x5939783dbf3e9f453a69bc9ddc1e492efac1fbcb' as const
export const ETH_FOX_STAKING_V8_CONTRACT = '0x662da6c777a258382f08b979d9489c3fbbbd8ac3' as const
export const ETH_FOX_STAKING_V9_CONTRACT = '0x721720784b76265aa3e34c1c7ba02a6027bcd3e5' as const
export const ETH_FOX_STAKING_EVERGREEN_CONTRACT =
'0xe7e16e2b05440c2e484c5c41ac3e5a4d15da2744' as const

export const foxEthStakingContractAddresses = [
ETH_FOX_STAKING_V9_CONTRACT,
Expand All @@ -30,6 +33,7 @@ export const foxEthStakingContractAddresses = [
ETH_FOX_STAKING_V3_CONTRACT,
ETH_FOX_STAKING_V2_CONTRACT,
ETH_FOX_STAKING_V1_CONTRACT,
ETH_FOX_STAKING_EVERGREEN_CONTRACT,
] as const

// Exported as a string literal for contract address discrimination purposes
Expand Down Expand Up @@ -67,6 +71,7 @@ export const CONTRACT_ADDRESS_TO_ABI = {
[ETH_FOX_STAKING_V7_CONTRACT]: FARMING_ABI,
[ETH_FOX_STAKING_V8_CONTRACT]: FARMING_ABI,
[ETH_FOX_STAKING_V9_CONTRACT]: FARMING_ABI,
[ETH_FOX_STAKING_EVERGREEN_CONTRACT]: EVERGREEN_FARMING_ABI,
[FOX_TOKEN_CONTRACT]: erc20Abi,
[UNISWAP_V2_ROUTER_02_CONTRACT_MAINNET]: UNISWAP_V2_ROUTER_02_ABI,
// THOR Router Mainnet
Expand Down
7 changes: 6 additions & 1 deletion packages/contracts/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GetContractReturnType, PublicClient } from 'viem'
import type { Address, GetContractReturnType, PublicClient } from 'viem'

import type { EVERGREEN_FARMING_ABI, FARMING_ABI } from './abis'
import type {
CONTRACT_ADDRESS_TO_ABI,
CONTRACT_TYPE_TO_ABI,
Expand Down Expand Up @@ -46,3 +47,7 @@ export type DefinedContract = {
}

export type FoxEthStakingContractAddress = (typeof foxEthStakingContractAddresses)[number]
export type FoxEthStakingContractAbi = typeof FARMING_ABI | typeof EVERGREEN_FARMING_ABI
export type FoxEthStakingContract<T extends FoxEthStakingContractAbi> = T extends typeof FARMING_ABI
? GetContractReturnType<typeof FARMING_ABI, PublicClient, Address>
: GetContractReturnType<typeof EVERGREEN_FARMING_ABI, PublicClient, Address>
15 changes: 8 additions & 7 deletions src/pages/Defi/components/EligibleCarousel.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CardProps } from '@chakra-ui/react'
import { Button, Card, Flex, Heading } from '@chakra-ui/react'
import { ETH_FOX_STAKING_V9_CONTRACT } from '@shapeshiftoss/contracts'
import { ETH_FOX_STAKING_EVERGREEN_CONTRACT } from '@shapeshiftoss/contracts'
import { uniqBy } from 'lodash'
import { useCallback, useMemo } from 'react'
import { useTranslate } from 'react-polyglot'
Expand Down Expand Up @@ -50,29 +50,30 @@ export const EligibleCarousel: React.FC<EligibleCarouselProps> = props => {
)
.slice(0, 5)

const foxFarmingV9 = eligibleOpportunities.find(
eligibleOpportunity => eligibleOpportunity.contractAddress === ETH_FOX_STAKING_V9_CONTRACT,
const foxFarmingEvergreen = eligibleOpportunities.find(
eligibleOpportunity =>
eligibleOpportunity.contractAddress === ETH_FOX_STAKING_EVERGREEN_CONTRACT,
)

const rfoxOpportunity = eligibleOpportunities.find(
eligibleOpportunity => eligibleOpportunity.provider === DefiProvider.rFOX,
)

if (!foxFarmingV9 && !rfoxOpportunity) {
if (!foxFarmingEvergreen && !rfoxOpportunity) {
return filteredEligibleOpportunities
}

const filteredEligibleOpportunitiesWithFoxFarmingV9 = uniqBy(
const filteredEligibleOpportunitiesWithFoxFarmingEvergreen = uniqBy(
[
rfoxOpportunity,
filteredEligibleOpportunities[0],
foxFarmingV9,
foxFarmingEvergreen,
...filteredEligibleOpportunities.slice(1),
].filter(isSome),
'contractAddress',
).slice(0, 5)

return filteredEligibleOpportunitiesWithFoxFarmingV9
return filteredEligibleOpportunitiesWithFoxFarmingEvergreen
}, [eligibleOpportunities])

const renderEligibleCards = useMemo(() => {
Expand Down
8 changes: 7 additions & 1 deletion src/state/slices/opportunitiesSlice/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { AssetId } from '@shapeshiftoss/caip'
import { ethAssetId, foxAssetId, foxOnArbitrumOneAssetId } from '@shapeshiftoss/caip'
import type { FoxEthStakingContractAddress } from '@shapeshiftoss/contracts'
import { foxEthStakingContractAddresses } from '@shapeshiftoss/contracts'
import {
ETH_FOX_STAKING_EVERGREEN_CONTRACT,
foxEthStakingContractAddresses,
} from '@shapeshiftoss/contracts'
import { getTypeGuardAssertion } from 'lib/utils'

import type { DefiProviderMetadata, LpId, StakingId } from './types'
Expand Down Expand Up @@ -45,6 +48,7 @@ export const foxEthStakingAssetIdV8: AssetId =
'eip155:1/erc20:0x662da6c777a258382f08b979d9489c3fbbbd8ac3'
export const foxEthStakingAssetIdV9: AssetId =
'eip155:1/erc20:0x721720784b76265aa3e34c1c7ba02a6027bcd3e5'
export const foxEthStakingAssetIdEvergreen: AssetId = `eip155:1/erc20:${ETH_FOX_STAKING_EVERGREEN_CONTRACT}`

// Tuple of all staking contracts as AssetIds, to iterate over and dispatch RTK queries for
export const foxEthAssetIds = [
Expand All @@ -57,6 +61,7 @@ export const foxEthAssetIds = [
foxEthStakingAssetIdV7,
foxEthStakingAssetIdV8,
foxEthStakingAssetIdV9,
// foxEthStakingAssetIdEvergreen, // TODO: Uncomment when Evergreen staking is ready for go-live
] as const
export const foxEthStakingIds = foxEthAssetIds as readonly StakingId[]

Expand All @@ -70,6 +75,7 @@ export const STAKING_ID_TO_VERSION = {
[foxEthStakingAssetIdV7]: 'V7',
[foxEthStakingAssetIdV8]: 'V8',
[foxEthStakingAssetIdV9]: 'V9',
[foxEthStakingAssetIdEvergreen]: 'Evergreen',
}

export const rFOXStakingIds = [foxOnArbitrumOneAssetId] as readonly StakingId[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { foxAssetId, fromAccountId, fromAssetId } from '@shapeshiftoss/caip'
import type { FoxEthStakingContract, FoxEthStakingContractAbi } from '@shapeshiftoss/contracts'
import {
ETH_FOX_POOL_CONTRACT,
fetchUniV2PairData,
Expand Down Expand Up @@ -53,7 +54,9 @@ export const ethFoxStakingMetadataResolver = async ({
}

assertIsFoxEthStakingContractAddress(contractAddress)
const foxFarmingContract = getOrCreateContractByAddress(contractAddress)
const foxFarmingContract = getOrCreateContractByAddress(
contractAddress,
) as FoxEthStakingContract<FoxEthStakingContractAbi>
const uniV2LPContract = getOrCreateContractByAddress(ETH_FOX_POOL_CONTRACT)

// tvl
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { FARMING_ABI } from '@shapeshiftoss/contracts'
import type { FoxEthStakingContract, FoxEthStakingContractAbi } from '@shapeshiftoss/contracts'
import { memoize } from 'lodash'
import type { Address, GetContractReturnType, PublicClient } from 'viem'
import { bnOrZero } from 'lib/bignumber/bignumber'

export const makeTotalLpApr = (foxRewardRatePerToken: string, foxEquivalentPerLPToken: string) =>
Expand All @@ -15,15 +14,12 @@ export const makeTotalLpApr = (foxRewardRatePerToken: string, foxEquivalentPerLP

// Rate of FOX given per second for all staked addresses)
const getRewardsRate = memoize(
async (
farmingRewardsContract: GetContractReturnType<typeof FARMING_ABI, PublicClient, Address>,
) => await farmingRewardsContract.read.rewardRate(),
async <T extends FoxEthStakingContractAbi>(farmingRewardsContract: FoxEthStakingContract<T>) =>
await farmingRewardsContract.read.rewardRate(),
)

const getTotalLpSupply = memoize(
async (
farmingRewardsContract: GetContractReturnType<typeof FARMING_ABI, PublicClient, Address>,
) => {
async <T extends FoxEthStakingContractAbi>(farmingRewardsContract: FoxEthStakingContract<T>) => {
try {
const totalSupply = await farmingRewardsContract.read.totalSupply()
return bnOrZero(totalSupply.toString())
Expand All @@ -36,9 +32,7 @@ const getTotalLpSupply = memoize(
)

export const rewardRatePerToken = memoize(
async (
farmingRewardsContract: GetContractReturnType<typeof FARMING_ABI, PublicClient, Address>,
) => {
async <T extends FoxEthStakingContractAbi>(farmingRewardsContract: FoxEthStakingContract<T>) => {
try {
const rewardRate = await getRewardsRate(farmingRewardsContract)
const totalSupply = await getTotalLpSupply(farmingRewardsContract)
Expand Down
Loading