Skip to content

Instantly share code, notes, and snippets.

@DenisCarriere
Created June 16, 2022 13:24
Show Gist options
  • Select an option

  • Save DenisCarriere/a2b29290ce8721cd745bb82299ab214c to your computer and use it in GitHub Desktop.

Select an option

Save DenisCarriere/a2b29290ce8721cd745bb82299ab214c to your computer and use it in GitHub Desktop.

Revisions

  1. DenisCarriere created this gist Jun 16, 2022.
    35 changes: 35 additions & 0 deletions stake2vote.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    /**
    * 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);
    })()