Created
June 16, 2022 13:24
-
-
Save DenisCarriere/a2b29290ce8721cd745bb82299ab214c to your computer and use it in GitHub Desktop.
EOS stake to vote
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* voteWeightToday computes the stake2vote weight for EOS, in order to compute the decaying value. | |
*/ | |
export function voteWeightToday(date: Date): number { | |
const seconds_per_day = 86400; | |
const block_timestamp_epoch = new Date(Date.UTC(2000, 0, 1, 0, 0, 0, 0)).getTime(); | |
return Math.floor( (date.getTime() - block_timestamp_epoch) / 1000 / (seconds_per_day * 7)) / 52; | |
} | |
/** | |
* Convert EOS stake into decaying value | |
* | |
* @param {number} vote vote | |
*/ | |
export function stake2vote( date: Date, staked: number ): number { | |
return staked * Math.pow(2, voteWeightToday(date)); | |
} | |
/** | |
* Convert vote decay value into EOS stake | |
* | |
* @param {number} staked staked | |
*/ | |
export function vote2stake( date: Date, vote: number ): number { | |
return vote / Math.pow(2, voteWeightToday(date)); | |
} | |
(() => { | |
const timestamp = new Date("2021-08-22T02:11:23Z"); | |
const last_vote_weight = 6636014682100090; | |
const staked = vote2stake(timestamp, last_vote_weight); | |
// => 1932337620.9129524 | |
console.log(staked); | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment