Skip to content

Commit

Permalink
Merge pull request #31 from foilxyz/trade-tests
Browse files Browse the repository at this point in the history
Add trades tests (and fixes) part 1
  • Loading branch information
leomassazza authored Aug 15, 2024
2 parents 317e667 + 98d7671 commit 3746fad
Show file tree
Hide file tree
Showing 3 changed files with 650 additions and 251 deletions.
69 changes: 43 additions & 26 deletions packages/protocol/src/contracts/modules/EpochTradeModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ contract EpochTradeModule is IEpochTradeModule {
);
}

position.updateCollateral(
Market.load().collateralAsset,
collateralAmount
);

// Validate after trading that collateral is enough
position.afterTradeCheck();
}
Expand Down Expand Up @@ -126,7 +131,6 @@ contract EpochTradeModule is IEpochTradeModule {
// with the collateral get vEth (Loan)
uint256 vEthLoan = collateralAmount; // 1:1

position.depositedCollateralAmount += collateralAmount;
position.borrowedVEth += vEthLoan;

if (tokenAmount < 0 || tokenAmountLimit < 0) {
Expand Down Expand Up @@ -156,6 +160,11 @@ contract EpochTradeModule is IEpochTradeModule {
// Refund excess vEth sent
position.borrowedVEth -= refundAmountVEth;

position.updateCollateral(
Market.load().collateralAsset,
collateralAmount
);

position.updateBalance(
tokenAmount,
tokenAmountVEth.toInt(),
Expand All @@ -181,7 +190,6 @@ contract EpochTradeModule is IEpochTradeModule {
// with the collateral get vGas (Loan)
uint256 vGasLoan = (tokenAmount * -1).toUint(); //(collateralAmount).divDecimal(getReferencePrice()); // collatera / vEth = 1/1 ; vGas/vEth = 1/currentPrice

position.depositedCollateralAmount += collateralAmount;
position.borrowedVGas += vGasLoan;

if (tokenAmount > 0 || tokenAmountLimit > 0) {
Expand All @@ -202,6 +210,11 @@ contract EpochTradeModule is IEpochTradeModule {
params
);

position.updateCollateral(
Market.load().collateralAsset,
collateralAmount
);

position.updateBalance(
tokenAmount,
tokenAmountVEth.toInt(),
Expand Down Expand Up @@ -242,13 +255,11 @@ contract EpochTradeModule is IEpochTradeModule {
uint256 delta = (tokenAmount - position.currentTokenAmount)
.toUint();
// with the collateral get vEth (Loan)
position.depositedCollateralAmount += collateralAmount;
uint256 vEthLoan = position.depositedCollateralAmount; // 1:1
position.borrowedVEth = vEthLoan;
uint256 vEthDeltaLoan = collateralAmount; // 1:1

SwapTokensExactOutParams memory params = SwapTokensExactOutParams({
epochId: position.epochId,
availableAmountInVEth: vEthLoan,
availableAmountInVEth: vEthDeltaLoan,
availableAmountInVGas: 0,
amountInLimitVEth: 0,
amountInLimitVGas: 0,
Expand All @@ -263,7 +274,9 @@ contract EpochTradeModule is IEpochTradeModule {
uint256 tokenAmountVEth,
uint256 tokenAmountVGas
) = swapTokensExactOut(params);
position.borrowedVEth -= refundAmountVEth;
// Adjust the delta loan with the refund
vEthDeltaLoan -= refundAmountVEth;
position.borrowedVEth += vEthDeltaLoan;

position.updateBalance(
delta.toInt(),
Expand All @@ -272,11 +285,6 @@ contract EpochTradeModule is IEpochTradeModule {
);
} else {
// Reduce the position (LONG)
if (collateralAmount > 0) {
revert Errors.InvalidData(
"Long Position: Unexpected collateral"
);
}

int256 delta = (tokenAmount - position.currentTokenAmount);

Expand All @@ -299,6 +307,11 @@ contract EpochTradeModule is IEpochTradeModule {

position.updateBalance(delta, 0, delta);
}

position.updateCollateral(
Market.load().collateralAsset,
collateralAmount
);
}

/**
Expand Down Expand Up @@ -333,18 +346,21 @@ contract EpochTradeModule is IEpochTradeModule {
// Increase the position (SHORT)

int256 delta = (tokenAmount - position.currentTokenAmount);
int256 deltaLimit = (tokenAmountLimit -
position.currentTokenAmount);
int256 deltaLimit;
if (tokenAmountLimit >= position.currentTokenAmount) {
deltaLimit = 0;
} else {
deltaLimit = (tokenAmountLimit - position.currentTokenAmount);
}

// with the collateral get vGas (Loan)
uint256 vGasLoan = (delta * -1).toUint(); //
position.depositedCollateralAmount += collateralAmount;
uint256 vGasLoan = (delta * -1).toUint();
position.borrowedVGas += vGasLoan;

SwapTokensExactInParams memory params = SwapTokensExactInParams({
epochId: position.epochId,
amountInVEth: 0,
amountInVGas: (delta * -1).toUint(),
amountInVGas: vGasLoan,
amountOutLimitVEth: 0,
amountOutLimitVGas: (deltaLimit * -1).toUint()
});
Expand All @@ -362,13 +378,8 @@ contract EpochTradeModule is IEpochTradeModule {
);
} else {
// Decrease the position (SHORT)
if (collateralAmount > 0) {
revert Errors.InvalidData(
"Short Position: Unexpected collateral"
);
}

int256 delta = (position.currentTokenAmount - tokenAmount);
position.borrowedVGas -= (delta * -1).toUint();

SwapTokensExactOutParams memory params = SwapTokensExactOutParams({
epochId: position.epochId,
Expand All @@ -394,6 +405,11 @@ contract EpochTradeModule is IEpochTradeModule {
0
);
}

position.updateCollateral(
Market.load().collateralAsset,
collateralAmount
);
}

/**
Expand Down Expand Up @@ -428,9 +444,6 @@ contract EpochTradeModule is IEpochTradeModule {
) internal {
// TODO check if after settlement and use the settlement price

// Add sent collateral
position.depositedCollateralAmount += collateralAmount;

if (position.currentTokenAmount > 0) {
// Close LONG position
SwapTokensExactInParams memory params = SwapTokensExactInParams({
Expand Down Expand Up @@ -459,6 +472,8 @@ contract EpochTradeModule is IEpochTradeModule {

position.borrowedVEth = 0;

position.updateCollateral(Market.load().collateralAsset, 0);

position.resetBalance();
} else {
// Close SHORT position
Expand Down Expand Up @@ -523,6 +538,8 @@ contract EpochTradeModule is IEpochTradeModule {

position.borrowedVGas = 0;

position.updateCollateral(Market.load().collateralAsset, 0);

position.resetBalance();
}
}
Expand Down
Loading

0 comments on commit 3746fad

Please sign in to comment.