Skip to content

Commit

Permalink
Update LibBonds.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
dantaik committed Jul 3, 2024
1 parent 9febe80 commit 9fdd6b9
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions packages/protocol/contracts/L1/libs/LibBonds.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ import "../TaikoData.sol";
/// @notice A library that offers helper functions to handle bonds.
/// @custom:security-contact [email protected]
library LibBonds {
/// @dev Emitted when token is credited back to a user's bond balance.
event BondCredited(address indexed user, uint256 amount);
event BondDedited(address indexed user, uint256 amount);

/// @dev Emitted when token is debited from a user's bond balance.
event BondDebited(address indexed user, uint256 amount);

/// @dev Deposits Taiko token to be used as bonds.
/// @param _state Current TaikoData.State.
/// @param _resolver Address resolver interface.
/// @param _amount The amount of token to deposit.
function depositBond(
TaikoData.State storage _state,
IAddressResolver _resolver,
Expand All @@ -26,6 +33,10 @@ library LibBonds {
_state.bondBalance[msg.sender] += _amount;
}

/// @dev Withdraws Taiko token.
/// @param _state Current TaikoData.State.
/// @param _resolver Address resolver interface.
/// @param _amount The amount of token to withdraw.
function withdrawBond(
TaikoData.State storage _state,
IAddressResolver _resolver,
Expand All @@ -38,6 +49,11 @@ library LibBonds {
tko.transfer(msg.sender, _amount);
}

/// @dev Debits Taiko tokens as bonds.
/// @param _state Current TaikoData.State.
/// @param _resolver Address resolver interface.
/// @param _user The user address to debit.
/// @param _amount The amount of token to debit.
function debitBond(
TaikoData.State storage _state,
IAddressResolver _resolver,
Expand All @@ -52,18 +68,26 @@ library LibBonds {
unchecked {
_state.bondBalance[_user] = balance - _amount;
}
emit BondDebited(_user, _amount);
} else {
IERC20 tko = IERC20(_resolver.resolve(LibStrings.B_TAIKO_TOKEN, false));
tko.transferFrom(_user, address(this), _amount);
}
emit BondDedited(_user, _amount);
}

/// @dev Credits Taiko tokens to user's bond balance.
/// @param _state Current TaikoData.State.
/// @param _user The user address to credit.
/// @param _amount The amount of token to credit.
function creditBond(TaikoData.State storage _state, address _user, uint256 _amount) internal {
_state.bondBalance[_user] += _amount;
emit BondCredited(_user, _amount);
}

/// @dev Gets a user's current Taiko token bond balance.
/// @param _state Current TaikoData.State.
/// @param _user The user address to credit.
/// @return The current token balance.
function bondBalanceOf(
TaikoData.State storage _state,
address _user
Expand Down

0 comments on commit 9fdd6b9

Please sign in to comment.