Integrate your Open Action in a website using the widgets SDK
The easiest way to integrate your Smart Post in a website
Last updated
The easiest way to integrate your Smart Post in a website
Last updated
Our Lens React Widgets SDK (forked from lens-protocol) allows anyone to render a Publication
component that supports Smart Posts (open actions).
Every open action module that is supported by the SDK renders the appropriate call-to-action, takes in the necessary inputs, and encodes the data necessary for the module's processPublicationAction
.
The development flow to get an open action module supported by the SDK is generally
deploy your open action to mumbai or polygon, and register the module metadata
fork our widgets repo: https://github.com/mad-finance/lens-widgets
add a new handler class in src/actions/handlers.ts
that implements the abstract HandlerBase
class
implement the necessary functions, including fetchActionModuleData
, getActionModuleConfig
, and others.
add your handler to the registry in src/actions/registry.ts
add a new modal component in src/actions/modals
to handle the "act" process - taking input form data, fetching state, and sending the act transaction
test that everything works
submit a PR to the widgets repo and see your open action featured on MadFi + other front-ends
For the rest of this guide, we'll be looking at our Crosschain Zora Mint action. Try it on testnet:
Install the SDK and get it set up with your local Nextjs/React app. There are some important peer dependencies listed in the readme, including a necessary nextjs config.
If you look at the src/actions/handlers/
directory, you'll see HandlerBase
and then a handler for each supported open action. Instances of these classes will manage everything around your open action, including
module metadata (name, description, button labels)
fetching any onchain / api data needed to render the correct state
how to encode init data + module act data
Let's look at the HandlerBase
class that all handlers must implement
You'll see a list of functions and their expected return types, and any custom functions are defined in each handler class. For example, let's look at the implementation for getActionModuleConfig
in the ZoraLzMintAction
handler
We assume you're registered your module's metadata with the Lens ModuleRegistry
(see their docs).
Another important function to implement is getActButtonLabel.
This function is called from within the Publication
component to render the "act" button on the bottom right.
Notice this function accesses a class variable this.hasMinted
which is actually set within the fetchActionModuleData
function.
Generally speaking, you should view your handler as a client which has all the data + utilities necessary to render a useful modal to the user before sending the act transaction. The full source code for the ZoraLzMintAction
handler can be found here.
We didn't go over every single function in the base class, but you can see what types are expected to be returned, and it'll be easier to fill in when you view it from the perspective of the modal.
When the Publication
component is rendered with publication data (a Lens post) it will look at the open action modules it was initialized with, and check whether the SDK has that address in the registry at src/actions/registry.ts
Here is the meat of that file
Now, in the Publication
component you'll find that it uses a hook called useSupportedActionModule
This hook returns 3 variables
isActionModuleSupported
boolean
whether the action module that the publication was initialized is supported by the sdk, ie its address was found in the registry
actionModuleHandler
implementation of HandlerBase
an instance of our handler class
isLoading
boolean
whether we are fetching the state data needed by the module to render the correct info
If isActionModuleSupported
is true
, that hook is already fetching the state data needed using the implemented function fetchActionModuleData
in our handler.
Once isLoading
flips to false, we'll see the CTA button rendered on the Publication
component.
Most of the time, we'll need to show the user more information before expecting them to sign / send any transaction - especially for the more complicated open actions.
For example, with ZoraLzMintAction
, we need to show the user the estimated quote for the cost of their crosschain mint - which includes service fees + a relay fee. Moreover, we'll need to show them some information after they've submitted the act transaction.
This is what our modal for ZoraLzMintAction
looks like
Again, the input needed for the action module encoded data is defined in the handler class.
The full source code for our modal can be found here, and the general template for the rendered component is
The most important thing this modal does is handle the "act" transaction, and in the case of this ZoraLzMintAction
, we can see its handleAct
function here broken into 3 steps
If you followed along with this guide, you should have a general idea for the steps necessary to have your open action module supported by the widgets sdk. Of course, the best way to fully grasp all the pieces is to run the project locally and see how the current open actions are implemented.
Both the ZoraLzMintAction and the Promote Social Club actions are part of the SDK and live on MadFi, and you can find their respective handlers and modals in the repo.
Finally, please submit any feedback/issues in the github issues.
Since this SDK does not export any CreatePost
component, it's expected that your app will handle this. You can still include the necessary functions in your handler class, to be imported in your app.