Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The
AbstractTBTCDepositor
contract (#778)
Refs: #750 Here we introduce the `AbstractTBTCDepositor ` contract meant to facilitate integration of protocols aiming to use tBTC as an underlying Bitcoin bridge. ### Usage of the `AbstractTBTCDepositor` contract An integrator willing to leverage the `AbstractTBTCDepositor` contract is supposed to: - Create a child contract inheriting from this abstract contract - Call the `__AbstractTBTCDepositor_initialize` initializer function - Use the `_initializeDeposit` and `_finalizeDeposit` as part of their business logic in order to initialize and finalize deposits. Example code: ```solidity // Example upgradeable integrator contract. contract ExampleTBTCIntegrator is AbstractTBTCDepositor, Initializable { /// @Custom:oz-upgrades-unsafe-allow constructor constructor() { // Prevents the contract from being initialized again. _disableInitializers(); } function initialize( address _bridge, address _tbtcVault ) external initializer { __AbstractTBTCDepositor_initialize(_bridge, _tbtcVault); } function startProcess( IBridgeTypes.BitcoinTxInfo calldata fundingTx, IBridgeTypes.DepositRevealInfo calldata reveal ) external { // Embed necessary context as extra data. bytes32 extraData = ...; uint256 depositKey = _initializeDeposit( fundingTx, reveal, extraData ); // Use the depositKey to track the process. } function finalizeProcess(uint256 depositKey) external { (uint256 tbtcAmount, bytes32 extraData) = _finalizeDeposit(depositKey); // Do something with the minted TBTC using context // embedded in the extraData. } } ``` ### The `tbtcAmount` caveat The `tbtcAmount` computed by `_calculateTbtcAmount` and returned by `_finalizeDeposit` may not correspond to the actual amount of TBTC minted for the deposit. Although the treasury fee cut upon minting is known precisely, this is not the case for the optimistic minting fee and the Bitcoin transaction fee. To overcome that problem, this contract just takes the current maximum values of both fees, at the moment of deposit finalization. For the great majority of the deposits, such an algorithm will return a `tbtcAmount` slightly lesser than the actual amount of TBTC minted for the deposit. This will cause some TBTC to be left in the contract and ensure there is enough liquidity to finalize the deposit. However, in some rare cases, where the actual values of those fees change between the deposit minting and finalization, the `tbtcAmount` returned by this function may be greater than the actual amount of TBTC minted for the deposit. If this happens and the reserve coming from previous deposits leftovers does not provide enough liquidity, the deposit will have to wait for finalization until the reserve is refilled by subsequent deposits or a manual top-up. The integrator is responsible for handling such cases.
- Loading branch information