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

Commit

Permalink
Guarantee a return type in getStream method
Browse files Browse the repository at this point in the history
  • Loading branch information
MexicanSparker committed Sep 16, 2022
1 parent 85ebf48 commit ddb65b7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 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.16",
"version": "3.0.0-alpha.17",
"description": "MSP Typescript SDK",
"private": false,
"main": "lib/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/msp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class MSP {
this.program = createProgram(this.connection, programId);
}

public async getStream(id: PublicKey): Promise<any> {
public async getStream(id: PublicKey): Promise<Stream | null> {
return getStream(this.program, id);
}

Expand Down
31 changes: 12 additions & 19 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const createProgram = (
export const getStream = async (
program: Program<Msp>,
address: PublicKey,
): Promise<any> => {
): Promise<Stream | null> => {
try {
const streamEventResponse = await program.simulate.getStream(
LATEST_IDL_FILE_VERSION,
Expand Down Expand Up @@ -144,8 +144,8 @@ export const getStreamRaw = async (
export const getStreamCached = async (
streamInfo: Stream,
): Promise<Stream> => {
//TODO: BN check
const timeDiff = new BN(streamInfo.lastRetrievedTimeInSeconds).toNumber() - new BN(streamInfo.lastRetrievedBlockTime).toNumber();
//TODO: BN check -> Checked by Yamel. Timestamps ar ok as number
const timeDiff = streamInfo.lastRetrievedTimeInSeconds - streamInfo.lastRetrievedBlockTime;
const blocktime = parseInt((Date.now() / 1_000).toString()) - timeDiff;

const parsedStream = parseStreamItemData(
Expand Down Expand Up @@ -202,9 +202,9 @@ export const listStreamsCached = async (
streamInfoList: Stream[],
): Promise<Stream[]> => {
const streamList: Stream[] = [];
//TODO: BN check
//TODO: BN check -> Checked by Yamel. Timestamps ar ok as number
for (const streamInfo of streamInfoList) {
const timeDiff = new BN(streamInfo.lastRetrievedTimeInSeconds).toNumber() - new BN(streamInfo.lastRetrievedBlockTime).toNumber();
const timeDiff = streamInfo.lastRetrievedTimeInSeconds - streamInfo.lastRetrievedBlockTime;
const blockTime = parseInt((Date.now() / 1_000).toString()) - timeDiff;

const parsedStream = parseStreamItemData(
Expand Down Expand Up @@ -361,14 +361,12 @@ export const calculateActionFees = async (
connection: Connection,
action: MSP_ACTIONS,
): Promise<TransactionFees> => {
const recentBlockhash = await connection.getRecentBlockhash(
connection.commitment as Commitment,
),
txFees: TransactionFees = {
blockchainFee: 0.0,
mspFlatFee: 0.0,
mspPercentFee: 0.0,
};
const txFees: TransactionFees = {
blockchainFee: 0.0,
mspFlatFee: 0.0,
mspPercentFee: 0.0,
};

let blockchainFee = 0;

switch (action) {
Expand Down Expand Up @@ -572,7 +570,7 @@ const parseGetStreamData = (
cliffVestAmount: event.cliffVestAmountUnits,
cliffVestPercent: event.cliffVestPercent.toNumber() / 10_000,
allocationAssigned: event.allocationAssignedUnits,
secondsSinceStart: event.currentBlockTime.sub(new BN(event.startUtc)).toNumber(), //TODO: BN check
secondsSinceStart: event.currentBlockTime.sub(new BN(event.startUtc)).toNumber(), //TODO: BN check -> Checked by Yamel. Timestamps ar ok as number
estimatedDepletionDate: depletionDate.toString(),
rateAmount: event.rateAmountUnits,
rateIntervalInSeconds: event.rateIntervalInSeconds.toNumber(),
Expand Down Expand Up @@ -619,12 +617,7 @@ export const parseStreamItemData = (
const effectiveCreatedOnUtcInSeconds =
createdOnUtcInSeconds > 0 ? createdOnUtcInSeconds : startUtcInSeconds;

// Since "now" in the blockchain (expressed by blocktime)
// is not equal to "now" in the client (expressed by
// the Date object returned by the browser)
// Store this client difference for further calculations
const timeDiff = Math.round((Date.now() / 1_000) - blockTime);

const startUtc = new Date(startUtcInSeconds * 1000);
const depletionDate = getStreamEstDepletionDate(stream);
const streamStatus = getStreamStatus(stream, timeDiff);
Expand Down

0 comments on commit ddb65b7

Please sign in to comment.