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

updates vaults #30

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
47 changes: 42 additions & 5 deletions proto/reserve/vaults/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ service Msg {
rpc ActiveCollateral(MsgActiveCollateral)
returns (MsgActiveCollateralResponse);

// UpdatesCollateral defines a method for update a collateral asset
rpc UpdatesCollateral(MsgUpdatesCollateral)
returns (MsgUpdatesCollateralResponse);

// CreateVault defines a method for creating a new vault and mint token
rpc CreateVault(MsgCreateVault) returns (MsgCreateVaultResponse);

Expand Down Expand Up @@ -97,19 +101,52 @@ message MsgActiveCollateral {
message MsgActiveCollateralResponse {}

// MsgCreateValidator defines a SDK message for creating a new validator.
message MsgCreateVault {
message MsgUpdatesCollateral {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;
option (cosmos.msg.v1.signer) = "owner";
option (cosmos.msg.v1.signer) = "authority";

string denom = 1;

string owner = 2 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
string min_collateral_ratio = 2 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(amino.dont_omitempty) = true,
(gogoproto.nullable) = false
];

string liquidation_ratio = 3 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(amino.dont_omitempty) = true,
(gogoproto.nullable) = false
];

string max_debt = 4 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(amino.dont_omitempty) = true,
(gogoproto.nullable) = false
];

string authority = 5 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}

// MsgActiveCollateralResponse defines the Msg/ActiveCollateral response type.
message MsgUpdatesCollateralResponse {}

// MsgCreateValidator defines a SDK message for creating a new validator.
message MsgCreateVault {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;
option (cosmos.msg.v1.signer) = "owner";

string owner = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];

cosmos.base.v1beta1.Coin collateral = 3
cosmos.base.v1beta1.Coin collateral = 2
[ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];

cosmos.base.v1beta1.Coin minted = 4
cosmos.base.v1beta1.Coin minted = 3
[ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];
}

Expand Down
24 changes: 23 additions & 1 deletion x/vaults/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Keeper struct {
accountKeeper types.AccountKeeper
// Temporarily leave it public to easily replace it with mocks.
// TODO: Make it private
OracleKeeper types.OracleKeeper
OracleKeeper types.OracleKeeper

// the address capable of executing a MsgUpdateParams message. Typically, this
// should be the x/gov module account.
Expand Down Expand Up @@ -92,6 +92,28 @@ func (k *Keeper) ActiveCollateralAsset(
return k.VaultsManager.Set(ctx, denom, vm)
}

func (k *Keeper) UpdatesCollateralAsset(
ctx context.Context,
denom string,
minCollateralRatio math.LegacyDec,
liquidationRatio math.LegacyDec,
maxDebt math.Int,
) error {
// Check if asset alreay be actived
vm, err := k.GetVaultManager(ctx, denom)
if err != nil {
return fmt.Errorf("denom %s not activated", denom)
}
amountMinted := vm.Params.MaxDebt.Sub(vm.MintAvailable)

vm.Params.MinCollateralRatio = minCollateralRatio
vm.Params.LiquidationRatio = liquidationRatio
vm.Params.MaxDebt = maxDebt
vm.MintAvailable = maxDebt.Sub(amountMinted)

return k.VaultsManager.Set(ctx, denom, vm)
}

func (k *Keeper) GetVaultManager(
ctx context.Context,
denom string,
Expand Down
19 changes: 18 additions & 1 deletion x/vaults/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParam
return &types.MsgUpdateParamsResponse{}, nil
}

// Add new Active Collateral via gov
func (k msgServer) ActiveCollateral(ctx context.Context, msg *types.MsgActiveCollateral) (*types.MsgActiveCollateralResponse, error) {
err := k.ActiveCollateralAsset(ctx, msg.Denom, msg.MinCollateralRatio, msg.LiquidationRatio, msg.MaxDebt)
if err != nil {
Expand All @@ -43,14 +44,26 @@ func (k msgServer) ActiveCollateral(ctx context.Context, msg *types.MsgActiveCol
return &types.MsgActiveCollateralResponse{}, nil
}

// Updates Collateral via gov
func (k msgServer) UpdatesCollateral(ctx context.Context, msg *types.MsgUpdatesCollateral) (*types.MsgUpdatesCollateralResponse, error) {
err := k.UpdatesCollateralAsset(ctx, msg.Denom, msg.MinCollateralRatio, msg.LiquidationRatio, msg.MaxDebt)
if err != nil {
return nil, err
}

return &types.MsgUpdatesCollateralResponse{}, nil
}

// Create new vault, send Collateral and receive back an amount Minted
func (k msgServer) CreateVault(ctx context.Context, msg *types.MsgCreateVault) (*types.MsgCreateVaultResponse, error) {
err := k.CreateNewVault(ctx, msg.Denom, sdk.MustAccAddressFromBech32(msg.Owner), msg.Collateral, msg.Minted)
err := k.CreateNewVault(ctx, sdk.MustAccAddressFromBech32(msg.Owner), msg.Collateral, msg.Minted)
if err != nil {
return nil, err
}
return &types.MsgCreateVaultResponse{}, nil
}

// Send additional Collateral
func (k msgServer) Deposit(ctx context.Context, msg *types.MsgDeposit) (*types.MsgDepositResponse, error) {
err := k.DepositToVault(ctx, msg.VaultId, sdk.MustAccAddressFromBech32(msg.Sender), msg.Amount)
if err != nil {
Expand All @@ -59,6 +72,7 @@ func (k msgServer) Deposit(ctx context.Context, msg *types.MsgDeposit) (*types.M
return &types.MsgDepositResponse{}, nil
}

// Withdraw a amount Collateral, make sure the remaining Collateral value is still more than the loan amount
func (k msgServer) Withdraw(ctx context.Context, msg *types.MsgWithdraw) (*types.MsgWithdrawResponse, error) {
err := k.WithdrawFromVault(ctx, msg.VaultId, sdk.MustAccAddressFromBech32(msg.Sender), msg.Amount)
if err != nil {
Expand All @@ -67,6 +81,7 @@ func (k msgServer) Withdraw(ctx context.Context, msg *types.MsgWithdraw) (*types
return &types.MsgWithdrawResponse{}, nil
}

// additional loan, collateral is still guaranteed
func (k msgServer) Mint(ctx context.Context, msg *types.MsgMint) (*types.MsgMintResponse, error) {
err := k.MintCoin(ctx, msg.VaultId, sdk.MustAccAddressFromBech32(msg.Sender), msg.Amount)
if err != nil {
Expand All @@ -75,6 +90,7 @@ func (k msgServer) Mint(ctx context.Context, msg *types.MsgMint) (*types.MsgMint
return &types.MsgMintResponse{}, nil
}

// repay part or all of a loan
func (k msgServer) Repay(ctx context.Context, msg *types.MsgRepay) (*types.MsgRepayResponse, error) {
err := k.RepayDebt(ctx, msg.VaultId, sdk.MustAccAddressFromBech32(msg.Sender), msg.Amount)
if err != nil {
Expand All @@ -83,6 +99,7 @@ func (k msgServer) Repay(ctx context.Context, msg *types.MsgRepay) (*types.MsgRe
return &types.MsgRepayResponse{}, nil
}

// claim back the CollateralLocked, ensuring the debt is paid off
func (k msgServer) Close(ctx context.Context, msg *types.MsgClose) (*types.MsgCloseResponse, error) {
vault, err := k.GetVault(ctx, msg.VaultId)
if err != nil {
Expand Down
Loading
Loading