Skip to content

Commit

Permalink
update virtual swap inventory tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
xdev10 committed Jun 16, 2023
1 parent 6aef361 commit 1b35173
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 46 deletions.
4 changes: 2 additions & 2 deletions contracts/data/Keys.sol
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,11 @@ library Keys {
// @dev the key for the virtual inventory for swaps
// @param the virtualMarketId the virtual market id
// @param the token to check the inventory for
function virtualInventoryForSwapsKey(bytes32 virtualMarketId, address token) internal pure returns (bytes32) {
function virtualInventoryForSwapsKey(bytes32 virtualMarketId, bool isLongToken) internal pure returns (bytes32) {
return keccak256(abi.encode(
VIRTUAL_INVENTORY_FOR_SWAPS,
virtualMarketId,
token
isLongToken
));
}

Expand Down
4 changes: 2 additions & 2 deletions contracts/deposit/ExecuteDepositUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ library ExecuteDepositUtils {
MarketUtils.applyDeltaToPoolAmount(
params.dataStore,
params.eventEmitter,
_params.market.marketToken,
_params.market,
_params.tokenOut,
positiveImpactAmount
);
Expand Down Expand Up @@ -394,7 +394,7 @@ library ExecuteDepositUtils {
MarketUtils.applyDeltaToPoolAmount(
params.dataStore,
params.eventEmitter,
_params.market.marketToken,
_params.market,
_params.tokenIn,
(fees.amountAfterFees + fees.feeAmountForPool).toInt256()
);
Expand Down
1 change: 1 addition & 0 deletions contracts/error/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ library Errors {
error InsufficientPoolAmount(uint256 poolAmount, uint256 amount);
error InsufficientReserve(uint256 reservedUsd, uint256 maxReservedUsd);
error UnableToGetOppositeToken(address inputToken, address market);
error UnexpectedTokenForVirtualInventory(address token, address market);
error EmptyMarketTokenSupply();
error InvalidSwapMarket(address market);
error UnableToGetCachedTokenPrice(address token, address market);
Expand Down
10 changes: 6 additions & 4 deletions contracts/market/MarketEventUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,18 @@ library MarketEventUtils {
function emitVirtualSwapInventoryUpdated(
EventEmitter eventEmitter,
address market,
address token,
bool isLongToken,
bytes32 virtualMarketId,
int256 delta,
uint256 nextValue
) external {
EventUtils.EventLogData memory eventData;

eventData.addressItems.initItems(2);
eventData.addressItems.setItem(0, "token", token);
eventData.addressItems.setItem(1, "market", market);
eventData.addressItems.initItems(1);
eventData.addressItems.setItem(0, "market", market);

eventData.boolItems.initItems(1);
eventData.boolItems.setItem(0, "isLongToken", isLongToken);

eventData.bytes32Items.initItems(1);
eventData.bytes32Items.setItem(0, "virtualMarketId", virtualMarketId);
Expand Down
38 changes: 26 additions & 12 deletions contracts/market/MarketUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -699,12 +699,12 @@ library MarketUtils {
function applyDeltaToPoolAmount(
DataStore dataStore,
EventEmitter eventEmitter,
address market,
Market.Props memory market,
address token,
int256 delta
) internal returns (uint256) {
uint256 nextValue = dataStore.applyDeltaToUint(
Keys.poolAmountKey(market, token),
Keys.poolAmountKey(market.marketToken, token),
delta,
"Invalid state, negative poolAmount"
);
Expand All @@ -717,7 +717,7 @@ library MarketUtils {
delta
);

MarketEventUtils.emitPoolAmountUpdated(eventEmitter, market, token, delta, nextValue);
MarketEventUtils.emitPoolAmountUpdated(eventEmitter, market.marketToken, token, delta, nextValue);

return nextValue;
}
Expand Down Expand Up @@ -1455,14 +1455,26 @@ library MarketUtils {
// @dev get the virtual inventory for swaps
// @param dataStore DataStore
// @param market the market to check
// @param token the token to check
function getVirtualInventoryForSwaps(DataStore dataStore, address market, address token) internal view returns (bool, uint256) {
// @return returns (has virtual inventory, virtual long token inventory, virtual short token inventory)
function getVirtualInventoryForSwaps(DataStore dataStore, address market) internal view returns (bool, uint256, uint256) {
bytes32 virtualMarketId = dataStore.getBytes32(Keys.virtualMarketIdKey(market));
if (virtualMarketId == bytes32(0)) {
return (false, 0);
return (false, 0, 0);
}

return (true, dataStore.getUint(Keys.virtualInventoryForSwapsKey(virtualMarketId, token)));
return (
true,
dataStore.getUint(Keys.virtualInventoryForSwapsKey(virtualMarketId, true)),
dataStore.getUint(Keys.virtualInventoryForSwapsKey(virtualMarketId, false))
);
}

function getIsLongToken(Market.Props memory market, address token) internal pure returns (bool) {
if (token != market.longToken || token != market.shortToken) {
revert Errors.UnexpectedTokenForVirtualInventory(token, market.marketToken);
}

return token == market.longToken;
}

// @dev get the virtual inventory for positions
Expand All @@ -1479,27 +1491,29 @@ library MarketUtils {

// @dev update the virtual inventory for swaps
// @param dataStore DataStore
// @param market the market to update
// @param marketAddress the market to update
// @param token the token to update
// @param delta the update amount
function applyDeltaToVirtualInventoryForSwaps(
DataStore dataStore,
EventEmitter eventEmitter,
address market,
Market.Props memory market,
address token,
int256 delta
) internal returns (bool, uint256) {
bytes32 virtualMarketId = dataStore.getBytes32(Keys.virtualMarketIdKey(market));
bytes32 virtualMarketId = dataStore.getBytes32(Keys.virtualMarketIdKey(market.marketToken));
if (virtualMarketId == bytes32(0)) {
return (false, 0);
}

bool isLongToken = getIsLongToken(market, token);

uint256 nextValue = dataStore.applyBoundedDeltaToUint(
Keys.virtualInventoryForSwapsKey(virtualMarketId, token),
Keys.virtualInventoryForSwapsKey(virtualMarketId, isLongToken),
delta
);

MarketEventUtils.emitVirtualSwapInventoryUpdated(eventEmitter, market, token, virtualMarketId, delta, nextValue);
MarketEventUtils.emitVirtualSwapInventoryUpdated(eventEmitter, market.marketToken, isLongToken, virtualMarketId, delta, nextValue);

return (true, nextValue);
}
Expand Down
18 changes: 9 additions & 9 deletions contracts/position/DecreasePositionCollateralUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ library DecreasePositionCollateralUtils {
MarketUtils.applyDeltaToPoolAmount(
params.contracts.dataStore,
params.contracts.eventEmitter,
params.market.marketToken,
params.market,
cache.pnlToken,
-deductionAmountForPool.toInt256()
);
Expand Down Expand Up @@ -174,7 +174,7 @@ library DecreasePositionCollateralUtils {
MarketUtils.applyDeltaToPoolAmount(
params.contracts.dataStore,
params.contracts.eventEmitter,
params.market.marketToken,
params.market,
cache.pnlToken,
-deductionAmountForPool.toInt256()
);
Expand Down Expand Up @@ -273,7 +273,7 @@ library DecreasePositionCollateralUtils {
MarketUtils.applyDeltaToPoolAmount(
params.contracts.dataStore,
params.contracts.eventEmitter,
params.market.marketToken,
params.market,
params.position.collateralToken(),
collateralCache.result.amountPaidInCollateralToken.toInt256()
);
Expand All @@ -283,7 +283,7 @@ library DecreasePositionCollateralUtils {
MarketUtils.applyDeltaToPoolAmount(
params.contracts.dataStore,
params.contracts.eventEmitter,
params.market.marketToken,
params.market,
values.output.secondaryOutputToken,
collateralCache.result.amountPaidInSecondaryOutputToken.toInt256()
);
Expand Down Expand Up @@ -321,7 +321,7 @@ library DecreasePositionCollateralUtils {
MarketUtils.applyDeltaToPoolAmount(
params.contracts.dataStore,
params.contracts.eventEmitter,
params.market.marketToken,
params.market,
params.position.collateralToken(),
fees.feeAmountForPool.toInt256()
);
Expand Down Expand Up @@ -352,7 +352,7 @@ library DecreasePositionCollateralUtils {
MarketUtils.applyDeltaToPoolAmount(
params.contracts.dataStore,
params.contracts.eventEmitter,
params.market.marketToken,
params.market,
params.position.collateralToken(),
collateralCache.result.amountPaidInCollateralToken.toInt256()
);
Expand All @@ -362,7 +362,7 @@ library DecreasePositionCollateralUtils {
MarketUtils.applyDeltaToPoolAmount(
params.contracts.dataStore,
params.contracts.eventEmitter,
params.market.marketToken,
params.market,
values.output.secondaryOutputToken,
collateralCache.result.amountPaidInSecondaryOutputToken.toInt256()
);
Expand Down Expand Up @@ -441,7 +441,7 @@ library DecreasePositionCollateralUtils {
MarketUtils.applyDeltaToPoolAmount(
params.contracts.dataStore,
params.contracts.eventEmitter,
params.market.marketToken,
params.market,
params.position.collateralToken(),
collateralCache.result.amountPaidInCollateralToken.toInt256()
);
Expand All @@ -458,7 +458,7 @@ library DecreasePositionCollateralUtils {
MarketUtils.applyDeltaToPoolAmount(
params.contracts.dataStore,
params.contracts.eventEmitter,
params.market.marketToken,
params.market,
values.output.secondaryOutputToken,
collateralCache.result.amountPaidInSecondaryOutputToken.toInt256()
);
Expand Down
2 changes: 1 addition & 1 deletion contracts/position/IncreasePositionUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ library IncreasePositionUtils {
MarketUtils.applyDeltaToPoolAmount(
params.contracts.dataStore,
params.contracts.eventEmitter,
params.market.marketToken,
params.market,
params.position.collateralToken(),
fees.feeAmountForPool.toInt256()
);
Expand Down
24 changes: 14 additions & 10 deletions contracts/pricing/SwapPricingUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,26 @@ library SwapPricingUtils {
// disincentivise the balancing of pools
if (priceImpactUsd >= 0) { return priceImpactUsd; }

(bool hasVirtualInventoryTokenA, uint256 virtualPoolAmountForTokenA) = MarketUtils.getVirtualInventoryForSwaps(
(bool hasVirtualInventory, uint256 virtualPoolAmountForLongToken, uint256 virtualPoolAmountForShortToken) = MarketUtils.getVirtualInventoryForSwaps(
params.dataStore,
params.market.marketToken,
params.tokenA
params.market.marketToken
);

(bool hasVirtualInventoryTokenB, uint256 virtualPoolAmountForTokenB) = MarketUtils.getVirtualInventoryForSwaps(
params.dataStore,
params.market.marketToken,
params.tokenB
);

if (!hasVirtualInventoryTokenA || !hasVirtualInventoryTokenB) {
if (!hasVirtualInventory) {
return priceImpactUsd;
}

uint256 virtualPoolAmountForTokenA;
uint256 virtualPoolAmountForTokenB;

if (params.tokenA == params.market.longToken) {
virtualPoolAmountForTokenA = virtualPoolAmountForLongToken;
virtualPoolAmountForTokenB = virtualPoolAmountForShortToken;
} else {
virtualPoolAmountForTokenA = virtualPoolAmountForShortToken;
virtualPoolAmountForTokenB = virtualPoolAmountForLongToken;
}

PoolParams memory poolParamsForVirtualInventory = getNextPoolAmountsParams(
params,
virtualPoolAmountForTokenA,
Expand Down
3 changes: 1 addition & 2 deletions contracts/reader/Reader.sol
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,7 @@ contract Reader {
DataStore dataStore,
Market.Props memory market
) internal view returns (VirtualInventory memory) {
(, uint256 virtualPoolAmountForLongToken) = MarketUtils.getVirtualInventoryForSwaps(dataStore, market.marketToken, market.longToken);
(, uint256 virtualPoolAmountForShortToken) = MarketUtils.getVirtualInventoryForSwaps(dataStore, market.marketToken, market.shortToken);
(, uint256 virtualPoolAmountForLongToken, uint256 virtualPoolAmountForShortToken) = MarketUtils.getVirtualInventoryForSwaps(dataStore, market.marketToken);
(, int256 virtualInventoryForPositions) = MarketUtils.getVirtualInventoryForPositions(dataStore, market.indexToken);

return VirtualInventory(
Expand Down
4 changes: 2 additions & 2 deletions contracts/swap/SwapUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ library SwapUtils {
MarketUtils.applyDeltaToPoolAmount(
params.dataStore,
params.eventEmitter,
_params.market.marketToken,
_params.market,
_params.tokenIn,
(cache.amountIn + fees.feeAmountForPool).toInt256()
);
Expand All @@ -301,7 +301,7 @@ library SwapUtils {
MarketUtils.applyDeltaToPoolAmount(
params.dataStore,
params.eventEmitter,
_params.market.marketToken,
_params.market,
cache.tokenOut,
-cache.poolAmountOut.toInt256()
);
Expand Down
4 changes: 2 additions & 2 deletions contracts/withdrawal/WithdrawalUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,15 @@ library WithdrawalUtils {
MarketUtils.applyDeltaToPoolAmount(
params.dataStore,
params.eventEmitter,
market.marketToken,
market,
market.longToken,
-cache.longTokenPoolAmountDelta.toInt256()
);

MarketUtils.applyDeltaToPoolAmount(
params.dataStore,
params.eventEmitter,
market.marketToken,
market,
market.shortToken,
-cache.shortTokenPoolAmountDelta.toInt256()
);
Expand Down

0 comments on commit 1b35173

Please sign in to comment.