Skip to content

Commit cb77939

Browse files
Batched aligned contracts in search contracts API (#322)
* Batched fetching aligned contracts in search * version bump
1 parent 80c8610 commit cb77939

File tree

7 files changed

+53
-21
lines changed

7 files changed

+53
-21
lines changed

packages/common/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@streamflow/common",
3-
"version": "9.2.1",
3+
"version": "9.2.2",
44
"description": "Common utilities and types used by streamflow packages.",
55
"homepage": "https://github.com/streamflow-finance/js-sdk/",
66
"type": "module",

packages/distributor/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@streamflow/distributor",
3-
"version": "9.2.1",
3+
"version": "9.2.2",
44
"description": "JavaScript SDK to interact with Streamflow Airdrop protocol.",
55
"homepage": "https://github.com/streamflow-finance/js-sdk/",
66
"main": "./dist/cjs/index.cjs",

packages/eslint-config/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@streamflow/eslint-config",
3-
"version": "9.2.1",
3+
"version": "9.2.2",
44
"description": "ESLint configuration for Streamflow protocol.",
55
"homepage": "https://github.com/streamflow-finance/js-sdk/",
66
"engines": {

packages/launchpad/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@streamflow/launchpad",
3-
"version": "9.2.1",
3+
"version": "9.2.2",
44
"description": "JavaScript SDK to interact with Streamflow Launchpad protocol.",
55
"homepage": "https://github.com/streamflow-finance/js-sdk/",
66
"main": "./dist/cjs/index.cjs",

packages/staking/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@streamflow/staking",
3-
"version": "9.2.1",
3+
"version": "9.2.2",
44
"description": "JavaScript SDK to interact with Streamflow Staking protocol.",
55
"homepage": "https://github.com/streamflow-finance/js-sdk/",
66
"main": "./dist/cjs/index.cjs",

packages/stream/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@streamflow/stream",
3-
"version": "9.2.1",
3+
"version": "9.2.2",
44
"description": "JavaScript SDK to interact with Streamflow protocol.",
55
"homepage": "https://github.com/streamflow-finance/js-sdk/",
66
"main": "./dist/cjs/index.cjs",

packages/stream/solana/StreamClient.ts

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
type OracleType,
5656
type IPrepareCreateStreamSolanaExt,
5757
type IPrepareStreamSolanaExt,
58+
type AlignedUnlocksContract,
5859
} from "./types.js";
5960
import {
6061
decodeStream,
@@ -116,7 +117,7 @@ import type { IPartnerLayout } from "./instructionTypes.js";
116117
import { calculateTotalAmountToDeposit } from "../common/utils.js";
117118
import { WITHDRAW_AVAILABLE_AMOUNT } from "../common/constants.js";
118119
import type { StreamflowAlignedUnlocks as AlignedUnlocksProgramType } from "./descriptor/streamflow_aligned_unlocks.js";
119-
import StreamflowAlignedUnlocksIDL from "./descriptor/idl/streamflow_aligned_unlocks.json" with { type: "json" };
120+
import StreamflowAlignedUnlocksIDL from "./descriptor/idl/streamflow_aligned_unlocks.json";
120121
import { deriveContractPDA, deriveEscrowPDA, deriveTestOraclePDA } from "./lib/derive-accounts.js";
121122
import { isCreateAlignedStreamData } from "../common/contractUtils.js";
122123

@@ -1632,20 +1633,51 @@ export class SolanaStreamClient extends BaseStreamClient {
16321633
filters,
16331634
});
16341635

1635-
const mapped = await Promise.all(
1636-
accounts.map(async ({ pubkey, account }) => {
1637-
const stream = decodeStream(account.data);
1638-
if (this.isAlignedUnlock(pubkey, stream.sender)) {
1639-
const alignedProxy = await this.alignedProxyProgram.account.contract.fetch(
1640-
deriveContractPDA(this.alignedProxyProgram.programId, pubkey),
1641-
);
1642-
invariant(alignedProxy, "Couldn't get aligned proxy account info");
1643-
return { publicKey: pubkey, account: new AlignedContract(stream, alignedProxy) };
1644-
}
1645-
return { publicKey: pubkey, account: new Contract(stream) };
1646-
}),
1647-
);
1648-
return mapped;
1636+
const decoded = this.decodeStreamsFromAccounts(accounts);
1637+
const alignedEntries = this.collectAlignedEntries(decoded);
1638+
const alignedProxies = await this.fetchAlignedProxiesByEntries(alignedEntries);
1639+
return this.mapDecodedToProgramAccounts(decoded, alignedEntries, alignedProxies);
1640+
}
1641+
1642+
private decodeStreamsFromAccounts(accounts: ReadonlyArray<{ pubkey: PublicKey; account: { data: Buffer } }>) {
1643+
return accounts.map(({ pubkey, account }) => ({
1644+
pubkey,
1645+
stream: decodeStream(account.data),
1646+
}));
1647+
}
1648+
1649+
private collectAlignedEntries(decoded: { pubkey: PublicKey; stream: DecodedStream }[]) {
1650+
const entries: { index: number; pda: PublicKey }[] = [];
1651+
decoded.forEach(({ pubkey, stream }, index) => {
1652+
if (this.isAlignedUnlock(pubkey, stream.sender)) {
1653+
entries.push({ index, pda: deriveContractPDA(this.alignedProxyProgram.programId, pubkey) });
1654+
}
1655+
});
1656+
return entries;
1657+
}
1658+
1659+
private async fetchAlignedProxiesByEntries(
1660+
aligned: { index: number; pda: PublicKey }[],
1661+
): Promise<ReadonlyArray<AlignedUnlocksContract | null>> {
1662+
if (aligned.length === 0) return [];
1663+
const res = await this.alignedProxyProgram.account.contract.fetchMultiple(aligned.map((a) => a.pda));
1664+
return res as ReadonlyArray<AlignedUnlocksContract | null>;
1665+
}
1666+
1667+
private mapDecodedToProgramAccounts(
1668+
decoded: { pubkey: PublicKey; stream: DecodedStream }[],
1669+
alignedEntries: { index: number; pda: PublicKey }[],
1670+
alignedProxies: ReadonlyArray<AlignedUnlocksContract | null>,
1671+
): IProgramAccount<Stream>[] {
1672+
return decoded.map(({ pubkey, stream }, idx) => {
1673+
const alignedIdx = alignedEntries.findIndex((a) => a.index === idx);
1674+
if (alignedIdx !== -1) {
1675+
const alignedProxy = alignedProxies[alignedIdx];
1676+
invariant(alignedProxy, "Couldn't get aligned proxy account info");
1677+
return { publicKey: pubkey, account: new AlignedContract(stream, alignedProxy) };
1678+
}
1679+
return { publicKey: pubkey, account: new Contract(stream) };
1680+
});
16491681
}
16501682

16511683
/**

0 commit comments

Comments
 (0)