Skip to content

Commit 231ded3

Browse files
committed
Add integration into swap worker for push notifications
1 parent 9a27337 commit 231ded3

File tree

6 files changed

+69
-10
lines changed

6 files changed

+69
-10
lines changed

pkg/code/async/geyser/external_deposit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func processPotentialExternalDepositIntoVm(ctx context.Context, data code_data.P
377377
}
378378

379379
// Best-effort processing for notification back to the user
380-
integration.OnDepositReceived(ctx, ownerAccount, mint, currencyName, uint64(deltaQuarksIntoOmnibus), usdMarketValue)
380+
integration.OnDepositReceived(ctx, ownerAccount, mint, currencyName, usdMarketValue)
381381

382382
return nil
383383
default:

pkg/code/async/geyser/integration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ import (
88

99
// Integration allows for notifications based on events processed by Geyser
1010
type Integration interface {
11-
OnDepositReceived(ctx context.Context, owner, mint *common.Account, currencyName string, quarksReceived uint64, usdMarketValue float64) error
11+
OnDepositReceived(ctx context.Context, owner, mint *common.Account, currencyName string, usdMarketValue float64) error
1212
}

pkg/code/async/swap/integration.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package async_swap
2+
3+
import (
4+
"context"
5+
6+
"github.com/code-payments/code-server/pkg/code/common"
7+
"github.com/code-payments/code-server/pkg/currency"
8+
)
9+
10+
// Integration allows for notifications based on events processed by the swap worker
11+
type Integration interface {
12+
OnSwapFinalized(ctx context.Context, owner, mint *common.Account, currencyName string, region currency.Code, nativeAmount float64) error
13+
}

pkg/code/async/swap/service.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,26 @@ import (
1212
)
1313

1414
type service struct {
15-
log *logrus.Entry
16-
conf *conf
17-
data code_data.Provider
15+
log *logrus.Entry
16+
conf *conf
17+
data code_data.Provider
18+
integration Integration
1819
}
1920

20-
func New(data code_data.Provider, configProvider ConfigProvider) async.Service {
21+
func New(data code_data.Provider, integration Integration, configProvider ConfigProvider) async.Service {
2122
return &service{
22-
log: logrus.StandardLogger().WithField("service", "swap"),
23-
conf: configProvider(),
24-
data: data,
23+
log: logrus.StandardLogger().WithField("service", "swap"),
24+
conf: configProvider(),
25+
data: data,
26+
integration: integration,
2527
}
2628

2729
}
2830

2931
func (p *service) Start(ctx context.Context, interval time.Duration) error {
3032

3133
for _, state := range []swap.State{
34+
swap.StateCreated,
3235
swap.StateFunding,
3336
swap.StateSubmitting,
3437
} {

pkg/code/async/swap/util.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,34 @@ func (p *service) updateBalances(ctx context.Context, record *swap.Record) error
152152
})
153153
}
154154

155+
func (p *service) notifySwapFinalized(ctx context.Context, swapRecord *swap.Record) error {
156+
owner, err := common.NewAccountFromPublicKeyString(swapRecord.Owner)
157+
if err != nil {
158+
return err
159+
}
160+
161+
toMint, err := common.NewAccountFromPublicKeyString(swapRecord.ToMint)
162+
if err != nil {
163+
return err
164+
}
165+
166+
currencyName := common.CoreMintName
167+
if !common.IsCoreMint(toMint) {
168+
currencyMetadataRecord, err := p.data.GetCurrencyMetadata(ctx, toMint.PublicKey().ToBase58())
169+
if err != nil {
170+
return nil
171+
}
172+
currencyName = currencyMetadataRecord.Name
173+
}
174+
175+
fundingIntentRecord, err := p.data.GetIntent(ctx, swapRecord.FundingId)
176+
if err != nil {
177+
return err
178+
}
179+
180+
return p.integration.OnSwapFinalized(ctx, owner, toMint, currencyName, fundingIntentRecord.SendPublicPaymentMetadata.ExchangeCurrency, fundingIntentRecord.SendPublicPaymentMetadata.NativeAmount)
181+
}
182+
155183
func (p *service) markNonceReleasedDueToSubmittedTransaction(ctx context.Context, record *swap.Record) error {
156184
err := p.validateSwapState(record, swap.StateSubmitting)
157185
if err != nil {

pkg/code/async/swap/worker.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ func (p *service) worker(serviceCtx context.Context, state swap.State, interval
7373

7474
func (p *service) handle(ctx context.Context, record *swap.Record) error {
7575
switch record.State {
76+
case swap.StateCreated:
77+
return p.handleStateCreated(ctx, record)
7678
case swap.StateFunding:
7779
return p.handleStateFunding(ctx, record)
7880
case swap.StateSubmitting:
@@ -81,6 +83,14 @@ func (p *service) handle(ctx context.Context, record *swap.Record) error {
8183
return nil
8284
}
8385

86+
func (p *service) handleStateCreated(ctx context.Context, record *swap.Record) error {
87+
if err := p.validateSwapState(record, swap.StateCreated); err != nil {
88+
return err
89+
}
90+
91+
return nil
92+
}
93+
8494
func (p *service) handleStateFunding(ctx context.Context, record *swap.Record) error {
8595
if err := p.validateSwapState(record, swap.StateFunding); err != nil {
8696
return err
@@ -118,7 +128,12 @@ func (p *service) handleStateSubmitting(ctx context.Context, record *swap.Record
118128
if err != nil {
119129
return errors.Wrap(err, "error updating balances")
120130
}
121-
return p.markSwapFinalized(ctx, record)
131+
err = p.markSwapFinalized(ctx, record)
132+
if err != nil {
133+
return errors.Wrap(err, "error marking swap as finalized")
134+
}
135+
136+
go p.notifySwapFinalized(ctx, record)
122137
}
123138
}
124139

0 commit comments

Comments
 (0)