Skip to content

Commit

Permalink
feat: added limitPriceFilter strategy to the SDK itself, updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
grisha87 committed Aug 31, 2023
1 parent 0a92dad commit 082bbd3
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 14 deletions.
88 changes: 80 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
# Golem JavaScript API

## Table of contents

<!-- TOC -->

- [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)
<!-- TOC -->

![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)
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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/)
2 changes: 1 addition & 1 deletion src/executor/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions src/market/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
};
10 changes: 5 additions & 5 deletions src/utils/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 082bbd3

Please sign in to comment.