Example

JS Task API Examples: switching to mainnet

Introduction

This section is aimed mainly at requestors wishing to switch from running simple test tasks on our testnet to launching production payloads utilizing the vast number of providers on the mainnet.

By default, the JS SDK will execute tasks on the testnet. It is a development network with a limited amount of providers and limited amounts of computational resources available. If you would rather need a larger amount of providers to compute on, you should switch to the mainnet.

In this article, we will show how to run your tasks on Polygon.

Prerequisites

Yagna service is installed and running with the try_golem app-key configured.

How to run examples

Create a project folder, initialize a Node.js project, and install libraries.

mkdir golem-example
cd golem-example
npm init
npm i @golem-sdk/task-executor
npm i @golem-sdk/pino-logger

Copy the code into the index.mjs file in the project folder and run:

node index.mjs

Running your tasks on the Polygon Network

In this example, we create the TaskExecutor with additional parameters that indicate we intend to run our task on the Polygon platform.

import { TaskExecutor } from "@golem-sdk/task-executor";
import { pinoPrettyLogger } from "@golem-sdk/pino-logger";

(async () => {
  const executor = await TaskExecutor.create({
    logger: pinoPrettyLogger(),
    api: { key: "try_golem" },
    demand: {
      workload: {
        imageTag: "golem/node:20-alpine",
      },
    },
    market: {
      rentHours: 0.5,
      pricing: {
        model: "linear",
        maxStartPrice: 0.5,
        maxCpuPerHourPrice: 1.0,
        maxEnvPerHourPrice: 0.5,
      },
    },
    payment: { network: "polygon" },
  });

  try {
    const result = await executor.run(async (exe) => (await exe.run("node -v")).stdout);
    console.log("Task result:", result);
  } catch (err) {
    console.error("Computation failed:", error);
  } finally {
    await executor.shutdown();
  }
})();

In the script output, you can see that now the network is a Polygon.

[12:59:00.166] INFO: Created allocation {"allocationId":"d833b2a8-971c-422f-8573-96c757c250ac","budget":"20","platform":"erc20-polygon-glm"}