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

add gov for Active Collateral module vaults #27

Merged
merged 6 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 21 additions & 0 deletions proto/reserve/vaults/proposal.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";
package reserve.vaults;

import "gogoproto/gogo.proto";
import "reserve/vaults/tx.proto";
import "amino/amino.proto";
import "cosmos_proto/cosmos.proto";


option go_package = "github.com/onomyprotocol/reserve/x/vaults/types";


message ActiveCollateralProposal {
option (gogoproto.goproto_getters) = false;
option (cosmos_proto.implements_interface) = "cosmos.gov.v1beta1.Content";
option (amino.name) = "reserve/ActiveCollateralProposal";

string title = 1;
string description = 2;
MsgActiveCollateral active_collateral = 3 [(gogoproto.nullable) = false];
}
13 changes: 13 additions & 0 deletions script/proposal-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"messages": [{
"@type": "/reserve.vaults.MsgActiveCollateral",
"denom": "atom",
"authority":"onomy10d07y265gmmuvt4z0w9aw880jnsr700jqr8n8k",
"min_collateral_ratio": "0.5",
"liquidation_ratio":"0.5",
"max_debt":"10000"
}],
"deposit": "100000000stake",
"title": "My proposal",
"summary": "A short summary of my proposal"
}
137 changes: 137 additions & 0 deletions script/vaults-gov-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/bin/bash
set -xeu

# always returns true so set -e doesn't exit if it is not running.
killall reserved || true
rm -rf $HOME/.reserved/

mkdir $HOME/.reserved
mkdir $HOME/.reserved/validator1
mkdir $HOME/.reserved/validator2
mkdir $HOME/.reserved/validator3

# init all three validators
reserved init --chain-id=testing-1 validator1 --home=$HOME/.reserved/validator1
reserved init --chain-id=testing-1 validator2 --home=$HOME/.reserved/validator2
reserved init --chain-id=testing-1 validator3 --home=$HOME/.reserved/validator3

# create keys for all three validators
mnemonic1="top toddler wrist parade hobby supply odor ginger resource copy square tell vanish pride volcano effort planet style transfer pipe wise bus tuition luxury"
mnemonic2="panther giant oyster hand song region chunk coil laundry glance ball denial void ramp palm fiscal pizza soccer before upset diet valid story cement"
mnemonic3="gap track crop knee galaxy square case resemble subway math moon mom casino trade finish exotic author comic gap margin elegant claw fire business"

echo $mnemonic1| reserved keys add validator1 --recover --keyring-backend=test --home=$HOME/.reserved/validator1
echo $mnemonic2| reserved keys add validator2 --recover --keyring-backend=test --home=$HOME/.reserved/validator2
echo $mnemonic3| reserved keys add validator3 --recover --keyring-backend=test --home=$HOME/.reserved/validator3

# create validator node with tokens to transfer to the three other nodes
reserved genesis add-genesis-account $(reserved keys show validator1 -a --keyring-backend=test --home=$HOME/.reserved/validator1) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator1
reserved genesis add-genesis-account $(reserved keys show validator2 -a --keyring-backend=test --home=$HOME/.reserved/validator2) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator1
reserved genesis add-genesis-account $(reserved keys show validator3 -a --keyring-backend=test --home=$HOME/.reserved/validator3) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator1
reserved genesis add-genesis-account $(reserved keys show validator1 -a --keyring-backend=test --home=$HOME/.reserved/validator1) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator2
reserved genesis add-genesis-account $(reserved keys show validator2 -a --keyring-backend=test --home=$HOME/.reserved/validator2) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator2
reserved genesis add-genesis-account $(reserved keys show validator3 -a --keyring-backend=test --home=$HOME/.reserved/validator3) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator2
reserved genesis add-genesis-account $(reserved keys show validator1 -a --keyring-backend=test --home=$HOME/.reserved/validator1) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator3
reserved genesis add-genesis-account $(reserved keys show validator2 -a --keyring-backend=test --home=$HOME/.reserved/validator2) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator3
reserved genesis add-genesis-account $(reserved keys show validator3 -a --keyring-backend=test --home=$HOME/.reserved/validator3) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator3
reserved genesis gentx validator1 1000000000000000000000stake --keyring-backend=test --home=$HOME/.reserved/validator1 --chain-id=testing-1
reserved genesis gentx validator2 1000000000000000000000stake --keyring-backend=test --home=$HOME/.reserved/validator2 --chain-id=testing-1
reserved genesis gentx validator3 1000000000000000000000stake --keyring-backend=test --home=$HOME/.reserved/validator3 --chain-id=testing-1

# cp validator2/config/gentx/*.json $HOME/.reserved/validator1/config/gentx/
# cp validator3/config/gentx/*.json $HOME/.reserved/validator1/config/gentx/
reserved genesis collect-gentxs --home=$HOME/.reserved/validator1

# change app.toml values
VALIDATOR1_APP_TOML=$HOME/.reserved/validator1/config/app.toml
VALIDATOR2_APP_TOML=$HOME/.reserved/validator2/config/app.toml
VALIDATOR3_APP_TOML=$HOME/.reserved/validator3/config/app.toml

# validator1
sed -i -E 's|0.0.0.0:9090|0.0.0.0:9050|g' $VALIDATOR1_APP_TOML
sed -i -E 's|127.0.0.1:9090|127.0.0.1:9050|g' $VALIDATOR1_APP_TOML
sed -i -E 's|minimum-gas-prices = ""|minimum-gas-prices = "0.0001stake"|g' $VALIDATOR1_APP_TOML

# validator2
sed -i -E 's|tcp://0.0.0.0:1317|tcp://0.0.0.0:1316|g' $VALIDATOR2_APP_TOML
sed -i -E 's|0.0.0.0:9090|0.0.0.0:9088|g' $VALIDATOR2_APP_TOML
sed -i -E 's|0.0.0.0:9091|0.0.0.0:9089|g' $VALIDATOR2_APP_TOML
sed -i -E 's|minimum-gas-prices = ""|minimum-gas-prices = "0.0001stake"|g' $VALIDATOR2_APP_TOML

# validator3
sed -i -E 's|tcp://0.0.0.0:1317|tcp://0.0.0.0:1315|g' $VALIDATOR3_APP_TOML
sed -i -E 's|0.0.0.0:9090|0.0.0.0:9086|g' $VALIDATOR3_APP_TOML
sed -i -E 's|0.0.0.0:9091|0.0.0.0:9087|g' $VALIDATOR3_APP_TOML
sed -i -E 's|minimum-gas-prices = ""|minimum-gas-prices = "0.0001stake"|g' $VALIDATOR3_APP_TOML

# change config.toml values
VALIDATOR1_CONFIG=$HOME/.reserved/validator1/config/config.toml
VALIDATOR2_CONFIG=$HOME/.reserved/validator2/config/config.toml
VALIDATOR3_CONFIG=$HOME/.reserved/validator3/config/config.toml


# validator1
sed -i -E 's|allow_duplicate_ip = false|allow_duplicate_ip = true|g' $VALIDATOR1_CONFIG
sed -i -E 's|prometheus = false|prometheus = true|g' $VALIDATOR1_CONFIG


# validator2
sed -i -E 's|tcp://127.0.0.1:26658|tcp://127.0.0.1:26655|g' $VALIDATOR2_CONFIG
sed -i -E 's|tcp://127.0.0.1:26657|tcp://127.0.0.1:26654|g' $VALIDATOR2_CONFIG
sed -i -E 's|tcp://0.0.0.0:26656|tcp://0.0.0.0:26653|g' $VALIDATOR2_CONFIG
sed -i -E 's|allow_duplicate_ip = false|allow_duplicate_ip = true|g' $VALIDATOR2_CONFIG
sed -i -E 's|prometheus = false|prometheus = true|g' $VALIDATOR2_CONFIG
sed -i -E 's|prometheus_listen_addr = ":26660"|prometheus_listen_addr = ":26630"|g' $VALIDATOR2_CONFIG

# validator3
sed -i -E 's|tcp://127.0.0.1:26658|tcp://127.0.0.1:26652|g' $VALIDATOR3_CONFIG
sed -i -E 's|tcp://127.0.0.1:26657|tcp://127.0.0.1:26651|g' $VALIDATOR3_CONFIG
sed -i -E 's|tcp://0.0.0.0:26656|tcp://0.0.0.0:26650|g' $VALIDATOR3_CONFIG
sed -i -E 's|allow_duplicate_ip = false|allow_duplicate_ip = true|g' $VALIDATOR3_CONFIG
sed -i -E 's|prometheus = false|prometheus = true|g' $VALIDATOR3_CONFIG
sed -i -E 's|prometheus_listen_addr = ":26660"|prometheus_listen_addr = ":26620"|g' $VALIDATOR3_CONFIG

# copy, update validator1 genesis file to validator2-3
update_test_genesis () {
cat $HOME/.reserved/validator1/config/genesis.json | jq "$1" > tmp.json && mv tmp.json $HOME/.reserved/validator1/config/genesis.json
}

update_test_genesis '.app_state["gov"]["params"]["voting_period"] = "15s"'
update_test_genesis '.app_state["gov"]["params"]["expedited_voting_period"] = "10s"'

cp $HOME/.reserved/validator1/config/genesis.json $HOME/.reserved/validator2/config/genesis.json
cp $HOME/.reserved/validator1/config/genesis.json $HOME/.reserved/validator3/config/genesis.json

# copy tendermint node id of validator1 to persistent peers of validator2-3
node1=$(reserved tendermint show-node-id --home=$HOME/.reserved/validator1)
node2=$(reserved tendermint show-node-id --home=$HOME/.reserved/validator2)
node3=$(reserved tendermint show-node-id --home=$HOME/.reserved/validator3)
sed -i -E "s|persistent_peers = \"\"|persistent_peers = \"$node1@localhost:26656,$node2@localhost:26656,$node3@localhost:26656\"|g" $HOME/.reserved/validator1/config/config.toml
sed -i -E "s|persistent_peers = \"\"|persistent_peers = \"$node1@localhost:26656,$node2@localhost:26656,$node3@localhost:26656\"|g" $HOME/.reserved/validator2/config/config.toml
sed -i -E "s|persistent_peers = \"\"|persistent_peers = \"$node1@localhost:26656,$node2@localhost:26656,$node3@localhost:26656\"|g" $HOME/.reserved/validator3/config/config.toml


# # start all three validators
screen -S onomy1 -t onomy1 -d -m reserved start --home=$HOME/.reserved/validator1
screen -S onomy2 -t onomy2 -d -m reserved start --home=$HOME/.reserved/validator2
screen -S onomy3 -t onomy3 -d -m reserved start --home=$HOME/.reserved/validator3

# submit proposal add usdt
sleep 7
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-1.json --home=$HOME/.reserved/validator1 --from validator1 --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

# wait voting_perio=15s
echo "========sleep=========="
sleep 15
reserved q gov proposals

killall reserved || true
2 changes: 1 addition & 1 deletion x/vaults/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"cosmossdk.io/collections"
storetypes "cosmossdk.io/core/store"
"cosmossdk.io/math"
"github.com/onomyprotocol/reserve/x/vaults/types"
oraclekeeper "github.com/onomyprotocol/reserve/x/oracle/keeper"
"github.com/onomyprotocol/reserve/x/vaults/types"

"github.com/cosmos/cosmos-sdk/codec"
)
Expand Down
4 changes: 4 additions & 0 deletions x/vaults/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParam
}

func (k msgServer) ActiveCollateral(ctx context.Context, msg *types.MsgActiveCollateral) (*types.MsgActiveCollateralResponse, error) {
if k.authority != msg.Authority {
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)
if err != nil {
return nil, err
Expand Down
16 changes: 8 additions & 8 deletions x/vaults/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

// govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"

abci "github.com/cometbft/cometbft/abci/types"

Expand All @@ -28,10 +28,10 @@ import (

"cosmossdk.io/core/appmodule"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/onomyprotocol/reserve/x/vaults/keeper"
modulev1 "github.com/onomyprotocol/reserve/api/reserve/vaults/module"
oraclekeeper "github.com/onomyprotocol/reserve/x/oracle/keeper"
"github.com/onomyprotocol/reserve/x/vaults/keeper"
"github.com/onomyprotocol/reserve/x/vaults/types"
modulev1 "github.com/onomyprotocol/reserve/api/reserve/vaults/module"
)

const consensusVersion uint64 = 1
Expand Down Expand Up @@ -175,9 +175,9 @@ type ModuleInputs struct {
type ModuleOutputs struct {
depinject.Out

PsmKeeper keeper.Keeper
Module appmodule.AppModule
// GovHandler govv1beta1.HandlerRoute
PsmKeeper keeper.Keeper
Module appmodule.AppModule
GovHandler govv1beta1.HandlerRoute
}

func ProvideModule(in ModuleInputs) ModuleOutputs {
Expand All @@ -203,7 +203,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
in.BankKeeper,
)

// govHandler := govv1beta1.HandlerRoute{RouteKey: types.RouterKey, Handler: NewStablecoinProposalHandler(&k)}
govHandler := govv1beta1.HandlerRoute{RouteKey: types.RouterKey, Handler: NewVaultsProposalHandler(k)}

return ModuleOutputs{PsmKeeper: *k, Module: m} //GovHandler: govHandler}
return ModuleOutputs{PsmKeeper: *k, Module: m, GovHandler: govHandler}
}
23 changes: 23 additions & 0 deletions x/vaults/module/proposal_handle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package vaults

import (
errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"

"github.com/onomyprotocol/reserve/x/vaults/keeper"
"github.com/onomyprotocol/reserve/x/vaults/types"
)

func NewVaultsProposalHandler(k *keeper.Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) error {
switch c := content.(type) {
case *types.ActiveCollateralProposal:
return k.ActiveCollateralAsset(ctx, c.ActiveCollateral.Denom, c.ActiveCollateral.MinCollateralRatio, c.ActiveCollateral.LiquidationRatio, c.ActiveCollateral.MaxDebt)
default:
return errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s proposal content type: %T", types.ModuleName, c)
}
}
}
14 changes: 13 additions & 1 deletion x/vaults/types/codec.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package types

import (
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
// this line is used by starport scaffolding # 1
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)

// RegisterLegacyAminoCodec registers all necessary param module types with a given LegacyAmino codec.
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&ActiveCollateralProposal{}, "reserve/ActiveCollateralProposal", nil)
}

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
// this line is used by starport scaffolding # 3

Expand All @@ -20,4 +26,10 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
&MsgRepay{},
)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)

registry.RegisterImplementations(
(*govtypes.Content)(nil),
&ActiveCollateralProposal{},
)

}
3 changes: 2 additions & 1 deletion x/vaults/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import (

// x/vaults module sentinel errors
var (
ErrInvalidSigner = sdkerrors.Register(ModuleName, 1100, "expected gov account as only signer for proposal message")
ErrInvalidSigner = sdkerrors.Register(ModuleName, 1100, "expected gov account as only signer for proposal message")
ErrInvalidActiveCollateralProposal = sdkerrors.Register(ModuleName, 2, "invalid active collateral proposal")
)
5 changes: 3 additions & 2 deletions x/vaults/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package types
import "cosmossdk.io/collections"

const (
ModuleName = "vaults"
ModuleName = "vaults"

// StoreKey is the string store representation
StoreKey = ModuleName

ReserveModuleName = "reserve"
RouterKey = ModuleName
)

var (
Expand Down
46 changes: 46 additions & 0 deletions x/vaults/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,52 @@
package types

import (
sdkerrors "cosmossdk.io/errors"
"cosmossdk.io/math"
)

var (
Query_serviceDesc = _Query_serviceDesc
Msg_serviceDesc = _Msg_serviceDesc
)

const (
ProposalTypeActiveCollateralProposal string = "ActiveCollateralProposal"
)

func (m *ActiveCollateralProposal) GetDescription() string {
return " "
}

func (m *ActiveCollateralProposal) GetTitle() string {
return " "
}

func (m *ActiveCollateralProposal) ProposalRoute() string {
return RouterKey
}

func (m *ActiveCollateralProposal) ProposalType() string {
return ProposalTypeActiveCollateralProposal
}

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
}
Loading
Loading