Skip to content

Commit

Permalink
feat: add pcs reward per year
Browse files Browse the repository at this point in the history
  • Loading branch information
Coac committed Mar 13, 2021
1 parent 638ef3a commit 5f2f70c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
82 changes: 43 additions & 39 deletions src/providers/bsc/MasterChef.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,31 @@ class MasterChef {

async stakedPool (poolID, walletAddress, tx) {
let lpTokenAmount = await this.getStakedLPAmount(poolID, walletAddress)
if ((new BigNumber(Web3.utils.fromWei(lpTokenAmount))).comparedTo(0.01) === -1) {
lpTokenAmount = parseFloat(Web3.utils.fromWei(lpTokenAmount))

if ((new BigNumber(lpTokenAmount)).comparedTo(0.01) === -1) {
return
}

const pendingReward = await this.getPendingReward(poolID, walletAddress)

const { 0: lpAddress } = await this.contract.methods.poolInfo(poolID).call()
const tokens = await getTokensEquivalent(this.web3, lpAddress, lpTokenAmount)
const { 0: lpAddress, allocPoint } = await this.contract.methods.poolInfo(poolID).call()

const isLP = await LiquidityPool.isLP(this.web3, lpAddress)
const Pool = isLP ? LiquidityPool : Token
const pool = new Pool(this.web3, lpAddress)
await pool.init()
const tokens = pool.stakedTokenEquivalent(lpTokenAmount)

const lpTransactions = this.lpTransactions(tx, poolID)
const totalDeposited = MasterChef.totalDeposited(lpTransactions)

lpTokenAmount = parseFloat(Web3.utils.fromWei(lpTokenAmount))
const poolRewardPerBlock = Web3.utils.fromWei(BigNumber(allocPoint).dividedBy(this.totalAllocPoint).multipliedBy(this.rewardPerBlock).integerValue().toFixed())
const userPoolRewardPerBlock = poolRewardPerBlock * pool.share(lpTokenAmount)
const rewardPerYear = userPoolRewardPerBlock * 3600 * 24 * 365 / 3

return {
poolID, lpTokenAmount, totalDeposited, pendingReward, tokens, lpTransactions
poolID, lpTokenAmount, totalDeposited, pendingReward, tokens, rewardPerYear, lpAddress, lpTransactions
}
}

Expand All @@ -89,6 +99,13 @@ class MasterChef {

async listStakedPools (walletAddress, tx) {
const poolLength = await this.getPoolLength()
this.totalAllocPoint = await this.contract.methods.totalAllocPoint().call()

try {
this.rewardPerBlock = await this.contract.methods.cakePerBlock().call() // TODO change reward per block
} catch (e) {
this.rewardPerBlock = 0
}

let pools = []
for (let poolID = 1; poolID < poolLength; poolID++) {
Expand All @@ -108,11 +125,11 @@ class LiquidityPool {
this.web3 = web3
}

async refresh () {
this.totalSupply = await this.contract.methods.totalSupply().call()
async init () {
this.totalSupply = Web3.utils.fromWei(await this.contract.methods.totalSupply().call())
const reserves = await this.contract.methods.getReserves().call()
this.reserves0 = reserves._reserve0
this.reserves1 = reserves._reserve1
this.reserves0 = Web3.utils.fromWei(reserves._reserve0)
this.reserves1 = Web3.utils.fromWei(reserves._reserve1)

const token0Address = await this.contract.methods.token0().call()
this.token0 = await (new Token(this.web3, token0Address)).init()
Expand All @@ -121,11 +138,24 @@ class LiquidityPool {
this.token1 = await (new Token(this.web3, token1Address)).init()
}

tokenAmount (lpTokenAmount) {
/**
* Calculate the number of tokens equivalent of the LP token amount
* @param {string} lpTokenAmount - LP Token amount
* @returns {object} Returns the equivalent number of tokens.
*/
stakedTokenEquivalent (lpTokenAmount) {
const proportion = BigNumber(lpTokenAmount).dividedBy(this.totalSupply)
const token0Amount = Web3.utils.fromWei(proportion.multipliedBy(this.reserves0).integerValue().toFixed())
const token1Amount = Web3.utils.fromWei(proportion.multipliedBy(this.reserves1).integerValue().toFixed())
return { token0Amount, token1Amount }
const token0Amount = proportion.multipliedBy(this.reserves0).toFixed()
const token1Amount = proportion.multipliedBy(this.reserves1).toFixed()

const result = {}
result[this.token0.symbol] = parseFloat(token0Amount)
result[this.token1.symbol] = parseFloat(token1Amount)
return result
}

share (lpTokenAmount) {
return lpTokenAmount / this.totalSupply
}

static async isLP (web3, address) {
Expand All @@ -139,30 +169,4 @@ class LiquidityPool {
}
}

/**
* Calculate the number of tokens equivalent of the LP token amount
* @param {string} lpAddress - Liquidity pool address
* @param {string} lpTokenAmount - LP Token amount
* @returns {object} Returns the equivalent number of tokens.
*/
async function getTokensEquivalent (web3, lpAddress, lpTokenAmount) {
const isLP = await LiquidityPool.isLP(web3, lpAddress)
if (!isLP) {
// Single asset vault
const token = new Token(web3, lpAddress)
await token.init()
const result = {}
result[token.symbol] = parseFloat(Web3.utils.fromWei(lpTokenAmount))
return result
}

const lp = new LiquidityPool(web3, lpAddress)
await lp.refresh()
const amounts = lp.tokenAmount(lpTokenAmount)
const result = {}
result[lp.token0.symbol] = parseFloat(amounts.token0Amount)
result[lp.token1.symbol] = parseFloat(amounts.token1Amount)
return result
}

module.exports = MasterChef
10 changes: 10 additions & 0 deletions src/providers/bsc/Token.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ class Token {
this.decimals = await this.contract.methods.decimals().call()
return this
}

stakedTokenEquivalent (lpTokenAmount) {
const result = {}
result[this.symbol] = parseFloat(lpTokenAmount)
return result
}

share (lpTokenAmount) {
return 0 // TODO implement this
}
}

module.exports = Token

0 comments on commit 5f2f70c

Please sign in to comment.