-
Notifications
You must be signed in to change notification settings - Fork 10
/
webhook.go
276 lines (248 loc) · 8.19 KB
/
webhook.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
266
267
268
269
270
271
272
273
274
275
276
package impl
import (
"bytes"
"context"
"encoding/json"
"fmt"
"html/template"
"net/http"
"net/url"
"strings"
"time"
"github.com/ethereum/go-ethereum/common"
logger "github.com/rs/zerolog/log"
"github.com/textileio/go-tableland/internal/tableland"
"github.com/textileio/go-tableland/pkg/eventprocessor"
"github.com/textileio/go-tableland/pkg/tables"
)
// webhookTemplate is the template used to generate the webhook content.
const contentTemplate = `
{{ if .Error }}
**Error processing Tableland event:**
Chain ID: [{{ .ChainID }}]({{ .TBLDocsURL }})
Block number: {{ .BlockNumber }}
Transaction hash: [{{ .TxnHash }}]({{ .TxnURL }})
Table IDs: {{ .TableIDs }}
Error: **{{ .Error }}**
Error event index: {{ .ErrorEventIdx }}
{{ else }}
**Tableland event processed successfully:**
Chain ID: [{{ .ChainID }}]({{ .TBLDocsURL }})
Block number: {{ .BlockNumber }}
Transaction hash: [{{ .TxnHash }}]({{ .TxnURL }})
Table IDs: {{ .TableIDs }}
{{ end }}
`
var webhookLogger = logger.With().Str("component", "webhook").Logger()
// Common function to send the webhook request.
func sendWebhookRequest(ctx context.Context, url string, body interface{}) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
postData, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("marshaling webhook JSON: %s", err)
}
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(postData))
if err != nil {
return fmt.Errorf("creating HTTP request: %s", err)
}
req.Header.Set("Content-Type", "application/json")
httpClient := &http.Client{
Timeout: 5 * time.Second,
}
resp, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("executing webhook: %s", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
webhookLogger.Error().Err(err).Msg("closing")
}
}()
if resp.StatusCode >= 400 {
return fmt.Errorf("webhook request failed with status code: %d", resp.StatusCode)
}
return nil
}
type chain struct {
ID int64
Name string
ContractAddr common.Address
TBLDocsURL string
BlockExplorerURL string
SupportsNFTView bool
}
var chains = map[tableland.ChainID]chain{
// Ethereum
1: {
ContractAddr: common.HexToAddress("0x012969f7e3439a9B04025b5a049EB9BAD82A8C12"),
TBLDocsURL: "https://docs.tableland.xyz/fundamentals/chains/ethereum",
BlockExplorerURL: "https://etherscan.io",
SupportsNFTView: true,
},
// Optimism
10: {
ContractAddr: common.HexToAddress("0xfad44BF5B843dE943a09D4f3E84949A11d3aa3e6"),
TBLDocsURL: "https://docs.tableland.xyz/fundamentals/chains/optimism",
BlockExplorerURL: "https://optimistic.etherscan.io",
SupportsNFTView: false,
},
// Polygon
137: {
ContractAddr: common.HexToAddress("0x5c4e6A9e5C1e1BF445A062006faF19EA6c49aFeA"),
TBLDocsURL: "https://docs.tableland.xyz/fundamentals/chains/polygon",
BlockExplorerURL: "https://polygonscan.com",
SupportsNFTView: false,
},
// Arbitrum One
42161: {
ContractAddr: common.HexToAddress("0x9aBd75E8640871A5a20d3B4eE6330a04c962aFfd"),
TBLDocsURL: "https://docs.tableland.xyz/fundamentals/chains/arbitrum",
BlockExplorerURL: "https://arbiscan.io",
SupportsNFTView: false,
},
// Arbitrum Nova
42170: {
ContractAddr: common.HexToAddress("0x1a22854c5b1642760a827f20137a67930ae108d2"),
TBLDocsURL: "https://docs.tableland.xyz/fundamentals/chains/arbitrum",
BlockExplorerURL: "https://nova.arbiscan.io",
SupportsNFTView: false,
},
// Filecoin
314: {
ContractAddr: common.HexToAddress("0x59EF8Bf2d6c102B4c42AEf9189e1a9F0ABfD652d"),
TBLDocsURL: "https://docs.tableland.xyz/fundamentals/chains/Filecoin",
BlockExplorerURL: "https://filfox.info/en",
SupportsNFTView: false,
},
// Goerli
5: {
ContractAddr: common.HexToAddress("0xDA8EA22d092307874f30A1F277D1388dca0BA97a"),
TBLDocsURL: "https://docs.tableland.xyz/fundamentals/chains/ethereum",
BlockExplorerURL: "https://goerli.etherscan.io",
SupportsNFTView: true,
},
// Sepolia
11155111: {
ContractAddr: common.HexToAddress("0xc50C62498448ACc8dBdE43DA77f8D5D2E2c7597D"),
TBLDocsURL: "https://docs.tableland.xyz/fundamentals/chains/ethereum",
BlockExplorerURL: "https://sepolia.etherscan.io",
SupportsNFTView: true,
},
// Optimism Goerli
420: {
ContractAddr: common.HexToAddress("0xC72E8a7Be04f2469f8C2dB3F1BdF69A7D516aBbA"),
TBLDocsURL: "https://docs.tableland.xyz/fundamentals/chains/optimism",
BlockExplorerURL: "https://blockscout.com/optimism/goerli",
SupportsNFTView: false,
},
// Arbitrum Goerli
421613: {
ContractAddr: common.HexToAddress("0x033f69e8d119205089Ab15D340F5b797732f646b"),
TBLDocsURL: "https://docs.tableland.xyz/fundamentals/chains/arbitrum",
BlockExplorerURL: "https://goerli-rollup-explorer.arbitrum.io",
SupportsNFTView: false,
},
// Filecoin Calibration
314159: {
ContractAddr: common.HexToAddress("0x030BCf3D50cad04c2e57391B12740982A9308621"),
TBLDocsURL: "https://docs.tableland.xyz/fundamentals/chains/Filecoin",
BlockExplorerURL: "https://calibration.filfox.info/en",
SupportsNFTView: false,
},
// Polygon Mumbai
80001: {
ContractAddr: common.HexToAddress("0x4b48841d4b32C4650E4ABc117A03FE8B51f38F68"),
TBLDocsURL: "https://docs.tableland.xyz/fundamentals/chains/polygon",
BlockExplorerURL: "https://mumbai.polygonscan.com",
SupportsNFTView: false,
},
}
type webhookContentData struct {
ChainID tableland.ChainID
TBLDocsURL string
BlockNumber int64
TxnHash string
TxnURL string
TableIDs string
Error *string
ErrorEventIdx *int
}
// getTableNFTViews returns the NFT views for the given table IDs.
func getTableNFTViews(tableIDs tables.TableIDs, chainID tableland.ChainID) string {
var tableNFTURLs []string
ch := chains[chainID]
for _, tableID := range tableIDs {
if !ch.SupportsNFTView {
tableNFTURLs = append(tableNFTURLs, tableID.String())
} else {
blockExplorerURL := ch.BlockExplorerURL
contractAddr := ch.ContractAddr
tableNFTURLs = append(tableNFTURLs,
fmt.Sprintf("[%s](%s/nft/%s/%s)", tableID.String(),
blockExplorerURL, contractAddr, tableID.String()))
}
}
return strings.Join(tableNFTURLs, ", ")
}
// Content function to return the formatted content for the webhook.
func content(r eventprocessor.Receipt) (string, error) {
var c bytes.Buffer
tmpl, err := template.New("content").Parse(contentTemplate)
if err != nil {
return "", fmt.Errorf("failed to parse template: %v", err)
}
txnURL := chains[r.ChainID].BlockExplorerURL + "/tx/" + r.TxnHash
docsURL := chains[r.ChainID].TBLDocsURL
err = tmpl.Execute(&c, webhookContentData{
ChainID: r.ChainID,
TBLDocsURL: docsURL,
BlockNumber: r.BlockNumber,
TxnHash: r.TxnHash,
TxnURL: txnURL,
TableIDs: getTableNFTViews(r.TableIDs, r.ChainID),
Error: r.Error,
ErrorEventIdx: r.ErrorEventIdx,
})
if err != nil {
return "", fmt.Errorf("failed to execute template: %v", err)
}
return c.String(), nil
}
// Webhook interface for sending webhooks to different services such as IFTTT or Discord etc.
type Webhook interface {
Send(ctx context.Context, content eventprocessor.Receipt) error
}
// DiscordWebhook struct.
type DiscordWebhook struct {
// URL is the webhook URL.
URL string
// WHData represents the webhook data that
// should be JSON marshaled before sending as the POST body.
WHData struct {
Content string `json:"content"`
}
}
// Send method formats the receipt as Webhook Data for Discord and Sends it.
func (w *DiscordWebhook) Send(ctx context.Context, r eventprocessor.Receipt) error {
whContent, err := content(r)
if err != nil {
return fmt.Errorf("failed to get webhook content: %v", err)
}
// Discord requres that the data should be placed in the "content" field.
w.WHData.Content = whContent
return sendWebhookRequest(ctx, w.URL, w.WHData)
}
// NewWebhook function to create a new webhook.
func NewWebhook(urlStr string) (Webhook, error) {
urlObject, err := url.Parse(urlStr)
if err != nil {
return nil, fmt.Errorf("invalid webhook url: %s", err)
}
if urlObject.Hostname() == "discord.com" {
return &DiscordWebhook{
URL: urlObject.String(),
}, nil
}
return nil, fmt.Errorf("invalid webhook url")
}