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

R4R: Refactor coinswap module #2123

Merged
merged 9 commits into from
Dec 13, 2019
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
14 changes: 13 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/supply"

"github.com/irisnet/irishub/modules/asset"
"github.com/irisnet/irishub/modules/coinswap"
"github.com/irisnet/irishub/modules/guardian"
"github.com/irisnet/irishub/modules/htlc"
"github.com/irisnet/irishub/modules/mint"
Expand Down Expand Up @@ -62,6 +63,7 @@ var (
asset.AppModuleBasic{},
guardian.AppModuleBasic{},
htlc.AppModuleBasic{},
coinswap.AppModuleBasic{},
)

// module account permissions
Expand All @@ -74,6 +76,7 @@ var (
gov.ModuleName: {supply.Burner},
asset.ModuleName: {supply.Minter, supply.Burner},
htlc.ModuleName: nil,
coinswap.ModuleName: {supply.Minter, supply.Burner},
}
)

Expand Down Expand Up @@ -117,6 +120,7 @@ type IrisApp struct {
assetKeeper asset.Keeper
guardianKeeper guardian.Keeper
htlcKeeper htlc.Keeper
coinswapKeeper coinswap.Keeper

// the module manager
mm *module.Manager
Expand All @@ -139,7 +143,7 @@ func NewIrisApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
bam.MainStoreKey, auth.StoreKey, staking.StoreKey, supply.StoreKey,
mint.StoreKey, distr.StoreKey, slashing.StoreKey, gov.StoreKey,
params.StoreKey, evidence.StoreKey, asset.StoreKey, guardian.StoreKey,
htlc.StoreKey,
htlc.StoreKey, coinswap.StoreKey,
)
tKeys := sdk.NewTransientStoreKeys(staking.TStoreKey, params.TStoreKey)

Expand All @@ -163,6 +167,7 @@ func NewIrisApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
crisisSubspace := app.paramsKeeper.Subspace(crisis.DefaultParamspace)
evidenceSubspace := app.paramsKeeper.Subspace(evidence.DefaultParamspace)
assetSubspace := app.paramsKeeper.Subspace(asset.DefaultParamspace)
coinswapSubspace := app.paramsKeeper.Subspace(coinswap.DefaultParamspace)

// add keepers
app.accountKeeper = auth.NewAccountKeeper(app.cdc, keys[auth.StoreKey], authSubspace, auth.ProtoBaseAccount)
Expand Down Expand Up @@ -217,6 +222,10 @@ func NewIrisApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
app.cdc, keys[htlc.StoreKey], app.supplyKeeper, htlc.DefaultCodespace,
)

app.coinswapKeeper = coinswap.NewKeeper(
app.cdc, keys[coinswap.StoreKey], app.bankKeeper, app.accountKeeper, app.supplyKeeper, coinswapSubspace,
)

// NOTE: Any module instantiated in the module manager that is later modified
// must be passed by reference here.
app.mm = module.NewManager(
Expand All @@ -234,6 +243,7 @@ func NewIrisApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
asset.NewAppModule(app.assetKeeper),
guardian.NewAppModule(app.guardianKeeper),
htlc.NewAppModule(app.htlcKeeper),
coinswap.NewAppModule(app.coinswapKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand All @@ -259,6 +269,7 @@ func NewIrisApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
slashing.ModuleName, gov.ModuleName, mint.ModuleName, supply.ModuleName,
crisis.ModuleName, genutil.ModuleName, evidence.ModuleName,
asset.ModuleName, guardian.ModuleName, htlc.ModuleName,
coinswap.ModuleName,
)

app.mm.RegisterInvariants(&app.crisisKeeper)
Expand All @@ -279,6 +290,7 @@ func NewIrisApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
slashing.NewAppModule(app.slashingKeeper, app.stakingKeeper),
asset.NewAppModule(app.assetKeeper),
htlc.NewAppModule(app.htlcKeeper),
coinswap.NewAppModule(app.coinswapKeeper),
)

app.sm.RegisterStoreDecoders()
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package config

const (
Testnet = "testnet"
Mainnet = "mainnet"
Testnet = "testnet"
Mainnet = "mainnet"
)

// Can be configured through environment variables
Expand Down
57 changes: 30 additions & 27 deletions modules/coinswap/alias.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
package coinswap

import (
"github.com/irisnet/irishub/app/v2/coinswap/internal/keeper"
"github.com/irisnet/irishub/app/v2/coinswap/internal/types"
"github.com/irisnet/irishub/modules/coinswap/internal/keeper"
"github.com/irisnet/irishub/modules/coinswap/internal/types"
)

const (
ModuleName = types.ModuleName
StoreKey = types.StoreKey
RouterKey = types.RouterKey
QuerierRoute = types.QuerierRoute
DefaultParamspace = types.DefaultParamspace
DefaultCodespace = types.DefaultCodespace

EventTypeSwap = types.EventTypeSwap
EventTypeAddLiquidity = types.EventTypeAddLiquidity
EventTypeRemoveLiquidity = types.EventTypeRemoveLiquidity
AttributeValueCategory = types.AttributeValueCategory
AttributeValueAmount = types.AttributeValueAmount
AttributeValueSender = types.AttributeValueSender
AttributeValueRecipient = types.AttributeValueRecipient
AttributeValueIsBuyOrder = types.AttributeValueIsBuyOrder
AttributeValueTokenPair = types.AttributeValueTokenPair
)

type (
Expand All @@ -17,31 +36,15 @@ type (
)

var (
DefaultParamSpace = types.DefaultParamSpace
QueryLiquidity = types.QueryLiquidity

RegisterCodec = types.RegisterCodec

NewMsgSwapOrder = types.NewMsgSwapOrder
NewMsgAddLiquidity = types.NewMsgAddLiquidity
NewMsgRemoveLiquidity = types.NewMsgRemoveLiquidity
NewKeeper = keeper.NewKeeper
NewQuerier = keeper.NewQuerier

ErrInvalidDeadline = types.ErrInvalidDeadline
ErrNotPositive = types.ErrNotPositive
ErrConstraintNotMet = types.ErrConstraintNotMet

GetUniId = types.GetUniId
GetCoinMinDenomFromUniDenom = types.GetCoinMinDenomFromUniDenom
GetUniDenom = types.GetUniDenom
GetUniCoinType = types.GetUniCoinType
CheckUniDenom = types.CheckUniDenom
CheckUniId = types.CheckUniId
NewKeeper = keeper.NewKeeper
NewQuerier = keeper.NewQuerier
RegisterCodec = types.RegisterCodec
ErrInvalidDeadline = types.ErrInvalidDeadline
ValidateParams = types.ValidateParams
DefaultParams = types.DefaultParams
)

const (
DefaultCodespace = types.DefaultCodespace
ModuleName = types.ModuleName
FormatUniABSPrefix = types.FormatUniABSPrefix
// exported variables and functions
var (
ModuleCdc = types.ModuleCdc
)
45 changes: 45 additions & 0 deletions modules/coinswap/client/rest/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package rest

import (
"fmt"
"net/http"

"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"

"github.com/irisnet/irishub/modules/coinswap/internal/types"
)

func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
// Query liquidity
r.HandleFunc("/coinswap/liquidities/{id}", queryLiquidityHandlerFn(cliCtx)).Methods("GET")
}

// queryLiquidityHandlerFn performs liquidity information query
func queryLiquidityHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
params := types.QueryLiquidityParams{
ID: id,
}

bz, err := cliCtx.Codec.MarshalJSON(params)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryLiquidity)
res, height, err := cliCtx.QueryWithData(route, bz)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}
52 changes: 52 additions & 0 deletions modules/coinswap/client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package rest

import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
)

// RegisterRoutes registers asset-related REST handlers to a router
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
registerQueryRoutes(cliCtx, r)
registerTxRoutes(cliCtx, r)
}

type AddLiquidityReq struct {
BaseTx rest.BaseReq `json:"base_tx" yaml:"base_tx"`
ID string `json:"id" yaml:"id"` // the unique liquidity id
MaxToken string `json:"max_token" yaml:"max_token"` // token to be deposited as liquidity with an upper bound for its amount
ExactStandardAmt string `json:"exact_standard_amt" yaml:"exact_standard_amt"` // exact amount of standard token being add to the liquidity pool
MinLiquidity string `json:"min_liquidity" yaml:"min_liquidity"` // lower bound UNI sender is willing to accept for deposited coins
Deadline string `json:"deadline" yaml:"deadline"` // deadline duration, e.g. 10m
Sender string `json:"sender" yaml:"sender"`
}

type RemoveLiquidityReq struct {
BaseTx rest.BaseReq `json:"base_tx" yaml:"base_tx"`
ID string `json:"id" yaml:"id"` // the unique liquidity id
MinToken string `json:"min_token" yaml:"min_token"` // coin to be withdrawn with a lower bound for its amount
WithdrawLiquidity string `json:"withdraw_liquidity" yaml:"withdraw_liquidity"` // amount of UNI to be burned to withdraw liquidity from a reserve pool
MinStandardAmt string `json:"min_standard_amt" yaml:"min_standard_amt"` // minimum amount of the native asset the sender is willing to accept
Deadline string `json:"deadline" yaml:"deadline"` // deadline duration, e.g. 10m
Sender string `json:"sender" yaml:"sender"`
}

type Input struct {
Address string `json:"address" yaml:"address"`
Coin sdk.Coin `json:"coin" yaml:"coin"`
}

type Output struct {
Address string `json:"address" yaml:"address"`
Coin sdk.Coin `json:"coin" yaml:"coin"`
}

type SwapOrderReq struct {
BaseTx rest.BaseReq `json:"base_tx" yaml:"base_tx"`
Input Input `json:"input" yaml:"input"` // the amount the sender is trading
Output Output `json:"output" yaml:"output"` // the amount the sender is receiving
Deadline string `json:"deadline" yaml:"deadline"` // deadline for the transaction to still be considered valid
}
Loading