Hook
Hooks are functions that can be added to the Loyalty Point contract to extend its functionality. They are executed during token transfers and can be used to implement custom logic, such as access control, rewards distribution, or external event triggers.
Creating a Hook
To create a hook, you need to implement the ILoyaltyPointHook
interface:
Now let’s see how to create a hook and add it to your Loyalty Point contract. For this example, we’ll create a hook that will trigger an event when user points are updated.
Deploying the Hook
Set up the environment variables:
Deploy the hook:
Setup hook on Loyalty Point contract
Set up necessary environment variables:
PRIVATE_KEY
: The private key of the admin account of the Loyalty Point contract.LOYALTY_POINT_ADDRESS
: The address of the Loyalty Point contract.HOOK_ADDRESS
: The address of the hook contract.
Add the hook to the Loyalty Point contract:
Testing the Hook
You can test the hook by minting or burning points to a user. The hook should be triggered and the event should be emitted.
Example
An example of a hook that distributes rewards to a user has been deployed here.
And here is a transaction that triggers the hook: 0xa4d01f742a1f532cecdb3b0c0a1a3b0922603cec4cf0edf46bd00c549ad03b75.
In the transaction logs, you can see that the hook is called and the PointsUpdated
event is emitted.