-
Notifications
You must be signed in to change notification settings - Fork 39
/
fee_tax.go
128 lines (100 loc) · 2.97 KB
/
fee_tax.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
package ante
import (
"regexp"
"strings"
"github.com/CosmWasm/wasmd/x/wasm"
sdk "github.com/cosmos/cosmos-sdk/types"
authz "github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
marketexported "github.com/classic-terra/core/v2/x/market/exported"
oracleexported "github.com/classic-terra/core/v2/x/oracle/exported"
)
var IBCRegexp = regexp.MustCompile("^ibc/[a-fA-F0-9]{64}$")
func isIBCDenom(denom string) bool {
return IBCRegexp.MatchString(strings.ToLower(denom))
}
// FilterMsgAndComputeTax computes the stability tax on messages.
func FilterMsgAndComputeTax(ctx sdk.Context, tk TreasuryKeeper, msgs ...sdk.Msg) sdk.Coins {
taxes := sdk.Coins{}
for _, msg := range msgs {
switch msg := msg.(type) {
case *banktypes.MsgSend:
if !tk.HasBurnTaxExemptionAddress(ctx, msg.FromAddress, msg.ToAddress) {
taxes = taxes.Add(computeTax(ctx, tk, msg.Amount)...)
}
case *banktypes.MsgMultiSend:
tainted := 0
for _, input := range msg.Inputs {
if tk.HasBurnTaxExemptionAddress(ctx, input.Address) {
tainted++
}
}
for _, output := range msg.Outputs {
if tk.HasBurnTaxExemptionAddress(ctx, output.Address) {
tainted++
}
}
if tainted != len(msg.Inputs)+len(msg.Outputs) {
for _, input := range msg.Inputs {
taxes = taxes.Add(computeTax(ctx, tk, input.Coins)...)
}
}
case *marketexported.MsgSwapSend:
taxes = taxes.Add(computeTax(ctx, tk, sdk.NewCoins(msg.OfferCoin))...)
case *wasm.MsgInstantiateContract:
taxes = taxes.Add(computeTax(ctx, tk, msg.Funds)...)
case *wasm.MsgInstantiateContract2:
taxes = taxes.Add(computeTax(ctx, tk, msg.Funds)...)
case *wasm.MsgExecuteContract:
if !tk.HasBurnTaxExemptionContract(ctx, msg.Contract) {
taxes = taxes.Add(computeTax(ctx, tk, msg.Funds)...)
}
case *authz.MsgExec:
messages, err := msg.GetMessages()
if err == nil {
taxes = taxes.Add(FilterMsgAndComputeTax(ctx, tk, messages...)...)
}
}
}
return taxes
}
// computes the stability tax according to tax-rate and tax-cap
func computeTax(ctx sdk.Context, tk TreasuryKeeper, principal sdk.Coins) sdk.Coins {
taxRate := tk.GetTaxRate(ctx)
if taxRate.Equal(sdk.ZeroDec()) {
return sdk.Coins{}
}
taxes := sdk.Coins{}
for _, coin := range principal {
if coin.Denom == sdk.DefaultBondDenom {
continue
}
if isIBCDenom(coin.Denom) {
continue
}
taxDue := sdk.NewDecFromInt(coin.Amount).Mul(taxRate).TruncateInt()
// If tax due is greater than the tax cap, cap!
taxCap := tk.GetTaxCap(ctx, coin.Denom)
if taxDue.GT(taxCap) {
taxDue = taxCap
}
if taxDue.Equal(sdk.ZeroInt()) {
continue
}
taxes = taxes.Add(sdk.NewCoin(coin.Denom, taxDue))
}
return taxes
}
func isOracleTx(msgs []sdk.Msg) bool {
for _, msg := range msgs {
switch msg.(type) {
case *oracleexported.MsgAggregateExchangeRatePrevote:
continue
case *oracleexported.MsgAggregateExchangeRateVote:
continue
default:
return false
}
}
return true
}