-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
143 lines (132 loc) · 4.18 KB
/
processor.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
package staking
import (
"context"
"github.com/LampardNguyen234/whale-alert/internal/clients/cosmos"
"github.com/LampardNguyen234/whale-alert/internal/common"
processorCommon "github.com/LampardNguyen234/whale-alert/internal/processor/common"
"github.com/LampardNguyen234/whale-alert/internal/store"
"github.com/LampardNguyen234/whale-alert/logger"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingTypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"sync"
"time"
)
type StakingProcessor struct {
*processorCommon.BaseProcessor
*cosmos.CosmosClient
queue chan *sdk.TxResponse
cfg StakingProcessorConfig
}
func NewStakingProcessor(cfg StakingProcessorConfig,
cosmosClient *cosmos.CosmosClient,
db *store.Store,
log logger.Logger,
) (*StakingProcessor, error) {
return &StakingProcessor{
BaseProcessor: &processorCommon.BaseProcessor{
Db: db,
Log: log.WithPrefix("Cosmos-staking"),
Mtx: new(sync.Mutex),
},
CosmosClient: cosmosClient,
queue: make(chan *sdk.TxResponse, cfg.QueueSize),
cfg: cfg,
}, nil
}
func (p *StakingProcessor) Queue(msg interface{}) {
receipt, ok := msg.(*sdk.TxResponse)
if !ok {
return
}
if receipt.Code != 0 {
return
}
p.Mtx.Lock()
defer p.Mtx.Unlock()
p.queue <- receipt
}
func (p *StakingProcessor) Start(ctx context.Context) {
p.Log.Infof("STARTED")
for {
select {
case <-ctx.Done():
p.Log.Infof("STOPPED")
return
case receipt := <-p.queue:
err := p.Process(ctx, receipt)
if err != nil {
p.Log.Errorf("failed to process receipt %v: %v", receipt, err)
}
default:
time.Sleep(common.DefaultSleepTime)
}
}
}
func (p *StakingProcessor) Process(ctx context.Context, receipt *sdk.TxResponse) error {
messages := receipt.GetTx().GetMsgs()
for _, msg := range messages {
switch msg.(type) {
case *stakingTypes.MsgDelegate:
tmpMsgDelegate := msg.(*stakingTypes.MsgDelegate)
go p.processMsgDelegate(ctx, receipt, tmpMsgDelegate)
case *stakingTypes.MsgUndelegate:
tmpMsgUndelegate := msg.(*stakingTypes.MsgUndelegate)
go p.processMsgUndelegate(ctx, receipt, tmpMsgUndelegate)
case *stakingTypes.MsgCreateValidator:
tmpMsgCreateValidator := msg.(*stakingTypes.MsgCreateValidator)
go p.processMsgCreateValidator(ctx, receipt, tmpMsgCreateValidator)
default:
return nil
}
}
return nil
}
func (p *StakingProcessor) processMsgDelegate(ctx context.Context, receipt *sdk.TxResponse, msg *stakingTypes.MsgDelegate) {
amtFloat := common.GetNormalizedValue(msg.Amount.Amount.BigInt())
if amtFloat >= p.Db.GetTokenDetail(common.AsaAddress).WhaleDefinition {
err := p.Whm.Alert(DelegateMsg{
TxMsg: processorCommon.TxMsg{
From: p.ParseAccountDetail(msg.DelegatorAddress),
Amount: common.FormatAmount(amtFloat),
Token: "0x",
TokenName: "ASA",
TxHash: receipt.TxHash,
},
Validator: p.getValidatorName(ctx, msg.ValidatorAddress),
}.String())
if err != nil {
p.Log.Errorf("failed to processMsgDelegate txHash %v: %v", receipt.TxHash, err)
}
}
}
func (p *StakingProcessor) processMsgUndelegate(ctx context.Context, receipt *sdk.TxResponse, msg *stakingTypes.MsgUndelegate) {
amtFloat := common.GetNormalizedValue(msg.Amount.Amount.BigInt())
if amtFloat >= p.Db.GetTokenDetail(common.AsaAddress).WhaleDefinition {
err := p.Whm.Alert(UndelegateMsg{
TxMsg: processorCommon.TxMsg{
From: p.ParseAccountDetail(msg.DelegatorAddress),
Amount: common.FormatAmount(amtFloat),
Token: "0x",
TokenName: "ASA",
TxHash: receipt.TxHash,
},
Validator: p.getValidatorName(ctx, msg.ValidatorAddress),
}.String())
if err != nil {
p.Log.Errorf("failed to processMsgDelegate txHash %v: %v", receipt.TxHash, err)
}
}
}
func (p *StakingProcessor) processMsgCreateValidator(_ context.Context, receipt *sdk.TxResponse, msg *stakingTypes.MsgCreateValidator) {
err := p.Whm.Alert(CreateValidatorMsg{
TxMsg: processorCommon.TxMsg{
TxHash: receipt.TxHash,
},
Address: parseValidatorDetail(msg.ValidatorAddress, msg.Description),
Name: msg.Description.Moniker,
Commission: msg.Commission.Rate.MustFloat64(),
}.String())
if err != nil {
p.Log.Errorf("failed to processMsgDelegate txHash %v: %v", receipt.TxHash, err)
}
}