diff --git a/examples/basic/transfer.ts b/examples/basic/transfer.ts new file mode 100644 index 000000000..b3596d4dd --- /dev/null +++ b/examples/basic/transfer.ts @@ -0,0 +1,67 @@ +import { MarketOrderSpec, GolemNetwork } from "@golem-sdk/golem-js"; +import { pinoPrettyLogger } from "@golem-sdk/pino-logger"; +import { appendFile, readFile, unlink } from "fs/promises"; + +const order: MarketOrderSpec = { + demand: { + workload: { imageTag: "golem/alpine:latest" }, + }, + market: { + maxAgreements: 2, + rentHours: 0.5, + pricing: { + model: "linear", + maxStartPrice: 0.5, + maxCpuPerHourPrice: 1.0, + maxEnvPerHourPrice: 0.5, + }, + }, +}; + +(async () => { + const glm = new GolemNetwork({ + logger: pinoPrettyLogger({ + level: "info", + }), + }); + + try { + await glm.connect(); + const pool = await glm.manyOf({ + concurrency: 2, + order, + }); + const lease1 = await pool.acquire(); + const lease2 = await pool.acquire(); + + const exe1 = await lease1.getExeUnit(); + const exe2 = await lease2.getExeUnit(); + + await exe1 + .beginBatch() + .run(`echo "Message from provider ${exe1.provider.name}. Hello 😻" >> /golem/work/message.txt`) + .downloadFile("/golem/work/message.txt", "./message.txt") + .end(); + + await appendFile("./message.txt", "Message from requestor. Hello 🤠\n"); + + await exe2 + .beginBatch() + .uploadFile("./message.txt", "/golem/work/message.txt") + .run(`echo "Message from provider ${exe2.provider.name}. Hello 👻" >> /golem/work/message.txt`) + .downloadFile("/golem/work/message.txt", "./results.txt") + .end(); + + console.log("File content: "); + console.log(await readFile("./results.txt", { encoding: "utf-8" })); + + await lease1.finalize(); + await lease2.finalize(); + } catch (err) { + console.error("Failed to run the example", err); + } finally { + await glm.disconnect(); + await unlink("./message.txt"); + await unlink("./results.txt"); + } +})().catch(console.error); diff --git a/examples/package.json b/examples/package.json index 3c0f0b8f9..4f92cb6f6 100644 --- a/examples/package.json +++ b/examples/package.json @@ -8,6 +8,7 @@ "basic-one-of": "tsx basic/one-of.ts", "basic-many-of": "tsx basic/many-of.ts", "basic-vpn": "tsx basic/vpn.ts", + "basic-transfer": "tsx basic/transfer.ts", "advanced-hello-world": "tsx advanced/hello-world.ts", "advanced-manual-pools": "tsx advanced/manual-pools.ts", "local-image": "tsx advanced/local-image/serveLocalGvmi.ts", diff --git a/tests/examples/examples.json b/tests/examples/examples.json index d81827926..3cd42ebb6 100644 --- a/tests/examples/examples.json +++ b/tests/examples/examples.json @@ -2,6 +2,7 @@ { "cmd": "tsx", "path": "examples/basic/many-of.ts" }, { "cmd": "tsx", "path": "examples/basic/one-of.ts" }, { "cmd": "tsx", "path": "examples/basic/vpn.ts" }, + { "cmd": "tsx", "path": "examples/basic/transfer.ts" }, { "cmd": "tsx", "path": "examples/advanced/hello-world.ts" }, { "cmd": "tsx", "path": "examples/advanced/manual-pools.ts" }, { "cmd": "tsx", "path": "examples/experimental/deployment/new-api.ts" }