diff --git a/README.md b/README.md index d9f8c1b4a..b211150ff 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,28 @@ # Golem JavaScript API +## Table of contents + + + +- [Golem JavaScript API](#golem-javascript-api) + - [Table of contents](#table-of-contents) + - [What's Golem and `golem-js`?](#whats-golem-and-golem-js) + - [Golem application development](#golem-application-development) + - [Installation](#installation) + - [Building](#building) + - [Usage](#usage) + - [Node.js context](#nodejs-context) + - [Web Browser context](#web-browser-context) + - [Testing](#testing) + - [Running unit tests](#running-unit-tests) + - [Running E2E tests](#running-e2e-tests) + - [NodeJS](#nodejs) + - [Cypress](#cypress) + - [Contributing](#contributing) + - [Controlling interactions and costs](#controlling-interactions-and-costs) + - [See also](#see-also) + + ![GitHub](https://img.shields.io/github/license/golemfactory/golem-js) ![npm](https://img.shields.io/npm/v/@golem-sdk/golem-js) ![node-current](https://img.shields.io/node/v/@golem-sdk/golem-js) @@ -10,13 +33,18 @@ ## What's Golem and `golem-js`? -**[The Golem Network](https://golem.network)** fosters a global group of creators building ambitious software solutions that will shape the technological landscape of future generations by accessing computing resources across the platform. Golem Network is an accessible, reliable, open access and censorship-resistant protocol, democratizing access to digital resources and connecting users through a flexible, open-source platform. +**[The Golem Network](https://golem.network)** fosters a global group of creators building ambitious software solutions +that will shape the technological landscape of future generations by accessing computing resources across the platform. +Golem Network is an accessible, reliable, open access and censorship-resistant protocol, democratizing access to digital +resources and connecting users through a flexible, open-source platform. -**@golem-sdk/golem-js** is the JavaScript API that allows developers to connect to their Golem nodes and manage their distributed, computational loads through Golem Network. +**@golem-sdk/golem-js** is the JavaScript API that allows developers to connect to their Golem nodes and manage their +distributed, computational loads through Golem Network. ## Golem application development -For a detailed introduction to using Golem and `@golem-sdk/golem-js` to run your tasks on Golem [please consult our quickstart section](https://docs.golem.network/creators/javascript/quickstart/). +For a detailed introduction to using Golem and `@golem-sdk/golem-js` to run your tasks on +Golem [please consult our quickstart section](https://docs.golem.network/creators/javascript/quickstart/). ### Installation @@ -68,7 +96,8 @@ import { TaskExecutor } from "@golem-sdk/golem-js"; ![hello_web](https://user-images.githubusercontent.com/26308335/217530424-a1dd4487-f95f-43e6-a91b-7106b6f30802.gif) -For more detailed usage examples and tutorials, see the [Java Script API section of the Golem Network Docs](https://docs.golem.network/creators/javascript/) +For more detailed usage examples and tutorials, see +the [Java Script API section of the Golem Network Docs](https://docs.golem.network/creators/javascript/) ### Testing @@ -84,9 +113,13 @@ yarn test:unit ### Running E2E tests -Both test cases for the NodeJS environment and the browser (cypress) require preparation of a test environment of the Golem Network with providers and all the necessary infrastructure. [Goth](https://github.com/golemfactory/goth) framework is used for this purpose. +Both test cases for the NodeJS environment and the browser (cypress) require preparation of a test environment of the +Golem Network with Providers and all the necessary infrastructure. [Goth](https://github.com/golemfactory/goth) +framework is used for this purpose. -To enable E2E testing, you need to ensure that `python -m goth` is executable. Therefore, you must first install [Goth](https://github.com/golemfactory/goth) according to the instructions described in the readme of the project. +To enable E2E testing, you need to ensure that `python -m goth` is executable. Therefore, you must first +install [Goth](https://github.com/golemfactory/goth) according to the instructions described in the readme of the +project. #### NodeJS @@ -114,12 +147,51 @@ yarn lint yarn format ``` +## Controlling interactions and costs + +The Golem Network provides an open marketplace where anyone can join as a Provider and supply the network with their +computing power. In return for their service, they are billing Requestors (users of this SDK) according to the pricing +that they define. As a Requestor, you might want to: + +- control the limit price so that you're not going to over-spend your funds +- control the interactions with the providers if you have a list of the ones which you like or the ones which you would + like to avoid + +To make this easy, we provided you with a set of predefined market proposal filters, which you can combine to implement +your own market strategy. For example: + +```typescript +import { TaskExecutor, ProposalFilters } from "@golem-sdk/golem-js"; + +const executor = await TaskExecutor.create({ + // What do you want to run + package: "golem/alpine:3.18.2", + + // How much you wish to spend + budget: 0.5, + proposalFilter: ProposalFilters.limitPriceFilter({ + start: 1, + cpuPerSec: 1 / 3600, + envPerSec: 1 / 3600, + }), + + // Where you want to spend + payment: { + network: "polygon", + }, +}); +``` + +To learn more about other filters, please check the [API reference of the market/strategy module](https://docs.golem.network/docs/golem-js/reference/modules/market_strategy) + ## See also - [Golem](https://golem.network), a global, open-source, decentralized supercomputer that anyone can access. - Learn what you need to know to set up your Golem requestor node: - [Requestor development: a quick primer](https://handbook.golem.network/requestor-tutorials/flash-tutorial-of-requestor-development) - [Quick start](https://docs.golem.network/creators/javascript/quickstart/) -- Have a look at the most important concepts behind any Golem application: [Golem application fundamentals](https://handbook.golem.network/requestor-tutorials/golem-application-fundamentals) -- Learn about preparing your own Docker-like images for the [VM runtime](https://handbook.golem.network/requestor-tutorials/vm-runtime) +- Have a look at the most important concepts behind any Golem + application: [Golem application fundamentals](https://handbook.golem.network/requestor-tutorials/golem-application-fundamentals) +- Learn about preparing your own Docker-like images for + the [VM runtime](https://handbook.golem.network/requestor-tutorials/vm-runtime) - Write your own app with [JavaScript API](https://docs.golem.network/creators/javascript/high-level/task-model/) diff --git a/src/executor/config.ts b/src/executor/config.ts index 2785d2fd4..0c71c4e88 100644 --- a/src/executor/config.ts +++ b/src/executor/config.ts @@ -7,7 +7,7 @@ const DEFAULTS = Object.freeze({ payment: { driver: "erc20", network: "goerli" }, budget: 1.0, subnetTag: "public", - logLevel: LogLevel.info, + logLevel: LogLevel.Info, basePath: "http://127.0.0.1:7465", maxParallelTasks: 5, taskTimeout: 1000 * 60 * 5, // 5 min, diff --git a/src/market/strategy.ts b/src/market/strategy.ts index 338a24dec..b517a8c21 100644 --- a/src/market/strategy.ts +++ b/src/market/strategy.ts @@ -26,3 +26,24 @@ export const whiteListProposalNamesFilter = (whiteListNames: string[]) => async /** Proposal filter that only allows offers from a provider whose name match to the regexp */ export const whiteListProposalRegexpFilter = (regexp: RegExp) => async (proposal: Proposal) => !!proposal.provider.name.match(regexp); + +type PriceLimits = { + start: number; + cpuPerSec: number; + envPerSec: number; +}; + +/** + * Proposal filter only allowing offers that do not exceed the defined usage + * + * @param priceLimits.start The maximum start price in GLM + * @param priceLimits.cpuPerSec The maximum price for CPU usage in GLM/s + * @param priceLimits.envPerSec The maximum price for the duration of the activity in GLM/s + */ +export const limitPriceFilter = (priceLimits: PriceLimits) => async (proposal: Proposal) => { + return ( + proposal.pricing.cpuSec < priceLimits.cpuPerSec && + proposal.pricing.envSec <= priceLimits.envPerSec && + proposal.pricing.start <= priceLimits.start + ); +}; diff --git a/src/utils/logger/logger.ts b/src/utils/logger/logger.ts index f6c455622..7d28030b6 100644 --- a/src/utils/logger/logger.ts +++ b/src/utils/logger/logger.ts @@ -1,9 +1,9 @@ export enum LogLevel { - debug = "debug", - info = "info", - warn = "warn", - error = "error", - log = "log", + Debug = "debug", + Info = "info", + Warn = "warn", + Error = "error", + Log = "log", } export interface Logger { level: string;