PHP如何计算当前账号Upvote的价值

in #cn-dev5 years ago (edited)

由于最近开发需要计算指定账号的赞的价值,因此研究了下。

代码如下:

<?php
    function postData($data) {
        $url = 'https://api.steemit.com';
        $options = array(
          'http' =>
            array(
              'header' => "Content-Type: application/json\r\n".
                          "Content-Length: ".strlen($data)."\r\n".
                          "User-Agent:SteemTools/1.0\r\n",
              'method'  => 'POST',
              'content' => $data,
            )
        );
        $context  = stream_context_create($options);
        try {
            $result = file_get_contents($url, false, $context);
            $r = json_decode($result, true);
            $result = $r['result'];
        } catch (\Exception $e) {
            return false;
        }
        return $result;
    }

    function getRewardFund() {
        $result = postData('{"id":0,"jsonrpc":"2.0","method":"call","params":["database_api","get_reward_fund",["post"]]}');
        if (isset($result['reward_balance'])) {
            $result['reward_balance'] = str_replace(' STEEM', '', $result['reward_balance']);
        }
        return $result;
    }

    function getAccountVests($username) {
        $result = postData('{"jsonrpc":"2.0", "method":"condenser_api.get_accounts", "params":[["'.trim($username).'"]], "id":1}');
        return str_replace(' VESTS', '', $result[0]['vesting_shares']) + str_replace(' VESTS', '', $result[0]['received_vesting_shares']) - str_replace(' VESTS', '', $result[0]['delegated_vesting_shares']);
    }

    function getCurrentMedianHistoryPrice() {
        $result = postData('{"id":1,"jsonrpc":"2.0","method":"call","params":["database_api","get_current_median_history_price",[]]}');
        return str_replace(' SBD', '', $result['base']) / str_replace(' STEEM', '', $result['quote']);
    }

    function getAccountUpvoteValue($username, $vp, $weight) {
        $power = (100*$vp * 100*$weight / 1e4 + 49) / 50;
        $total_vests = getAccountVests($username);
        $final_vests = $total_vests * 1e6;
        $rshares = $power * $final_vests / 1e4;
        $rewards = getRewardFund();
        $sbd_median_price = getCurrentMedianHistoryPrice();
        $estimate = $rshares / $rewards['recent_claims'] * $rewards['reward_balance'] * $sbd_median_price;
        return $estimate;
    }

    // var_dump(getRewardFund());
    // var_dump(getAccountVests('ety001'));
    // var_dump(getCurrentMedianHistoryPrice());
    var_dump(getAccountUpvoteValue('ety001', 100, 100));

官网的公式已经很明确了,其中遇到的问题就是 voting powerweight 的精度问题了。由于在官网公式上并没有指明,所以很容易被忽略。

在我的代码中,很清楚的看到我传入的 voting power90weight100,由于 Steem 在百分比上的精度是下面这样子的:

100.00%

而存储在区块上的是整数 10000。因此官方给出的代码中,power = (voting_power * weight / 10000) / 50 要看你传入是什么精度的数据了。按照我的传参,我必须要给传入的值再乘以100才是正确结果,也就是我的代码是 $power = (100*$vp * 100*$weight / 1e4 + 49) / 50;。其中的 +49 是挺高精度的一种方式,可以去掉。

完工!


欢迎使用 SteemEditor 来编写文章,获取 @steemeditor.bot 的大力点赞!


欢迎使用 SteemMention 获取最新的 Steem 回复提醒。


感谢你的阅读,我是中文区见证人之一,欢迎通过 SteemConnect 来给我投票,或者打开 https://steemit.com/~witnesses/ 页面,输入 ety001 进行投票。

2.gif

中文区的见证人目前有:
支持一下他们(按字母顺序),一人可以有30票


Thank you for reading. I'm a witness. I would really appreciate your witness vote! You can vote by SteemConnect. Or open https://steemit.com/~witnesses page, input ety001 to vote.

2.gif

Sort:  

很给力。稍候抄代码

Made API:

$ curl -s -X POST "https://anothervps.com/api/steemit/vote-value/" -d "id=justyy"

{"full_vote_value":2.7953587449609953,"voting_power":"78.46"}

Coin Marketplace

STEEM 0.25
TRX 0.11
JST 0.032
BTC 61275.32
ETH 2983.86
USDT 1.00
SBD 3.76