🌾Key Function - harvest
In-depth descriptions of key functions
harvest
/**
* @notice harvest the strategy. This involves accruing profits from the strategy and depositing
* user funds to the strategy. The funds are split into their constituents and then distributed
* to their appropriate location.
* For the shortPosition a perpetual position is opened, for the long position funds are swapped
* to the long asset. For the buffer position the funds are deposited to the margin account idle.
* @dev only callable by the owner
*/
function harvest() public onlyOwner {
uint256 shortPosition;
uint256 longPosition;
uint256 bufferPosition;
isUnwind = false;
mcLiquidityPool.forceToSyncState();
// determine the profit since the last harvest and remove profits from the margin
// account to be redistributed
uint256 amount;
bool loss;
if (positions.unitAccumulativeFunding != 0) {
(amount, loss) = _determineFee();
}
// update the vault with profits/losses accrued and receive deposits
uint256 newFunds = vault.update(amount, loss);
// combine the funds and check that they are larger than 0
uint256 toActivate = IERC20(want).balanceOf(address(this));
if (toActivate > 0) {
// determine the split of the funds and trade for the spot position of long
(shortPosition, longPosition, bufferPosition) = _calculateSplit(
toActivate
);
// deposit the bufferPosition to the margin account
_depositToMarginAccount(bufferPosition);
// open a short perpetual position and store the number of perp contracts
positions.perpContracts += _openPerpPosition(shortPosition, true);
}
// record incremented positions
positions.margin = getMargin();
positions.unitAccumulativeFunding = getUnitAccumulativeFunding();
emit Harvest(
positions.perpContracts,
IERC20(long).balanceOf(address(this)),
positions.margin
);
}Last updated