Skip to content

Commit

Permalink
add more plugin creation docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ross-weir committed Oct 9, 2023
1 parent c66056a commit d56ba76
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 7 deletions.
43 changes: 41 additions & 2 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,51 @@

## Creating a Plugin

Firstly decide on a `id` for your plugin, this should be in `snake_case` format.
For this exercise lets assume I am creating a plugin for my dapp that requires off-chain bot functionality, my dapp is called "Degenz".

Firstly decide on a `id` for your plugin, this should be in `snake_case` format, in my case I will use `degenz`.

The easiest way to get started is to use the provided plugin template:

```
cp -r plugins/_template plugins/{my_plugin_id}
cp -r plugins/_template plugins/degenz
```

Next update the naming of various variables and classes to match your dapp, for example:

```
export const _TEMPLATE_PLUGIN_ID = "_template_plugin"; -> export const DEGENZ_PLUGIN_ID = "degenz";
interface _TemplatePluginConfig -> interface DegenzPluginConfig
class TemplatePlugin -> class DegenzPlugin
```

The template contains placeholder implementations for each plugin method, update these to implement your plugins logic as required.
`onStart` is where you should do any initialization logic such as starting of tasks, etc.
`onStop` is where any clean-up logic should be placed, destroying of tasks, persisting of any data, etc.

Finally register your plugin for usage by added it to `pluginConstructorMap` in `plugins/mod.ts` like so:

```ts

import { PluginConstructor } from "../src/plugins/plugin.ts";
import { EXAMPLE_PLUGIN_ID, ExamplePlugin } from "./example_plugin/mod.ts";
import { DEGENZ_PLUGIN_ID, DegenzPlugin } from "./degenz/mod.ts";

export const pluginConstructorMap: Record<string, PluginConstructor> = {
[EXAMPLE_PLUGIN_ID]: ExamplePlugin,
[DEGENZ_PLUGIN_ID]: DegenzPlugin,
};
```

Now you should be all set to add your plugin to your `ergomatic` configuration file. For example:

```toml
logLevel: INFO
plugins:
- id: degenz
enabled: true
logLevel: DEBUG
config:
configValue: "testing"
otherConfigValue: 5
```
58 changes: 53 additions & 5 deletions plugins/_template/mod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Block, SignedTransaction } from "@fleet-sdk/common";
import { BlockchainSnapshot } from "../../src/blockchain/blockchain_monitor.ts";
import { Plugin, PluginDescriptor } from "../../src/plugins/mod.ts";
import { z } from "zod/mod.ts";

export const _TEMPLATE_PLUGIN_ID = "_template_plugin";

interface _TemplatePluginConfig {
tokenId: string;
exitAtPage: number;
configValue: string;
otherConfigValue: number;
}

export class _TemplatePlugin extends Plugin<_TemplatePluginConfig> {
Expand All @@ -15,21 +17,67 @@ export class _TemplatePlugin extends Plugin<_TemplatePluginConfig> {
name: "Template Plugin",
// Description of your plugins functionality and anything else users should be aware of
description:
"This is an example plugin showcasing how to create & implement ergomatic plugins.",
"Template for developers to get started creating their own plugins",
// Version of your plugin
version: "0.1.0",
};
}

onStart(): Promise<void> {
this.logger.info(`started with configuration: ${this.config}`);

return Promise.resolve();
}

onStop(): Promise<void> {
this.logger.info("Plugin shutting down, performing cleanup!");

return Promise.resolve();
}

onNewBlock(
block: Block,
snapshot: Readonly<BlockchainSnapshot>,
): Promise<void> {
this.logger.info(`block: ${block}, snapshot: ${snapshot}`);

return Promise.resolve();
}

onMempoolTx(
tx: SignedTransaction,
snapshot: Readonly<BlockchainSnapshot>,
): Promise<void> {
this.logger.info(`mempool tx: ${tx}, snapshot: ${snapshot}`);

return Promise.resolve();
}

onIncludedTx(
tx: SignedTransaction,
snapshot: Readonly<BlockchainSnapshot>,
): Promise<void> {
this.logger.info(`tx included in block: ${tx}, snapshot: ${snapshot}`);

return Promise.resolve();
}

onMempoolTxDrop(
tx: SignedTransaction,
snapshot: Readonly<BlockchainSnapshot>,
): Promise<void> {
this.logger.warning(
`tx dropped from mempool without being included in block: ${tx}, snapshot: ${snapshot}`,
);

return Promise.resolve();
}

// deno-lint-ignore no-explicit-any
configSchema(): z.ZodObject<any> | undefined {
return z.object({
tokenId: z.string(),
exitAtPage: z.number(),
configValue: z.string(),
otherConfigValue: z.number(),
});
}
}

0 comments on commit d56ba76

Please sign in to comment.