 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来实现,比较简单。这有点类似缓存的意思:先从缓存中读数据,然后再慢慢更新缓存。这样就可以有效地显示和更新年化数据啰!
author | lemooljiang |
---|---|
permlink | nutbox-apy |
category | hive-105017 |
json_metadata | {"tags":["cn","blockchain","nutbox","pnut","steem","justswap","apy"],"image":["https://steemjiang.com:8081/ipfs/QmQjbPFLn49tBThSDENMoWd1ESKiZ8sv6zr7iaFFAPYfbu"],"app":"hiveblog/0.1","format":"markdown"} |
created | 2020-11-03 12:10:42 |
last_update | 2020-11-03 12:10:42 |
depth | 0 |
children | 0 |
last_payout | 2020-11-10 12:10:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 2.350 HBD |
curator_payout_value | 2.212 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 2,370 |
author_reputation | 438,608,506,193,732 |
root_title | Nutbox更新:增加计算APY |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 100,373,134 |
net_rshares | 22,451,145,843,074 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
mangou007 | 0 | 4,878,866,501 | 6.41% | ||
lemooljiang | 0 | 49,275,175,571 | 100% | ||
ace108 | 0 | 1,008,299,264,912 | 26% | ||
bullionstackers | 0 | 161,951,463,641 | 20% | ||
bert0 | 0 | 34,197,114,587 | 6.41% | ||
rivalhw | 0 | 2,577,794,655,693 | 100% | ||
nextgen622 | 0 | 1,782,904,941,175 | 100% | ||
sweetsssj | 0 | 13,712,966,974,253 | 33% | ||
penguinpablo | 0 | 794,252,311,725 | 12% | ||
funnyman | 0 | 194,283,018,483 | 4.8% | ||
dapeng | 0 | 43,519,777,462 | 100% | ||
laodr | 0 | 3,595,664,126 | 100% | ||
miniature-tiger | 0 | 98,155,060,370 | 50% | ||
htliao | 0 | 11,619,102,919 | 35% | ||
catwomanteresa | 0 | 182,153,830,618 | 50% | ||
liangfengyouren | 0 | 3,434,496,101 | 50% | ||
stevenmosoes | 0 | 559,817,804 | 50% | ||
aafeng | 0 | 279,384,905,462 | 50% | ||
cn-reader | 0 | 16,071,879,786 | 50% | ||
tvb | 0 | 38,981,892,687 | 50% | ||
karja | 0 | 110,927,827,820 | 5% | ||
kimzwarch | 0 | 11,150,012,970 | 4% | ||
yellowbird | 0 | 115,239,446,614 | 100% | ||
dancingapple | 0 | 12,132,311,723 | 50% | ||
xiaoshancun | 0 | 80,062,418,726 | 100% | ||
blc | 0 | 4,016,053,047 | 100% | ||
flamingirl | 0 | 1,668,093,253 | 6.41% | ||
lebin | 0 | 151,828,256,883 | 100% | ||
cryptonized | 0 | 63,469,356,453 | 12% | ||
fun2learn | 0 | 1,341,751,678 | 3% | ||
onepercentbetter | 0 | 15,101,444,730 | 12% | ||
competeapp | 0 | 1,082,014,211 | 100% | ||
merlion | 0 | 4,057,747,925 | 12% | ||
moneybaby | 0 | 797,477,646 | 2.5% | ||
raiseup | 0 | 58,994,764,477 | 12% | ||
steemaction | 0 | 100,698,412,593 | 12% | ||
pet.society | 0 | 176,028,658,421 | 6% | ||
memeteca | 0 | 812,951,295 | 6.41% | ||
tagalong | 0 | 1,108,838,611 | 100% | ||
hungrybear | 0 | 371,507,812 | 12% | ||
dailyke20 | 0 | 37,267,843,122 | 100% | ||
btscn | 0 | 383,728,841,759 | 100% | ||
policewala | 0 | 54,912,980,844 | 12% | ||
hivecur2 | 0 | 31,071,260,956 | 5% | ||
intacto | 0 | 34,995,355,629 | 3% |