-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
131 lines (117 loc) · 3.19 KB
/
handlers.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
package main
import (
"context"
"fmt"
"log"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pinebit/eth-listener/token"
)
func handleTransfer(transfer *Transfer, app *App, ctx context.Context) {
value := transfer.Token.RenderValue(&transfer.Value)
getBalanceStr := func(addr common.Address) string {
balanceStr := "N/A"
balance, err := app.tokensManager.FetchBalance(ctx, transfer.Token, addr)
if err == nil {
balanceStr = transfer.Token.RenderValue(balance)
} else {
log.Printf("Failed to fetch balance for %s: %v", transfer.Token.Symbol, err)
}
return balanceStr
}
var msg string
switch transfer.Direction {
case Sent:
msg = fmt.Sprintf("%s sent %s to %s, new balance: %s",
app.accounts.Lookup(transfer.From),
value,
app.accounts.Lookup(transfer.To),
getBalanceStr(transfer.From))
case Received:
msg = fmt.Sprintf("%s received %s from %s, new balance: %s",
app.accounts.Lookup(transfer.To),
value,
app.accounts.Lookup(transfer.From),
getBalanceStr(transfer.To))
}
log.Println(msg)
app.telegram.Notify(msg)
}
func handleHeader(ctx context.Context, header *types.Header, transfersCh chan<- *Transfer, app *App) {
block, err := app.client.BlockByNumber(ctx, header.Number)
if err != nil {
return
}
for _, tx := range block.Transactions() {
msg, err := tx.AsMessage(types.LatestSignerForChainID(tx.ChainId()), big.NewInt(1))
if err != nil {
continue
}
if tx.To() != nil {
if _, has := app.accounts[*tx.To()]; has && tx.Value() != nil {
transfersCh <- &Transfer{
Direction: Received,
From: msg.From(),
To: *tx.To(),
Value: *tx.Value(),
Token: token.ETHToken,
}
}
}
if _, has := app.accounts[msg.From()]; has && msg.Value() != nil && msg.To() != nil {
transfersCh <- &Transfer{
Direction: Sent,
From: msg.From(),
To: *tx.To(),
Value: *tx.Value(),
Token: token.ETHToken,
}
}
}
filterQuery := ethereum.FilterQuery{}
filterQuery.FromBlock = block.Number()
filterQuery.ToBlock = block.Number()
logs, err := app.client.FilterLogs(ctx, filterQuery)
if err != nil {
return
}
for _, logItem := range logs {
if len(logItem.Topics) != 3 || logItem.Topics[0] != LogTransferSigHash {
continue
}
token, err := app.tokensManager.GetToken(ctx, logItem.Address)
if err != nil {
log.Printf("Skipping log for non-ERC20 contract: %v", err)
continue
}
type logTransfer struct {
Value *big.Int
}
var transfer logTransfer
if err := ERC20ABI.UnpackIntoInterface(&transfer, "Transfer", logItem.Data); err != nil || transfer.Value == nil {
continue
}
from := common.HexToAddress(logItem.Topics[1].Hex())
to := common.HexToAddress(logItem.Topics[2].Hex())
if _, has := app.accounts[from]; has {
transfersCh <- &Transfer{
Direction: Sent,
From: from,
To: to,
Value: *transfer.Value,
Token: token,
}
}
if _, has := app.accounts[to]; has {
transfersCh <- &Transfer{
Direction: Received,
From: from,
To: to,
Value: *transfer.Value,
Token: token,
}
}
}
}