Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: BuyBack refactor #86

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 81 additions & 12 deletions contracts/BuyBackBurner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ pragma solidity ^0.8.28;
import {IUniswapV3} from "./interfaces/IUniswapV3.sol";
import {TickMath} from "./libraries/TickMath.sol";

// ERC20 interface
interface IERC20 {
/// @dev Gets the amount of tokens owned by a specified account.
/// @param account Account address.
/// @return Amount of tokens owned.
function balanceOf(address account) external view returns (uint256);
}

/// @dev Only `owner` has a privilege, but the `sender` was provided.
/// @param sender Sender address.
/// @param owner Required sender address as an owner.
Expand All @@ -12,14 +20,20 @@ error OwnerOnly(address sender, address owner);
/// @dev Provided zero address.
error ZeroAddress();

/// @dev Provided zero value.
error ZeroValue();

/// @dev The contract is already initialized.
error AlreadyInitialized();

/// @title BuyBackBurner - BuyBackBurner implementation contract
abstract contract BuyBackBurner {
event ImplementationUpdated(address indexed implementation);
event OwnerUpdated(address indexed owner);
event BridgeItLikeItsBurn(uint256 olasAmount);
event OracleUpdated(address indexed oracle);
event MinBridgedAmountUpdated(uint256 minBridgedAmount);
event BuyBack(uint256 olasAmount);
event BridgeAndBurn(uint256 olasAmount);

// Version number
string public constant VERSION = "0.2.0";
Expand All @@ -32,10 +46,23 @@ abstract contract BuyBackBurner {
// Seconds ago to look back for TWAP pool values
uint32 public constant SECONDS_AGO = 1800;

// Reentrancy lock
uint256 internal _locked = 1;
// Contract owner
address public owner;
// OLAS token address
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this all in a parent contract, as they are needed in each.

address public olas;
// Native token (ERC-20) address
address public nativeToken;
// Oracle address
address public oracle;
// L2 token relayer bridge address
address public l2TokenRelayer;

// Oracle max slippage for ERC-20 native token <=> OLAS
uint256 public maxSlippage;
// Minimum bridge amount
uint256 public minBridgedAmount;
// Reentrancy lock
uint256 internal _locked = 1;

/// @dev Bridges OLAS amount back to L1 and burns.
/// @param olasAmount OLAS amount.
Expand Down Expand Up @@ -127,6 +154,40 @@ abstract contract BuyBackBurner {
emit OwnerUpdated(newOwner);
}

/// @dev Changes contract oracle address.
/// @param newOracle Address of a new oracle.
function changeOracle(address newOracle) external virtual {
// Check for the ownership
if (msg.sender != owner) {
revert OwnerOnly(msg.sender, owner);
}

// Check for the zero address
if (newOracle == address(0)) {
revert ZeroAddress();
}

oracle = newOracle;
emit OracleUpdated(newOracle);
}

/// @dev Changes minimum OLAS bridge amount.
/// @param newMinBridgedAmount New minimum bridged amount.
function changeMinBridgedAmount(uint256 newMinBridgedAmount) external virtual {
// Check for the ownership
if (msg.sender != owner) {
revert OwnerOnly(msg.sender, owner);
}

// Check for the zero value
if (newMinBridgedAmount == 0) {
revert ZeroValue();
}

minBridgedAmount = newMinBridgedAmount;
emit MinBridgedAmountUpdated(newMinBridgedAmount);
}

function checkPoolPrices(
address token0,
address token1,
Expand Down Expand Up @@ -173,19 +234,27 @@ abstract contract BuyBackBurner {
/// @dev Bridges OLAS to Ethereum mainnet for burn.
/// @notice if nativeTokenAmount is zero or above the balance, it will be adjusted to current native token balance.
/// @param nativeTokenAmount Suggested native token amount.
/// @param tokenGasLimit Token gas limit for bridging OLAS to L1.
/// @param bridgePayload Optional additional bridge payload.
function sendToHigherHeights(
uint256 nativeTokenAmount,
uint256 tokenGasLimit,
bytes memory bridgePayload
) external payable {
function buyBack(uint256 nativeTokenAmount) external virtual {
require(_locked == 1, "Reentrancy guard");
_locked = 2;

// Buy OLAS
uint256 olasAmount = _buyOLAS(nativeTokenAmount);
require(olasAmount > 0, "Nothing to bridge");

emit BuyBack(olasAmount);

_locked = 1;
}

/// @dev Bridges OLAS to Ethereum mainnet for burn.
/// @param tokenGasLimit Token gas limit for bridging OLAS to L1.
/// @param bridgePayload Optional additional bridge payload.
function bridgeAndBurn(uint256 tokenGasLimit, bytes memory bridgePayload) external virtual payable {
require(_locked == 1, "Reentrancy guard");
_locked = 2;

uint256 olasAmount = IERC20(olas).balanceOf(address(this));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just get full OLAS amount

require(olasAmount >= minBridgedAmount, "Not enough OLAS to bridge");

// Burn OLAS
uint256 leftovers = _bridgeAndBurn(olasAmount, tokenGasLimit, bridgePayload);
Expand All @@ -197,7 +266,7 @@ abstract contract BuyBackBurner {
require(success, "Leftovers transfer failed");
}

emit BridgeItLikeItsBurn(olasAmount);
emit BridgeAndBurn(olasAmount);

_locked = 1;
}
Expand Down
10 changes: 0 additions & 10 deletions contracts/BuyBackBurnerBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,6 @@ contract BuyBackBurnerBase is BuyBackBurner {
// This is safe as the value is practically bigger than observed ones on numerous chains
uint32 public constant TOKEN_GAS_LIMIT = 300_000;

// OLAS token address
address public olas;
// Native token (ERC-20) address
address public nativeToken;
// Oracle address
address public oracle;
// L2 token relayer bridge address
address public l2TokenRelayer;
// Oracle max slippage for ERC-20 native token <=> OLAS
uint256 public maxSlippage;
// Balancer vault address
address public balancerVault;
// Balancer pool Id
Expand Down
10 changes: 0 additions & 10 deletions contracts/BuyBackBurnerCelo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@ contract BuyBackBurnerCelo is BuyBackBurner {
// Ethereum mainnet chain Id in Wormhole format
uint16 public constant WORMHOLE_ETH_CHAIN_ID = 2;

// OLAS token address
address public olas;
// Native token (ERC-20) address
address public nativeToken;
// Oracle address
address public oracle;
// L2 token relayer bridge address
address public l2TokenRelayer;
// Oracle max slippage for ERC-20 native token <=> OLAS
uint256 public maxSlippage;
// Ubeswap router address
address public router;

Expand Down
Loading