Skip to content

Commit

Permalink
cherry pick balancer v3 contracts for custom pool inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPereira committed Mar 11, 2024
1 parent 1b89cf9 commit dd64483
Show file tree
Hide file tree
Showing 13 changed files with 1,628 additions and 87 deletions.
58 changes: 58 additions & 0 deletions packages/hardhat/contracts/ConstantPricePool.sol
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;
}
}
87 changes: 0 additions & 87 deletions packages/hardhat/contracts/YourContract.sol

This file was deleted.

14 changes: 14 additions & 0 deletions packages/hardhat/contracts/interfaces/IAuthorizer.sol
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);
}
75 changes: 75 additions & 0 deletions packages/hardhat/contracts/interfaces/IBasePool.sol
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);
}
11 changes: 11 additions & 0 deletions packages/hardhat/contracts/interfaces/IRateProvider.sol
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);
}
24 changes: 24 additions & 0 deletions packages/hardhat/contracts/interfaces/IVault.sol
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);
}
Loading

0 comments on commit dd64483

Please sign in to comment.