create account

Nutbox更新:增加计算APY by lemooljiang

View this thread on: hive.blogpeakd.comecency.com
· @lemooljiang ·
$4.56
Nutbox更新:增加计算APY
![apy.jpg](https://steemjiang.com:8081/ipfs/QmQjbPFLn49tBThSDENMoWd1ESKiZ8sv6zr7iaFFAPYfbu)

APY是一项重要的投资回报的数据,如果很高就会有足够的吸引力,甚至于很多人根本就不会去查考项目本身,比如之前的YAM、YFII。

Nutbox已经稳定地运行了十多天了,资金池也慢慢地涨起来,计算和呈现APY还是很有必要的。

要计算年化主要是要实时获取Pnut 和 Steem 的价格,年化大约等于Pnut的年产量除以代理SP的总价值,以公式表示:`APY = 28800 * rewardsPerBlock * 365 * pnutPrice * tronPrice / (totalDepositedSP * steemPrice)` 如果要顺利地计算出来,有两个重要的数据pnutPrice、steemPrice。

```js
async getSteemPrice(){
    let res = await this.axios.request({
        method:"get",
        url:'https://api.coingecko.com/api/v3/coins/steem',
        headers: {
        "accept": "application/json",
        }
    })
    // console.log(111,res.data.tickers)
    let arr = res.data.tickers
    for(let i = 0; i < arr.length; i++){
        if(arr[i].target === "USDT"){
        return arr[i].last
        }
    }
    },
async getTronPrice(){
    let res = await this.axios.request({
        method:"get",
        url:'https://api.coingecko.com/api/v3/coins/tron',
        headers: {
        "accept": "application/json",
        }
    })
    // console.log(111,res.data.tickers)
    let arr = res.data.tickers
    for(let i = 0; i < arr.length; i++){
        if(arr[i].target === "USDT"){
        return arr[i].last
        }
    }
    },
async getPnutPrice(){
    let res = await this.axios.request({
        method:"get",
        url:'https://api.justswap.io/v2/allpairs',
        headers: {
        "accept": "application/json",
        },
        params: {
        page_size : 1500,
        page_num: 1
        }
    })
    let price = res.data.data["0_TPZddNpQJHu8UtKPY1PYDBv2J5p5QpJ6XW"].price
    res = null
    return price
    },
async calPnutApy(){
    let steemPrice = await this.getSteemPrice()
    let tronPrice = await this.getTronPrice()
    let pnutPrice = await this.getPnutPrice()
    let apy = 28800 * this.rewardsPerBlock * 365 * pnutPrice * tronPrice / (this.totalDepositedSP2 * steemPrice)
    this.apy = (apy * 100).toFixed(3)
    localStorage.setItem('apy', this.apy)
    },
```

pnutPrice、steemPrice要从两个不同的API中获取,特别是justswap中获取数据,速度很慢,目测要十几秒!那怎么解决这个速度的问题呢?

想到的办法是初始化年化数据:**将本次的数据保存下来,下次访问时可以优先展示,而实时的数据可以在后台慢慢查,查到了再显示并且保存下来!**

```
//初始化年化收益率
if(localStorage.getItem("apy") == null){
   this.apy = 52.786
}else{
  this.apy = localStorage.getItem("apy")
}
```

用localStorage来实现,比较简单。这有点类似缓存的意思:先从缓存中读数据,然后再慢慢更新缓存。这样就可以有效地显示和更新年化数据啰!
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorlemooljiang
permlinknutbox-apy
categoryhive-105017
json_metadata{"tags":["cn","blockchain","nutbox","pnut","steem","justswap","apy"],"image":["https://steemjiang.com:8081/ipfs/QmQjbPFLn49tBThSDENMoWd1ESKiZ8sv6zr7iaFFAPYfbu"],"app":"hiveblog/0.1","format":"markdown"}
created2020-11-03 12:10:42
last_update2020-11-03 12:10:42
depth0
children0
last_payout2020-11-10 12:10:42
cashout_time1969-12-31 23:59:59
total_payout_value2.350 HBD
curator_payout_value2.212 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,370
author_reputation438,608,506,193,732
root_titleNutbox更新:增加计算APY
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id100,373,134
net_rshares22,451,145,843,074
author_curate_reward""
vote details (45)