From ebbbba5b80587b3f718a7f7ddc2b62d2575f278e Mon Sep 17 00:00:00 2001 From: Ayaz Date: Mon, 19 Sep 2022 21:42:49 -0500 Subject: [PATCH] getStreamEstDepletionDate fixes --- package.json | 2 +- src/utils.ts | 33 +++++++++++++++++---------------- tests/index.test.ts | 16 ++++++++-------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index 5d0b788..d9cfb2f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/utils.ts b/src/utils.ts index 7b181e0..2fafb06 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,5 @@ import { AccountInfo, - Commitment, ConfirmedSignaturesForAddress2Options, ConfirmOptions, Connection, @@ -12,6 +11,7 @@ import { PublicKey, SystemProgram, TransactionInstruction, + MemcmpFilter, } from '@solana/web3.js'; import { BN, BorshInstructionCoder, Idl, Program } from '@project-serum/anchor'; /** @@ -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()); @@ -80,8 +79,9 @@ export const getStream = async ( ): Promise => { 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) { @@ -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); @@ -719,6 +720,7 @@ export const parseStreamItemData = ( }, } as Stream; + console.log(`************* eof stream: ${address.toString()} *******************`); return streamInfo; }; @@ -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) => { diff --git a/tests/index.test.ts b/tests/index.test.ts index 050635f..b39ad9e 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -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);