diff --git a/docs/PLUGINS.md b/docs/PLUGINS.md index 405e858af..693867617 100644 --- a/docs/PLUGINS.md +++ b/docs/PLUGINS.md @@ -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 }; @@ -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); @@ -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); ``` @@ -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 { @@ -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 @@ -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, });