Skip to content

Commit

Permalink
add field MsgActiveCollateral, MsgUpdatesCollateral
Browse files Browse the repository at this point in the history
  • Loading branch information
DongLieu committed Nov 11, 2024
1 parent 8c00f73 commit 836b0cb
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 134 deletions.
4 changes: 4 additions & 0 deletions proto/reserve/vaults/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ message MsgActiveCollateral {
uint64 oracl_script = 8;

string authority = 9 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
string sym_bol = 10;
string mint_denom = 11;
}

// MsgActiveCollateralResponse defines the Msg/ActiveCollateral response type.
Expand Down Expand Up @@ -175,6 +177,8 @@ message MsgUpdatesCollateral {
uint64 oracl_script = 8;

string authority = 9 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
string sym_bol = 10;
string mint_denom = 11;
}

// MsgActiveCollateralResponse defines the Msg/ActiveCollateral response type.
Expand Down
14 changes: 13 additions & 1 deletion script/proposal-vault-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@
"authority":"onomy10d07y265gmmuvt4z0w9aw880jnsr700jqr8n8k",
"min_collateral_ratio": "0.5",
"liquidation_ratio":"0.5",
"max_debt":"1000000000000000000000000000"
"max_debt":"1000000000000000000000000000",
"sym_bol": "usdt",
"mint_denom": "nomUSD"
},
{
"@type": "/reserve.vaults.MsgActiveCollateral",
"denom": "atom",
"authority":"onomy10d07y265gmmuvt4z0w9aw880jnsr700jqr8n8k",
"min_collateral_ratio": "0.5",
"liquidation_ratio":"0.5",
"max_debt":"1000000000000000000000000000",
"sym_bol": "atom",
"mint_denom": "nomUSD"
}],
"deposit": "100000000stake",
"title": "My proposal",
Expand Down
13 changes: 0 additions & 13 deletions script/proposal-vault-2.json

This file was deleted.

7 changes: 1 addition & 6 deletions script/vaults-gov-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,16 @@ reserved q gov proposals
# reserved tx gov submit-legacy-proposal active-collateral "title" "description" "atom" "10" "0.1" "10000" 10000000000000000000stake --keyring-backend=test --home=$HOME/.reserved/validator1 --from validator1 -y --chain-id testing-1 --fees 20stake

reserved tx gov submit-proposal ./script/proposal-vault-1.json --home=$HOME/.reserved/validator1 --from validator1 --keyring-backend test --fees 20stake --chain-id testing-1 -y
reserved tx gov submit-proposal ./script/proposal-vault-2.json --home=$HOME/.reserved/validator2 --from validator2 --keyring-backend test --fees 20stake --chain-id testing-1 -y

# # vote
sleep 7
reserved tx gov vote 1 yes --from validator1 --keyring-backend test --home ~/.reserved/validator1 --chain-id testing-1 -y --fees 20stake
reserved tx gov vote 1 yes --from validator2 --keyring-backend test --home ~/.reserved/validator2 --chain-id testing-1 -y --fees 20stake
reserved tx gov vote 1 yes --from validator3 --keyring-backend test --home ~/.reserved/validator3 --chain-id testing-1 -y --fees 20stake
sleep 7
reserved tx gov vote 2 yes --from validator1 --keyring-backend test --home ~/.reserved/validator1 --chain-id testing-1 -y --fees 20stake
reserved tx gov vote 2 yes --from validator2 --keyring-backend test --home ~/.reserved/validator2 --chain-id testing-1 -y --fees 20stake
reserved tx gov vote 2 yes --from validator3 --keyring-backend test --home ~/.reserved/validator3 --chain-id testing-1 -y --fees 20stake

# wait voting_perio=15s
echo "========sleep=========="
sleep 8
sleep 15
reserved q gov proposals
reserved tx oracle set-price usdt 1 --home=$HOME/.reserved/validator1 --from validator1 --keyring-backend test --fees 20stake --chain-id testing-1 -y
reserved tx oracle set-price atom 8 --home=$HOME/.reserved/validator2 --from validator2 --keyring-backend test --fees 20stake --chain-id testing-1 -y
Expand Down
4 changes: 2 additions & 2 deletions x/vaults/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s *KeeperTestSuite) TestBeginBlock() {
name: "success: one vault",
setup: func() { // 100000000000atom debt 210000000nomUSD(get 200000000nomUSD + 10000000nomUSD MintingFee)
err := s.k.ActiveCollateralAsset(s.Ctx,
denom, math.LegacyMustNewDecFromStr("1.6"),
denom, denom, "nomUSD", math.LegacyMustNewDecFromStr("1.6"),
math.LegacyMustNewDecFromStr("1.5"),
maxDebt, stabilityFee,
types.DefaultMintingFee,
Expand Down Expand Up @@ -62,7 +62,7 @@ func (s *KeeperTestSuite) TestBeginBlock() {
name: "success: no vault, LastUpdateTime updates",
setup: func() { // 100000000000atom debt 210000000nomUSD(get 200000000nomUSD + 10000000nomUSD MintingFee)
err := s.k.ActiveCollateralAsset(s.Ctx,
denom, math.LegacyMustNewDecFromStr("1.6"),
denom, denom, "nomUSD", math.LegacyMustNewDecFromStr("1.6"),
math.LegacyMustNewDecFromStr("1.5"),
maxDebt, stabilityFee,
types.DefaultMintingFee,
Expand Down
7 changes: 6 additions & 1 deletion x/vaults/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func (k Keeper) GetAuthority() string {
func (k *Keeper) ActiveCollateralAsset(
ctx context.Context,
denom string,
symbol string,
mintDenom string,
minCollateralRatio math.LegacyDec,
liquidationRatio math.LegacyDec,
maxDebt math.Int,
Expand All @@ -90,8 +92,11 @@ func (k *Keeper) ActiveCollateralAsset(
return fmt.Errorf("denom %s already be actived", denom)
}
vm := types.VaultMamager{
Denom: denom,
Denom: denom,
Symbol: symbol,
Params: types.VaultMamagerParams{
MintDenom: mintDenom,
MintSymbol: symbol,
MinCollateralRatio: minCollateralRatio,
LiquidationRatio: liquidationRatio,
MaxDebt: maxDebt,
Expand Down
4 changes: 2 additions & 2 deletions x/vaults/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (k msgServer) ActiveCollateral(ctx context.Context, msg *types.MsgActiveCol
return nil, errorsmod.Wrapf(types.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, msg.Authority)
}

err = k.ActiveCollateralAsset(ctx, msg.Denom, msg.MinCollateralRatio, msg.LiquidationRatio, msg.MaxDebt, msg.StabilityFee, msg.MintingFee, msg.LiquidationPenalty, int64(msg.OraclScript))
err = k.ActiveCollateralAsset(ctx, msg.Denom, msg.SymBol, msg.MintDenom, msg.MinCollateralRatio, msg.LiquidationRatio, msg.MaxDebt, msg.StabilityFee, msg.MintingFee, msg.LiquidationPenalty, int64(msg.OraclScript))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func (k msgServer) Close(ctx context.Context, msg *types.MsgClose) (*types.MsgCl
if err != nil {
return nil, fmt.Errorf("vault %d was not found", msg.VaultId)
}

err = k.CloseVault(ctx, msg.Sender, vault)
if err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions x/vaults/keeper/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (k *Keeper) CreateNewVault(
return fmt.Errorf("%s was not actived", denom)
}
collateralSymbol := vm.Symbol
mintSymbol := vm.Params.MintSymbol
mintDenom := vm.Params.MintDenom

allowedMintDenoms := k.GetAllowedMintDenoms(ctx)
// TODO: Check if mint denom is allowed
Expand All @@ -45,7 +45,8 @@ func (k *Keeper) CreateNewVault(
}

// Calculate collateral ratio
price := k.OracleKeeper.GetPrice(ctx, collateralSymbol, mintSymbol)
price := k.OracleKeeper.GetPrice(ctx, collateralSymbol, mintDenom)
fmt.Println(price, collateralSymbol, mintDenom)
if price == nil || price.IsNil() {
return errors.Wrapf(oracletypes.ErrInvalidOracle, "CreateNewVault: can not get price with base %s quote %s", denom, types.DefaultMintDenoms)
}
Expand Down
16 changes: 8 additions & 8 deletions x/vaults/keeper/vaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *KeeperTestSuite) TestCreateNewVault() {
collateral = sdk.NewCoin(denom, math.NewInt(10_000_000)) // 10 atom = 80$
maxDebt = math.NewInt(100_000_000)
)
err := s.k.ActiveCollateralAsset(s.Ctx, denom, math.LegacyMustNewDecFromStr("1.6"), math.LegacyMustNewDecFromStr("1.5"), maxDebt, types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
err := s.k.ActiveCollateralAsset(s.Ctx, denom, denom, "nomUSD", math.LegacyMustNewDecFromStr("1.6"), math.LegacyMustNewDecFromStr("1.5"), maxDebt, types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
s.Require().NoError(err)

tests := []struct {
Expand Down Expand Up @@ -166,7 +166,7 @@ func (s *KeeperTestSuite) TestRepayDebt() {
maxDebt = math.NewInt(2000000000)
mintedCoin = sdk.NewCoin(types.DefaultMintDenoms[0], math.NewInt(300000000))
)
err := s.k.ActiveCollateralAsset(s.Ctx, denom, math.LegacyMustNewDecFromStr("0.1"), math.LegacyMustNewDecFromStr("0.1"), maxDebt, types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
err := s.k.ActiveCollateralAsset(s.Ctx, denom, denom, "nomUSD", math.LegacyMustNewDecFromStr("0.1"), math.LegacyMustNewDecFromStr("0.1"), maxDebt, types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
s.Require().NoError(err)

tests := []struct {
Expand Down Expand Up @@ -284,7 +284,7 @@ func (s *KeeperTestSuite) TestDepositToVault() {
maxDebt = math.NewInt(2000000000)
mintedCoin = sdk.NewCoin(types.DefaultMintDenoms[0], math.NewInt(200000000))
)
err := s.k.ActiveCollateralAsset(s.Ctx, denom, math.LegacyMustNewDecFromStr("0.1"), math.LegacyMustNewDecFromStr("0.1"), maxDebt, types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
err := s.k.ActiveCollateralAsset(s.Ctx, denom, denom, "nomUSD", math.LegacyMustNewDecFromStr("0.1"), math.LegacyMustNewDecFromStr("0.1"), maxDebt, types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
s.Require().NoError(err)

tests := []struct {
Expand Down Expand Up @@ -407,7 +407,7 @@ func (s *KeeperTestSuite) TestWithdrawFromVault() {
setup: func() {
s.FundAccount(s.TestAccs[0], types.ModuleName, sdk.NewCoins(fund))

err := s.k.ActiveCollateralAsset(s.Ctx, denom, math.LegacyMustNewDecFromStr("0.1"), math.LegacyMustNewDecFromStr("0.1"), maxDebt, types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
err := s.k.ActiveCollateralAsset(s.Ctx, denom, denom, "nomUSD", math.LegacyMustNewDecFromStr("0.1"), math.LegacyMustNewDecFromStr("0.1"), maxDebt, types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
s.Require().NoError(err)

err = s.k.CreateNewVault(s.Ctx, s.TestAccs[0], coinMintToAcc, mintedCoin)
Expand Down Expand Up @@ -443,7 +443,7 @@ func (s *KeeperTestSuite) TestWithdrawFromVault() {
setup: func() {
s.FundAccount(s.TestAccs[0], types.ModuleName, sdk.NewCoins(fund))

err := s.k.ActiveCollateralAsset(s.Ctx, denom, math.LegacyMustNewDecFromStr("0.1"), math.LegacyMustNewDecFromStr("0.1"), maxDebt, types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
err := s.k.ActiveCollateralAsset(s.Ctx, denom, denom, "nomUSD", math.LegacyMustNewDecFromStr("0.1"), math.LegacyMustNewDecFromStr("0.1"), maxDebt, types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
s.Require().NoError(err)

err = s.k.CreateNewVault(s.Ctx, s.TestAccs[0], coinMintToAcc, mintedCoin)
Expand All @@ -460,7 +460,7 @@ func (s *KeeperTestSuite) TestWithdrawFromVault() {
setup: func() {
s.FundAccount(s.TestAccs[0], types.ModuleName, sdk.NewCoins(fund))

err := s.k.ActiveCollateralAsset(s.Ctx, denom, math.LegacyMustNewDecFromStr("0.1"), math.LegacyMustNewDecFromStr("0.1"), maxDebt, types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
err := s.k.ActiveCollateralAsset(s.Ctx, denom, denom, "nomUSD", math.LegacyMustNewDecFromStr("0.1"), math.LegacyMustNewDecFromStr("0.1"), maxDebt, types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
s.Require().NoError(err)

err = s.k.CreateNewVault(s.Ctx, s.TestAccs[0], coinMintToAcc, mintedCoin)
Expand All @@ -477,7 +477,7 @@ func (s *KeeperTestSuite) TestWithdrawFromVault() {
setup: func() {
s.FundAccount(s.TestAccs[0], types.ModuleName, sdk.NewCoins(fund))

err := s.k.ActiveCollateralAsset(s.Ctx, denom, math.LegacyMustNewDecFromStr("0.1"), math.LegacyMustNewDecFromStr("0.1"), maxDebt, types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
err := s.k.ActiveCollateralAsset(s.Ctx, denom, denom, "nomUSD", math.LegacyMustNewDecFromStr("0.1"), math.LegacyMustNewDecFromStr("0.1"), maxDebt, types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
s.Require().NoError(err)

err = s.k.CreateNewVault(s.Ctx, s.TestAccs[0], coinMintToAcc, mintedCoin)
Expand Down Expand Up @@ -709,7 +709,7 @@ func (s *KeeperTestSuite) TestLiquidate() {
for _, t := range tests {
s.Run(t.name, func() {
s.SetupTest()
err := s.k.ActiveCollateralAsset(s.Ctx, "atom", math.LegacyMustNewDecFromStr("0.1"), math.LegacyMustNewDecFromStr("0.1"), math.NewInt(1000_000_000), types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
err := s.k.ActiveCollateralAsset(s.Ctx, "atom", "atom", "nomUSD", math.LegacyMustNewDecFromStr("0.1"), math.LegacyMustNewDecFromStr("0.1"), math.NewInt(1000_000_000), types.DefaultStabilityFee, types.DefaultMintingFee, types.DefaultLiquidationPenalty, 1)
s.Require().NoError(err)

for _, vault := range t.liquidation.LiquidatingVaults {
Expand Down
55 changes: 22 additions & 33 deletions x/vaults/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package types
import (
"fmt"

sdkerrors "cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
Expand Down Expand Up @@ -123,6 +122,14 @@ func (msg *MsgActiveCollateral) ValidateBasic() error {
return fmt.Errorf("denom is empty")
}

if msg.SymBol == "" {
return fmt.Errorf("symbol is empty")
}

if msg.MintDenom == "" {
return fmt.Errorf("mintDenom is empty")
}

if msg.Authority == "" {
return fmt.Errorf("authority is empty")
}
Expand Down Expand Up @@ -162,6 +169,14 @@ func (msg *MsgUpdatesCollateral) ValidateBasic() error {
return fmt.Errorf("denom is empty")
}

if msg.SymBol == "" {
return fmt.Errorf("symbol is empty")
}

if msg.MintDenom == "" {
return fmt.Errorf("mintDenom is empty")
}

if msg.Authority == "" {
return fmt.Errorf("authority is empty")
}
Expand Down Expand Up @@ -210,6 +225,8 @@ func NewMsgActiveCollateral(a *ActiveCollateralProposal) *MsgActiveCollateral {
MintingFee: a.ActiveCollateral.MintingFee,
OraclScript: a.ActiveCollateral.OraclScript,
Authority: a.ActiveCollateral.Authority,
SymBol: a.ActiveCollateral.SymBol,
MintDenom: a.ActiveCollateral.MintDenom,
}
}

Expand All @@ -224,6 +241,8 @@ func NewMsgUpdatesCollateral(u *UpdatesCollateralProposal) *MsgUpdatesCollateral
MintingFee: u.UpdatesCollateral.MintingFee,
OraclScript: u.UpdatesCollateral.OraclScript,
Authority: u.UpdatesCollateral.Authority,
SymBol: u.UpdatesCollateral.SymBol,
MintDenom: u.UpdatesCollateral.MintDenom,
}
}

Expand All @@ -245,23 +264,8 @@ func (m *ActiveCollateralProposal) ProposalType() string {

func (m *ActiveCollateralProposal) ValidateBasic() error {
a := m.ActiveCollateral
if a.Denom == "" {
return sdkerrors.Wrap(ErrInvalidActiveCollateralProposal, "empty denom")
}

if a.MinCollateralRatio.LT(math.LegacyZeroDec()) {
return sdkerrors.Wrap(ErrInvalidActiveCollateralProposal, "less than zero")
}

if a.LiquidationRatio.LT(math.LegacyZeroDec()) {
return sdkerrors.Wrap(ErrInvalidActiveCollateralProposal, "less than zero")
}

if a.MaxDebt.LT(math.ZeroInt()) {
return sdkerrors.Wrap(ErrInvalidActiveCollateralProposal, "less than zero")
}

return nil
return a.ValidateBasic()
}

func (m *UpdatesCollateralProposal) GetDescription() string {
Expand All @@ -282,21 +286,6 @@ func (m *UpdatesCollateralProposal) ProposalType() string {

func (m *UpdatesCollateralProposal) ValidateBasic() error {
a := m.UpdatesCollateral
if a.Denom == "" {
return sdkerrors.Wrap(ErrInvalidUpdatesCollateralProposal, "empty denom")
}

if a.MinCollateralRatio.LT(math.LegacyZeroDec()) {
return sdkerrors.Wrap(ErrInvalidUpdatesCollateralProposal, "less than zero")
}

if a.LiquidationRatio.LT(math.LegacyZeroDec()) {
return sdkerrors.Wrap(ErrInvalidUpdatesCollateralProposal, "less than zero")
}

if a.MaxDebt.LT(math.ZeroInt()) {
return sdkerrors.Wrap(ErrInvalidUpdatesCollateralProposal, "less than zero")
}

return nil
return a.ValidateBasic()
}
Loading

0 comments on commit 836b0cb

Please sign in to comment.