forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feekeeper.go
161 lines (139 loc) · 4.67 KB
/
feekeeper.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package auth
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/params"
"fmt"
"errors"
)
var (
collectedFeesKey = []byte("collectedFees")
NativeFeeTokenKey = "feeToken/native"
NativeGasPriceThresholdKey = "feeToken/gasPriceThreshold"
FeeExchangeRatePrefix = "feeToken/exchangeRate/" // key = gov/feeToken/exchangeRate/<denomination>, rate = BigInt(value)/10^9
RatePrecision = int64(1000000000) //10^9
)
// This FeeCollectionKeeper handles collection of fees in the anteHandler
// and setting of MinFees for different fee tokens
type FeeCollectionKeeper struct {
getter params.GetterProxy
// The (unexposed) key used to access the fee store from the Context.
key sdk.StoreKey
// The wire codec for binary encoding/decoding of accounts.
cdc *wire.Codec
}
// NewFeeKeeper returns a new FeeKeeper
func NewFeeCollectionKeeper(cdc *wire.Codec, key sdk.StoreKey, getter params.GetterProxy) FeeCollectionKeeper {
return FeeCollectionKeeper{
key: key,
cdc: cdc,
getter: getter,
}
}
// Adds to Collected Fee Pool
func (fck FeeCollectionKeeper) GetCollectedFees(ctx sdk.Context) sdk.Coins {
store := ctx.KVStore(fck.key)
bz := store.Get(collectedFeesKey)
if bz == nil {
return sdk.Coins{}
}
feePool := &(sdk.Coins{})
fck.cdc.MustUnmarshalBinary(bz, feePool)
return *feePool
}
// Sets to Collected Fee Pool
func (fck FeeCollectionKeeper) setCollectedFees(ctx sdk.Context, coins sdk.Coins) {
bz := fck.cdc.MustMarshalBinary(coins)
store := ctx.KVStore(fck.key)
store.Set(collectedFeesKey, bz)
}
// Adds to Collected Fee Pool
func (fck FeeCollectionKeeper) addCollectedFees(ctx sdk.Context, coins sdk.Coins) sdk.Coins {
newCoins := fck.GetCollectedFees(ctx).Plus(coins)
fck.setCollectedFees(ctx, newCoins)
return newCoins
}
func (fck FeeCollectionKeeper) refundCollectedFees(ctx sdk.Context, coins sdk.Coins) sdk.Coins {
newCoins := fck.GetCollectedFees(ctx).Minus(coins)
if !newCoins.IsNotNegative() {
panic("fee collector contains negative coins")
}
fck.setCollectedFees(ctx, newCoins)
return newCoins
}
func (fck FeeCollectionKeeper) GetNativeFeeToken(ctx sdk.Context, coins sdk.Coins) sdk.Coin {
nativeFeeToken, err := fck.getter.GetString(ctx, NativeFeeTokenKey)
if err != nil {
panic(err)
}
for _,coin := range coins {
if coin.Denom == nativeFeeToken {
return coin
}
}
return sdk.Coin{}
}
// Clears the collected Fee Pool
func (fck FeeCollectionKeeper) ClearCollectedFees(ctx sdk.Context) {
fck.setCollectedFees(ctx, sdk.Coins{})
}
func (fck FeeCollectionKeeper) FeePreprocess(ctx sdk.Context, coins sdk.Coins, gasLimit int64) sdk.Error {
if gasLimit <= 0 {
return sdk.ErrInternal(fmt.Sprintf("gaslimit %d should be larger than 0", gasLimit))
}
nativeFeeToken, err := fck.getter.GetString(ctx, NativeFeeTokenKey)
if err != nil {
panic(err)
}
nativeGasPriceThreshold, err := fck.getter.GovGetter().GetString(ctx, NativeGasPriceThresholdKey)
if err != nil {
panic(err)
}
threshold, ok := sdk.NewIntFromString(nativeGasPriceThreshold)
if !ok {
panic(errors.New("failed to parse gas price from string"))
}
if len(coins) < 1 || coins[0].Denom != nativeFeeToken {
return sdk.ErrInvalidCoins(fmt.Sprintf("no native fee token, expected native token is %s", nativeFeeToken))
}
/*
equivalentTotalFee := sdk.NewInt(0)
for _,coin := range coins {
if coin.Denom != nativeFeeToken {
exchangeRateKey := FeeExchangeRatePrefix + coin.Denom
rateString, err := fck.getter.GetString(ctx, exchangeRateKey)
if err != nil {
continue
}
rate, ok := sdk.NewIntFromString(rateString)
if !ok {
panic(errors.New("failed to parse rate from string"))
}
equivalentFee := rate.Mul(coin.Amount).Div(sdk.NewInt(RatePrecision))
equivalentTotalFee = equivalentTotalFee.Add(equivalentFee)
} else {
equivalentTotalFee = equivalentTotalFee.Add(coin.Amount)
}
}
*/
equivalentTotalFee := coins[0].Amount
gasPrice := equivalentTotalFee.Div(sdk.NewInt(gasLimit))
if gasPrice.LT(threshold) {
return sdk.ErrInsufficientCoins(fmt.Sprintf("equivalent gas price (%s%s) is less than threshold (%s%s)", gasPrice.String(), nativeFeeToken, threshold.String(), nativeFeeToken))
}
return nil
}
type GenesisState struct {
FeeTokenNative string `json:"fee_token_native"`
GasPriceThreshold int64 `json:"gas_price_threshold"`
}
func DefaultGenesisState() GenesisState {
return GenesisState{
FeeTokenNative: "steak",
GasPriceThreshold: 20000000000, //2*10^10
}
}
func InitGenesis(ctx sdk.Context, setter params.SetterProxy, data GenesisState) {
setter.SetString(ctx, NativeFeeTokenKey, data.FeeTokenNative)
setter.GovSetter().SetString(ctx, NativeGasPriceThresholdKey, sdk.NewInt(data.GasPriceThreshold).String())
}