-
Notifications
You must be signed in to change notification settings - Fork 182
/
proposal_handler.go
56 lines (48 loc) · 1.76 KB
/
proposal_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package dex
import (
"fmt"
"github.com/okex/exchain/x/common"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"github.com/okex/exchain/x/dex/types"
govTypes "github.com/okex/exchain/x/gov/types"
)
// NewProposalHandler handles "gov" type message in "dex"
func NewProposalHandler(k *Keeper) govTypes.Handler {
return func(ctx sdk.Context, proposal *govTypes.Proposal) (err sdk.Error) {
switch c := proposal.Content.(type) {
case types.DelistProposal:
return handleDelistProposal(ctx, k, proposal)
default:
return common.ErrUnknownProposalType(DefaultCodespace, fmt.Sprintf("%T", c))
}
}
}
func handleDelistProposal(ctx sdk.Context, keeper *Keeper, proposal *govTypes.Proposal) (err sdk.Error) {
p := proposal.Content.(types.DelistProposal)
logger := ctx.Logger().With("module", types.ModuleName)
logger.Debug("execute DelistProposal begin")
tokenPairName := fmt.Sprintf("%s_%s", p.BaseAsset, p.QuoteAsset)
tokenPair := keeper.GetTokenPair(ctx, tokenPairName)
if tokenPair == nil {
return ErrTokenPairNotFound(tokenPairName)
}
if keeper.IsTokenPairLocked(ctx, tokenPairName) {
return types.ErrIsTokenPairLocked(tokenPairName)
}
// withdraw
if tokenPair.Deposits.IsPositive() {
if err := keeper.Withdraw(ctx, tokenPair.Name(), tokenPair.Owner, tokenPair.Deposits); err != nil {
return types.ErrWithdrawFailed(err.Error())
}
}
// delete the token pair by its name from store and cache
keeper.DeleteTokenPairByName(ctx, tokenPair.Owner, tokenPairName)
// remove the delistProposal from the active proposal queue
keeper.RemoveFromActiveProposalQueue(ctx, proposal.ProposalID, proposal.VotingEndTime)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute("token-pair-deleted", tokenPairName),
))
return nil
}