feat: add balancer v2 executor
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
pragma solidity >=0.7.0 <0.9.0;
|
||||
|
||||
/**
|
||||
* @notice Interface for ExternalWeightedMath, a contract-wrapper for Weighted Math, Joins and Exits.
|
||||
*/
|
||||
interface IExternalWeightedMath {
|
||||
/**
|
||||
* @dev See `WeightedMath._calculateInvariant`.
|
||||
*/
|
||||
function calculateInvariant(uint256[] memory normalizedWeights, uint256[] memory balances)
|
||||
external
|
||||
pure
|
||||
returns (uint256);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedMath._calcOutGivenIn`.
|
||||
*/
|
||||
function calcOutGivenIn(
|
||||
uint256 balanceIn,
|
||||
uint256 weightIn,
|
||||
uint256 balanceOut,
|
||||
uint256 weightOut,
|
||||
uint256 amountIn
|
||||
) external pure returns (uint256);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedMath._calcInGivenOut`.
|
||||
*/
|
||||
function calcInGivenOut(
|
||||
uint256 balanceIn,
|
||||
uint256 weightIn,
|
||||
uint256 balanceOut,
|
||||
uint256 weightOut,
|
||||
uint256 amountOut
|
||||
) external pure returns (uint256);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedMath._calcBptOutGivenExactTokensIn`.
|
||||
*/
|
||||
function calcBptOutGivenExactTokensIn(
|
||||
uint256[] memory balances,
|
||||
uint256[] memory normalizedWeights,
|
||||
uint256[] memory amountsIn,
|
||||
uint256 bptTotalSupply,
|
||||
uint256 swapFeePercentage
|
||||
) external pure returns (uint256);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedMath._calcBptOutGivenExactTokenIn`.
|
||||
*/
|
||||
function calcBptOutGivenExactTokenIn(
|
||||
uint256 balance,
|
||||
uint256 normalizedWeight,
|
||||
uint256 amountIn,
|
||||
uint256 bptTotalSupply,
|
||||
uint256 swapFeePercentage
|
||||
) external pure returns (uint256);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedMath._calcTokenInGivenExactBptOut`.
|
||||
*/
|
||||
function calcTokenInGivenExactBptOut(
|
||||
uint256 balance,
|
||||
uint256 normalizedWeight,
|
||||
uint256 bptAmountOut,
|
||||
uint256 bptTotalSupply,
|
||||
uint256 swapFeePercentage
|
||||
) external pure returns (uint256);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedMath._calcAllTokensInGivenExactBptOut`.
|
||||
*/
|
||||
function calcAllTokensInGivenExactBptOut(
|
||||
uint256[] memory balances,
|
||||
uint256 bptAmountOut,
|
||||
uint256 totalBPT
|
||||
) external pure returns (uint256[] memory);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedMath._calcBptInGivenExactTokensOut`.
|
||||
*/
|
||||
function calcBptInGivenExactTokensOut(
|
||||
uint256[] memory balances,
|
||||
uint256[] memory normalizedWeights,
|
||||
uint256[] memory amountsOut,
|
||||
uint256 bptTotalSupply,
|
||||
uint256 swapFeePercentage
|
||||
) external pure returns (uint256);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedMath._calcBptInGivenExactTokenOut`.
|
||||
*/
|
||||
function calcBptInGivenExactTokenOut(
|
||||
uint256 balance,
|
||||
uint256 normalizedWeight,
|
||||
uint256 amountOut,
|
||||
uint256 bptTotalSupply,
|
||||
uint256 swapFeePercentage
|
||||
) external pure returns (uint256);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedMath._calcTokenOutGivenExactBptIn`.
|
||||
*/
|
||||
function calcTokenOutGivenExactBptIn(
|
||||
uint256 balance,
|
||||
uint256 normalizedWeight,
|
||||
uint256 bptAmountIn,
|
||||
uint256 bptTotalSupply,
|
||||
uint256 swapFeePercentage
|
||||
) external pure returns (uint256);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedMath._calcTokensOutGivenExactBptIn`.
|
||||
*/
|
||||
function calcTokensOutGivenExactBptIn(
|
||||
uint256[] memory balances,
|
||||
uint256 bptAmountIn,
|
||||
uint256 totalBPT
|
||||
) external pure returns (uint256[] memory);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedMath._calcBptOutAddToken`.
|
||||
*/
|
||||
function calcBptOutAddToken(uint256 totalSupply, uint256 normalizedWeight) external pure returns (uint256);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedJoinsLib.joinExactTokensInForBPTOut`.
|
||||
*/
|
||||
function joinExactTokensInForBPTOut(
|
||||
uint256[] memory balances,
|
||||
uint256[] memory normalizedWeights,
|
||||
uint256[] memory scalingFactors,
|
||||
uint256 totalSupply,
|
||||
uint256 swapFeePercentage,
|
||||
bytes memory userData
|
||||
) external pure returns (uint256, uint256[] memory);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedJoinsLib.joinTokenInForExactBPTOut`.
|
||||
*/
|
||||
function joinTokenInForExactBPTOut(
|
||||
uint256[] memory balances,
|
||||
uint256[] memory normalizedWeights,
|
||||
uint256 totalSupply,
|
||||
uint256 swapFeePercentage,
|
||||
bytes memory userData
|
||||
) external pure returns (uint256, uint256[] memory);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedJoinsLib.joinAllTokensInForExactBPTOut`.
|
||||
*/
|
||||
function joinAllTokensInForExactBPTOut(
|
||||
uint256[] memory balances,
|
||||
uint256 totalSupply,
|
||||
bytes memory userData
|
||||
) external pure returns (uint256 bptAmountOut, uint256[] memory amountsIn);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedExitsLib.exitExactBPTInForTokenOut`.
|
||||
*/
|
||||
function exitExactBPTInForTokenOut(
|
||||
uint256[] memory balances,
|
||||
uint256[] memory normalizedWeights,
|
||||
uint256 totalSupply,
|
||||
uint256 swapFeePercentage,
|
||||
bytes memory userData
|
||||
) external pure returns (uint256, uint256[] memory);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedExitsLib.exitExactBPTInForTokensOut`.
|
||||
*/
|
||||
function exitExactBPTInForTokensOut(
|
||||
uint256[] memory balances,
|
||||
uint256 totalSupply,
|
||||
bytes memory userData
|
||||
) external pure returns (uint256 bptAmountIn, uint256[] memory amountsOut);
|
||||
|
||||
/**
|
||||
* @dev See `WeightedExitsLib.exitBPTInForExactTokensOut`.
|
||||
*/
|
||||
function exitBPTInForExactTokensOut(
|
||||
uint256[] memory balances,
|
||||
uint256[] memory normalizedWeights,
|
||||
uint256[] memory scalingFactors,
|
||||
uint256 totalSupply,
|
||||
uint256 swapFeePercentage,
|
||||
bytes memory userData
|
||||
) external pure returns (uint256, uint256[] memory);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
pragma solidity >=0.7.0 <0.9.0;
|
||||
|
||||
import "../solidity-utils/openzeppelin/IERC20.sol";
|
||||
|
||||
library WeightedPoolUserData {
|
||||
// In order to preserve backwards compatibility, make sure new join and exit kinds are added at the end of the enum.
|
||||
enum JoinKind { INIT, EXACT_TOKENS_IN_FOR_BPT_OUT, TOKEN_IN_FOR_EXACT_BPT_OUT, ALL_TOKENS_IN_FOR_EXACT_BPT_OUT }
|
||||
enum ExitKind { EXACT_BPT_IN_FOR_ONE_TOKEN_OUT, EXACT_BPT_IN_FOR_TOKENS_OUT, BPT_IN_FOR_EXACT_TOKENS_OUT }
|
||||
|
||||
function joinKind(bytes memory self) internal pure returns (JoinKind) {
|
||||
return abi.decode(self, (JoinKind));
|
||||
}
|
||||
|
||||
function exitKind(bytes memory self) internal pure returns (ExitKind) {
|
||||
return abi.decode(self, (ExitKind));
|
||||
}
|
||||
|
||||
// Joins
|
||||
|
||||
function initialAmountsIn(bytes memory self) internal pure returns (uint256[] memory amountsIn) {
|
||||
(, amountsIn) = abi.decode(self, (JoinKind, uint256[]));
|
||||
}
|
||||
|
||||
function exactTokensInForBptOut(bytes memory self)
|
||||
internal
|
||||
pure
|
||||
returns (uint256[] memory amountsIn, uint256 minBPTAmountOut)
|
||||
{
|
||||
(, amountsIn, minBPTAmountOut) = abi.decode(self, (JoinKind, uint256[], uint256));
|
||||
}
|
||||
|
||||
function tokenInForExactBptOut(bytes memory self) internal pure returns (uint256 bptAmountOut, uint256 tokenIndex) {
|
||||
(, bptAmountOut, tokenIndex) = abi.decode(self, (JoinKind, uint256, uint256));
|
||||
}
|
||||
|
||||
function allTokensInForExactBptOut(bytes memory self) internal pure returns (uint256 bptAmountOut) {
|
||||
(, bptAmountOut) = abi.decode(self, (JoinKind, uint256));
|
||||
}
|
||||
|
||||
// Exits
|
||||
|
||||
function exactBptInForTokenOut(bytes memory self) internal pure returns (uint256 bptAmountIn, uint256 tokenIndex) {
|
||||
(, bptAmountIn, tokenIndex) = abi.decode(self, (ExitKind, uint256, uint256));
|
||||
}
|
||||
|
||||
function exactBptInForTokensOut(bytes memory self) internal pure returns (uint256 bptAmountIn) {
|
||||
(, bptAmountIn) = abi.decode(self, (ExitKind, uint256));
|
||||
}
|
||||
|
||||
function bptInForExactTokensOut(bytes memory self)
|
||||
internal
|
||||
pure
|
||||
returns (uint256[] memory amountsOut, uint256 maxBPTAmountIn)
|
||||
{
|
||||
(, amountsOut, maxBPTAmountIn) = abi.decode(self, (ExitKind, uint256[], uint256));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user