-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathswap.go
145 lines (119 loc) · 3.84 KB
/
swap.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
package async_account
import (
"context"
"errors"
"sync"
"time"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/sirupsen/logrus"
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
"github.com/code-payments/code-server/pkg/code/balance"
"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/account"
"github.com/code-payments/code-server/pkg/code/push"
"github.com/code-payments/code-server/pkg/metrics"
"github.com/code-payments/code-server/pkg/retry"
"github.com/code-payments/code-server/pkg/usdc"
)
const (
swapPushRetryThreshold = time.Minute
minUsdcSwapBalance = usdc.QuarksPerUsdc / 100 // $0.01 USD
)
func (p *service) swapRetryWorker(serviceCtx context.Context, interval time.Duration) error {
delay := interval
err := retry.Loop(
func() (err error) {
time.Sleep(delay)
nr := serviceCtx.Value(metrics.NewRelicContextKey).(*newrelic.Application)
m := nr.StartTransaction("async__account_service__handle_swap_retry")
defer m.End()
tracedCtx := newrelic.NewContext(serviceCtx, m)
records, err := p.data.GetPrioritizedAccountInfosRequiringSwapRetry(tracedCtx, swapPushRetryThreshold, 100)
if err == account.ErrAccountInfoNotFound {
return nil
} else if err != nil {
m.NoticeError(err)
return err
}
var wg sync.WaitGroup
for _, record := range records {
wg.Add(1)
go func(record *account.Record) {
defer wg.Done()
err := p.maybeTriggerAnotherSwap(tracedCtx, record)
if err != nil {
m.NoticeError(err)
}
}(record)
}
wg.Wait()
return nil
},
retry.NonRetriableErrors(context.Canceled),
)
return err
}
// todo: Handle the case where there are no push tokens
func (p *service) maybeTriggerAnotherSwap(ctx context.Context, accountInfoRecord *account.Record) error {
log := p.log.WithFields(logrus.Fields{
"method": "maybeTriggerAnotherSwap",
"owner_account": accountInfoRecord.OwnerAccount,
"token_account": accountInfoRecord.TokenAccount,
})
if accountInfoRecord.AccountType != commonpb.AccountType_SWAP {
log.Trace("skipping account that isn't a swap account")
return errors.New("expected a swap account")
}
ownerAccount, err := common.NewAccountFromPublicKeyString(accountInfoRecord.OwnerAccount)
if err != nil {
log.WithError(err).Warn("invalid owner account")
return err
}
tokenAccount, err := common.NewAccountFromPublicKeyString(accountInfoRecord.TokenAccount)
if err != nil {
log.WithError(err).Warn("invalid token account")
return err
}
balance, _, err := balance.CalculateFromBlockchain(ctx, p.data, tokenAccount)
if err != nil {
log.WithError(err).Warn("failure getting token account balance")
return err
}
// Mark the swap as successful if the account is left with only dust
if balance < minUsdcSwapBalance {
err = markSwapRetrySuccessful(ctx, p.data, accountInfoRecord)
if err != nil {
log.WithError(err).Warn("failure updating account info record")
}
return err
}
err = push.SendTriggerSwapRpcPushNotification(
ctx,
p.data,
p.pusher,
ownerAccount,
)
if err != nil {
log.WithError(err).Warn("failure sending push notification")
}
err = markSwapRetriedNow(ctx, p.data, accountInfoRecord)
if err != nil {
log.WithError(err).Warn("failure updating account info record")
}
return err
}
func markSwapRetrySuccessful(ctx context.Context, data code_data.Provider, record *account.Record) error {
if !record.RequiresSwapRetry {
return nil
}
record.RequiresSwapRetry = false
return data.UpdateAccountInfo(ctx, record)
}
func markSwapRetriedNow(ctx context.Context, data code_data.Provider, record *account.Record) error {
if !record.RequiresSwapRetry {
return nil
}
record.LastSwapRetryAt = time.Now()
return data.UpdateAccountInfo(ctx, record)
}