-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from gammaswap/feat/upd-v1core1.2.1
publish package
- Loading branch information
Showing
7 changed files
with
201 additions
and
10 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,43 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
pragma solidity ^0.8.0; | ||
|
||
import "@gammaswap/v1-core/contracts/interfaces/IGammaPoolExternal.sol"; | ||
|
||
import "./interfaces/IPositionManagerExternal.sol"; | ||
import "./PositionManagerWithStaking.sol"; | ||
|
||
/// @title PositionManagerExternalWithStaking, concrete implementation of IPositionManagerExternal | ||
/// @author Daniel D. Alcarraz (https://github.com/0xDanr) | ||
/// @notice Inherits PositionManager functionality from PositionManagerWithStaking and defines functionality to rebalance | ||
/// @notice loan collateral using external contracts by calling GammaPool::rebalanceExternally() | ||
contract PositionManagerExternalWithStaking is PositionManagerWithStaking, IPositionManagerExternal { | ||
|
||
/// @dev Constructs the PositionManagerWithStaking contract. | ||
/// @param _factory Address of the contract factory. | ||
/// @param _WETH Address of the Wrapped Ether (WETH) contract. | ||
constructor(address _factory, address _WETH) PositionManagerWithStaking(_factory, _WETH) {} | ||
|
||
/// @dev Flash loan pool's collateral and/or lp tokens to external address. Rebalanced loan collateral is acceptable | ||
/// @dev in repayment of flash loan. Function can be used for other purposes besides rebalancing collateral. | ||
/// @param gammaPool - address of GammaPool of the loan | ||
/// @param tokenId - unique id identifying loan | ||
/// @param amounts - collateral amounts being flash loaned | ||
/// @param lpTokens - amount of CFMM LP tokens being flash loaned | ||
/// @param to - address that will receive flash loan swaps and potentially rebalance loan's collateral | ||
/// @param data - optional bytes parameter for custom user defined data | ||
/// @param minCollateral - minimum amount of expected collateral after re-balancing. Used for slippage control | ||
/// @return loanLiquidity - updated loan liquidity, includes flash loan fees | ||
/// @return tokensHeld - updated collateral token amounts backing loan | ||
function rebalanceCollateralExternally(address gammaPool, uint256 tokenId, uint128[] memory amounts, uint256 lpTokens, address to, bytes calldata data, uint128[] memory minCollateral) internal virtual returns(uint256 loanLiquidity, uint128[] memory tokensHeld) { | ||
(loanLiquidity, tokensHeld) = IGammaPoolExternal(gammaPool).rebalanceExternally(tokenId, amounts, lpTokens, to, data); | ||
checkMinCollateral(tokensHeld, minCollateral); | ||
emit RebalanceCollateralExternally(gammaPool, tokenId, loanLiquidity, tokensHeld); | ||
} | ||
|
||
/// @dev See {IPositionManagerExternal-rebalanceCollateralExternally}. | ||
function rebalanceCollateralExternally(RebalanceCollateralExternallyParams calldata params) external virtual override isAuthorizedForToken(params.tokenId) isExpired(params.deadline) returns(uint256 loanLiquidity, uint128[] memory tokensHeld) { | ||
address gammaPool = getGammaPoolAddress(params.cfmm, params.protocolId); | ||
(loanLiquidity,tokensHeld) = rebalanceCollateralExternally(gammaPool, params.tokenId, params.amounts, params.lpTokens, params.to, params.data, params.minCollateral); | ||
_logPrice(gammaPool); | ||
} | ||
} |
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,47 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
pragma solidity ^0.8.0; | ||
|
||
import "./IPositionManager.sol"; | ||
|
||
/// @title Interface for PositionManagerExternal | ||
/// @author Daniel D. Alcarraz (https://github.com/0xDanr) | ||
/// @notice Defines external functions and events emitted by PositionManagerExternal | ||
/// @dev Interface also defines all GammaPool events through inheritance of IGammaPool and IGammaPoolEvents | ||
interface IPositionManagerExternal is IPositionManager { | ||
|
||
/// @dev Emitted when re-balancing a loan's collateral amounts (swapping one collateral token for another) using an external contract | ||
/// @param pool - loan's pool address | ||
/// @param tokenId - id identifying loan in pool | ||
/// @param loanLiquidity - liquidity borrowed in invariant terms | ||
/// @param tokensHeld - new loan collateral amounts | ||
event RebalanceCollateralExternally(address indexed pool, uint256 tokenId, uint256 loanLiquidity, uint128[] tokensHeld); | ||
|
||
/// @dev Struct parameters for `rebalanceCollateralExternally` function. | ||
struct RebalanceCollateralExternallyParams { | ||
/// @dev protocolId of GammaPool (e.g. version of GammaPool) | ||
uint16 protocolId; | ||
/// @dev address of CFMM, along with protocolId can be used to calculate GammaPool address | ||
address cfmm; | ||
/// @dev tokenId of loan whose collateral will change | ||
uint256 tokenId; | ||
/// @dev amounts of reserve tokens to swap (>0 buy token, <0 sell token). At least one index value must be set to zero | ||
uint128[] amounts; | ||
/// @dev CFMM LP tokens requesting to borrow during external rebalancing. Must be returned at function call end | ||
uint256 lpTokens; | ||
/// @dev address of contract that will rebalance collateral. This address must return collateral back to GammaPool | ||
address to; | ||
/// @param data - optional bytes parameter for custom user defined data | ||
bytes data; | ||
/// @dev timestamp after which the transaction expires. Used to prevent stale transactions from executing | ||
uint256 deadline; | ||
/// @dev minimum amounts of collateral expected to have after re-balancing collateral. Slippage protection | ||
uint128[] minCollateral; | ||
} | ||
|
||
/// @dev Re-balance loan collateral tokens by swapping one for another using an external source | ||
/// @param params - struct containing params to identify a GammaPool and loan with information to re-balance its collateral | ||
/// @return loanLiquidity - updated loan liquidity, includes flash loan fees | ||
/// @return tokensHeld - new loan collateral token amounts | ||
function rebalanceCollateralExternally(RebalanceCollateralExternallyParams calldata params) external returns(uint256 loanLiquidity, uint128[] memory tokensHeld); | ||
|
||
} |
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,40 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
|
||
import "@gammaswap/v1-core/contracts/interfaces/IGammaPoolExternal.sol"; | ||
import "./TestGammaPool2.sol"; | ||
|
||
contract TestGammaPoolExternal is TestGammaPool2, IGammaPoolExternal { | ||
|
||
struct params { | ||
uint256 num1; | ||
address to; | ||
} | ||
|
||
address immutable public override externalRebalanceStrategy; | ||
address immutable public override externalLiquidationStrategy; | ||
|
||
constructor(uint16 protocolId_, address factory_, address borrowStrategy_, address repayStrategy_, address rebalanceStrategy_, | ||
address shortStrategy_, address singleLiquidationStrategy_, address batchLiquidationStrategy_, address viewer_, | ||
address externalRebalanceStrategy_, address externalLiquidationStrategy_) TestGammaPool2(protocolId_, factory_, | ||
borrowStrategy_, repayStrategy_, rebalanceStrategy_, shortStrategy_, singleLiquidationStrategy_, batchLiquidationStrategy_, | ||
viewer_) { | ||
externalRebalanceStrategy = externalRebalanceStrategy_; | ||
externalLiquidationStrategy = externalLiquidationStrategy_; | ||
} | ||
|
||
function rebalanceExternally(uint256 tokenId, uint128[] calldata amounts, uint256 lpTokens, address to, bytes calldata data) external virtual override returns(uint256 loanLiquidity, uint128[] memory tokensHeld) { | ||
params memory _params = abi.decode(data, (params)); | ||
tokensHeld = new uint128[](2); | ||
tokensHeld[0] = uint128(amounts[0] + 10); | ||
tokensHeld[1] = uint128(amounts[1] + 20); | ||
loanLiquidity = uint256(uint160(_params.to)) + _params.num1 + lpTokens; | ||
} | ||
|
||
function liquidateExternally(uint256 tokenId, uint128[] calldata amounts, uint256 lpTokens, address to, bytes calldata data) external virtual override returns(uint256 loanLiquidity, uint256[] memory refund) { | ||
refund = new uint256[](2); | ||
refund[0] = uint256(amounts[0]) + 30; | ||
refund[1] = uint256(amounts[1]) + 40; | ||
return(lpTokens, refund); | ||
} | ||
} |
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
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
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
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