-
Notifications
You must be signed in to change notification settings - Fork 179
/
fee.go
66 lines (54 loc) · 2.22 KB
/
fee.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
57
58
59
60
61
62
63
64
65
66
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"strings"
"github.com/okex/okexchain/x/order/types"
)
const minFee = "0.00000001"
// GetFeeKeeper is an interface for calculating handling fees
type GetFeeKeeper interface {
GetLastPrice(ctx sdk.Context, product string) sdk.Dec
}
// GetOrderNewFee is used to calculate the handling fee that needs to be locked when placing an order
func GetOrderNewFee(order *types.Order) sdk.SysCoins {
orderExpireBlocks := sdk.NewDec(order.OrderExpireBlocks)
amount := order.FeePerBlock.Amount.Mul(orderExpireBlocks)
return sdk.SysCoins{sdk.NewDecCoinFromDec(order.FeePerBlock.Denom, amount)}
}
// GetOrderCostFee is used to calculate the handling fee when quiting an order
func GetOrderCostFee(order *types.Order, ctx sdk.Context) sdk.SysCoins {
currentHeight := ctx.BlockHeight()
orderHeight := types.GetBlockHeightFromOrderID(order.OrderID)
blockNum := currentHeight - orderHeight
if blockNum < 0 {
ctx.Logger().Error(fmt.Sprintf("currentHeight(%d) should not less than orderHeight(%d)", currentHeight, orderHeight))
return GetZeroFee()
} else if blockNum > order.OrderExpireBlocks {
blockNum = order.OrderExpireBlocks
ctx.Logger().Error(fmt.Sprintf("currentHeight(%d) - orderHeight(%d) > OrderExpireBlocks(%d)", currentHeight, orderHeight, order.OrderExpireBlocks))
}
costFee := order.FeePerBlock.Amount.Mul(sdk.NewDec(blockNum))
return sdk.SysCoins{sdk.NewDecCoinFromDec(order.FeePerBlock.Denom, costFee)}
}
// GetZeroFee returns zeroFee
func GetZeroFee() sdk.SysCoins {
return sdk.SysCoins{sdk.ZeroFee()}
}
// GetDealFee is used to calculate the handling fee when matching an order
func GetDealFee(order *types.Order, fillAmt sdk.Dec, ctx sdk.Context, keeper GetFeeKeeper,
feeParams *types.Params) sdk.SysCoins {
symbols := strings.Split(order.Product, "_")
symbol := symbols[0]
quantity := fillAmt
if order.Side == types.SellOrder {
symbol = symbols[1]
quantity = fillAmt.Mul(keeper.GetLastPrice(ctx, order.Product))
}
minFeeDec := sdk.MustNewDecFromStr(minFee)
feeAmt := quantity.Mul(feeParams.TradeFeeRate)
if feeAmt.GT(minFeeDec) {
return sdk.SysCoins{sdk.NewDecCoinFromDec(symbol, feeAmt)}
}
return sdk.SysCoins{sdk.NewDecCoinFromDec(symbol, minFeeDec)}
}