Skip to content

Commit

Permalink
Merge pull request #196 from Popcorn-Limited/feat/AnyLBTManager
Browse files Browse the repository at this point in the history
Feat/any lbt manager
  • Loading branch information
RedVeil authored Oct 22, 2024
2 parents b4a18f3 + a481dae commit c2b2493
Show file tree
Hide file tree
Showing 11 changed files with 1,435 additions and 10 deletions.
8 changes: 7 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ optimism = "${OPTIMISM_RPC_URL}"
arbitrum = "${ARBITRUM_RPC_URL}"
goerli = "${GOERLI_RPC_URL}"
sepolia = "${SEPOLIA_RPC_URL}"
base = "${BASE_RPC_URL}"
fraxtal = "${FRAXTAL_RPC_URL}"
avalanche = "${AVALANCHE_RPC_URL}"

[etherscan]
mainnet = { key = "${ETH_EXPLORE_KEY}" }
polygon = { key = "${POLYGON_EXPLORE_KEY}", url = "https://api.polygonscan.com/api" }
optimism = { key = "${OPTIMISM_EXPLORE_KEY}", url = "https://api-optimistic.etherscan.io/api" }
arbitrum = { key = "${ARBITRUM_EXPLORE_KEY}", url = "https://api.arbiscan.io/api" }
goerli = { key = "${ETH_EXPLORE_KEY}", url = "https://api-goerli.etherscan.io/api?" }
sepolia = { key = "${ETH_EXPLORE_KEY}", url = ""}
sepolia = { key = "${ETH_EXPLORE_KEY}", url = ""}
base = { key = "${BASE_EXPLORE_KEY}", url = "https://api.basescan.org/api"}
fraxtal = { key = "${FRAXTAL_EXPLORE_KEY}", url = "https://api.fraxscan.com/api"}
avalanche = { key = "${AVALANCHE_EXPLORE_KEY}", url = "https://api.snowscan.xyz/api"}
7 changes: 7 additions & 0 deletions src/interfaces/external/lfj/ILBClaimer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: GPL-3.0
// Docgen-SOLC: 0.8.0
pragma solidity ^0.8.25;

interface ILBClaimer {
function claim(address user, uint256[] memory ids) external;
}
144 changes: 144 additions & 0 deletions src/interfaces/external/lfj/ILBRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// SPDX-License-Identifier: GPL-3.0
// Docgen-SOLC: 0.8.0
pragma solidity ^0.8.25;

interface ILBRouter {
/**
* @dev The liquidity parameters, such as:
* - tokenX: The address of token X
* - tokenY: The address of token Y
* - binStep: The bin step of the pair
* - amountX: The amount to send of token X
* - amountY: The amount to send of token Y
* - amountXMin: The min amount of token X added to liquidity
* - amountYMin: The min amount of token Y added to liquidity
* - activeIdDesired: The active id that user wants to add liquidity from
* - idSlippage: The number of id that are allowed to slip
* - deltaIds: The list of delta ids to add liquidity (`deltaId = activeId - desiredId`)
* - distributionX: The distribution of tokenX with sum(distributionX) = 1e18 (100%) or 0 (0%)
* - distributionY: The distribution of tokenY with sum(distributionY) = 1e18 (100%) or 0 (0%)
* - to: The address of the recipient
* - refundTo: The address of the recipient of the refunded tokens if too much tokens are sent
* - deadline: The deadline of the transaction
*/
struct LiquidityParameters {
address tokenX;
address tokenY;
uint256 binStep;
uint256 amountX;
uint256 amountY;
uint256 amountXMin;
uint256 amountYMin;
uint256 activeIdDesired;
uint256 idSlippage;
int256[] deltaIds;
uint256[] distributionX;
uint256[] distributionY;
address to;
address refundTo;
uint256 deadline;
}

/**
* @notice Add liquidity while performing safety checks
* @dev This function is compliant with fee on transfer tokens
* @param liquidityParameters The liquidity parameters
* @return amountXAdded The amount of token X added
* @return amountYAdded The amount of token Y added
* @return amountXLeft The amount of token X left (sent back to liquidityParameters.refundTo)
* @return amountYLeft The amount of token Y left (sent back to liquidityParameters.refundTo)
* @return depositIds The ids of the deposits
* @return liquidityMinted The amount of liquidity minted
*/
function addLiquidity(
LiquidityParameters calldata liquidityParameters
)
external
returns (
uint256 amountXAdded,
uint256 amountYAdded,
uint256 amountXLeft,
uint256 amountYLeft,
uint256[] memory depositIds,
uint256[] memory liquidityMinted
);

/**
* @notice Add liquidity with NATIVE while performing safety checks
* @dev This function is compliant with fee on transfer tokens
* @param liquidityParameters The liquidity parameters
* @return amountXAdded The amount of token X added
* @return amountYAdded The amount of token Y added
* @return amountXLeft The amount of token X left (sent back to liquidityParameters.refundTo)
* @return amountYLeft The amount of token Y left (sent back to liquidityParameters.refundTo)
* @return depositIds The ids of the deposits
* @return liquidityMinted The amount of liquidity minted
*/
function addLiquidityNATIVE(
LiquidityParameters calldata liquidityParameters
)
external
payable
returns (
uint256 amountXAdded,
uint256 amountYAdded,
uint256 amountXLeft,
uint256 amountYLeft,
uint256[] memory depositIds,
uint256[] memory liquidityMinted
);

/**
* @notice Remove liquidity while performing safety checks
* @dev This function is compliant with fee on transfer tokens
* @param tokenX The address of token X
* @param tokenY The address of token Y
* @param binStep The bin step of the LBPair
* @param amountXMin The min amount to receive of token X
* @param amountYMin The min amount to receive of token Y
* @param ids The list of ids to burn
* @param amounts The list of amounts to burn of each id in `_ids`
* @param to The address of the recipient
* @param deadline The deadline of the tx
* @return amountX Amount of token X returned
* @return amountY Amount of token Y returned
*/
function removeLiquidity(
address tokenX,
address tokenY,
uint16 binStep,
uint256 amountXMin,
uint256 amountYMin,
uint256[] memory ids,
uint256[] memory amounts,
address to,
uint256 deadline
) external returns (uint256 amountX, uint256 amountY);

/**
* @notice Remove NATIVE liquidity while performing safety checks
* @dev This function is **NOT** compliant with fee on transfer tokens.
* This is wanted as it would make users pays the fee on transfer twice,
* use the `removeLiquidity` function to remove liquidity with fee on transfer tokens.
* @param token The address of token
* @param binStep The bin step of the LBPair
* @param amountTokenMin The min amount to receive of token
* @param amountNATIVEMin The min amount to receive of NATIVE
* @param ids The list of ids to burn
* @param amounts The list of amounts to burn of each id in `_ids`
* @param to The address of the recipient
* @param deadline The deadline of the tx
* @return amountToken Amount of token returned
* @return amountNATIVE Amount of NATIVE returned
*/
function removeLiquidityNATIVE(
address token,
uint16 binStep,
uint256 amountTokenMin,
uint256 amountNATIVEMin,
uint256[] memory ids,
uint256[] memory amounts,
address to,
uint256 deadline
) external returns (uint256 amountToken, uint256 amountNATIVE);
}
65 changes: 65 additions & 0 deletions src/interfaces/external/lfj/ILBT.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-License-Identifier: GPL-3.0
// Docgen-SOLC: 0.8.0
pragma solidity ^0.8.25;

interface ILBT {
/**
* @notice Returns the amount of tokens of type `id` owned by `account`.
* @param account The address of the owner.
* @param id The token id.
* @return The amount of tokens of type `id` owned by `account`.
*/
function balanceOf(
address account,
uint256 id
) external view returns (uint256);

/**
* @notice Returns the total supply of token of type `id`.
* /**
* @dev This is the amount of token of type `id` minted minus the amount burned.
* @param id The token id.
* @return The total supply of that token id.
*/
function totalSupply(uint256 id) external view returns (uint256);

/**
* @notice Return the balance of multiple (account/id) pairs.
* @param accounts The addresses of the owners.
* @param ids The token ids.
* @return batchBalances The balance for each (account, id) pair.
*/
function balanceOfBatch(
address[] calldata accounts,
uint256[] calldata ids
) external view returns (uint256[] memory batchBalances);

/**
* @notice Returns the reserves of a bin
* @param id The id of the bin
* @return binReserveX The reserve of token X in the bin
* @return binReserveY The reserve of token Y in the bin
*/
function getBin(uint24 id) external view returns (uint128, uint128);

/**
* @notice Returns the active id of the Liquidity Book Pair
* @dev The active id is the id of the bin that is currently being used for swaps.
* The price of the active bin is the price of the Liquidity Book Pair and can be calculated as follows:
* `price = (1 + binStep / 10_000) ^ (activeId - 2^23)`
* @return activeId The active id of the Liquidity Book Pair
*/
function getActiveId() external view returns (uint24);

/**
* @notice Returns the token X of the Liquidity Book Pair
* @return tokenX The address of the token X
*/
function getTokenX() external view returns (address);

/**
* @notice Returns the token Y of the Liquidity Book Pair
* @return tokenY The address of the token Y
*/
function getTokenY() external view returns (address);
}
44 changes: 44 additions & 0 deletions src/peripheral/oracles/adapter/pushOracle/PushOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,39 @@ contract PushOracle is BaseAdapter, Owned {

constructor(address _owner) Owned(_owner) {}

/*//////////////////////////////////////////////////////////////
SET PRICE LOGIC
//////////////////////////////////////////////////////////////*/

function setPrice(
address base,
address quote,
uint256 bqPrice,
uint256 qbPrice
) external onlyOwner {
_setPrice(base, quote, bqPrice, qbPrice);
}

function setPrices(
address[] memory bases,
address[] memory quotes,
uint256[] memory bqPrices,
uint256[] memory qbPrices
) external onlyOwner checkLength(bases.length, quotes.length) {
_checkLength(bases.length, bqPrices.length);
_checkLength(bases.length, qbPrices.length);

for (uint256 i = 0; i < bases.length; i++) {
_setPrice(bases[i], quotes[i], bqPrices[i], qbPrices[i]);
}
}

function _setPrice(
address base,
address quote,
uint256 bqPrice,
uint256 qbPrice
) internal {
// Both prices must be set
if ((bqPrice == 0 && qbPrice != 0) || (qbPrice == 0 && bqPrice != 0))
revert Misconfigured();
Expand All @@ -41,6 +68,10 @@ contract PushOracle is BaseAdapter, Owned {
emit PriceUpdated(base, quote, bqPrice, qbPrice);
}

/*//////////////////////////////////////////////////////////////
QUOTE LOGIC
//////////////////////////////////////////////////////////////*/

function _getQuote(
uint256 inAmount,
address base,
Expand All @@ -62,4 +93,17 @@ contract PushOracle is BaseAdapter, Owned {
false
);
}

/*//////////////////////////////////////////////////////////////
UTILS
//////////////////////////////////////////////////////////////*/

modifier checkLength(uint256 lengthA, uint256 lengthB) {
_checkLength(lengthA, lengthB);
_;
}

function _checkLength(uint256 lengthA, uint256 lengthB) internal pure {
if (lengthA != lengthB) revert Misconfigured();
}
}
24 changes: 24 additions & 0 deletions src/peripheral/oracles/adapter/pushOracle/PushOracleOwner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ interface IPushOracle {
uint256 bqPrice,
uint256 qbPrice
) external;

function setPrices(
address[] memory bases,
address[] memory quotes,
uint256[] memory bqPrices,
uint256[] memory qbPrices
) external;
}

contract PushOracleOwner is Owned {
Expand All @@ -24,6 +31,10 @@ contract PushOracleOwner is Owned {
oracle = IPushOracle(_oracle);
}

/*//////////////////////////////////////////////////////////////
SET PRICE LOGIC
//////////////////////////////////////////////////////////////*/

function setPrice(
address base,
address quote,
Expand All @@ -33,6 +44,19 @@ contract PushOracleOwner is Owned {
oracle.setPrice(base, quote, bqPrice, qbPrice);
}

function setPrices(
address[] memory bases,
address[] memory quotes,
uint256[] memory bqPrices,
uint256[] memory qbPrices
) external onlyKeeperOrOwner {
oracle.setPrices(bases, quotes, bqPrices, qbPrices);
}

/*//////////////////////////////////////////////////////////////
MANAGEMENT LOGIC
//////////////////////////////////////////////////////////////*/

function setKeeper(address _keeper) external onlyOwner {
emit KeeperUpdated(keeper, _keeper);
keeper = _keeper;
Expand Down
Loading

0 comments on commit c2b2493

Please sign in to comment.