How to implement a custom writer?
If you want more control over the writing part of the mintBlue Machine, you can implement a custom MintblueWriter. In this tutorial, you'll learn which methods you need to implement to build a successful custom Writer.
We recommend you complete the Getting Started section before implementing a custom Writer.
A Writer class expects only three methods to be implemented. The fetchWriteStatus() function is optional.
- static async create(sdkToken, project_id)
- async write(action)
Below is the skeleton for your class. The constructor expects an initialized mintBlue client object and your project ID.
Implementation of the create() method is optional. However, it is recommended to define this method to facilitate the instantiation of the class. Direct instantiation via the constructor is not possible for a mintblue object due to the requirement of asynchronous logic.
The create() function is a static method to create an instance of your CustomMintblueWriter. Make sure to create a new instance of your class name. It uses an instantiated mintBlue client object and your project ID.
You can omit the
The write logic sends an action to the mintBlue platform, writing it to storage. An action always contains the function name f and function arguments args. You must use the createTransaction function to send the data to the mintBlue platform.
The createTransaction function expects an object with outputs. First of all, you need to send a data output. The value property expects the function name f and function arguments args. However, you can modify this object and add optional data like a time property.
It's also possible to send multiple data outputs, like an additional data output, or a micropayment or file output.
Here's an example with an additional data output.
Finally, let's use your CustomMintblueWriter. Ensure you import your Writer and initialize it like in the Getting Started tutorial.
Additionally, you can verify if your custom Writer works as expected by inspecting your transactions in the mintBlue project overview. Navigate to your project's transactions tab, and you can verify the transaction and its data.
Click the "View" button to inspect the transaction details. You'll find a similar output with one or multiple data outputs like the JSON data below.
Note: mintBlue adds a payment output as the last input to store the data on the chain.
Below, you can find the full code implementation.