Skip to content

Commit

Permalink
fix: Quick for for community pools APY (pancakeswap#72)
Browse files Browse the repository at this point in the history
* fix: Quick for for community pools APY

To be removed later, see the code comment.

* Fix the fix
  • Loading branch information
RabbitDoge authored Nov 9, 2020
1 parent df35a3c commit b9f1fa7
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"trailingComma": "all",
"semi": false,
"singleQuote": true
"singleQuote": true,
"printWidth": 120
}
Binary file removed public/images/tokens/category-NAR.png
Binary file not shown.
Binary file removed public/images/tokens/category-NYA.png
Binary file not shown.
Binary file removed public/images/tokens/category-STAX.png
Binary file not shown.
2 changes: 1 addition & 1 deletion src/sushi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export const getSousStaked = async (sousChefContract, account) => {
const { amount } = await sousChefContract.methods.userInfo(account).call()
return new BigNumber(amount)
} catch (err) {
console.err(err)
console.error(err)
return new BigNumber(0)
}
}
Expand Down
66 changes: 54 additions & 12 deletions src/views/CakeStaking/Syrup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,34 @@ import { getContract } from '../../utils/erc20'
import useSushi from '../../hooks/useSushi'
import useI18n from '../../hooks/useI18n'
import useAllStakedValue from '../../hooks/useAllStakedValue'
import { useTokenBalance2 } from 'hooks/useTokenBalance'
import { getPools } from '../../sushi/utils'

import PoolCardv2 from './components/PoolCardv2'
import Coming from './components/Coming'
import SyrupWarning from './components/SyrupWarning'
import { sousChefTeam } from '../../sushi/lib/constants'

const CAKE_ADDRESS = '0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82'
const COMMUNITY_ADDR = {
STAX: {
lp: '0x7cd05f8b960ba071fdf69c750c0e5a57c8366500',
token: '0x0Da6Ed8B13214Ff28e9Ca979Dd37439e8a88F6c4',
},
NAR: {
lp: '0x745c4fd226e169d6da959283275a8e0ecdd7f312',
token: '0xa1303e6199b319a891b79685f0537d289af1fc83',
},
NYA: {
lp: '0x2730bf486d658838464a4ef077880998d944252d',
token: '0xbfa0841f7a90c4ce6643f651756ee340991f99d5',
},
bROOBEE: {
lp: '0x970858016C963b780E06f7DCfdEf8e809919BcE8',
token: '0xe64f5cb844946c1f102bd25bbd87a5ab4ae89fbe',
},
}

interface SyrupRowProps {
syrupAddress: string
sousId: number
Expand All @@ -39,17 +60,34 @@ const SyrupRow: React.FC<SyrupRowProps> = ({
}) => {
const { ethereum } = useWallet()
const syrup = useMemo(() => {
return getContract(
ethereum as provider,
'0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82',
)
return getContract(ethereum as provider, '0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82')
}, [ethereum])

// /!\ Dirty fix
// The community LP are all against CAKE instead of BNB. Thus, the usual function for price computation didn't work.
// This quick fix aim to properly compute the price of CAKE pools in order to get the correct APY.
// This fix will need to be cleaned, by using config files instead of the COMMUNITY_ADDR,
// and factorise the price computation logic.

const cakeBalanceOnLP = useTokenBalance2(CAKE_ADDRESS, COMMUNITY_ADDR[tokenName]?.lp)
const tokenBalanceOnLP = useTokenBalance2(COMMUNITY_ADDR[tokenName]?.token, COMMUNITY_ADDR[tokenName]?.lp)

const price = (() => {
if (community) {
if (cakeBalanceOnLP === 0 || tokenBalanceOnLP === 0) return new BigNumber(0)
const tokenBalanceOnLP_BN = new BigNumber(tokenBalanceOnLP)
const cakeBalanceOnLP_BN = new BigNumber(cakeBalanceOnLP)
const ratio = cakeBalanceOnLP_BN.div(tokenBalanceOnLP_BN)
return ratio.times(cakePrice)
}
return tokenPrice
})()

return (
<PoolCardv2
syrup={syrup}
cakePrice={cakePrice}
tokenPrice={tokenPrice}
tokenPrice={price}
tokenPerBlock={tokenPerBlock}
{...{ sousId, tokenName, projectLink, harvest, community }}
/>
Expand All @@ -61,6 +99,7 @@ const Farm: React.FC = () => {
const TranslateString = useI18n()
const stakedValue = useAllStakedValue()
const pools = getPools(sushi) || sousChefTeam

const renderPools = useMemo(() => {
const stakedValueObj = stakedValue.reduce(
(a, b) => ({
Expand All @@ -70,12 +109,15 @@ const Farm: React.FC = () => {
{},
)

return pools.map((pool) => ({
...pool,
cakePrice: stakedValueObj['CAKE']?.tokenPriceInWeth || new BigNumber(0),
tokenPrice:
stakedValueObj[pool.tokenName]?.tokenPriceInWeth || new BigNumber(0),
}))
return pools.map((pool) => {
const cakePrice = stakedValueObj['CAKE']?.tokenPriceInWeth || new BigNumber(0)
const tokenPrice = stakedValueObj[pool.tokenName]?.tokenPriceInWeth || new BigNumber(0)
return {
...pool,
cakePrice,
tokenPrice,
}
})
}, [stakedValue, pools])

useEffect(() => {
Expand All @@ -100,7 +142,7 @@ const Farm: React.FC = () => {
</Hero>
<Pools>
{renderPools.map((pool) => (
<SyrupRow key={pool.sousId} {...pool} />
<SyrupRow key={pool.sousId} stakedValue={stakedValue} {...pool} />
))}
<Coming />
</Pools>
Expand Down
7 changes: 3 additions & 4 deletions src/views/CakeStaking/components/PoolCardv2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import React, { useCallback, useMemo, useState } from 'react'
import styled from 'styled-components'
import { useWallet } from 'use-wallet'
import { Contract } from 'web3-eth-contract'
import { COMMUNITY_FARMS } from 'sushi/lib/constants'
import { COMMUNITY_FARMS, BLOCKS_PER_YEAR } from 'sushi/lib/constants'
import Button from 'components/Button'
import HarvestButton from './HarvestButton'
import IconButton from 'components/IconButton'
import { AddIcon } from 'components/icons'
import Label from 'components/Label'
import { BLOCKS_PER_YEAR } from 'sushi/lib/constants'

import { useSousAllowance } from 'hooks/useAllowance'
import { useSousApprove } from 'hooks/useApprove'
Expand Down Expand Up @@ -93,8 +92,8 @@ const PoolCardv2: React.FC<HarvestProps> = ({
const isCommunityFarm = COMMUNITY_FARMS.includes(tokenName)

const apy = useMemo(() => {
if (!harvest || cakePrice.isLessThanOrEqualTo(0) || isCommunityFarm)
return '-'
if (!harvest || cakePrice.isLessThanOrEqualTo(0)) return '-'

const a = tokenPrice.times(BLOCKS_PER_YEAR).times(tokenPerBlock)
const b = cakePrice.times(getBalanceNumber(totalStaked))

Expand Down

0 comments on commit b9f1fa7

Please sign in to comment.