/** * Fetch current reward fund, pick `recent_claims` and `reward_balance` * @returns {Promise<{recentClaims: number, rewardBalance: number}>} */ const getFund = async () => { const response = await fetch( 'https://api.hive.blog/', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: 0, jsonrpc: '2.0', method: 'condenser_api.get_reward_fund', params: ['post'], }) } ) const json = await response.json() const result = json.result return { recentClaims: parseInt(result.recent_claims, 10), rewardBalance: parseInt(result.reward_balance.split(' ')[0], 10), } } /** * Get HBD price. `quote` is the desired value (i.e. 1 USD), and the `base` is the current value. * @returns {Promise<{quote: number, base: number}>} */ const getHbdPrice = async () => { const response = await fetch( 'https://api.hive.blog/', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: 0, jsonrpc: '2.0', method: 'condenser_api.get_current_median_history_price', params: [], }) } ) const json = await response.json() const result = json.result return { base: parseFloat(result.base.split(' ')[0]), quote: parseFloat(result.quote.split(' ')[0]), } } /** * Fetch user info. We mostly care about the `vests` of that user, and optionally * about the voting power that the user currently has. * @param username * @returns {Promise<{votingPower: number, vests: number, username: string}>} */ const getUser = async ({ username }) => { const response = await fetch( 'https://api.hive.blog/', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: 0, jsonrpc: '2.0', method: 'condenser_api.get_accounts', params: [[username]], }) } ) const json = await response.json() const result = json.result[0] const totalVests = ( parseFloat(result.vesting_shares.split(' ')[0]) + parseFloat(result.received_vesting_shares.split(' ')[0]) - parseFloat(result.delegated_vesting_shares.split(' ')[0]) ) return { username, vests: totalVests, votingPower: result.voting_power, } } /** * Estimate a USD vote value, based on an account `vests` and the current reward balance. * @param {number} vests The amount of vests that an account holds * @param {number} votingPower Voting power of that account, 0-100 (max by default) * @param {number} voteWeight The weight of the vote, 0-100 (max by default) * @param {{recentClaims:number,rewardBalance:number}} fund * @param {number} hbdValue The current value of HBD * @returns {number} */ const estimateVoteValue = ({ vests = 0, votingPower = 100, voteWeight = 100, fund = { recentClaims: 0, rewardBalance: 0 }, hbdValue = 1.0, }) => { const power = ((votingPower * 100) * (voteWeight * 100) / 10000) / 50 const rShares = power * vests * 1000000 / 10000 return rShares / fund.recentClaims * fund.rewardBalance * hbdValue } /** * Get the USD vote value of a given user. By default, it will check the vote value * when 100% voting power, but you can pass fullVotingPower=false to check the current vote value. * @param {string} username * @param {boolean} fullVotingPower * @returns {Promise} */ const getUserVoteValue = async ({ username, fullVotingPower = true }) => { const fund = await getFund() const hbdPrice = await getHbdPrice() const user = await getUser({ username }) return estimateVoteValue({ vests: user.vests, votingPower: fullVotingPower ? 100 : user.votingPower, fund, hbdValue: hbdPrice.base, }) } getUserVoteValue({ username: 'cryptosharon' }) .then(voteValue => { console.info('Vote value: ' + voteValue) })