-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
123 lines (107 loc) · 2.95 KB
/
handler.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
package main
import (
"PumpScan/db"
"PumpScan/parser"
"github.com/0xjeffro/tx-parser/solana/types"
"log"
"slices"
"strings"
"time"
)
var jitoTipAddress = []string{
"96gYZGLnJYVFmbjzopPSU6QiEV5fGqZNyN9nmNhvrZU5",
"HFqU5x63VTqvQss8hp11i4wVV8bD44PvwucfZ2bU7gRe",
"Cw8CFyM9FkoMi7K7Crf6HNQqf4uEMzpKw6QNghXLvLkY",
"ADaUMid9yfUytqMBgopwjb2DTLSokTSzL1zt6iGPaS49",
"DfXygSm4jCyNCybVYYK6DwvWqjKee8pbDmJGcLWNDXjh",
"ADuUkR4vqLUMWXxW9gh6D6L8pMSawimctcNZ5pGwDcEt",
"DttWaMuVvTiduZRnguLF7jNxTgiMBZ1hyAumKUiL2KRL",
"3AVi9Tg9Uo68tJfuvoKvqKNWKkC5wPdSSdeBnizKZ6jT",
}
func joinStrings(strs []string) string {
if len(strs) == 0 {
return ""
}
if len(strs) == 1 {
return strs[0]
}
return strings.Join(strs, ",")
}
func WebhookHandler(bytes []byte) {
actions, ctx, err := parser.Parser(bytes)
if err != nil {
log.Println("[WebhookHandler] Parser error: ", err)
return
}
if len(actions) == 0 {
log.Println("[WebhookHandler] No actions found tx: ", ctx.RawTx.Transaction.Signatures[0])
return
}
var txRow db.Txs
txRow.Tx = ctx.RawTx.Transaction.Signatures[0]
txRow.BlockTime = time.Unix(ctx.RawTx.BlockTime, 0)
txRow.Slot = ctx.RawTx.Slot
var userMap = make(map[string]bool)
var mintMap = make(map[string]bool)
for _, action := range actions {
switch a := action.(type) {
case *types.PumpFunCreateAction:
userMap[a.Who] = true
mintMap[a.Mint] = true
txRow.IsCreate = true
case *types.PumpFunAnchorSelfCPILogSwapAction:
userMap[a.User] = true
mintMap[a.Mint] = true
if a.IsBuy {
txRow.BuyAmt += a.TokenAmount
txRow.BuySOLAmt += a.SolAmount
txRow.NBuy++
// VirtualTokenReserves and VirtualSolReserves may be overwritten by subsequent actions;
// only applies when there's exactly one swap (NBuy + NSell == 1)
txRow.VirtualTokenReserves = a.VirtualTokenReserves
txRow.VirtualSolReserves = a.VirtualSolReserves
} else {
txRow.SellAmt += a.TokenAmount
txRow.SellSOLAmt += a.SolAmount
txRow.NSell++
// VirtualTokenReserves and VirtualSolReserves may be overwritten by subsequent actions;
// only applies when there's exactly one swap (NBuy + NSell == 1)
txRow.VirtualTokenReserves = a.VirtualTokenReserves
txRow.VirtualSolReserves = a.VirtualSolReserves
}
case *types.SystemProgramTransferAction:
if slices.Contains(jitoTipAddress, a.To) {
txRow.JitotipAmt += a.Lamports
}
}
}
numMint := 0
var mints []string
for k, v := range mintMap {
if v {
numMint++
mints = append(mints, k)
}
}
numUser := 0
var users []string
for k, v := range userMap {
if v {
numUser++
users = append(users, k)
}
}
txRow.NMint = int8(numMint)
txRow.Mint = joinStrings(mints)
txRow.NUser = int8(numUser)
txRow.User = joinStrings(users)
if numUser == 0 {
log.Println("[WebhookHandler] 'Who' is empty, tx: ", ctx.RawTx.Transaction.Signatures[0])
return
} else {
err := db.Insert(txRow)
if err != nil {
log.Println("[WebhookHandler] Error inserting tx: ", err)
}
}
}