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

Commit

Permalink
Change streamUnitsPerSecond to be number to have the ability to use d…
Browse files Browse the repository at this point in the history
…ecimal values
  • Loading branch information
MexicanSparker committed Aug 30, 2022
1 parent 4102677 commit f6e13c3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 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.7",
"version": "3.0.0-alpha.8",
"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,7 @@ export type Stream = {
fundsLeftInStream: BN;
fundsSentToBeneficiary: BN;
remainingAllocationAmount: BN;
streamUnitsPerSecond: BN;
streamUnitsPerSecond: number;
// Dates
startUtc: string;
createdOnUtc: string;
Expand Down
34 changes: 13 additions & 21 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,17 +550,14 @@ const parseGetStreamData = (
? event.createdOnUtc.toNumber()
: 0;

// const startUtcInSeconds = event.startUtc.toNumber();
const startUtcInSeconds = getStreamStartUtcInSeconds(event);
const effectiveCreatedOnUtcInSeconds =
createdOnUtcInSeconds > 0
? createdOnUtcInSeconds
: event.startUtc.toNumber();

const startUtc = new Date(startUtcInSeconds * 1000);
// console.log('startUtc:', startUtc.toString());
const depletionDate = getStreamEstDepletionDate(event);
// console.log('depletionDate:', depletionDate.toString());

const stream = {
id: address,
Expand Down Expand Up @@ -630,9 +627,7 @@ export const parseStreamItemData = (
const timeDiff = Math.round((Date.now() / 1_000) - blockTime);

const startUtc = new Date(startUtcInSeconds * 1000);
// console.log('startUtc:', startUtc.toString());
const depletionDate = getStreamEstDepletionDate(stream);
// console.log('depletionDate:', depletionDate.toString());
const streamStatus = getStreamStatus(stream, timeDiff);
const streamMissedEarningUnitsWhilePaused = getStreamMissedEarningUnitsWhilePaused(stream);
const streamWithdrawableAmount = getStreamWithdrawableAmount(stream, timeDiff);
Expand Down Expand Up @@ -1346,7 +1341,7 @@ export const getStreamWithdrawableAmount = (stream: any, timeDiff = 0) => {
const startUtcInSeconds = getStreamStartUtcInSeconds(stream);
const timeSinceStart = blocktimeRelativeNow - startUtcInSeconds;
// const nonStopEarningUnits = cliffAmount + streamedUnitsPerSecond * timeSinceStart;
const nonStopEarningUnits = streamedUnitsPerSecond.muln(timeSinceStart).add(cliffAmount);
const nonStopEarningUnits = cliffAmount.addn(streamedUnitsPerSecond * timeSinceStart);
// const totalSecondsPaused =
// stream.lastKnownTotalSecondsInPausedStatus.toNumber().length >= 10
// ? parseInt(
Expand All @@ -1359,12 +1354,11 @@ export const getStreamWithdrawableAmount = (stream: any, timeDiff = 0) => {
? parseInt((stream.lastKnownTotalSecondsInPausedStatus.toNumber() / 1_000).toString())
: stream.lastKnownTotalSecondsInPausedStatus.toNumber();

// const missedEarningUnitsWhilePaused = streamedUnitsPerSecond * totalSecondsPaused;
const missedEarningUnitsWhilePaused = streamedUnitsPerSecond.muln(totalSecondsPaused);
const missedEarningUnitsWhilePaused = streamedUnitsPerSecond * totalSecondsPaused;
let entitledEarnings = nonStopEarningUnits;

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

let withdrawableUnitsWhileRunning = entitledEarnings;
Expand Down Expand Up @@ -1395,16 +1389,18 @@ export const getStreamStatus = (stream: any, timeDiff: number) => {
return STREAM_STATUS.Paused;
}

// Running or automatically paused (ran out of funds)
const totalSecondsPaused = stream.lastKnownTotalSecondsInPausedStatus.toString().length >= 10
? parseInt((stream.lastKnownTotalSecondsInPausedStatus.toNumber() / 1_000).toString())
: stream.lastKnownTotalSecondsInPausedStatus.toNumber();
const cliffUnits = getStreamCliffAmount(stream);
const secondsSinceStart = blocktimeRelativeNow - startUtcInSeconds;
const streamedUnitsPerSecond = getStreamUnitsPerSecond(stream);
const nonStopEarningUnits = streamedUnitsPerSecond.muln(secondsSinceStart).add(cliffUnits);
const missedEarningUnitsWhilePaused = streamedUnitsPerSecond.mul(stream.lastKnownTotalSecondsInPausedStatus);
const nonStopEarningUnits = cliffUnits.addn(streamedUnitsPerSecond * secondsSinceStart);
const missedEarningUnitsWhilePaused = streamedUnitsPerSecond * totalSecondsPaused;
let entitledEarnings = nonStopEarningUnits;

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

// Running
Expand All @@ -1428,17 +1424,13 @@ export const getStreamUnitsPerSecond = (stream: any) => {

const interval = stream.rateIntervalInSeconds.toNumber();
if (interval === 0) {
return new BN(0);
return 0;
}

const rateAmountUnits = new BigNumber(stream.rateAmountUnits.toString());
const streamUnitsPerSecond = rateAmountUnits.dividedBy(interval);

if (streamUnitsPerSecond.lt(1)) {
return new BN(1);
}

return new BN(streamUnitsPerSecond.toNumber());
return streamUnitsPerSecond.toNumber();
};

export const getStreamStartUtcInSeconds = (stream: any): number => {
Expand Down
11 changes: 5 additions & 6 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,19 +472,18 @@ describe('MSP Tests\n', async () => {
streamWithdrawableAmount = new BN(0);
} else {
const streamUnitsPerSecond = getStreamUnitsPerSecond(stream);
console.log('streamUnitsPerSecond:', streamUnitsPerSecond);
const blocktimeRelativeNow = Math.round((Date.now() / 1_000) - timeDiff);
const startUtcInSeconds = getStreamStartUtcInSeconds(stream);
const timeSinceStart = blocktimeRelativeNow - startUtcInSeconds;
const nonStopEarningUnits = streamUnitsPerSecond.muln(timeSinceStart).add(cliffAmount);
const nonStopEarningUnits = cliffAmount.addn(streamUnitsPerSecond * timeSinceStart);
const totalSecondsPaused = stream.lastKnownTotalSecondsInPausedStatus.toString().length >= 10
? parseInt((stream.lastKnownTotalSecondsInPausedStatus.toNumber() / 1_000).toString())
: stream.lastKnownTotalSecondsInPausedStatus.toNumber();
const missedEarningUnitsWhilePaused = streamUnitsPerSecond.muln(totalSecondsPaused);
const missedEarningUnitsWhilePaused = streamUnitsPerSecond * totalSecondsPaused;
let entitledEarnings = nonStopEarningUnits;

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

let withdrawableUnitsWhileRunning = entitledEarnings;
Expand All @@ -497,7 +496,7 @@ describe('MSP Tests\n', async () => {

streamWithdrawableAmount = BN.max(new BN(0), withdrawableAmount);

debugObject.streamUnitsPerSecond = streamUnitsPerSecond.toString();
debugObject.streamUnitsPerSecond = streamUnitsPerSecond;
debugObject.startUtcInSeconds = startUtcInSeconds;
debugObject.timeSinceStart = timeSinceStart;
debugObject.nonStopEarningUnits = nonStopEarningUnits.toString();
Expand Down

0 comments on commit f6e13c3

Please sign in to comment.