Skip to content
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
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ func New(
AddRoute(paramproposal.RouterKey, params.NewParamChangeProposalHandler(app.ParamsKeeper)).
AddRoute(distrtypes.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)).
AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper)).
AddRoute(ibcclienttypes.RouterKey, ibcclient.NewClientProposalHandler(app.IBCKeeper.ClientKeeper))
AddRoute(ibcclienttypes.RouterKey, ibcclient.NewClientProposalHandler(app.IBCKeeper.ClientKeeper)).
AddRoute(dexmoduletypes.RouterKey, dexmodule.NewRegisterPairsProposalHandler(app.DexKeeper))
if len(enabledProposals) != 0 {
govRouter.AddRoute(wasm.RouterKey, wasm.NewWasmProposalHandler(app.WasmKeeper, enabledProposals))
}
Expand Down
23 changes: 23 additions & 0 deletions proto/dex/gov.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
syntax = "proto3";
package seiprotocol.seichain.dex;

import "gogoproto/gogo.proto";
import "dex/pair.proto";

option go_package = "github.com/sei-protocol/sei-chain/x/dex/types";

// RegisterPairsProposal is a gov Content type for adding a new whitelisted token
// pair to the dex module. It must specify a list of contract addresses and their respective
// token pairs to be registered.
message RegisterPairsProposal {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;
option (gogoproto.goproto_stringer) = false;

string title = 1 [ (gogoproto.moretags) = "yaml:\"title\"" ];
string description = 2 [ (gogoproto.moretags) = "yaml:\"description\"" ];
repeated BatchContractPair batchcontractpair = 3 [
(gogoproto.moretags) = "yaml:\"batch_contract_pair\"",
(gogoproto.nullable) = false
];
}
7 changes: 6 additions & 1 deletion proto/dex/pair.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ message Pair {
Denom assetDenom = 2 [
(gogoproto.jsontag) = "asset_denom"
];
}
}

message BatchContractPair {
string contractAddr = 1 [(gogoproto.jsontag) = "contract_addr"];
repeated Pair pairs = 2 [(gogoproto.jsontag) = "pairs"];
}
9 changes: 0 additions & 9 deletions proto/dex/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ service Msg {
rpc PlaceOrders(MsgPlaceOrders) returns (MsgPlaceOrdersResponse);
rpc CancelOrders(MsgCancelOrders) returns (MsgCancelOrdersResponse);
rpc Liquidate(MsgLiquidation) returns (MsgLiquidationResponse);
rpc RegisterPair(MsgRegisterPair) returns (MsgRegisterPairResponse);
rpc RegisterContract(MsgRegisterContract) returns(MsgRegisterContractResponse);
// privileged endpoints below

Expand Down Expand Up @@ -54,14 +53,6 @@ message MsgLiquidation {

message MsgLiquidationResponse {}

message MsgRegisterPair {
string creator = 1;
string contractAddr = 2;
Pair pair = 3;
}

message MsgRegisterPairResponse {}

message MsgRegisterContract {
string creator = 1;
ContractInfo contract = 2;
Expand Down
17 changes: 17 additions & 0 deletions x/dex/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ TODO: Populate dex README Contents below.

## State

## Governance

Token pairs can be whitelisted with a contract address to the `dex` module via governance. The following is an example
proposal json to whitelist a token pair.

```json
"dex": {
"params": {
"code_upload_access": {
"permission": "Everybody"
},
"instantiate_default_permission": "Everybody"
}
},
```

## Messages

## Events
Expand All @@ -19,3 +35,4 @@ TODO: Populate dex README Contents below.
## Transactions

## Queries

56 changes: 56 additions & 0 deletions x/dex/client/cli/gov_tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cli

import (

"github.com/sei-protocol/sei-chain/x/dex/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
cutils "github.com/sei-protocol/sei-chain/x/dex/client/utils"

"github.com/spf13/cobra"
)

// NewSubmitParamChangeProposalTxCmd returns a CLI command handler for creating
// a parameter change proposal governance transaction.
func NewRegisterPairsProposalTxCmd() *cobra.Command {
return &cobra.Command{
Use: "register-pairs-proposal [proposal-file]",
Args: cobra.ExactArgs(1),
Short: "Submit a register pairs proposal",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
proposal, err := cutils.ParseRegisterPairsProposalJSON(clientCtx.LegacyAmino, args[0])
if err != nil {
return err
}

// Convert proposal to RegisterPairsProposal Type
from := clientCtx.GetFromAddress()
proposal_batch_contract_pair, err := proposal.BatchContractPair.ToMultipleBatchContractPair()
if err != nil {
return err
}
content := types.NewRegisterPairsProposal(
proposal.Title, proposal.Description, proposal_batch_contract_pair,
)

deposit, err := sdk.ParseCoinsNormalized(proposal.Deposit)
if err != nil {
return err
}

msg, err := govtypes.NewMsgSubmitProposal(&content, deposit, from)
if err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
}
2 changes: 1 addition & 1 deletion x/dex/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func GetTxCmd() *cobra.Command {
cmd.AddCommand(CmdPlaceOrders())
cmd.AddCommand(CmdCancelOrders())
cmd.AddCommand(CmdLiquidate())
cmd.AddCommand(CmdRegisterPair())
cmd.AddCommand(CmdRegisterContract())
cmd.AddCommand(NewRegisterPairsProposalTxCmd())
// this line is used by starport scaffolding # 1

return cmd
Expand Down
59 changes: 0 additions & 59 deletions x/dex/client/cli/tx_register_pair.go

This file was deleted.

100 changes: 100 additions & 0 deletions x/dex/client/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package utils

import (
"errors"
"io/ioutil"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/sei-protocol/sei-chain/x/dex/types"
)

type (
PairJSON struct {
PriceDenom string `json:"price_denom" yaml:"price_denom"`
AssetDenom string `json:"asset_denom" yaml:"asset_denom"`
}

PairsJSON []PairJSON

// ParamChangeJSON defines a parameter change used in JSON input. This
// allows values to be specified in raw JSON instead of being string encoded.
BatchContractPairJSON struct {
ContractAddr string `json:"contract_addr" yaml:"contract_addr"`
Pairs PairsJSON `json:"pairs" yaml:"pairs"`
}

MultipleBatchContractPairJSON []BatchContractPairJSON

// RegisterPairsProposalJSON defines a RegisterPairsProposal
// to parse register pair proposals from a JSON file.
RegisterPairsProposalJSON struct {
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
BatchContractPair MultipleBatchContractPairJSON `json:"batch_contract_pair" yaml:"batch_contract_pair"`
Deposit string `json:"deposit" yaml:"deposit"`
}
)

// TODO: ADD utils to convert Each type to dex/type (string to denom)
func NewPair(pair PairJSON) (types.Pair, error) {

PriceDenom, unit, err := types.GetDenomFromStr(pair.PriceDenom)
if err != nil {
return types.Pair{}, err
}
if unit != types.Unit_STANDARD {
return types.Pair{}, errors.New("Denom must be in standard/whole unit (e.g. sei instead of usei)")
}
AssetDenom, unit, err := types.GetDenomFromStr(pair.AssetDenom)
if err != nil {
return types.Pair{}, err
}
if unit != types.Unit_STANDARD {
return types.Pair{}, errors.New("Denom must be in standard/whole unit (e.g. sei instead of usei)")
}
return types.Pair{PriceDenom, AssetDenom}, nil
}

// ToParamChange converts a ParamChangeJSON object to ParamChange.
func (bcp BatchContractPairJSON) ToBatchContractPair() (types.BatchContractPair, error) {
pairs := make([]*types.Pair, len(bcp.Pairs))
for i, p := range bcp.Pairs {
new_pair, err := NewPair(p)
if err != nil {
return types.BatchContractPair{}, nil
}
pairs[i] = &new_pair
}
return types.BatchContractPair{bcp.ContractAddr, pairs}, nil
}

// ToParamChanges converts a slice of ParamChangeJSON objects to a slice of
// ParamChange.
func (mbcp MultipleBatchContractPairJSON) ToMultipleBatchContractPair() ([]types.BatchContractPair, error) {
res := make([]types.BatchContractPair, len(mbcp))
for i, bcp := range mbcp {
new_batch, err := bcp.ToBatchContractPair()
if err != nil {
return res, nil
}
res[i] = new_batch
}
return res, nil
}

// ParseRegisterPairsProposalJSON reads and parses a RegisterPairsProposalJSON from
// a file.
func ParseRegisterPairsProposalJSON(cdc *codec.LegacyAmino, proposalFile string) (RegisterPairsProposalJSON, error) {
proposal := RegisterPairsProposalJSON{}

contents, err := ioutil.ReadFile(proposalFile)
if err != nil {
return proposal, err
}

if err := cdc.UnmarshalJSON(contents, &proposal); err != nil {
return proposal, err
}

return proposal, nil
}
16 changes: 13 additions & 3 deletions x/dex/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

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

"github.com/sei-protocol/sei-chain/utils/tracing"
"github.com/sei-protocol/sei-chain/x/dex/keeper"
"github.com/sei-protocol/sei-chain/x/dex/types"
Expand All @@ -27,9 +29,6 @@ func NewHandler(k keeper.Keeper, tracingInfo *tracing.TracingInfo) sdk.Handler {
case *types.MsgLiquidation:
res, err := msgServer.Liquidate(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgRegisterPair:
res, err := msgServer.RegisterPair(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgRegisterContract:
res, err := msgServer.RegisterContract(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
Expand All @@ -40,3 +39,14 @@ func NewHandler(k keeper.Keeper, tracingInfo *tracing.TracingInfo) sdk.Handler {
}
}
}

func NewRegisterPairsProposalHandler(k keeper.Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) error {
switch c := content.(type) {
case *types.RegisterPairsProposal:
return k.HandleRegisterPairsProposal(ctx, c)
default:
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized dex proposal content type: %T", c)
}
}
}
24 changes: 24 additions & 0 deletions x/dex/keeper/gov.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

dexcache "github.com/sei-protocol/sei-chain/x/dex/cache"
"github.com/sei-protocol/sei-chain/x/dex/types"
)

func (k Keeper) HandleRegisterPairsProposal(ctx sdk.Context, p *types.RegisterPairsProposal) error {
// Loop through each batch contract pair an individual contract pair, token pair
// tuple and register them individually
for _, batchContractPair := range p.Batchcontractpair {
var contractAddress string = batchContractPair.ContractAddr
for _, pair := range batchContractPair.Pairs {
k.AddRegisteredPair(ctx, contractAddress, *pair)
k.Orders[contractAddress][(*pair).String()] = dexcache.NewOrders()
k.OrderPlacements[contractAddress][(*pair).String()] = dexcache.NewOrderPlacements()
k.OrderCancellations[contractAddress][(*pair).String()] = dexcache.NewOrderCancellations()
}
}

return nil
}
Loading