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

Commit

Permalink
getStreamEstDepletionDate fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ayazzz committed Sep 20, 2022
1 parent 06cb574 commit ebbbba5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 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.21",
"version": "3.0.0-alpha.22",
"description": "MSP Typescript SDK",
"private": false,
"main": "lib/index.js",
Expand Down
33 changes: 17 additions & 16 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
AccountInfo,
Commitment,
ConfirmedSignaturesForAddress2Options,
ConfirmOptions,
Connection,
Expand All @@ -12,6 +11,7 @@ import {
PublicKey,
SystemProgram,
TransactionInstruction,
MemcmpFilter,
} from '@solana/web3.js';
import { BN, BorshInstructionCoder, Idl, Program } from '@project-serum/anchor';
/**
Expand Down Expand Up @@ -48,7 +48,6 @@ import {
TOKEN_PROGRAM_ID,
} from '@solana/spl-token';
import * as anchor from '@project-serum/anchor';
import { MemcmpFilter } from '@solana/web3.js';

String.prototype.toPublicKey = function (): PublicKey {
return new PublicKey(this.toString());
Expand Down Expand Up @@ -80,8 +79,9 @@ export const getStream = async (
): Promise<Stream | null> => {
try {
const event: any = await getStreamRaw(program, address);

const streamInfo = parseGetStreamData(event, address);
console.log('getStream result after parse:', streamInfo);
//console.log('getStream result after parse:', streamInfo);

return streamInfo;
} catch (error: any) {
Expand Down Expand Up @@ -600,6 +600,7 @@ export const parseStreamItemData = (

const timeDiff = Math.round((Date.now() / 1_000) - blockTime);
const startUtc = new Date(startUtcInSeconds * 1000);
console.log(`************* stream: ${address.toString()} *******************`);
const depletionDate = getStreamEstDepletionDate(stream);
const streamStatus = getStreamStatus(stream, timeDiff);
const streamMissedEarningUnitsWhilePaused = getStreamMissedEarningUnitsWhilePaused(stream);
Expand Down Expand Up @@ -719,6 +720,7 @@ export const parseStreamItemData = (
},
} as Stream;

console.log(`************* eof stream: ${address.toString()} *******************`);
return streamInfo;
};

Expand Down Expand Up @@ -1212,31 +1214,30 @@ export const parseStreamTemplateData = (
};

export const getStreamEstDepletionDate = (stream: any) => {
const interval = stream.rateIntervalInSeconds as BN;
const interval = new BN(stream.rateIntervalInSeconds);
if (interval.isZero()) {
return new Date();
const startUtcInSeconds = getStreamStartUtcInSeconds(stream);
const startUtc = new Date(startUtcInSeconds * 1_000);
return startUtc;
}

const cliffAmount = getStreamCliffAmount(stream);
const allocationAssignedUnits = new BN(stream.allocationAssignedUnits);
const allocationMinusCliff = allocationAssignedUnits.sub(cliffAmount)
const allocationMinusCliff = new BN(stream.allocationAssignedUnits).sub(cliffAmount)
const streamableAmount = BN.max(new BN(0), allocationMinusCliff);

const rateInterval = new BN(interval);
const rateAmountUnits = new BN(stream.rateAmountUnits);
const rateAmount = rateAmountUnits.div(rateInterval);

const streamableSeconds = streamableAmount.div(rateAmount);
const duration = streamableSeconds.add(new BN(stream.lastKnownTotalSecondsInPausedStatus));
// 10000 / 2629750 = 0.003...
const rateAmount = Number(stream.rateAmountUnits.toString()) / Number(interval.toString());
const duration = (Number(streamableAmount.toString()) / rateAmount) + (Number(stream.lastKnownTotalSecondsInPausedStatus.toString()));
const startUtcInSeconds = getStreamStartUtcInSeconds(stream);

//TODO: shoud we be worry duration.toNumber()?
const depletionTimestamp = (startUtcInSeconds + duration.toNumber()) * 1_000;
const depletionDate = new Date(depletionTimestamp);
const depletionDate = new Date((startUtcInSeconds + duration) * 1_000);

if (depletionDate.toString() !== 'Invalid Date') {
return depletionDate;
} else {
return new Date();
}
return new Date();
};

export const getStreamCliffAmount = (stream: any) => {
Expand Down
16 changes: 8 additions & 8 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ describe('MSP Tests\n', async () => {
msp = new MSP(endpoint, programId, 'confirmed');
});

xit('Test stream running', async () => {
const strmId = new PublicKey('FEsT4HG1WG24sb785x9WvrnFPZuG4ic8fvg28aKKzFn1');
const strmId2 = new PublicKey('4tA5bz8Ky3fAjyycvmNUFciUGgtS1qWZpnN8ii6MguRB');
it('Test stream running', async () => {
const strmId = new PublicKey('H2jAZk3DeS8G9wHkv35H2G4UDna3axfkEn4Dbhw2ti41');
const strmId2 = new PublicKey('H2jAZk3DeS8G9wHkv35H2G4UDna3axfkEn4Dbhw2ti41');
const data = await msp.getStream(strmId);
console.log(data);
const data2 = await msp.getStreamRaw(strmId2);
console.log(data2);
const data4 = await msp.listStreams({ treasurer: new PublicKey('468Z5p52439dAqjLzBm2FCNxvDSnpbMsNx85b7Kmz3TQ'), commitment: "confirmed" });
console.log(data4);
// const data2 = await msp.getStreamRaw(strmId2);
// console.log(data2);
// const data4 = await msp.listStreams({ treasurer: new PublicKey('HGb43H86jJNLN4MW1sDWKruUZYrs3rkNQkf3acF8uXiv'), commitment: "confirmed" });
// console.log(data4);

});

it('Enum casting', () => {
xit('Enum casting', () => {
const scheduled = 'Scheduled';
const scheduledEnum = STREAM_STATUS[scheduled];
console.log(scheduled, scheduledEnum);
Expand Down

0 comments on commit ebbbba5

Please sign in to comment.