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 the @golem-sdk/task-executor
library.
mkdir golem-example
cd golem-example
npm init
npm i @golem-sdk/task-executor
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, pinoPrettyLogger } from "@golem-sdk/task-executor";
(async () => {
const executor = await TaskExecutor.create({
payment: { network: "polygon" },
package: "golem/node:20-alpine",
logger: pinoPrettyLogger(),
yagnaOptions: { apiKey: "try_golem" },
});
try {
const result = await executor.run(async (ctx) => (await ctx.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
.