/
slashing.go
39 lines (33 loc) · 1.09 KB
/
slashing.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
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/functionx/fx-core/x/crosschain/types"
)
func (k Keeper) SlashOracle(ctx sdk.Context, oracleAddress string, params types.Params) {
oracleAddr, err := sdk.AccAddressFromBech32(oracleAddress)
if err != nil {
panic(err)
}
oracle, found := k.GetOracle(ctx, oracleAddr)
if !found {
panic(fmt.Sprintf("not found oracle:%s", oracleAddr.String()))
}
if oracle.Jailed {
return
}
slashAmount := oracle.DepositAmount.Amount.ToDec().Mul(params.SlashFraction).TruncateInt()
tokensToBurn := sdk.MinInt(slashAmount, oracle.DepositAmount.Amount)
tokensToBurn = sdk.MaxInt(tokensToBurn, sdk.ZeroInt())
slashCoin := sdk.NewCoin(oracle.DepositAmount.Denom, tokensToBurn)
if slashCoin.IsPositive() {
oracle.DepositAmount = oracle.DepositAmount.Sub(slashCoin)
if err = k.bankKeeper.BurnCoins(ctx, k.moduleName, sdk.NewCoins(slashCoin)); err != nil {
panic(err)
}
}
oracle.Jailed = true
oracle.JailedHeight = ctx.BlockHeight()
k.SetOracle(ctx, oracle)
k.SetLastOracleSlashBlockHeight(ctx, uint64(ctx.BlockHeight()))
}