From 185787beee9e0ab22d368cd4b4b866ebc0cbd59a Mon Sep 17 00:00:00 2001 From: Grzegorz Godlewski Date: Wed, 4 Oct 2023 14:26:44 +0200 Subject: [PATCH] refactor: re-arranging code and providing docs and readme --- README.md | 76 ++++++++++++++++++++++++++++++++----------- src/index.ts | 2 +- src/market/helpers.ts | 7 ++++ src/market/index.ts | 2 +- 4 files changed, 66 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 2da5a0352..762436022 100644 --- a/README.md +++ b/README.md @@ -4,24 +4,25 @@ -- [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) - +- [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) + - [Limit price limits to filter out offers that are too expensive](#limit-price-limits-to-filter-out-offers-that-are-too-expensive) + - [Work with reliable providers](#work-with-reliable-providers) +- [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) @@ -158,7 +159,9 @@ that they define. As a Requestor, you might want to: 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: +your own market strategy. + +### Limit price limits to filter out offers that are too expensive ```typescript import { TaskExecutor, ProposalFilters } from "@golem-sdk/golem-js"; @@ -184,6 +187,41 @@ const executor = await TaskExecutor.create({ 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) +### Work with reliable providers + +The `getHealthyProvidersWhiteList` helper will provide you with a list of Provider ID's that were checked with basic health-checks. Using this whitelist will increase the chance of working with a reliable provider. Please note, that you can also build up your own list of favourite providers and use it in a similar fashion. + +```typescript +import { TaskExecutor, ProposalFilters, MarketHelpers } from "@golem-sdk/golem-js"; + +// Prepare the price filter +const acceptablePrice = ProposalFilters.limitPriceFilter({ + start: 1, + cpuPerSec: 1 / 3600, + envPerSec: 1 / 3600, +}); + +// Collect the whitelist +const verifiedProviders = await MarketHelpers.getHealthyProvidersWhiteList(); + +// Prepare the whitelist filter +const whiteList = ProposalFilters.whiteListProposalIdsFilter(verifiedProviders); + +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: async (proposal) => (await acceptablePrice(proposal)) && (await whiteList(proposal)), + + // Where you want to spend + payment: { + network: "polygon", + }, +}); +``` + ## See also - [Golem](https://golem.network), a global, open-source, decentralized supercomputer that anyone can access. diff --git a/src/index.ts b/src/index.ts index 6a009a77d..ea0ebfd89 100755 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ export { } from "./storage"; export { ActivityStateEnum, Result } from "./activity"; export { AgreementCandidate, AgreementSelectors } from "./agreement"; -export { ProposalFilters, ProposalFilter, Helpers } from "./market"; +export { ProposalFilters, ProposalFilter, MarketHelpers } from "./market"; export { Package, PackageOptions } from "./package"; export { PaymentFilters } from "./payment"; export { Events, BaseEvent, EventType } from "./events"; diff --git a/src/market/helpers.ts b/src/market/helpers.ts index 282433e23..a8d5eb4f2 100644 --- a/src/market/helpers.ts +++ b/src/market/helpers.ts @@ -1,3 +1,10 @@ +/** + * Helps to obtain a whitelist of providers which were health-tested. + * + * Important: This helper requires internet access to function properly. + * + * @return An array with Golem Node IDs of the whitelisted providers. In case of any issues, it will return an empty whitelist. + */ export async function getHealthyProvidersWhiteList(): Promise { try { const response = await fetch("https://provider-health.golem.network/v1/provider-whitelist"); diff --git a/src/market/index.ts b/src/market/index.ts index 4070b9506..5ea8eca87 100644 --- a/src/market/index.ts +++ b/src/market/index.ts @@ -4,4 +4,4 @@ export { Proposal, ProposalDetails } from "./proposal"; export { MarketDecoration } from "./builder"; export { DemandConfig } from "./config"; export * as ProposalFilters from "./strategy"; -export * as Helpers from "./helpers"; +export * as MarketHelpers from "./helpers";