-
Notifications
You must be signed in to change notification settings - Fork 19
/
scan.ts
67 lines (62 loc) · 1.87 KB
/
scan.ts
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
/**
* This example demonstrates how to scan the market for providers that meet specific requirements.
*/
import { GolemNetwork, ScanOptions } from "@golem-sdk/golem-js";
import { last, map, scan, takeUntil, tap, timer } from "rxjs";
// What providers are we looking for?
const scanOptions: ScanOptions = {
// fairly powerful machine but not too powerful
workload: {
runtime: {
name: "vm",
},
minCpuCores: 4,
maxCpuCores: 16,
minMemGib: 4,
maxMemGib: 8,
capabilities: ["vpn"],
minStorageGib: 16,
},
// let's look at mainnet providers only
payment: {
network: "polygon",
},
};
(async () => {
const glm = new GolemNetwork();
await glm.connect();
const spec = glm.market.buildScanSpecification(scanOptions);
// For advanced users: you can also add constraints manually:
// spec.constraints.push("(golem.node.id.name=my-favorite-provider)");
const SCAN_DURATION_MS = 10_000;
console.log(`Scanning for ${SCAN_DURATION_MS / 1000} seconds...`);
glm.market
.scan(spec)
.pipe(
tap((scannedOffer) => {
console.log("Found offer from", scannedOffer.provider.name);
}),
// calculate the cost of an hour of work
map(
(scannedOffer) =>
scannedOffer.pricing.start + //
scannedOffer.pricing.cpuSec * 3600 +
scannedOffer.pricing.envSec * 3600,
),
// calculate the running average
scan((total, cost) => total + cost, 0),
map((totalCost, index) => totalCost / (index + 1)),
// stop scanning after SCAN_DURATION_MS
takeUntil(timer(SCAN_DURATION_MS)),
last(),
)
.subscribe({
next: (averageCost) => {
console.log("Average cost for an hour of work:", averageCost.toFixed(6), "GLM");
},
complete: () => {
console.log("Scan completed, shutting down...");
glm.disconnect();
},
});
})();