This repository has been archived by the owner on Dec 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaymentListener.go
114 lines (98 loc) · 2.63 KB
/
PaymentListener.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
package main
import (
"encoding/json"
"fmt"
"github.com/streadway/amqp"
"os"
)
func failOnError(err error, msg string) {
if err != nil {
panic(fmt.Sprintf("%s: %s", msg, err))
}
}
func main() {
fmt.Println("Entering PaymentListner")
conn, err := amqp.Dial(os.Getenv("AMQPConn"))
failOnError(err, "Failed to connect to RabbitMQ")
defer conn.Close()
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer ch.Close()
err = ch.ExchangeDeclare(
"stellar", // name
"direct", // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
nil, // arguments
)
failOnError(err, "Failed to declare an exchange")
q, err := ch.QueueDeclare(
"client1", // name
true, // durable
false, // delete when usused
true, // exclusive
false, // no-wait
nil, // arguments
)
failOnError(err, "Failed to declare a queue")
err = ch.QueueBind(
q.Name, // queue name
"BlockchainListener", // routing key
"stellar", // exchange
false,
nil)
failOnError(err, "Failed to bind a queue")
msgs, err := ch.Consume(
q.Name, // queue
"", // consumer
false, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
failOnError(err, "Failed to register a consumer")
forever := make(chan bool)
go func() {
for d := range msgs {
var ledger Ledger
json.Unmarshal(d.Body, &ledger)
for i := range ledger.Embedded.Record {
if ledger.Embedded.Record[i].AssetType == "native" {
ledger.Embedded.Record[i].AssetCode = "XLM"
} else {
ledger.Embedded.Record[i].AssetCode = fmt.Sprintf("%v.%v", ledger.Embedded.Record[i].AssetCode, ledger.Embedded.Record[i].AssetIssuer)
}
if ledger.Embedded.Record[i].Type == "payment" {
payment := Payment{
Currency: ledger.Embedded.Record[i].AssetCode,
Address: ledger.Embedded.Record[i].To,
Amount: ledger.Embedded.Record[i].Amount,
Hash: ledger.Embedded.Record[i].Hash,
}
paymentJSON, err := json.Marshal(payment)
if err != nil {
fmt.Println(err)
return
}
err = ch.Publish(
"stellar", // exchange
"payments", // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
DeliveryMode: amqp.Persistent,
ContentType: "text/plain",
Body: []byte(paymentJSON),
})
fmt.Println(" [x] Sent %s", payment.String())
failOnError(err, "Failed to publish a message")
}
}
}
}()
fmt.Println(" [*] Waiting for messages. To exit press CTRL+C")
<-forever
}