Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TRA-621] Sweep bank funds of megavault into subaccount. #2293

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,8 @@ func New(
app.VaultKeeper = *vaultmodulekeeper.NewKeeper(
appCodec,
keys[vaultmoduletypes.StoreKey],
app.AssetsKeeper,
app.BankKeeper,
app.ClobKeeper,
app.DelayMsgKeeper,
app.PerpetualsKeeper,
Expand Down
201 changes: 201 additions & 0 deletions protocol/mocks/AssetsKeeper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions protocol/mocks/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ mock-gen:
@go run github.com/vektra/mockery/v2 --name=OracleClient --dir=$(GOPATH)/pkg/mod/github.com/skip-mev/slinky@$(SLINKY_VERSION)/service/clients/oracle --recursive --output=./mocks
@go run github.com/vektra/mockery/v2 --name=ExtendVoteHandler --dir=$(GOPATH)/pkg/mod/github.com/dydxprotocol/cosmos-sdk@$(COSMOS_VERSION)/types --recursive --output=./mocks
@go run github.com/vektra/mockery/v2 --name=UpdateMarketPriceTxDecoder --dir=./app/process --recursive --output=./mocks
@go run github.com/vektra/mockery/v2 --name=AssetsKeeper --dir=./x//types --recursive --output=./mocks
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct the directory path in the mock generation command

The --dir argument in the mock generation command contains an extra slash (//) which may cause issues when generating the mock for AssetsKeeper. It should point to the correct directory where AssetsKeeper is defined.

Apply this diff to fix the directory path:

-	@go run github.com/vektra/mockery/v2 --name=AssetsKeeper --dir=./x//types --recursive --output=./mocks
+	@go run github.com/vektra/mockery/v2 --name=AssetsKeeper --dir=./x/assets/types --recursive --output=./mocks
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@go run github.com/vektra/mockery/v2 --name=AssetsKeeper --dir=./x//types --recursive --output=./mocks
@go run github.com/vektra/mockery/v2 --name=AssetsKeeper --dir=./x/assets/types --recursive --output=./mocks

2 changes: 2 additions & 0 deletions protocol/testutil/keeper/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func createVaultKeeper(
k := keeper.NewKeeper(
cdc,
storeKey,
&mocks.AssetsKeeper{},
&mocks.BankKeeper{},
&mocks.ClobKeeper{},
&mocks.DelayMsgKeeper{},
&mocks.PerpetualsKeeper{},
Expand Down
31 changes: 31 additions & 0 deletions protocol/x/assets/types/types.go
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
package types

import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
)

type AssetsKeeper interface {
ConvertAssetToCoin(ctx sdk.Context, assetId uint32, quantums *big.Int) (*big.Int, sdk.Coin, error)
CreateAsset(
ctx sdk.Context,
assetId uint32,
symbol string,
denom string,
denomExponent int32,
hasMarket bool,
marketId uint32,
atomicResolution int32,
) (
Asset,
error,
)

GetAsset(ctx sdk.Context, id uint32) (Asset, bool)

GetAllAssets(ctx sdk.Context) []Asset

IsPositionUpdatable(ctx sdk.Context, id uint32) (bool, error)

ModifyAsset(ctx sdk.Context, id uint32, hasMarket bool, marketId uint32) (Asset, error)
}
1 change: 1 addition & 0 deletions protocol/x/vault/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func EndBlocker(
// halting the chain.
if err := abci.RunCached(ctx, func(ctx sdk.Context) error {
keeper.RefreshAllVaultOrders(ctx)
keeper.SweepMainVaultBankBalance(ctx)
return nil
}); err != nil {
log.ErrorLog(
Expand Down
6 changes: 6 additions & 0 deletions protocol/x/vault/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
assetsKeeper types.AssetsKeeper
bankKeeper types.BankKeeper
clobKeeper types.ClobKeeper
delayMsgKeeper types.DelayMsgKeeper
perpetualsKeeper types.PerpetualsKeeper
Expand All @@ -30,6 +32,8 @@ type (
func NewKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
assetsKeeper types.AssetsKeeper,
bankKeeper types.BankKeeper,
clobKeeper types.ClobKeeper,
delayMsgKeeper types.DelayMsgKeeper,
perpetualsKeeper types.PerpetualsKeeper,
Expand All @@ -42,6 +46,8 @@ func NewKeeper(
return &Keeper{
cdc: cdc,
storeKey: storeKey,
assetsKeeper: assetsKeeper,
bankKeeper: bankKeeper,
clobKeeper: clobKeeper,
delayMsgKeeper: delayMsgKeeper,
perpetualsKeeper: perpetualsKeeper,
Expand Down
48 changes: 48 additions & 0 deletions protocol/x/vault/keeper/sweep_funds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/lib/log"
assettypes "github.com/dydxprotocol/v4-chain/protocol/x/assets/types"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
)

// SweepMainVaultBankBalances deposits any usdc balance from the Megavault main vault bank balance
// into the Megavault main vault subaccount balance.
func (k Keeper) SweepMainVaultBankBalance(
ctx sdk.Context,
) {
usdcAsset, exists := k.assetsKeeper.GetAsset(ctx, assettypes.AssetUsdc.Id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can also just use assetstypes.AssetUsdc to be simpler? don't need to pass in AssetsKeeper anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Led to a flaky simulation test when I removed it, going to add it back in for now.

if !exists {
log.ErrorLog(
ctx,
"SweepMainVaultBankBalance: Usdc asset not found in state",
)
return
}
mainVaultBalance := k.bankKeeper.GetBalance(
ctx,
types.MegavaultMainAddress,
usdcAsset.Denom,
)
// No funds to sweep
if mainVaultBalance.Amount.BigInt().Sign() <= 0 {
return
}

err := k.subaccountsKeeper.DepositFundsFromAccountToSubaccount(
ctx,
types.MegavaultMainAddress,
types.MegavaultMainSubaccount,
usdcAsset.Id,
mainVaultBalance.Amount.BigInt(),
)
if err != nil {
log.ErrorLogWithError(
ctx,
"SweepMainVaultBankBalance: Failed to sweep funds from main vault bank balance to subaccount",
err,
)
return
}
}
Loading
Loading