Skip to content

Commit d56ba76

Browse files
committed
add more plugin creation docs
1 parent c66056a commit d56ba76

File tree

2 files changed

+94
-7
lines changed

2 files changed

+94
-7
lines changed

docs/plugins.md

+41-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,51 @@
22

33
## Creating a Plugin
44

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

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

911
```
10-
cp -r plugins/_template plugins/{my_plugin_id}
12+
cp -r plugins/_template plugins/degenz
13+
```
14+
15+
Next update the naming of various variables and classes to match your dapp, for example:
16+
17+
```
18+
export const _TEMPLATE_PLUGIN_ID = "_template_plugin"; -> export const DEGENZ_PLUGIN_ID = "degenz";
19+
interface _TemplatePluginConfig -> interface DegenzPluginConfig
20+
class TemplatePlugin -> class DegenzPlugin
21+
```
22+
23+
The template contains placeholder implementations for each plugin method, update these to implement your plugins logic as required.
24+
`onStart` is where you should do any initialization logic such as starting of tasks, etc.
25+
`onStop` is where any clean-up logic should be placed, destroying of tasks, persisting of any data, etc.
26+
27+
Finally register your plugin for usage by added it to `pluginConstructorMap` in `plugins/mod.ts` like so:
28+
29+
```ts
30+
31+
import { PluginConstructor } from "../src/plugins/plugin.ts";
32+
import { EXAMPLE_PLUGIN_ID, ExamplePlugin } from "./example_plugin/mod.ts";
33+
import { DEGENZ_PLUGIN_ID, DegenzPlugin } from "./degenz/mod.ts";
34+
35+
export const pluginConstructorMap: Record<string, PluginConstructor> = {
36+
[EXAMPLE_PLUGIN_ID]: ExamplePlugin,
37+
[DEGENZ_PLUGIN_ID]: DegenzPlugin,
38+
};
1139
```
1240

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

43+
```toml
44+
logLevel: INFO
45+
plugins:
46+
- id: degenz
47+
enabled: true
48+
logLevel: DEBUG
49+
config:
50+
configValue: "testing"
51+
otherConfigValue: 5
52+
```

plugins/_template/mod.ts

+53-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import { Block, SignedTransaction } from "@fleet-sdk/common";
2+
import { BlockchainSnapshot } from "../../src/blockchain/blockchain_monitor.ts";
13
import { Plugin, PluginDescriptor } from "../../src/plugins/mod.ts";
24
import { z } from "zod/mod.ts";
35

46
export const _TEMPLATE_PLUGIN_ID = "_template_plugin";
57

68
interface _TemplatePluginConfig {
7-
tokenId: string;
8-
exitAtPage: number;
9+
configValue: string;
10+
otherConfigValue: number;
911
}
1012

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

2426
onStart(): Promise<void> {
27+
this.logger.info(`started with configuration: ${this.config}`);
28+
29+
return Promise.resolve();
30+
}
31+
32+
onStop(): Promise<void> {
33+
this.logger.info("Plugin shutting down, performing cleanup!");
34+
35+
return Promise.resolve();
36+
}
37+
38+
onNewBlock(
39+
block: Block,
40+
snapshot: Readonly<BlockchainSnapshot>,
41+
): Promise<void> {
42+
this.logger.info(`block: ${block}, snapshot: ${snapshot}`);
43+
44+
return Promise.resolve();
45+
}
46+
47+
onMempoolTx(
48+
tx: SignedTransaction,
49+
snapshot: Readonly<BlockchainSnapshot>,
50+
): Promise<void> {
51+
this.logger.info(`mempool tx: ${tx}, snapshot: ${snapshot}`);
52+
53+
return Promise.resolve();
54+
}
55+
56+
onIncludedTx(
57+
tx: SignedTransaction,
58+
snapshot: Readonly<BlockchainSnapshot>,
59+
): Promise<void> {
60+
this.logger.info(`tx included in block: ${tx}, snapshot: ${snapshot}`);
61+
62+
return Promise.resolve();
63+
}
64+
65+
onMempoolTxDrop(
66+
tx: SignedTransaction,
67+
snapshot: Readonly<BlockchainSnapshot>,
68+
): Promise<void> {
69+
this.logger.warning(
70+
`tx dropped from mempool without being included in block: ${tx}, snapshot: ${snapshot}`,
71+
);
72+
2573
return Promise.resolve();
2674
}
2775

2876
// deno-lint-ignore no-explicit-any
2977
configSchema(): z.ZodObject<any> | undefined {
3078
return z.object({
31-
tokenId: z.string(),
32-
exitAtPage: z.number(),
79+
configValue: z.string(),
80+
otherConfigValue: z.number(),
3381
});
3482
}
3583
}

0 commit comments

Comments
 (0)