-
Notifications
You must be signed in to change notification settings - Fork 182
/
keeper.go
112 lines (98 loc) · 3.71 KB
/
keeper.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
package stream
import (
"fmt"
"github.com/okex/exchain/x/ammswap"
backend "github.com/okex/exchain/x/backend/types"
"github.com/okex/exchain/x/dex"
"github.com/okex/exchain/x/stream/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/okex/exchain/x/common/monitor"
"github.com/tendermint/tendermint/libs/log"
)
// nolint
type Keeper struct {
metric *monitor.StreamMetrics
stream *Stream
}
// nolint
func NewKeeper(orderKeeper types.OrderKeeper, tokenKeeper types.TokenKeeper, dexKeeper types.DexKeeper, accountKeeper types.AccountKeeper,
swapKeeper types.SwapKeeper, farmKeeper types.FarmKeeper, cdc *codec.Codec, logger log.Logger, cfg *config.Config, metrics *monitor.StreamMetrics) Keeper {
logger = logger.With("module", "stream")
k := Keeper{
metric: metrics,
stream: NewStream(orderKeeper, tokenKeeper, dexKeeper, swapKeeper, farmKeeper, cdc, logger, cfg),
}
if k.stream.engines != nil {
dexKeeper.SetObserverKeeper(k)
accountKeeper.SetObserverKeeper(k)
swapKeeper.SetObserverKeeper(k)
farmKeeper.SetObserverKeeper(k)
}
return k
}
// nolint
func (k Keeper) SyncTx(ctx sdk.Context, tx *auth.StdTx, txHash string, timestamp int64) {
if k.stream.engines[EngineAnalysisKind] != nil {
k.stream.logger.Debug(fmt.Sprintf("[stream engine] get new tx, txHash: %s", txHash))
txs := backend.GenerateTx(tx, txHash, ctx, k.stream.orderKeeper, timestamp)
for _, tx := range txs {
k.stream.Cache.AddTransaction(tx)
}
}
}
// GetMarketKeeper returns market keeper
func (k Keeper) GetMarketKeeper() MarketKeeper {
return k.stream.marketKeeper
}
// AnalysisEnable returns true when analysis is enable
func (k Keeper) AnalysisEnable() bool {
return k.stream.AnalysisEnable
}
// OnAddNewTokenPair called by dex when new token pair listed
func (k Keeper) OnAddNewTokenPair(ctx sdk.Context, tokenPair *dex.TokenPair) {
k.stream.logger.Debug(fmt.Sprintf("OnAddNewTokenPair:%s", tokenPair.Name()))
k.stream.Cache.AddNewTokenPair(tokenPair)
k.stream.Cache.SetTokenPairChanged(true)
}
// OnTokenPairUpdated called by dex when token pair updated
func (k Keeper) OnTokenPairUpdated(ctx sdk.Context) {
k.stream.logger.Debug("OnTokenPairUpdated:true")
k.stream.Cache.SetTokenPairChanged(true)
}
// OnAccountUpdated called by auth when account updated
func (k Keeper) OnAccountUpdated(acc auth.Account) {
k.stream.logger.Debug(fmt.Sprintf("OnAccountUpdated:%s", acc.GetAddress()))
k.stream.Cache.AddUpdatedAccount(acc)
}
// OnSwapToken called by swap
func (k Keeper) OnSwapToken(ctx sdk.Context, address sdk.AccAddress, swapTokenPair ammswap.SwapTokenPair, sellAmount sdk.SysCoin, buyAmount sdk.SysCoin) {
swapInfo := &backend.SwapInfo{
Address: address.String(),
TokenPairName: swapTokenPair.TokenPairName(),
BaseTokenAmount: swapTokenPair.BasePooledCoin.String(),
QuoteTokenAmount: swapTokenPair.QuotePooledCoin.String(),
SellAmount: sellAmount.String(),
BuysAmount: buyAmount.String(),
Price: swapTokenPair.BasePooledCoin.Amount.Quo(swapTokenPair.QuotePooledCoin.Amount).String(),
Timestamp: ctx.BlockTime().Unix(),
}
k.stream.Cache.AddSwapInfo(swapInfo)
}
func (k Keeper) OnSwapCreateExchange(ctx sdk.Context, swapTokenPair ammswap.SwapTokenPair) {
k.stream.Cache.AddNewSwapTokenPair(&swapTokenPair)
}
func (k Keeper) OnFarmClaim(ctx sdk.Context, address sdk.AccAddress, poolName string, claimedCoins sdk.SysCoins) {
if claimedCoins.IsZero() {
return
}
claimInfo := &backend.ClaimInfo{
Address: address.String(),
PoolName: poolName,
Claimed: claimedCoins.String(),
Timestamp: ctx.BlockTime().Unix(),
}
k.stream.Cache.AddClaimInfo(claimInfo)
}