Skip to content

Commit

Permalink
Add basic APR calculation to poolpair-controller (#323)
Browse files Browse the repository at this point in the history
* Added basic APR

* Fix client tests

* PR suggestions

* PR suggestions

* Fix code smell

* PR suggestions

* Update packages/whale-api-client/src/api/poolpairs.ts

* Add error for non dfi

* Refactor code smell
Refactor types to be cleaner by referencing instead of declaring
Error should not fail but return undefined
Should return fractional number not percentages
Updated ttl to 60 minutes

Co-authored-by: Fuxing Loh <[email protected]>
Co-authored-by: Fuxing Loh <[email protected]>
  • Loading branch information
3 people authored Aug 6, 2021
1 parent a1ada0d commit 042f2b5
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 5 deletions.
13 changes: 12 additions & 1 deletion apps/whale-api/src/module.api/poolpair.controller.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ async function setup (): Promise<void> {
amountB: 431.51288,
shareAddress: await getNewAddress(container)
})

await container.call('setgov', [{ LP_SPLITS: { 8: 1.0 } }])
await container.generate(1)
}

describe('list', () => {
Expand All @@ -100,6 +103,10 @@ describe('list', () => {
reserve: '300',
blockCommission: '0'
},
apr: {
reward: 2229.42,
total: 2229.42
},
commission: '0',
totalLiquidity: {
token: '122.47448713',
Expand All @@ -111,7 +118,7 @@ describe('list', () => {
ab: '0.16666666',
ba: '6'
},
rewardPct: '0',
rewardPct: '1',
customRewards: undefined,
creation: {
tx: expect.any(String),
Expand Down Expand Up @@ -172,6 +179,10 @@ describe('get', () => {
reserve: '200',
blockCommission: '0'
},
apr: {
reward: 0,
total: 0
},
commission: '0',
totalLiquidity: {
token: '141.42135623',
Expand Down
8 changes: 8 additions & 0 deletions apps/whale-api/src/module.api/poolpair.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ describe('list', () => {
reserve: '300',
blockCommission: '0'
},
apr: {
reward: 0,
total: 0
},
commission: '0',
totalLiquidity: {
token: '122.47448713',
Expand Down Expand Up @@ -208,6 +212,10 @@ describe('get', () => {
reserve: '200',
blockCommission: '0'
},
apr: {
reward: 0,
total: 0
},
commission: '0',
totalLiquidity: {
token: '141.42135623',
Expand Down
11 changes: 7 additions & 4 deletions apps/whale-api/src/module.api/poolpair.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export class PoolPairController {
const items: PoolPairData[] = []
for (const [id, info] of Object.entries(result)) {
const totalLiquidityUsd = await this.poolPairService.getTotalLiquidityUsd(info)
items.push(mapPoolPair(id, info, totalLiquidityUsd))
const apr = await this.poolPairService.getAPR(info)
items.push(mapPoolPair(id, info, totalLiquidityUsd, apr))
}

return ApiPagedResponse.of(items, query.size, item => {
Expand All @@ -56,11 +57,12 @@ export class PoolPairController {
}

const totalLiquidityUsd = await this.poolPairService.getTotalLiquidityUsd(info)
return mapPoolPair(String(id), info, totalLiquidityUsd)
const apr = await this.poolPairService.getAPR(info)
return mapPoolPair(String(id), info, totalLiquidityUsd, apr)
}
}

export function mapPoolPair (id: string, info: PoolPairInfo, totalLiquidityUsd?: BigNumber): PoolPairData {
function mapPoolPair (id: string, info: PoolPairInfo, totalLiquidityUsd?: BigNumber, apr?: PoolPairData['apr']): PoolPairData {
return {
id: id,
symbol: info.symbol,
Expand Down Expand Up @@ -92,6 +94,7 @@ export function mapPoolPair (id: string, info: PoolPairInfo, totalLiquidityUsd?:
creation: {
tx: info.creationTx,
height: info.creationHeight.toNumber()
}
},
apr
}
}
73 changes: 73 additions & 0 deletions apps/whale-api/src/module.api/poolpair.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { JsonRpcClient } from '@defichain/jellyfish-api-jsonrpc'
import BigNumber from 'bignumber.js'
import { PoolPairInfo } from '@defichain/jellyfish-api-core/dist/category/poolpair'
import { SemaphoreCache } from '@src/module.api/cache/semaphore.cache'
import { PoolPairData } from '@whale-api-client/api/poolpairs'

@Injectable()
export class PoolPairService {
Expand Down Expand Up @@ -74,4 +75,76 @@ export class PoolPairService {
ttl: 180
})
}

private async getDailyDFIReward (): Promise<BigNumber | undefined> {
return await this.cache.get<BigNumber>('LP_DAILY_DFI_REWARD', async () => {
const rpcResult = await this.rpcClient.masternode.getGov('LP_DAILY_DFI_REWARD')
return new BigNumber(rpcResult.LP_DAILY_DFI_REWARD)
}, {
ttl: 3600 // 60 minutes
})
}

private async getYearlyCustomRewardUSD (info: PoolPairInfo): Promise<BigNumber | undefined> {
if (info.customRewards === undefined) {
return new BigNumber(0)
}

const dfiPriceUsdt = await this.getUSDT_PER_DFI()
if (dfiPriceUsdt === undefined) {
return undefined
}

return info.customRewards.reduce<BigNumber>((accum, customReward) => {
const [reward, token] = customReward.split('@')
if (token !== '0' && token !== 'DFI') {
// Unhandled if not DFI
return accum
}

const yearly = new BigNumber(reward)
.times(60 * 60 * 24 / 30) // 30 seconds = 1 block
.times(365) // 1 year
.times(dfiPriceUsdt)

return accum.plus(yearly)
}, new BigNumber(0))
}

private async getYearlyRewardPCTUSD (info: PoolPairInfo): Promise<BigNumber | undefined> {
if (info.rewardPct === undefined) {
return new BigNumber(0)
}

const dfiPriceUsdt = await this.getUSDT_PER_DFI()
const dailyDfiReward = await this.getDailyDFIReward()

if (dfiPriceUsdt === undefined || dailyDfiReward === undefined) {
return undefined
}

return info.rewardPct
.times(dailyDfiReward)
.times(365)
.times(dfiPriceUsdt)
}

async getAPR (info: PoolPairInfo): Promise<PoolPairData['apr'] | undefined> {
const customUSD = await this.getYearlyCustomRewardUSD(info)
const pctUSD = await this.getYearlyRewardPCTUSD(info)
const totalLiquidityUSD = await this.getTotalLiquidityUsd(info)

if (customUSD === undefined || pctUSD === undefined || totalLiquidityUSD === undefined) {
return undefined
}

const yearlyUSD = customUSD.plus(pctUSD)
// 1 == 100%, 0.1 = 10%
const apr = yearlyUSD.div(totalLiquidityUSD).toNumber()

return {
reward: apr,
total: apr
}
}
}

0 comments on commit 042f2b5

Please sign in to comment.