Skip to content

Instantly share code, notes, and snippets.

@mciszczon
Last active April 27, 2022 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mciszczon/6ab9528c4100b73d3a45a0ef761afcca to your computer and use it in GitHub Desktop.
Save mciszczon/6ab9528c4100b73d3a45a0ef761afcca to your computer and use it in GitHub Desktop.
Below code is a fairly simple and bare-bones implementation of calculating the USD vote value of a Hive blockchain user.
/**
* 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<number>}
*/
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)
})
Copyright 2022 Mateusz Ciszczoń <contact@mciszczon.pl>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment