Skip to content

Commit

Permalink
proper stream types and filters (#84)
Browse files Browse the repository at this point in the history
* STREAM-508:  proper stream types and filters
  • Loading branch information
Yolley authored Aug 21, 2023
1 parent 2ee1393 commit 6ffa4ec
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 15 deletions.
7 changes: 5 additions & 2 deletions packages/stream/aptos/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { WalletContextState } from "@manahippo/aptos-wallet-adapter";
import BN from "bn.js";

import { calculateUnlockedAmount } from "../common/contractUtils";
import { Stream } from "../common/types";
import { buildStreamType, calculateUnlockedAmount } from "../common/contractUtils";
import { Stream, StreamType } from "../common/types";
import { getNumberFromBN } from "../common/utils";

export interface ICreateStreamAptosExt {
Expand Down Expand Up @@ -138,6 +138,8 @@ export class Contract implements Stream {

fundsUnlockedAtLastRateChange: BN;

type: StreamType;

constructor(stream: StreamResource, tokenId: string) {
this.magic = 0;
this.version = 0;
Expand Down Expand Up @@ -182,6 +184,7 @@ export class Contract implements Stream {
this.pauseCumulative = new BN(stream.pause_cumulative);
this.lastRateChangeTime = parseInt(stream.last_rate_change_time);
this.fundsUnlockedAtLastRateChange = new BN(stream.funds_unlocked_at_last_rate_change);
this.type = buildStreamType(this);
}

unlocked(currentTimestamp: number): BN {
Expand Down
30 changes: 30 additions & 0 deletions packages/stream/common/contractUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import BN from "bn.js";

import { StreamType } from "./types";

interface ICalculateUnlockedAmount {
depositedAmount: BN;
cliff: number;
Expand Down Expand Up @@ -37,3 +39,31 @@ export const calculateUnlockedAmount = ({

return streamed.lt(deposited) ? streamed : deposited;
};

export const isPayment = (streamData: { canTopup: boolean }): boolean => {
return streamData.canTopup;
};

export const isTokenLock = (streamData: {
canTopup: boolean;
depositedAmount: BN;
cliffAmount: BN;
}): boolean => {
return (
!streamData.canTopup && streamData.cliffAmount.gte(streamData.depositedAmount.sub(new BN("1")))
);
};

export const buildStreamType = (streamData: {
canTopup: boolean;
depositedAmount: BN;
cliffAmount: BN;
}): StreamType => {
if (isPayment(streamData)) {
return StreamType.Payment;
}
if (isTokenLock(streamData)) {
return StreamType.Lock;
}
return StreamType.Vesting;
};
7 changes: 5 additions & 2 deletions packages/stream/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ export enum StreamDirection {
}

export enum StreamType {
Stream = "stream",
Vesting = "vesting",
All = "all",
Payment = "payment",
Vesting = "vesting",
Lock = "lock",
}

export enum IChain {
Expand Down Expand Up @@ -166,5 +167,7 @@ export interface Stream {
lastRateChangeTime: number;
fundsUnlockedAtLastRateChange: BN;

type: StreamType;

unlocked(currentTimestamp: number): BN;
}
7 changes: 5 additions & 2 deletions packages/stream/evm/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import BN from "bn.js";
import { BigNumber } from "ethers";

import { calculateUnlockedAmount } from "../common/contractUtils";
import { Stream } from "../common/types";
import { buildStreamType, calculateUnlockedAmount } from "../common/contractUtils";
import { Stream, StreamType } from "../common/types";
import { getNumberFromBN } from "../common/utils";

export interface StreamAbiResult {
Expand Down Expand Up @@ -133,6 +133,8 @@ export class EvmContract implements Stream {

fundsUnlockedAtLastRateChange: BN;

type: StreamType;

constructor(stream: StreamAbiResult) {
this.magic = 0;
this.version = 0;
Expand Down Expand Up @@ -178,6 +180,7 @@ export class EvmContract implements Stream {
this.fundsUnlockedAtLastRateChange = new BN(
stream.funds_unlocked_at_last_rate_change.toString()
);
this.type = buildStreamType(this);
}

unlocked(currentTimestamp: number): BN {
Expand Down
2 changes: 1 addition & 1 deletion packages/stream/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/stream",
"version": "5.1.1",
"version": "5.2.0",
"description": "JavaScript SDK to interact with Streamflow protocol.",
"main": "dist/index.js",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
Expand Down
8 changes: 3 additions & 5 deletions packages/stream/solana/StreamClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,22 +689,20 @@ export default class SolanaStreamClient extends BaseStreamClient {
accounts = await getProgramAccounts(this.connection, publicKey, offset, this.programId);
}

let streams: Record<string, any> = {};
let streams: Record<string, Contract> = {};

accounts.forEach((account) => {
const decoded = new Contract(decodeStream(account.account.data));
streams = { ...streams, [account.pubkey.toBase58()]: decoded };
});

const sortedStreams = Object.entries(streams).sort(
([, stream1], [, stream2]) => stream2.startTime - stream1.startTime
([, stream1], [, stream2]) => stream2.start - stream1.start
);

if (type === "all") return sortedStreams;

return type === "stream"
? sortedStreams.filter((stream) => stream[1].canTopup)
: sortedStreams.filter((stream) => !stream[1].canTopup);
return sortedStreams.filter((stream) => stream[1].type === type);
}

private async sign(
Expand Down
5 changes: 4 additions & 1 deletion packages/stream/solana/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "@solana/web3.js";
import BN from "bn.js";

import { calculateUnlockedAmount } from "../common/contractUtils";
import { buildStreamType, calculateUnlockedAmount } from "../common/contractUtils";
import { IRecipient, Stream, StreamDirection, StreamType } from "../common/types";
import { getNumberFromBN } from "../common/utils";

Expand Down Expand Up @@ -255,6 +255,8 @@ export class Contract implements Stream {

fundsUnlockedAtLastRateChange: BN;

type: StreamType;

constructor(stream: DecodedStream) {
this.magic = stream.magic.toNumber();
this.version = stream.version.toNumber();
Expand Down Expand Up @@ -298,6 +300,7 @@ export class Contract implements Stream {
this.pauseCumulative = stream.pauseCumulative;
this.lastRateChangeTime = stream.lastRateChangeTime.toNumber();
this.fundsUnlockedAtLastRateChange = stream.fundsUnlockedAtLastRateChange;
this.type = buildStreamType(this);
}

unlocked(currentTimestamp: number): BN {
Expand Down
7 changes: 5 additions & 2 deletions packages/stream/sui/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
} from "@mysten/sui.js/client";
import BN from "bn.js";

import { calculateUnlockedAmount } from "../common/contractUtils";
import { Stream } from "../common/types";
import { buildStreamType, calculateUnlockedAmount } from "../common/contractUtils";
import { Stream, StreamType } from "../common/types";
import { getNumberFromBN } from "../common/utils";

export interface ICreateStreamSuiExt {
Expand Down Expand Up @@ -174,6 +174,8 @@ export class Contract implements Stream {

fundsUnlockedAtLastRateChange: BN;

type: StreamType;

constructor(stream: StreamResource, tokenId: string) {
const meta = stream.meta.fields;
const fees = stream.fees.fields;
Expand Down Expand Up @@ -221,6 +223,7 @@ export class Contract implements Stream {
this.pauseCumulative = new BN(stream.pause_cumulative);
this.lastRateChangeTime = parseInt(stream.last_rate_change_time);
this.fundsUnlockedAtLastRateChange = new BN(stream.funds_unlocked_at_last_rate_change);
this.type = buildStreamType(this);
}

unlocked(currentTimestamp: number): BN {
Expand Down

0 comments on commit 6ffa4ec

Please sign in to comment.