[Javascript] STEEM POWERを表示してみました

in Steem Japan5 years ago (edited)

こんにちは、@yasuです。

ソートもできるようにしました^^

実行方法

https://ojagggyo.github.io/steemitapi/steempower.html

実行結果

image.png

ソース

html
<!doctype html>
<html lang="en">
    <head>
        <title>steempower</title>
        <meta charset="utf-8">
        <script src="https://github.com/steemit/steem-js/releases/download/v0.7.7/steem.min.js"></script>
        <script src="https://ojagggyo.github.io/steemitapi/steempower.js"></script>
    </head>
    <body>        
        <p>アカウント名を入力してください。</p>
        <textarea id="text1" rows="10" cols="20">yasu
japansteemit</textarea>
        <br/>
        <input type="button" value="STEEM POWERを表示する" onclick="clickBtn()" /><a id="progress"></a>
        <span id="text"></span>
    </body>
</html>
javascript
async function aaa(usernames){
  let items = [];
  let globalProperties = await steem.api.getDynamicGlobalPropertiesAsync();
  let total_vesting_shares = parseFloat(globalProperties.total_vesting_shares.replace(" VESTS", ""));
  let total_vesting_fund_steem = parseFloat(globalProperties.total_vesting_fund_steem.replace(" STEEM", ""));
  let k = total_vesting_fund_steem / total_vesting_shares;
  let accounts = await steem.api.getAccountsAsync(usernames)
  for(var i=0;i<accounts.length;i=i+1){
    let vesting_shares = parseFloat(accounts[i].vesting_shares.replace(" VESTS", ""));
    let received_vesting_shares = parseFloat(accounts[i].received_vesting_shares.replace(" VESTS", ""));
    let delegated_vesting_shares = parseFloat(accounts[i].delegated_vesting_shares.replace(" VESTS", ""));
    let item = {};
    sp = vesting_shares * k;//保持しているSP
    sp1 = received_vesting_shares * k;//委任されたSP
    sp2 = delegated_vesting_shares * k;//委任したSP
    item['name'] = accounts[i].name;
    item['sp'] = sp.toFixed(3);
    item['received_sp'] = sp1.toFixed(3);
    item['delegated_sp'] = sp2.toFixed(3);
    item['effective_sp'] = (sp + sp1 - sp2).toFixed(3);
    items.push(item);
  }
  return items;
}

function clickBtn(){
    let t1 = document.getElementById("text1").value;
    let usernames = t1.split(/\n/);//改行で分割する
    aaa(usernames).then(result => { 
        _stok = result;
        makeTable(result);
    }).catch(err => {
    });
}

//ソート
function effective_sp(){
    if(_stok.length < 2) return;
    if(parseFloat(_stok[0].effective_sp) > parseFloat(_stok[_stok.length - 1].effective_sp)) {
        k = -1;
    }else{
        k = 1;
    }
    _stok = _stok.sort(function(a,b){
        if(parseFloat(a.effective_sp) < parseFloat(b.effective_sp)) return k;
        if(parseFloat(a.effective_sp) > parseFloat(b.effective_sp)) return -k;
        return 0;
    });
    makeTable(_stok);
}
    
function makeTable(records){
    let html = '<table border=1 >';
    //テーブルのヘッダー
    html = html + '<tr>';
    html = html + '<th>name</th><th><a href=javascript:effective_sp();>effective sp</a></th><th>sp</th><th>received sp</th><th>delegated sp</th>';
    html = html + '</tr>';
    for(let i=0; i<records.length; i=i+1){
        html = html + '<tr>';
        html = html + '<td>' + records[i].name + '</td>';//
        html = html + '<td align=right>' + records[i].effective_sp + '</a></td>';
        html = html + '<td align=right>' + records[i].sp + '</td>';
        html = html + '<td align=right>' + records[i].received_sp + '</td>';
        html = html + '<td align=right>' + records[i].delegated_sp + '</td>';
    }
    html = html + '</table>';
    document.getElementById("text").innerHTML = html;
}

steem.api.setOptions({url: 'https://api.steemit.com'})
let _stok ;
Sort:  
 5 years ago 

@yasu さん、こんにちは。

image.png
Steemitチームがアップボートしてくれるので #steemexclusive のタグ、 パワーアップしたときは #club5050 のタグを使って下さい。
Power Up and use #club5050 #steemexclusive for more upvotes from Steemit team.
How to use #steemexclusive 使い方
How to use #club5050 使い方

💡 Curation Guide (アップボートガイド) 💡

  • Post about Japan (Japanese or English)
  • Set 10~30% reward to japansteemit
  • No upvote if you power down 100%
  • Upvote community posts
  • Follow our Curation Trail HERE
  • Delegate SP 100 SP 500 SP 1000 SP 2000 SP

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.083
BTC 64095.69
ETH 1726.16
USDT 1.00
SBD 0.42