Skip to content

Commit

Permalink
Merge pull request #27 from onomyprotocol/dong/gov-dev
Browse files Browse the repository at this point in the history
add gov for Active Collateral module vaults
  • Loading branch information
vuong177 authored Oct 4, 2024
2 parents 0e2e30b + eaaceac commit 79e9025
Show file tree
Hide file tree
Showing 15 changed files with 1,000 additions and 10 deletions.
31 changes: 31 additions & 0 deletions proto/reserve/vaults/proposal.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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];
}

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

string title = 1;
string description = 2;
MsgUpdatesCollateral updates_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
6 changes: 6 additions & 0 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ func (k *Keeper) ScopedKeeper() exported.ScopedKeeper {
return k.scopedKeeper
}

// need to delete when merging with oracle
func (k *Keeper) GetPrice(ctx context.Context, denom string) math.LegacyDec {
return math.LegacyZeroDec()
}

// need to delete when merging with oracle
func (k *Keeper) AddNewSymbolToBandOracleRequest(ctx context.Context, symbol string, oracleScriptId int64) error {
return nil
}
5 changes: 5 additions & 0 deletions x/vaults/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func (k *Keeper) ActiveCollateralAsset(
},
MintAvailable: maxDebt,
}
err := k.OracleKeeper.AddNewSymbolToBandOracleRequest(ctx, denom, 1)
if err != nil {
return err
}

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

Expand Down
4 changes: 4 additions & 0 deletions x/vaults/keeper/mock/oracle_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ func (s *MockOracleKeeper) GetPrice(ctx context.Context, denom string) math.Lega
func (s *MockOracleKeeper) SetPrice(denom string, price math.LegacyDec) {
s.prices[denom] = price
}

func (s *MockOracleKeeper) AddNewSymbolToBandOracleRequest(ctx context.Context, symbol string, oracleScriptId int64) error {
return nil
}
4 changes: 4 additions & 0 deletions x/vaults/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParam

// Add new Active Collateral via gov
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
12 changes: 6 additions & 6 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 Down Expand Up @@ -178,9 +178,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 @@ -206,7 +206,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}
}
25 changes: 25 additions & 0 deletions x/vaults/module/proposal_handle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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)
case *types.UpdatesCollateralProposal:
return k.UpdatesCollateralAsset(ctx, c.UpdatesCollateral.Denom, c.UpdatesCollateral.MinCollateralRatio, c.UpdatesCollateral.LiquidationRatio, c.UpdatesCollateral.MaxDebt)
default:
return errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s proposal content type: %T", types.ModuleName, c)
}
}
}
16 changes: 15 additions & 1 deletion x/vaults/types/codec.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
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)
cdc.RegisterConcrete(&UpdatesCollateralProposal{}, "reserve/UpdatesCollateralProposal", nil)
}

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

Expand All @@ -21,4 +28,11 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
&MsgRepay{},
)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)

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

}
4 changes: 3 additions & 1 deletion x/vaults/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ 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")
ErrInvalidUpdatesCollateralProposal = sdkerrors.Register(ModuleName, 3, "invalid updates collateral proposal")
)
1 change: 1 addition & 0 deletions x/vaults/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ type BankKeeper interface {

type OracleKeeper interface {
GetPrice(ctx context.Context, denom string) math.LegacyDec
AddNewSymbolToBandOracleRequest(ctx context.Context, symbol string, oracleScriptId int64) error
}
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
Loading

0 comments on commit 79e9025

Please sign in to comment.