-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnapshot.go
112 lines (99 loc) · 2.79 KB
/
snapshot.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
package ant
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"time"
bot "github.com/MixinNetwork/bot-api-go-client"
)
const (
PollInterval = 100 * time.Millisecond
CheckpointMixinNetworkSnapshots = "exchange-checkpoint-mixin-network-snapshots"
)
type Asset struct {
AssetId string `json:"asset_id" gorm:"type:varchar(36)"`
}
type Snapshot struct {
SnapshotId string `json:"snapshot_id" gorm:"primary_key;type:varchar(36)"`
Amount string `json:"amount" gorm:"type:varchar(36)"`
TraceId string `json:"trace_id" gorm:"type:varchar(36)"`
UserId string `json:"user_id" gorm:"type:varchar(36)"`
OpponentId string `json:"opponent_id" gorm:"type:varchar(36)"`
Data string `json:"data" gorm:"type:varchar(255)"`
CreatedAt time.Time `json:"created_at" gorm:"type:timestamp"`
Asset `json:"asset" gorm:"type:varchar(36)"`
}
func (Snapshot) TableName() string {
return "ant_snapshots"
}
func (ex *Ant) requestMixinNetwork(ctx context.Context, checkpoint time.Time, limit int) ([]*Snapshot, error) {
uri := fmt.Sprintf("/network/snapshots?offset=%s&order=ASC&limit=%d", checkpoint.Format(time.RFC3339Nano), limit)
token, err := bot.SignAuthenticationToken(ClientId, SessionId, PrivateKey, "GET", uri, "")
if err != nil {
return nil, err
}
body, err := bot.Request(ctx, "GET", uri, nil, token)
if err != nil {
return nil, err
}
var resp struct {
Data []*Snapshot `json:"data"`
Error string `json:"error"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
if resp.Error != "" {
return nil, errors.New(resp.Error)
}
return resp.Data, nil
}
func (ex *Ant) PollMixinNetwork(ctx context.Context) {
const limit = 500
checkpoint := time.Now().UTC()
for {
snapshots, err := ex.requestMixinNetwork(ctx, checkpoint, limit)
if err != nil {
log.Println("PollMixinNetwork ERROR", err)
time.Sleep(PollInterval)
continue
}
for _, s := range snapshots {
if ex.snapshots[s.SnapshotId] {
continue
}
ex.ensureProcessSnapshot(ctx, s)
checkpoint = s.CreatedAt
ex.snapshots[s.SnapshotId] = true
}
if len(snapshots) < limit {
time.Sleep(PollInterval)
}
}
}
func (ex *Ant) ensureProcessSnapshot(ctx context.Context, s *Snapshot) {
for {
err := ex.processSnapshot(ctx, s)
if err == nil {
break
}
log.Println("ensureProcessSnapshot", err)
time.Sleep(100 * time.Millisecond)
}
}
func (ex *Ant) processSnapshot(ctx context.Context, s *Snapshot) error {
if len(s.OpponentId) == 0 || s.Asset.AssetId == CNB {
return nil
}
if err := ex.HandleSnapshot(ctx, s); err != nil {
log.Println(err)
return err
}
if err := Database(ctx).FirstOrCreate(s).Error; err != nil {
return err
}
return nil
}