Skip to content

Commit

Permalink
docs: improved plugin docs example readability
Browse files Browse the repository at this point in the history
  • Loading branch information
grisha87 committed Nov 19, 2024
1 parent 76e8f71 commit 7b20dd2
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions docs/PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { GolemNetwork, GolemPluginInitializer } from "@golem-sdk/golem-js";
*/
const providerTracker: GolemPluginInitializer = (glm) => {
const seenProviders: { id: string; name: string }[] = [];

glm.market.events.on("offerProposalReceived", (event) => {
const { id, name } = event.offerProposal.provider;
const providerInfo = { id, name };
Expand All @@ -61,6 +62,7 @@ const providerTracker: GolemPluginInitializer = (glm) => {
console.log("Saw new provider %s named %s", id, name);
}
});

// Return a cleanup function that will be executed during the `disconnect`
return () => {
console.log("Provider tracker found a total of %d providers", seenProviders.length);
Expand All @@ -72,6 +74,7 @@ You can connect this plugin to your main script as follows:

```ts
const glm = new GolemNetwork();

// Register the plugin that will be initialized during `connect` call
glm.use(providerTracker);
```
Expand All @@ -89,6 +92,7 @@ const checkGlmPriceBeforeStarting: GolemPluginInitializer<{
}> = async (_glm, opts) => {
// Call an exchange to get the quotes
const response = await fetch("https://api.coinpaprika.com/v1/tickers/glm-golem");

if (!response.ok) {
throw new Error("Failed to fetch GLM price");
} else {
Expand All @@ -99,6 +103,7 @@ const checkGlmPriceBeforeStarting: GolemPluginInitializer<{
console.log("=== GLM Price ===");
console.log("GLM price is", price);
console.log("=== GLM Price ===");

if (price > opts.maxPrice) {
// Throwing inside the plugin will make `connect` throw, and then prevent
// execution of the rest of the script
Expand All @@ -111,9 +116,8 @@ const checkGlmPriceBeforeStarting: GolemPluginInitializer<{
Here's how to use the plugin:

```ts
import { GolemNetwork } from "./golem-network";

const glm = new GolemNetwork();

glm.use(checkGlmPriceBeforeStarting, {
maxPrice: 0.5,
});
Expand Down

0 comments on commit 7b20dd2

Please sign in to comment.