Skip to content

Commit

Permalink
Merge pull request #2292 from keep-network/apy-calc-update
Browse files Browse the repository at this point in the history
Fetch liquidity reward per week from contract

We can compute weekly rewards based on data in LPRewards contract
instead of hardcoded values -> LPReward.rewardRate * 7 days in seconds
  • Loading branch information
michalsmiarowski authored Jan 18, 2021
2 parents 79c9100 + 7fd2e47 commit 30c608f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
3 changes: 0 additions & 3 deletions solidity/dashboard/src/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,20 @@ export const LIQUIDITY_REWARD_PAIRS = {
label: "KEEP + ETH",
viewPoolLink:
"https://info.uniswap.org/pair/0xe6f19dab7d43317344282f803f8e8d240708174a",
rewardPoolPerWeek: 150000,
address: "0xe6f19dab7d43317344282f803f8e8d240708174a",
},
KEEP_TBTC: {
contractName: LP_REWARDS_KEEP_TBTC_CONTRACT_NAME,
label: "KEEP + TBTC",
viewPoolLink:
"https://info.uniswap.org/pair/0x38c8ffee49f286f25d25bad919ff7552e5daf081",
rewardPoolPerWeek: 200000,
address: "0x38c8ffee49f286f25d25bad919ff7552e5daf081",
},
TBTC_ETH: {
contractName: LP_REWARDS_TBTC_ETH_CONTRACT_NAME,
label: "TBTC + ETH",
viewPoolLink:
"https://info.uniswap.org/pair/0x854056fd40c1b52037166285b2e54fee774d33f6",
rewardPoolPerWeek: 50000,
address: "0x854056fd40c1b52037166285b2e54fee774d33f6",
},
}
14 changes: 12 additions & 2 deletions solidity/dashboard/src/sagas/liquidity-rewards.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ function* fetchLiquidityRewardsData(liquidityRewardPair, address) {
// Fetching total deposited liqidity tokens in the `LPRewards` contract.
const totalSupply = yield call(fetchLPRewardsTotalSupply, LPRewardsContract)
if (gt(totalSupply, 0)) {
apy = yield call(calculateAPY, totalSupply, liquidityRewardPair.name)
apy = yield call(
calculateAPY,
totalSupply,
liquidityRewardPair.name,
LPRewardsContract
)
}

let reward = 0
Expand Down Expand Up @@ -151,7 +156,12 @@ function* fetchLiquidityRewardsAPY(liquidityRewardPair) {
let apy = Infinity
const totalSupply = yield call(fetchLPRewardsTotalSupply, LPRewardsContract)
if (gt(totalSupply, 0)) {
apy = yield call(calculateAPY, totalSupply, liquidityRewardPair.name)
apy = yield call(
calculateAPY,
totalSupply,
liquidityRewardPair.name,
LPRewardsContract
)
}

yield put({
Expand Down
7 changes: 6 additions & 1 deletion solidity/dashboard/src/sagas/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,12 @@ function* lpTokensStakedOrWithdrawn(
const { user, amount } = eventValues
const totalSupply = yield call(fetchLPRewardsTotalSupply, LPRewardsContract)

const apy = yield call(calculateAPY, totalSupply, liquidityRewardPairName)
const apy = yield call(
calculateAPY,
totalSupply,
liquidityRewardPairName,
LPRewardsContract
)

const reward = yield call(
fetchRewardBalance,
Expand Down
16 changes: 13 additions & 3 deletions solidity/dashboard/src/services/liquidity-rewards.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import BigNumber from "bignumber.js"
import { LIQUIDITY_REWARD_PAIRS } from "../constants/constants"
import { toTokenUnit } from "../utils/token.utils"
import { getPairData, getKeepTokenPriceInUSD } from "./uniswap-api"
import moment from "moment"

// lp contract address -> wrapped ERC20 token as web3 contract instance
const LPRewardsToWrappedTokenCache = {}
Expand Down Expand Up @@ -49,13 +50,22 @@ export const fetchRewardBalance = async (address, LPrewardsContract) => {
return await LPrewardsContract.methods.earned(address).call()
}

export const calculateAPY = async (totalSupplyOfLPRewards, pairSymbol) => {
export const fetchRewardRate = async (LPRewardsContract) => {
return await LPRewardsContract.methods.rewardRate().call()
}

export const calculateAPY = async (
totalSupplyOfLPRewards,
pairSymbol,
LPRewardsContract
) => {
totalSupplyOfLPRewards = toTokenUnit(totalSupplyOfLPRewards)

const pairData = await getPairData(LIQUIDITY_REWARD_PAIRS[pairSymbol].address)
const rewardRate = await fetchRewardRate(LPRewardsContract)

const rewardPoolPerWeek = new BigNumber(
LIQUIDITY_REWARD_PAIRS[pairSymbol].rewardPoolPerWeek
const rewardPoolPerWeek = toTokenUnit(rewardRate).multipliedBy(
moment.duration(7, "days").asSeconds()
)

const totalLPTokensInLPRewardsInUSD = totalSupplyOfLPRewards
Expand Down

0 comments on commit 30c608f

Please sign in to comment.