-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
265 lines (234 loc) · 7.48 KB
/
main.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"bytes"
"context"
"flag"
"fmt"
"log"
"math/big"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/ubiq/bishop-discord/nft"
ERC721OrdersFeature "github.com/ubiq/dikembe-discord/contracts"
ERC20 "github.com/ubiq/dikembe-discord/contracts/ERC20"
ubiq "github.com/ubiq/go-ubiq/v7"
"github.com/ubiq/go-ubiq/v7/accounts/abi"
"github.com/ubiq/go-ubiq/v7/common"
"github.com/ubiq/go-ubiq/v7/core/types"
"github.com/ubiq/go-ubiq/v7/ethclient"
"github.com/ubiq/go-ubiq/v7/params"
)
var (
rpcURL string
wsRPCURL string
webhookID string
webhookToken string
avatarUsername string
avatarURL string
lastTXID string
// 0x address
address string = "0x19aaD856cE8c4C7e813233b21d56dA97796cC052"
tokenMap = map[string]string{
"0x1e1628A35C82169F876F97C9CE5B6533895c2B22": "CHIMP",
"0xc3D585288D35215754e970A23DD8DE37a08e71B7": "Cryptsters",
"0x0e2fbBA88C5E526f5160Af1b9Ad79a20130b2216": "GB89",
"0x81f1a6e026d49c2260a8D6D8e14Bca1628c1Df43": "nCeption",
}
)
func init() {
flag.StringVar(&rpcURL, "rpcURL", "http://127.0.0.1:8588", "RPC URL")
flag.StringVar(&wsRPCURL, "wsRPCURL", "ws://127.0.0.1:8589", "WS RPC URL")
flag.StringVar(&webhookID, "webhookID", "", "Webhook ID")
flag.StringVar(&webhookToken, "webhookToken", "", "Webhook Token")
flag.StringVar(&avatarUsername, "avatarUsername", "Jawa", "Avatar username")
flag.StringVar(&avatarURL, "avatarURL", "https://i.pinimg.com/originals/3e/6f/39/3e6f39b3d264bbbf6479553383adc905.jpg", "Avatar image URL")
flag.Parse()
}
func main() {
clientWS, err := ethclient.Dial(wsRPCURL)
if err != nil {
log.Fatalln(err)
}
defer clientWS.Close()
clientRPC, err := ethclient.Dial(rpcURL)
if err != nil {
log.Fatal(err)
}
defer clientRPC.Close()
subch := make(chan types.Log)
go func() {
for i := 0; ; i++ {
if i > 0 {
time.Sleep(2 * time.Second)
}
subscribeFilterLogs(clientWS, subch)
}
}()
erc721OrderFilled := common.HexToHash("0x50273fa02273cceea9cf085b42de5c8af60624140168bd71357db833535877af")
erc721OrderPreSigned := common.HexToHash("0x8c5d0c41fb16a7317a6c55ff7ba93d9d74f79e434fefa694e50d6028afbfa3f0")
erc20Transfer := common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")
contractAbi, err := abi.JSON(strings.NewReader(string(ERC721OrdersFeature.ERC721OrdersFeatureMetaData.ABI)))
if err != nil {
log.Fatal(err)
}
contractERC20Abi, err := abi.JSON(strings.NewReader(string(ERC20.ERC20ABI)))
if err != nil {
log.Fatal(err)
}
for vLog := range subch {
if len(vLog.Topics) == 0 {
continue
}
if vLog.TxHash.String() == lastTXID {
log.Println("Duplicate TX: ", lastTXID)
continue
}
switch vLog.Topics[0].Hex() {
case erc721OrderFilled.Hex():
var event ERC721OrdersFeature.ERC721OrdersFeatureERC721OrderFilled
err := contractAbi.UnpackIntoInterface(&event, "ERC721OrderFilled", vLog.Data)
if err != nil {
log.Fatal(err)
}
// Query the blockchain for the amount sent as event log does not show the Fees
tx, _, err := clientRPC.TransactionByHash(context.Background(), vLog.TxHash)
if err != nil {
log.Fatal(err)
}
value := big.NewInt(0)
valueString := ""
if len(tx.Value().Bits()) == 0 {
// WUBQ handling
rx, _ := clientRPC.TransactionReceipt(context.Background(), vLog.TxHash)
for _, rxLog := range rx.Logs {
switch rxLog.Topics[0].Hex() {
case erc20Transfer.Hex():
var event ERC20.ERC20Transfer
if len(rxLog.Topics) == 4 {
continue
}
err := contractERC20Abi.UnpackIntoInterface(&event, "Transfer", rxLog.Data)
if err != nil {
log.Println("Unpack error: ", err)
continue
}
value.Add(value, event.Value)
}
}
valueString = fmt.Sprintf("%.5f WUBQ", weiToEther(value))
} else {
// UBQ
valueString = fmt.Sprintf("%.5f UBQ", weiToEther(tx.Value()))
}
msg := fmt.Sprintf("**Seller:** %s\n**Buyer:** %s\n**Price:** %s",
shortAddress(event.Maker), shortAddress(event.Taker), valueString)
title := fmt.Sprintf("🤑 SALE! - %s #%d",
tokenMap[event.Erc721Token.String()], event.Erc721TokenId)
webhookExecuteTradeNFTEvent(msg, vLog.TxHash.String(), title,
tokenMap[event.Erc721Token.String()], event.Erc721TokenId)
lastTXID = vLog.TxHash.String()
case erc721OrderPreSigned.Hex():
var event ERC721OrdersFeature.ERC721OrdersFeatureERC721OrderPreSigned
err := contractAbi.UnpackIntoInterface(&event, "ERC721OrderPreSigned", vLog.Data)
if err != nil {
log.Fatal(err)
}
// Process Fee
fees := big.NewInt(0)
erc20TokenAmount := big.NewInt(0)
if len(event.Fees) > 0 {
for _, fee := range event.Fees {
fees.Add(fees, fee.Amount)
}
}
erc20TokenAmount.Add(fees, event.Erc20TokenAmount)
// Direction
emojiVerb := "🫰 LIST!"
if event.Direction == 1 {
emojiVerb = "🫴 OFFER!"
}
msg := fmt.Sprintf("**Seller:** %s\n**Price:** %.5f UBQ",
shortAddress(event.Maker), weiToEther(erc20TokenAmount))
title := fmt.Sprintf("%s - %s #%d",
emojiVerb, tokenMap[event.Erc721Token.String()], event.Erc721TokenId)
webhookExecuteTradeNFTEvent(msg, vLog.TxHash.String(), title,
tokenMap[event.Erc721Token.String()], event.Erc721TokenId)
lastTXID = vLog.TxHash.String()
}
}
}
func subscribeFilterLogs(client *ethclient.Client, subch chan types.Log) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
contractAddress := common.HexToAddress(address)
query := ubiq.FilterQuery{
Addresses: []common.Address{
contractAddress,
},
}
// Subscribe to log events
sub, err := client.SubscribeFilterLogs(ctx, query, subch)
if err != nil {
log.Println("subscribe error:", err)
return
}
// The connection is established now.
// Update the channel
var logs types.Log
subch <- logs
// The subscription will deliver events to the channel. Wait for the
// subscription to end for any reason, then loop around to re-establish
// the connection.
log.Println("connection lost: ", <-sub.Err())
}
func webhookExecuteTradeNFTEvent(msg string, txid string, title string, token string, tokenID *big.Int) {
titleURL := fmt.Sprintf("https://ubiqscan.io/tx/%s", txid)
var iNFT nft.NFT
switch token {
case "CHIMP":
iNFT = nft.HandleChimp(rpcURL, tokenID)
case "Cryptsters":
titleURL = fmt.Sprintf("https://jawacamp.ubiqsmart.com/collections/0xc3d585288d35215754e970a23dd8de37a08e71b7/%s", tokenID)
case "GB89":
iNFT = nft.HandleGB89(rpcURL, tokenID)
case "nCeption":
iNFT = nft.HandleNception(rpcURL, tokenID)
}
session, _ := discordgo.New("")
msgembed := discordgo.MessageEmbed{
URL: titleURL,
Type: discordgo.EmbedTypeLink,
Title: title,
Description: msg,
Color: 16750848,
Image: &discordgo.MessageEmbedImage{
URL: "attachment://output.png",
},
}
attachment := discordgo.File{
Name: "output.png",
ContentType: "image/png",
Reader: bytes.NewReader(iNFT.Picture),
}
webhook := &discordgo.WebhookParams{
Username: avatarUsername,
AvatarURL: avatarURL,
Embeds: []*discordgo.MessageEmbed{&msgembed},
Files: []*discordgo.File{&attachment},
}
_, err := session.WebhookExecute(webhookID, webhookToken, true, webhook)
if err != nil {
log.Println(err)
}
}
func weiToEther(wei *big.Int) *big.Float {
if len(wei.Bits()) == 0 {
return nil
}
return new(big.Float).Quo(new(big.Float).SetInt(wei), big.NewFloat(params.Ether))
}
func shortAddress(a common.Address) string {
aS := a.String()
return fmt.Sprintf("%s...%s", aS[:4], aS[len(aS)-4:])
}