Hooks
Step-by-step guide to implementing, testing, and deploying custom Lotus hooks with Solidity and TypeScript examples, including a MaxSupplyHook example.
Prerequisites
Step 1 — Implement the Interface
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {BaseLotusHook} from "lotus/hooks/base/BaseLotusHook.sol";
import {ILotusHooks} from "lotus/hooks/interfaces/ILotusHooks.sol";
import {ILotus} from "lotus/interfaces/ILotus.sol";
import {Id} from "lotus/interfaces/Types.sol";
contract MaxSupplyHook is BaseLotusHook {
ILotus public immutable lotus;
address public owner;
// Maximum supply (in loan token assets) per market per tranche
mapping(Id => mapping(uint256 => uint256)) public maxSupply;
error NotOwner();
error SupplyCapExceeded(uint256 currentSupply, uint256 cap);
modifier onlyOwner() {
if (msg.sender != owner) revert NotOwner();
_;
}
constructor(address _owner, ILotus _lotus) {
owner = _owner;
lotus = _lotus;
}
function setMaxSupply(Id id, uint256 trancheIndex, uint256 cap) external onlyOwner {
maxSupply[id][trancheIndex] = cap;
}
function afterSupply(
ILotusHooks.SupplyParams calldata params
) external view override returns (bytes4) {
uint256 cap = maxSupply[params.id][params.trancheIndex];
if (cap == 0) return ILotusHooks.afterSupply.selector; // unconfigured
// Read current tranche supply from Lotus
ILotus.Tranche[] memory tranches = lotus.getMarketTranches(params.id);
uint256 currentSupply = tranches[params.trancheIndex].trancheSupplyAssets;
if (currentSupply > cap) {
revert SupplyCapExceeded(currentSupply, cap);
}
return ILotusHooks.afterSupply.selector;
}
}Step 2 — Set Permission Flags
Step 3 — Deploy and Register
Step 4 — Test the Hook
Design Tips
See Also
Last updated

