-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cherry pick balancer v3 contracts for custom pool inheritance
- Loading branch information
1 parent
1b89cf9
commit dd64483
Showing
13 changed files
with
1,628 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.0 <0.9.0; | ||
|
||
import "./interfaces/IVault.sol"; | ||
import "./interfaces/IBasePool.sol"; | ||
import "./vault/BalancerPoolToken.sol"; | ||
|
||
contract ConstantPricePool is IBasePool, BalancerPoolToken { | ||
constructor( | ||
IVault vault, | ||
string name, | ||
string symbol | ||
) BalancerPoolToken(vault, name, symbol) {} | ||
|
||
/** | ||
* @notice Execute a swap in the pool. | ||
* @param params Swap parameters | ||
* @return amountCalculatedScaled18 Calculated amount for the swap | ||
*/ | ||
function onSwap( | ||
SwapParams calldata params | ||
) external returns (uint256 amountCalculatedScaled18) { | ||
amountCalculatedScaled18 = request.amountGivenScaled18; | ||
} | ||
|
||
/** | ||
* @notice Computes and returns the pool's invariant. | ||
* @dev This function computes the invariant based on current balances | ||
* @param balancesLiveScaled18 Array of current pool balances for each token in the pool, scaled to 18 decimals | ||
* @return invariant The calculated invariant of the pool, represented as a uint256 | ||
*/ | ||
function computeInvariant( | ||
uint256[] memory balancesLiveScaled18 | ||
) external view returns (uint256 invariant) { | ||
invariant = balancesLiveScaled18[0] + balancesLiveScaled18[1]; | ||
} | ||
|
||
/** | ||
* @dev Computes the new balance of a token after an operation, given the invariant growth ratio and all other | ||
* balances. | ||
* @param balancesLiveScaled18 Current live balances (adjusted for decimals, rates, etc.) | ||
* @param tokenInIndex The index of the token we're computing the balance for, in token registration order | ||
* @param invariantRatio The ratio of the new invariant (after an operation) to the old | ||
* @return newBalance The new balance of the selected token, after the operation | ||
*/ | ||
function computeBalance( | ||
uint256[] memory balancesLiveScaled18, | ||
uint256 tokenInIndex, | ||
uint256 invariantRatio | ||
) external pure returns (uint256 newBalance) { | ||
uint256 invariant = computeInvariant(balancesLiveScaled18); | ||
|
||
newBalance = | ||
(balancesLiveScaled18[tokenInIndex] + | ||
invariant.mulDown(invariantRatio)) - | ||
invariant; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
pragma solidity ^0.8.4; | ||
|
||
interface IAuthorizer { | ||
/** | ||
* @notice Returns true if `account` can perform the action described by `actionId` in the contract `where`. | ||
* @param actionId Identifier for the action to be performed | ||
* @param account Account trying to perform the action | ||
* @param where Target contract for the action | ||
* @return True if the action is permitted | ||
*/ | ||
function canPerform(bytes32 actionId, address account, address where) external view returns (bool); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
pragma solidity ^0.8.4; | ||
|
||
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
|
||
import { SwapKind } from "./VaultTypes.sol"; | ||
|
||
/// @notice Interface for a Base Pool | ||
interface IBasePool { | ||
/** | ||
* @notice Gets the tokens registered to a pool. | ||
* @dev Delegated to the Vault; added here as a convenience, mainly for off-chain processes. | ||
* @return tokens List of tokens in the pool | ||
*/ | ||
function getPoolTokens() external view returns (IERC20[] memory tokens); | ||
|
||
/*************************************************************************** | ||
Invariant | ||
***************************************************************************/ | ||
|
||
/** | ||
* @notice Computes and returns the pool's invariant. | ||
* @dev This function computes the invariant based on current balances | ||
* @param balancesLiveScaled18 Array of current pool balances for each token in the pool, scaled to 18 decimals | ||
* @return invariant The calculated invariant of the pool, represented as a uint256 | ||
*/ | ||
function computeInvariant(uint256[] memory balancesLiveScaled18) external view returns (uint256 invariant); | ||
|
||
/** | ||
* @dev Computes the new balance of a token after an operation, given the invariant growth ratio and all other | ||
* balances. | ||
* @param balancesLiveScaled18 Current live balances (adjusted for decimals, rates, etc.) | ||
* @param tokenInIndex The index of the token we're computing the balance for, in token registration order | ||
* @param invariantRatio The ratio of the new invariant (after an operation) to the old | ||
* @return newBalance The new balance of the selected token, after the operation | ||
*/ | ||
function computeBalance( | ||
uint256[] memory balancesLiveScaled18, | ||
uint256 tokenInIndex, | ||
uint256 invariantRatio | ||
) external view returns (uint256 newBalance); | ||
|
||
/*************************************************************************** | ||
Swaps | ||
***************************************************************************/ | ||
|
||
/** | ||
* @dev Data for a swap operation. | ||
* @param kind Type of swap (exact in or exact out) | ||
* @param pool Address of the liquidity pool | ||
* @param amountGivenScaled18 Amount given based on kind of the swap (e.g., tokenIn for exact in) | ||
* @param balancesScaled18 Current pool balances | ||
* @param indexIn Index of tokenIn | ||
* @param indexOut Index of tokenOut | ||
* @param sender Originator of the swap transaction | ||
* @param userData Additional (optional) data required for the swap | ||
*/ | ||
struct SwapParams { | ||
SwapKind kind; | ||
uint256 amountGivenScaled18; | ||
uint256[] balancesScaled18; | ||
uint256 indexIn; | ||
uint256 indexOut; | ||
address sender; | ||
bytes userData; | ||
} | ||
|
||
/** | ||
* @notice Execute a swap in the pool. | ||
* @param params Swap parameters (see above for struct definition) | ||
* @return amountCalculatedScaled18 Calculated amount for the swap | ||
*/ | ||
function onSwap(SwapParams calldata params) external returns (uint256 amountCalculatedScaled18); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
pragma solidity ^0.8.4; | ||
|
||
interface IRateProvider { | ||
/** | ||
* @dev Returns an 18 decimal fixed point number that is the exchange rate of the token to some other underlying | ||
* token. The meaning of this rate depends on the context. | ||
*/ | ||
function getRate() external view returns (uint256); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
pragma solidity ^0.8.4; | ||
|
||
import { IVaultAdmin } from "./IVaultAdmin.sol"; | ||
import { IVaultExtension } from "./IVaultExtension.sol"; | ||
import { IVaultMain } from "./IVaultMain.sol"; | ||
import { IVaultErrors } from "./IVaultErrors.sol"; | ||
import { IVaultEvents } from "./IVaultEvents.sol"; | ||
|
||
interface IVault is | ||
IVaultMain, | ||
IVaultExtension, | ||
IVaultAdmin, | ||
IVaultErrors, | ||
IVaultEvents | ||
{ | ||
/// @dev Returns the main Vault address. | ||
function vault() | ||
external | ||
view | ||
override(IVaultAdmin, IVaultExtension) | ||
returns (IVault); | ||
} |
Oops, something went wrong.