Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
Fix calculation for nonStopEarningUnits
Browse files Browse the repository at this point in the history
  • Loading branch information
MexicanSparker committed Aug 30, 2022
1 parent f6e13c3 commit c6cdd54
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mean-dao/msp",
"version": "3.0.0-alpha.8",
"version": "3.0.0-alpha.9",
"description": "MSP Typescript SDK",
"private": false,
"main": "lib/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ export type Stream = {
fundsLeftInStream: BN;
fundsSentToBeneficiary: BN;
remainingAllocationAmount: BN;
streamUnitsPerSecond: number;
// Dates
startUtc: string;
createdOnUtc: string;
Expand All @@ -252,6 +251,7 @@ export type Stream = {
initialized: boolean;
version: number;
name: string;
streamUnitsPerSecond: number;
cliffVestPercent: number;
upgradeRequired: boolean;
status: STREAM_STATUS | string;
Expand Down
25 changes: 13 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1335,13 +1335,13 @@ export const getStreamWithdrawableAmount = (stream: any, timeDiff = 0) => {


const streamedUnitsPerSecond = getStreamUnitsPerSecond(stream);
const cliffAmount = getStreamCliffAmount(stream);
const cliffUnits = new BigNumber(getStreamCliffAmount(stream).toString());
// Get the blockchain kind of "now" given the client timeDiff
const blocktimeRelativeNow = Math.round((Date.now() / 1_000) - timeDiff);
const startUtcInSeconds = getStreamStartUtcInSeconds(stream);
const timeSinceStart = blocktimeRelativeNow - startUtcInSeconds;
const secondsSinceStart = blocktimeRelativeNow - startUtcInSeconds;
// const nonStopEarningUnits = cliffAmount + streamedUnitsPerSecond * timeSinceStart;
const nonStopEarningUnits = cliffAmount.addn(streamedUnitsPerSecond * timeSinceStart);
const nonStopEarningUnits = cliffUnits.plus(streamedUnitsPerSecond * secondsSinceStart);
// const totalSecondsPaused =
// stream.lastKnownTotalSecondsInPausedStatus.toNumber().length >= 10
// ? parseInt(
Expand All @@ -1357,17 +1357,17 @@ export const getStreamWithdrawableAmount = (stream: any, timeDiff = 0) => {
const missedEarningUnitsWhilePaused = streamedUnitsPerSecond * totalSecondsPaused;
let entitledEarnings = nonStopEarningUnits;

if (nonStopEarningUnits.gten(missedEarningUnitsWhilePaused)) {
entitledEarnings = nonStopEarningUnits.subn(missedEarningUnitsWhilePaused);
if (nonStopEarningUnits.gte(missedEarningUnitsWhilePaused)) {
entitledEarnings = nonStopEarningUnits.plus(missedEarningUnitsWhilePaused);
}

let withdrawableUnitsWhileRunning = entitledEarnings;

if (entitledEarnings.gte(stream.totalWithdrawalsUnits)) {
withdrawableUnitsWhileRunning = entitledEarnings.sub(stream.totalWithdrawalsUnits);
withdrawableUnitsWhileRunning = entitledEarnings.minus(stream.totalWithdrawalsUnits);
}

const withdrawableAmount = BN.min(remainingAllocation, withdrawableUnitsWhileRunning);
const withdrawableAmount = BN.min(remainingAllocation, new BN(withdrawableUnitsWhileRunning.toNumber()));

return BN.max(new BN(0), withdrawableAmount);
};
Expand All @@ -1392,19 +1392,20 @@ export const getStreamStatus = (stream: any, timeDiff: number) => {
const totalSecondsPaused = stream.lastKnownTotalSecondsInPausedStatus.toString().length >= 10
? parseInt((stream.lastKnownTotalSecondsInPausedStatus.toNumber() / 1_000).toString())
: stream.lastKnownTotalSecondsInPausedStatus.toNumber();
const cliffUnits = getStreamCliffAmount(stream);
const cliffUnits = new BigNumber(getStreamCliffAmount(stream).toString());
const secondsSinceStart = blocktimeRelativeNow - startUtcInSeconds;
const streamedUnitsPerSecond = getStreamUnitsPerSecond(stream);
const nonStopEarningUnits = cliffUnits.addn(streamedUnitsPerSecond * secondsSinceStart);
const nonStopEarningUnits = cliffUnits.plus(streamedUnitsPerSecond * secondsSinceStart);
const missedEarningUnitsWhilePaused = streamedUnitsPerSecond * totalSecondsPaused;
let entitledEarnings = nonStopEarningUnits;

if (nonStopEarningUnits.gten(missedEarningUnitsWhilePaused)) {
entitledEarnings = nonStopEarningUnits.subn(missedEarningUnitsWhilePaused);
if (nonStopEarningUnits.gte(missedEarningUnitsWhilePaused)) {
entitledEarnings = nonStopEarningUnits.minus(missedEarningUnitsWhilePaused);
}

const allocation = new BigNumber(stream.allocationAssignedUnits.toString());
// Running
if (stream.allocationAssignedUnits.gt(entitledEarnings)) {
if (allocation.gt(entitledEarnings)) {
return STREAM_STATUS.Running;
}

Expand Down

0 comments on commit c6cdd54

Please sign in to comment.