BNB Chain Innovation Hackathon — Diving Deeper into Algebra Integral and Plugins
Plugins, also known as “hooks”, made popular recently thanks to Uniswap V4, will soon revolutionize the BNB Chain DeFi ecosystem.
We are also thrilled to have our key strategic partners to join the effort by co-sponsoring the prize pool, now increased from $8,000 to $20,000.
But what is all the fuzz about, and how can you as a developer participate? Let’s dive in.
What is an Algebra Integral?
Algebra Integral is an AMM with concentrated liquidity, which also supports the use of plugins. The basic architecture and interfaces of key smart contracts are close to UniswapV3.
The most important AMM contracts are located in our main monorepository:
https://github.com/cryptoalgebra/Algebra
General documentation is on this page:
https://cryptoalgebra.gitbook.io/algebra-integral/
It is important to note that, unlike UniswapV3, according to the Algebra Integral architecture, only one pool can be created for each pair of tokens: this makes life easier for liquidity providers and reduces liquidity fragmentation. Each pool provides a Uniswap3-style concentrated liquidity mechanic. A description of the fundamental logic of the pools’ operation is in the documentation:
https://cryptoalgebra.gitbook.io/algebra-integral/core-logic/pool-overview
What is a Plugin?
The functionality of the Algebra Integral AMM can be extended, changed and supplemented by using customizable plugins. A plugin is a separate smart contract that is connected to a pool contract and extends its functionality. At any given time, only one contract can be connected to the pool as a plugin, but its functionality can be almost as complex as desired.
Interaction of plugins with Algebra Integral pools is based on the use of hooks: special calls that the pool can make before and after the main user actions. If a plugin is attached to the pool and a corresponding hook is activated, the liquidity pool will call a function defined in the Algebra plugin interface from the plugin. Within this function, arbitrary logic can be implemented in the plugin, including even interaction with the same liquidity pool.
The pool provides the following hooks:
- beforeInitialize — called at the beginning of pool initialization
- afterInitialize — called after the initialization of the pool
- beforeModifyPosition — called before a change in any liquidity position. The call can occur before a liquidity mint or burn
- afterModifyPosition — called after a change in any liquidity position. The call can occur before a liquidity mint or burn
- beforeSwap — called before the swap
- afterSwap — called after the swap
- beforeFlash — called before the flashloan
- afterFlash — called after the flashloan
This way, the plugin can perform any actions before and after key events in the pool.
Information about how plugins work and interact with pools is also available on the documentation page for Algebra Integral:
https://cryptoalgebra.gitbook.io/algebra-integral/core-logic/plugins
What can be done with plugins?
In fact, a smart contract used as a plugin can implement almost arbitrary logic. If the corresponding hook in the pool is activated, then the plugin can perform any actions before or after the main events in the pool. Including but not limited to:
- Call other contracts
- Perform swaps or liquidity management in the same liquidity pool
- Change the fee value in the pool
- Prohibit some use cases of AMM using reverts
Various mechanics can be built using plugins. For example, we have plugin that contains logic from the previous version of the Algebra protocol:
The plugin contains the following functionality:
- TWAP Oracle. Using the plugin, a time-weighted average price oracle is implemented, similar in principle to working with the oracle in UniswapV3.
- Volatility-based dynamic fee. Based on statistical price volatility over the last 24 hours, the plugin automatically changes the value of the current commission in the pool.
- Farming. The plugin makes it possible to connect and use farming, close to classical in the style of UniswapV2. Rewards are distributed over time among active liquidity positions.
In addition, our team has prepared several other plugins. Repository with plugins from the Algebra team:
https://github.com/cryptoalgebra/integral-team-plugins
The following plugins are presented here:
- Limit orders. Simple implementation of limit orders using automatically closed liquidity positions.
- Emergency braking contract (stub). A contract that can be used for emergency stop of protocol functionality.
- Discount provided for owning NFTs. A plugin that uses crosschain technology and provides the user with a reduced fee for swaps if he owns the required NFT on the Ethereum network.
Some other ideas:
- different dynamic & elastic fees
- a whitelist / blacklist / KYC system
- gas discount, or compensation mechanisms
- solutions for JIT liquidity issues
- LVR (Loss-Versus-Rebalancing) / Impermanent Loss reduction
- Perpetual Contracts
How to Start Developing My Own Plugin?
To simplify the creation of new plugins, you can use a special template:
Clone repository:
git clone https://github.com/cryptoalgebra/algebra-plugin-template — recursive
Install dependencies:
npm i
Now you will have a template repository with DummyPlugin which does nothing but can be used as a basis for developing a new plugin!
This project uses hardhat by default, so the project can be compiled using the command:
npm run compile
And tested with:
npm run test
If you prefer to use another framework, for example Foundry, the contracts from our template repository can be transferred to your respective project without any problems.
Where is Algebra Integral Deployed Now?
Swapsicle protocol, Telos:
AlgebraFactory: 0xA09BAbf9A48003ae9b9333966a8Bda94d820D0d9
SwapRouter: 0xc96afc666A4195366a46E4ca8C4f10f3C39Ee363
NonfungiblePositionManager:
0xf85bA8be4E5B82da25D68970b03F4FEBefD6Fa98
Mantle testnet:
AlgebraFactory: 0xc074c6551a9cB8f63ff3dC2eA9616996db9B4eCF
SwapRouter: 0x6c14110bc649FFe0013BBF7dC7a808D7E0401f2d
NonfungiblePositionManager:
0x1B9335eB8C67CcacA41be3f4e53E45a7DB653A9D
BSC testnet:
AlgebraFactory: 0xFBFB64eD1C70bb8d4c8bFCc338C10a5120809538
SwapRouter: 0xc12f40f584A751C032e18f5757d3b7EE6fD74289
NonfungiblePositionManager:
0xF1E919e24159b14aC32790dD4828B671E2158982
Get Started
Join the Innovation Hackathon and help us to push forward the whole BNB ecosystem. Make sure to also check out BNB Chain blog for more details. Apply by February 1st in order to participate.