-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
82 lines (68 loc) · 2.82 KB
/
index.ts
File metadata and controls
82 lines (68 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { NodeTNClient, StreamId, EthereumAddress } from "@trufnetwork/sdk-js";
import { Wallet } from "ethers";
const wallet = new Wallet("0000000000000000000000000000000000000000000000000000000000000001");
const client = new NodeTNClient({
endpoint: "https://gateway.mainnet.truf.network",
signerInfo: {
address: wallet.address,
signer: wallet,
},
chainId: "tn-v2.1",
timeout: 30000,
});
// Create a stream locator for the AI Index
const aiIndexLocator = {
streamId: StreamId.fromString("st527bf3897aa3d6f5ae15a0af846db6").throw(),
dataProvider: EthereumAddress.fromString("0x4710a8d8f0d845da110086812a32de6d90d7ff5c").throw(),
};
// Load the action client
const stream = client.loadAction();
// Get the latest records
const records = await stream.getRecord({
stream: aiIndexLocator,
});
console.log("AI Index records:", records);
// Example: Query recent taxonomies using the new taxonomy querying functionality
console.log("\n=== Taxonomy Querying Examples ===");
try {
// Get recent taxonomies with the new SDK methods
const recentTaxonomies = await client.listTaxonomiesByHeight({
fromHeight: 180000,
toHeight: 190000,
limit: 5,
latestOnly: true
});
console.log(`Found ${recentTaxonomies.length} recent taxonomies:`);
recentTaxonomies.forEach((taxonomy, index) => {
console.log(`${index + 1}. Stream: ${taxonomy.streamId.getId()}`);
console.log(` Child: ${taxonomy.childStreamId.getId()}`);
console.log(` Weight: ${taxonomy.weight}`);
console.log(` Block Height: ${taxonomy.createdAt}`);
});
// Example: Get taxonomies for specific streams (batch processing)
console.log("\n--- Batch Processing Example ---");
// Use first few unique streams from results
const uniqueStreams = new Map();
recentTaxonomies.forEach(t => {
const key = `${t.dataProvider.getAddress()}_${t.streamId.getId()}`;
if (!uniqueStreams.has(key)) {
uniqueStreams.set(key, {
dataProvider: t.dataProvider,
streamId: t.streamId
});
}
});
const streamsToQuery = Array.from(uniqueStreams.values()).slice(0, 2);
if (streamsToQuery.length > 0) {
const batchResults = await client.getTaxonomiesForStreams({
streams: streamsToQuery,
latestOnly: true
});
console.log(`Batch query results for ${streamsToQuery.length} streams:`);
batchResults.forEach((result) => {
console.log(`- ${result.streamId.getId()} -> ${result.childStreamId.getId()} (${result.weight})`);
});
}
} catch (error) {
console.log("Taxonomy querying example error (expected if no taxonomies exist):", error.message);
}