Skip to content

Commit

Permalink
some fixes + interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
leomassazza committed Aug 5, 2024
1 parent 866ae1c commit 17ea246
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 290 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.2 <0.9.0;

import {IFoilStructs} from "./IFoilStructs.sol";
import "../storage/Position.sol";
import "../storage/FAccount.sol";

interface IEpochConfigurationModule {
function initializeMarket(
address owner,
address collateralAsset,
address uniswapPositionManager,
address uniswapQuoter,
address uniswapSwapRouter,
address optimisticOracleV3,
IFoilStructs.EpochParams memory epochParams
) external;

function updateMarket(
address owner,
address uniswapPositionManager,
address uniswapQuoter,
address uniswapSwapRouter,
address optimisticOracleV3,
IFoilStructs.EpochParams memory epochParms
) external;

function createEpoch(
uint256 startTime,
uint256 endTime,
uint160 startingSqrtPriceX96
) external;

function getMarket()
external
view
returns (
address owner,
address collateralAsset,
address uniswapPositionManager,
address uniswapQuoter,
address uniswapSwapRouter,
address optimisticOracleV3,
IFoilStructs.EpochParams memory epochParams
);

function getEpoch(
uint256 id
)
external
view
returns (
uint256 startTime,
uint256 endTime,
address pool,
address ethToken,
address gasToken
);

function getPositionData(
uint256 accountId
) external returns (Position.Data memory);

function getAccountData(
uint256 accountId
) external returns (FAccount.Data memory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ interface IEpochLiquidityModule {
);

function decreaseLiquidityPosition(
uint256 epochId,
uint256 accountId,
uint256 collateralAmount,
uint128 liquidity,
Expand All @@ -44,6 +45,7 @@ interface IEpochLiquidityModule {
) external returns (uint256 amount0, uint256 amount1);

function increaseLiquidityPosition(
uint256 epochId,
uint256 accountId,
uint256 collateralAmount,
uint256 gasTokenAmount,
Expand All @@ -53,6 +55,7 @@ interface IEpochLiquidityModule {
) external returns (uint128 liquidity, uint256 amount0, uint256 amount1);

function getTokenAmounts(
uint256 epochId,
uint256 collateralAmount,
uint160 sqrtPriceX96,
uint160 sqrtPriceAX96,
Expand All @@ -63,6 +66,7 @@ interface IEpochLiquidityModule {
returns (uint256 amount0, uint256 amount1, uint128 liquidity);

function collectFees(
uint256 epochId,
uint256 tokenId
) external returns (uint256 amount0, uint256 amount1);

Expand Down
25 changes: 25 additions & 0 deletions packages/protocol/src/contracts/interfaces/IEpochTradeModule.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.2 <0.9.0;

import {IFoilStructs} from "./IFoilStructs.sol";

interface IEpochTradeModule {
function createTraderPosition(
uint256 epochId,
uint256 collateralAmount,
int256 tokenAmount,
int256 tokenAmountLimit
) external returns (uint256 accountId);

function modifyTraderPosition(
uint256 epochId,
uint256 accountId,
uint256 collateralAmount,
int256 tokenAmount,
int256 tokenAmountLimit
) external;

function getReferencePrice(
uint epochId
) external view returns (uint256 price18Digits);
}
59 changes: 8 additions & 51 deletions packages/protocol/src/contracts/interfaces/IFoil.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,11 @@ import "./IFoilStructs.sol";
import "../storage/Position.sol";
import "../storage/FAccount.sol";
import "./IEpochLiquidityModule.sol";

interface IFoil is IEpochLiquidityModule {
function getEpoch(
uint256 epochId
) external view returns (address pool, address ethToken, address gasToken);

// function getPosition(
// uint256 accountId
// ) external view returns (uint256 tokenAmount0, uint256 tokenAmount1);

function createTraderPosition(
uint256 epochId,
uint256 collateral,
int256 tokenAmount,
int256 tokenAmountLimit
) external returns (uint256 tokenId);

function modifyTraderPosition(
uint256 epochId,
uint256 tokenId,
uint256 collateral,
int256 tokenAmount,
int256 tokenAmountLimit
) external;

function swapTokens(
uint256 amountInVEth,
uint256 amountInVGas
) external returns (uint256 amountOutVEth, uint256 amountOutVGas);

function fakeSettle(uint256 settlementPrice) external;

function getPositionData(
uint256 accountId
) external view returns (Position.Data memory);

function getAccountData(
uint256 accountId
) external view returns (FAccount.Data memory);

function getTokenAmounts(
uint256 collateralAmountETH, // in ETH terms (18 decimals)
int24 tickLower,
int24 tickUpper,
uint160 sqrtPriceX96
) external pure returns (uint256 amountGWEI, uint256 amountGAS);

function getReferencePrice(
uint256 epochId
) external view returns (uint256 price);
}
import "./IEpochTradeModule.sol";
import "./IEpochConfigurationModule.sol";

interface IFoil is
IEpochLiquidityModule,
IEpochTradeModule,
IEpochConfigurationModule
{}
10 changes: 10 additions & 0 deletions packages/protocol/src/contracts/interfaces/IFoilStructs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ interface IFoilStructs {
uint256 collateralAmount;
int size;
}

struct EpochParams {
int24 baseAssetMinPriceTick;
int24 baseAssetMaxPriceTick;
uint24 feeRate;
uint64 assertionLiveness;
address bondCurrency;
uint256 bondAmount;
bytes32 priceUnit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ import "../storage/Position.sol";
import "../storage/ERC721Storage.sol";
import "../storage/ERC721EnumerableStorage.sol";
import "../storage/Market.sol";
import "../interfaces/IEpochConfigurationModule.sol";
import "../interfaces/IFoilStructs.sol";

import "forge-std/console2.sol";

contract EpochConfigurationModule is ReentrancyGuard {
contract EpochConfigurationModule is
IEpochConfigurationModule,
ReentrancyGuard
{
using Epoch for Epoch.Data;
using FAccount for FAccount.Data;
using Position for Position.Data;
Expand All @@ -33,8 +39,8 @@ contract EpochConfigurationModule is ReentrancyGuard {
address uniswapQuoter,
address uniswapSwapRouter,
address optimisticOracleV3,
Market.EpochParams memory epochParams
) external {
IFoilStructs.EpochParams memory epochParams
) external override {
Market.createValid(
owner,
collateralAsset,
Expand All @@ -52,8 +58,8 @@ contract EpochConfigurationModule is ReentrancyGuard {
address uniswapQuoter,
address uniswapSwapRouter,
address optimisticOracleV3,
Market.EpochParams memory epochParms
) external onlyOwner {
IFoilStructs.EpochParams memory epochParms
) external override onlyOwner {
Market.updateValid(
owner, // should be nominate/accept
uniswapPositionManager,
Expand All @@ -68,7 +74,7 @@ contract EpochConfigurationModule is ReentrancyGuard {
uint256 startTime,
uint256 endTime,
uint160 startingSqrtPriceX96
) external onlyOwner {
) external override onlyOwner {
Market.Data storage market = Market.loadValid();

Epoch.createValid(startTime, endTime, startingSqrtPriceX96);
Expand All @@ -77,14 +83,15 @@ contract EpochConfigurationModule is ReentrancyGuard {
function getMarket()
external
view
override
returns (
address owner,
address collateralAsset,
address uniswapPositionManager,
address uniswapQuoter,
address uniswapSwapRouter,
address optimisticOracleV3,
Market.EpochParams memory epochParams
IFoilStructs.EpochParams memory epochParams
)
{
Market.Data storage market = Market.load();
Expand All @@ -104,6 +111,7 @@ contract EpochConfigurationModule is ReentrancyGuard {
)
external
view
override
returns (
uint256 startTime,
uint256 endTime,
Expand All @@ -124,13 +132,13 @@ contract EpochConfigurationModule is ReentrancyGuard {

function getPositionData(
uint256 accountId
) external pure returns (Position.Data memory) {
) external pure override returns (Position.Data memory) {
return Position.load(accountId);
}

function getAccountData(
uint256 accountId
) external pure returns (FAccount.Data memory) {
) external pure override returns (FAccount.Data memory) {
return FAccount.load(accountId);
}
}
43 changes: 12 additions & 31 deletions packages/protocol/src/contracts/modules/EpochLiquidityModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,41 +128,17 @@ contract EpochLiquidityModule is
// TODO: emit event
}

function getPosition(
uint256 positionId
)
external
view
returns (
uint96 nonce,
address operator,
address token0,
address token1,
uint24 fee,
int24 tickLower,
int24 tickUpper,
uint128 liquidity,
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128,
uint128 tokensOwed0,
uint128 tokensOwed1
)
{
Market.Data storage market = Market.load();
return market.uniswapPositionManager.positions(positionId);
}

function updateLiquidityPosition(
function decreaseLiquidityPosition(
uint256 epochId,
uint256 tokenId,
uint256 collateral,
uint256 accountId,
uint256 collateralAmount,
uint128 liquidity,
uint256 minGasAmount,
uint256 minEthAmount
) external override returns (uint256 amount0, uint256 amount1) {
Market.Data storage market = Market.load();
Epoch.Data storage epoch = Epoch.load(epochId);
FAccount.Data storage account = FAccount.load(accountId);
Epoch.Data storage epoch = Epoch.load(epochId);

(, , , , , int24 lowerTick, int24 upperTick, , , , , ) = market
.uniswapPositionManager
Expand All @@ -185,7 +161,7 @@ contract EpochLiquidityModule is
);

account.updateLoan(account.tokenId, collateralAmount, amount0, amount1);
Epoch.load().validateProvidedLiquidity(
epoch.validateProvidedLiquidity(
collateralAmount,
liquidity,
lowerTick,
Expand All @@ -198,6 +174,7 @@ contract EpochLiquidityModule is
}

function increaseLiquidityPosition(
uint256 epochId,
uint256 accountId,
uint256 collateralAmount,
uint256 gasTokenAmount,
Expand All @@ -207,6 +184,7 @@ contract EpochLiquidityModule is
) external returns (uint128 liquidity, uint256 amount0, uint256 amount1) {
Market.Data storage market = Market.load();
FAccount.Data storage account = FAccount.load(accountId);
Epoch.Data storage epoch = Epoch.load(epochId);

(, , , , , int24 lowerTick, int24 upperTick, , , , , ) = market
.uniswapPositionManager
Expand All @@ -230,7 +208,7 @@ contract EpochLiquidityModule is
.increaseLiquidity(increaseParams);

account.updateLoan(account.tokenId, collateralAmount, amount0, amount1);
Epoch.load().validateProvidedLiquidity(
epoch.validateProvidedLiquidity(
collateralAmount,
liquidity,
lowerTick,
Expand All @@ -246,6 +224,7 @@ contract EpochLiquidityModule is
}

function getTokenAmounts(
uint256 epochId,
uint256 collateralAmount,
uint160 sqrtPriceX96,
uint160 sqrtPriceAX96,
Expand All @@ -256,6 +235,8 @@ contract EpochLiquidityModule is
override
returns (uint256 amount0, uint256 amount1, uint128 liquidity)
{
Epoch.Data storage epoch = Epoch.load(epochId);

// calculate for unit
uint128 unitLiquidity = LiquidityAmounts.getLiquidityForAmounts(
sqrtPriceX96,
Expand All @@ -269,7 +250,7 @@ contract EpochLiquidityModule is
uint256 requiredCollateral,
uint256 unitAmount0,
uint256 uintAmount1
) = Epoch.load().requiredCollateralForLiquidity(
) = epoch.requiredCollateralForLiquidity(
unitLiquidity,
sqrtPriceX96,
sqrtPriceAX96,
Expand Down
Loading

0 comments on commit 17ea246

Please sign in to comment.