Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tqin7 committed Sep 23, 2024
1 parent 23c1c7d commit c0f4665
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
2 changes: 1 addition & 1 deletion protocol/x/vault/keeper/grpc_query_withdrawal_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (k Keeper) MegavaultWithdrawalInfo(
redeemedQuoteQuantums, megavaultEquity, totalShares, err := k.RedeemFromMainAndSubVaults(
ctx,
req.SharesToWithdraw.NumShares.BigInt(),
false,
true, // simulate the redemption (even though queries do execute on branched contexts).
)
if err != nil {
return nil, err
Expand Down
67 changes: 56 additions & 11 deletions protocol/x/vault/keeper/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (k Keeper) WithdrawFromMegavault(
// 2. Redeem from main and sub vaults.
// Note that in below function, quote quantums redeemed from each sub vault are transferred to the main vault.
redeemedQuoteQuantums, megavaultEquity, totalShares, err :=
k.RedeemFromMainAndSubVaults(ctx, sharesToWithdraw, true)
k.RedeemFromMainAndSubVaults(ctx, sharesToWithdraw, false) // set `simulate` to false.
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -223,12 +223,12 @@ func (k Keeper) WithdrawFromMegavault(
}

// RedeemFromMainAndSubVaults redeems `shares` number of shares from main and sub vaults.
// If and only if `txMode` is true, logs are enabled and quote quantums redeemed from each
// If and only if `simulate` is false, logs are enabled and quote quantums redeemed from each
// sub vault are transferred to the main vault.
func (k Keeper) RedeemFromMainAndSubVaults(
ctx sdk.Context,
shares *big.Int,
txMode bool,
simulate bool,
) (
redeemedQuoteQuantums *big.Int,
megavaultEquity *big.Int,
Expand All @@ -247,7 +247,9 @@ func (k Keeper) RedeemFromMainAndSubVaults(
}
megavaultEquity, err = k.GetSubaccountEquity(ctx, types.MegavaultMainSubaccount)
if err != nil {
if txMode {
if simulate {
log.DebugLog(ctx, "Megavault withdrawal: failed to get megavault main vault equity", "error", err)
} else {
log.ErrorLogWithError(ctx, "Megavault withdrawal: failed to get megavault main vault equity", err)
}
return nil, nil, totalShares, err
Expand All @@ -269,7 +271,14 @@ func (k Keeper) RedeemFromMainAndSubVaults(

vaultId, err := types.GetVaultIdFromStateKey(vaultParamsIterator.Key())
if err != nil {
if txMode {
if simulate {
log.DebugLog(
ctx,
"Megavault withdrawal: failed to get vault ID from state key. Skipping this vault",
"error",
err,
)
} else {
log.ErrorLogWithError(
ctx,
"Megavault withdrawal: error when getting vault ID from state key. Skipping this vault",
Expand All @@ -281,7 +290,16 @@ func (k Keeper) RedeemFromMainAndSubVaults(

_, perpetual, marketParam, marketPrice, err := k.GetVaultClobPerpAndMarket(ctx, *vaultId)
if err != nil {
if txMode {
if simulate {
log.DebugLog(
ctx,
"Megavault withdrawal: failed to get perpetual and market. Skipping this vault",
"Vault ID",
vaultId,
"Error",
err,
)
} else {
log.ErrorLogWithError(
ctx,
"Megavault withdrawal: error when getting perpetual and market. Skipping this vault",
Expand All @@ -294,7 +312,16 @@ func (k Keeper) RedeemFromMainAndSubVaults(
}
leverage, equity, err := k.GetVaultLeverageAndEquity(ctx, *vaultId, &perpetual, &marketPrice)
if err != nil {
if txMode {
if simulate {
log.DebugLog(
ctx,
"Megavault withdrawal: failed to get vault leverage and equity. Skipping this vault",
"Vault ID",
vaultId,
"Error",
err,
)
} else {
log.ErrorLogWithError(
ctx,
"Megavault withdrawal: error when getting vault leverage and equity. Skipping this vault",
Expand All @@ -316,7 +343,16 @@ func (k Keeper) RedeemFromMainAndSubVaults(
&marketParam,
)
if err != nil {
if txMode {
if simulate {
log.DebugLog(
ctx,
"Megavault withdrawal: failed to get vault withdrawal slippage. Skipping this vault",
"Vault ID",
vaultId,
"Error",
err,
)
} else {
log.ErrorLogWithError(
ctx,
"Megavault withdrawal: error when getting vault withdrawal slippage. Skipping this vault",
Expand All @@ -335,8 +371,17 @@ func (k Keeper) RedeemFromMainAndSubVaults(
quantumsToTransfer := new(big.Int).Quo(redeemedFromSubVault.Num(), redeemedFromSubVault.Denom())

if quantumsToTransfer.Sign() <= 0 || !quantumsToTransfer.IsUint64() {
if txMode {
log.InfoLog(
if simulate {
log.DebugLog(
ctx,
"Megavault withdrawal: quantums to transfer is invalid. Skipping this vault",
"Vault ID",
vaultId,
"Quantums",
quantumsToTransfer,
)
} else {
log.ErrorLog(
ctx,
"Megavault withdrawal: quantums to transfer is invalid. Skipping this vault",
"Vault ID",
Expand All @@ -347,7 +392,7 @@ func (k Keeper) RedeemFromMainAndSubVaults(
}
continue
}
if txMode {
if !simulate {
// Transfer from sub vault to main vault.
err = k.sendingKeeper.ProcessTransfer(
ctx,
Expand Down

0 comments on commit c0f4665

Please sign in to comment.