πŸ”
Getting Data
How to use the Core-SDK to write subgraph queries and get account data
When working on a Superfluid related application, you’ll likely need to use the graph to query data about what’s happening with your users & Superfluid agreements.
Luckily, the SDK-Core has functionality which will allow you to easily query the graph to get data about accounts & their interaction with the Superfluid protocol. You can also use the CFAV1 & IDAV1 methods to get data that is specific to those agreements. In this section, we’ll show allow you to get this data. If you'd like to go deeper on the topic, we'd recommend reading through our section on the Superfluid Subgraph.

Pre-Defined Subgraph Queries

Here is a list of pre defined queries you can use via the sdk. Once the framework is initialized, you can use the pattern modeled below to run each query (i.e. sf.query.INSERT_QUERY ).
1
const { Framework } = require("@superfluid-finance/sdk-core");
2
const { ethers } = require("ethers");
3
​
4
const provider = new ethers.providers.InfuraProvider(
5
"matic",
6
"<INFURA_API_KEY>"
7
);
8
const sf = await Framework.create({
9
networkName: "matic",
10
provider
11
});
12
​
13
type Paging = { take: number, skip?: number, lastId?: string };
14
​
15
const pageResult = await sf.query.
16
// The different queries can take different order by properties
17
// given the properties that exist on the entity itself.
18
listAllSuperTokens({ isListed?: boolean },
19
paging: Paging,
20
ordering: Ordering<Token_OrderBy>
21
);
22
​
23
listIndexes({ indexId?: string, publisher?: string, token?: string },
24
paging: Paging,
25
ordering: Ordering<Index_OrderBy>
26
);
27
​
28
listIndexSubscriptions({ subscriber?: string, approved?: boolean },
29
paging: Paging,
30
ordering: Ordering<IndexSubscription_OrderBy>
31
);
32
​
33
listStreams({ sender?: string, receiver?: string, token?: string },
34
paging: Paging,
35
ordering: Ordering<Stream_OrderBy>
36
);
37
​
38
listUserInteractedSuperTokens({ account?: string, token?: string },
39
paging: Paging,
40
ordering: Ordering<AccountTokenSnapshot_OrderBy>
41
);
42
​
43
listEvents({ account?: string, timestamp_gt?: number },
44
paging: Paging,
45
ordering: Ordering<Event_OrderBy>
46
);
47
​
48
// A subscription function which allows you to subscribe to events via polling.
49
on(
50
callback: (events: AllEvents[], unsubscribe: () => void) => void,
51
ms: number,
52
account?: string,
53
timeout?: number
54
)
Copied!

Direct Initialization

If you'd like, you can also initialize the Query class as a standalone class like so:
1
import { Query } from "@superfluid-finance/sdk-core";
2
const query = new Query({
3
customSubgraphQueriesEndpoint: "<A_CUSTOM_ENDPOINT>",
4
dataMode: "SUBGRAPH_ONLY" | "SUBGRAPH_WEB3" | "WEB3_ONLY"
5
});
Copied!

Pagination

All of the pre-defined query functions will accept pagination options: ({ skip: number, take: number }), if you don't pass anything in, it will use a default of: { skip: 0, take: 100 }. You can also paginate by lastId, this allows you to bypass the limitation of the max skip of 5000 entities.
Note: this example uses the graphql-request library, but you just need to provide a valid query which is a string.

Ordering

You can also pass in an ordering object for the different queries, each query function will accept different ordering properties depending on the properties on the entity. We have different defaults for each so you don't need to actually pass anything in.

Example Usage:

1
const { Framework } = require("@superfluid-finance/sdk-core");
2
const { ethers } = require("ethers");
3
​
4
const provider = new ethers.providers.InfuraProvider(
5
"matic",
6
"<INFURA_API_KEY>"
7
);
8
const sf = await Framework.create({
9
networkName: "matic",
10
provider
11
});
12
const results = await sf.query.listAllSuperTokens(
13
{ isListed: true },
14
{ skip: 5, take: 150 },
15
{
16
orderBy: "createdAtBlockNumber",
17
orderDirection: "desc"
18
});
Copied!

Getting Data from Agreements

CFAV1 Read Operations

1
// Read functions
2
await sf.cfaV1.getFlow({
3
superToken: string,
4
sender: string,
5
receiver: string,
6
providerOrSigner: ethers.providers.Provider | ethers.Signer
7
});
8
​
9
await sf.cfaV1.getAccountFlowInfo({
10
superToken: string,
11
account: string,
12
providerOrSigner: ethers.providers.Provider | ethers.Signer
13
});
14
​
15
await sf.cfaV1.getNetFlow({
16
superToken: string,
17
account: string,
18
providerOrSigner: ethers.providers.Provider | ethers.Signer
19
});
Copied!

IDAV1 Read Operations

1
// Read functions
2
await sf.idaV1.getSubscription({
3
superToken: string,
4
publisher: string,
5
indexId: string,
6
subscriber: string,
7
providerOrSigner: string
8
});
9
​
10
await sf.idaV1.getIndex({
11
superToken: string,
12
publisher: string,
13
indexId: string,
14
providerOrSigner: string
15
});
Copied!

Super Token Read Operations

1
// Read functions
2
await usdcx.balanceOf({
3
account: string,
4
providerOrSigner: ethers.providers.Provider | ethers.Signer
5
});
6
​
7
await usdcx.allowance({
8
owner: string,
9
spender: string,
10
providerOrSigner: ethers.providers.Provider | ethers.Signer
11
});
12
​
13
await usdcx.name({
14
providerOrSigner: ethers.providers.Provider | ethers.Signer
15
});
16
​
17
await usdcx.symbol({
18
providerOrSigner: ethers.providers.Provider | ethers.Signer
19
});
20
​
21
await usdcx.totalSupply({
22
providerOrSigner: ethers.providers.Provider | ethers.Signer
23
});
Copied!