Skip to content

Commit aa73487

Browse files
authored
Support custom push payload with initial currency info navigation (#13)
* Support custom push payload with initial currency info navigation * Run proto validation on custom push payload
1 parent f8cafbb commit aa73487

File tree

7 files changed

+109
-26
lines changed

7 files changed

+109
-26
lines changed

geyser/integration.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ func (i *Integration) OnDepositReceived(ctx context.Context, owner, mint *ocp_co
3737
if ocp_common.IsCoreMint(mint) {
3838
return push.SendUsdfDepositedPush(ctx, i.pusher, userID, usdMarketValue)
3939
}
40-
return push.SendFlipcashCurrencyDepositedPush(ctx, i.pusher, userID, currencyName, usdMarketValue)
40+
protoMint := &commonpb.PublicKey{Value: mint.PublicKey().ToBytes()}
41+
return push.SendFlipcashCurrencyDepositedPush(ctx, i.pusher, userID, protoMint, currencyName, usdMarketValue)
4142
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ go 1.25.0
55
require (
66
firebase.google.com/go/v4 v4.18.0
77
github.com/ReneKroon/ttlcache v1.7.0
8-
github.com/code-payments/flipcash2-protobuf-api v0.2.0
9-
github.com/code-payments/ocp-protobuf-api v0.10.0
10-
github.com/code-payments/ocp-server v0.24.0
8+
github.com/code-payments/flipcash2-protobuf-api v0.3.0
9+
github.com/code-payments/ocp-protobuf-api v0.11.0
10+
github.com/code-payments/ocp-server v0.26.0
1111
github.com/devsisters/go-applereceipt v0.0.0-20240805020915-fa22a0160fc2
1212
github.com/georgysavva/scany/v2 v2.1.4
1313
github.com/google/uuid v1.6.0

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ github.com/cockroachdb/cockroach-go/v2 v2.2.0 h1:/5znzg5n373N/3ESjHF5SMLxiW4RKB0
4040
github.com/cockroachdb/cockroach-go/v2 v2.2.0/go.mod h1:u3MiKYGupPPjkn3ozknpMUpxPaNLTFWAya419/zv6eI=
4141
github.com/code-payments/code-vm-indexer v1.2.0 h1:rSHpBMiT9BKgmKcXg/VIoi/h0t7jNxGx07Qz59m+6Q0=
4242
github.com/code-payments/code-vm-indexer v1.2.0/go.mod h1:vn91YN2qNqb+gGJeZe2+l+TNxVmEEiRHXXnIn2Y40h8=
43-
github.com/code-payments/flipcash2-protobuf-api v0.2.0 h1:5uL9g0D+WyoCP95rg37CJOPA+a8sPOQrKWrfEEIDM+U=
44-
github.com/code-payments/flipcash2-protobuf-api v0.2.0/go.mod h1:s/1pOsb4FTRD+LcvRKGjfmm6ygRS/m1ep34EIW0fuDs=
45-
github.com/code-payments/ocp-protobuf-api v0.10.0 h1:l9Yh3eXdhvgBQS/evg1HYMERXlr7ymASHekAhW/RUmA=
46-
github.com/code-payments/ocp-protobuf-api v0.10.0/go.mod h1:tw6BooY5a8l6CtSZnKOruyKII0W04n89pcM4BizrgG8=
47-
github.com/code-payments/ocp-server v0.24.0 h1:QF7K3Nbrapl2tXx4EgL3xPWpxqCM+gCNiP2+RralSBM=
48-
github.com/code-payments/ocp-server v0.24.0/go.mod h1:+E1VncCtaZGgvM9wKu+qe/AA+gpcbbpu0BXw76vcD4k=
43+
github.com/code-payments/flipcash2-protobuf-api v0.3.0 h1:gz+InymDZKp3n7HyOQCidd80ZmZj784pIPdHaebDs18=
44+
github.com/code-payments/flipcash2-protobuf-api v0.3.0/go.mod h1:s/1pOsb4FTRD+LcvRKGjfmm6ygRS/m1ep34EIW0fuDs=
45+
github.com/code-payments/ocp-protobuf-api v0.11.0 h1:Zq0H3tg+bjTuXF9T6kRXEX8sGXu1NQ0szvUD1MdVs4Q=
46+
github.com/code-payments/ocp-protobuf-api v0.11.0/go.mod h1:tw6BooY5a8l6CtSZnKOruyKII0W04n89pcM4BizrgG8=
47+
github.com/code-payments/ocp-server v0.26.0 h1:0KicwwrwFac+j04mtEngDwGqK8OOEfP7BIko2PWgBb0=
48+
github.com/code-payments/ocp-server v0.26.0/go.mod h1:TZFN0NlGW/q0ipQGYJl1ZMy9pcpG1kNwRwt2GBz6E5E=
4949
github.com/containerd/continuity v0.4.3 h1:6HVkalIp+2u1ZLH1J/pYX2oBVXlJZvh1X1A7bEZ9Su8=
5050
github.com/containerd/continuity v0.4.3/go.mod h1:F6PTNCKepoxEaXLQp3wDAjygEnImnZ/7o4JzpodfroQ=
5151
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=

push/pusher.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@ package push
22

33
import (
44
"context"
5+
"encoding/base64"
6+
"fmt"
57

68
"firebase.google.com/go/v4/messaging"
79
"go.uber.org/zap"
10+
"google.golang.org/protobuf/proto"
811

912
commonpb "github.com/code-payments/flipcash2-protobuf-api/generated/go/common/v1"
13+
pushpb "github.com/code-payments/flipcash2-protobuf-api/generated/go/push/v1"
14+
"github.com/mr-tron/base58"
1015
)
1116

1217
type Pusher interface {
13-
SendBasicPushes(ctx context.Context, title, body string, users ...*commonpb.UserId) error
18+
SendPushes(ctx context.Context, title, body string, customPayload *pushpb.Payload, users ...*commonpb.UserId) error
1419
}
1520

1621
type NoOpPusher struct{}
1722

18-
func (n *NoOpPusher) SendBasicPushes(_ context.Context, _, _ string, _ ...*commonpb.UserId) error {
23+
func (n *NoOpPusher) SendPushes(_ context.Context, _, _ string, _ *pushpb.Payload, _ ...*commonpb.UserId) error {
1924
return nil
2025
}
2126

@@ -41,8 +46,7 @@ func NewFCMPusher(log *zap.Logger, tokens TokenStore, client FCMClient) *FCMPush
4146
}
4247
}
4348

44-
// todo: Some duplicated code, but the existing push per message flow is likely going away anyways. We'll refactor when we get to that.
45-
func (p *FCMPusher) SendBasicPushes(ctx context.Context, title, body string, users ...*commonpb.UserId) error {
49+
func (p *FCMPusher) SendPushes(ctx context.Context, title, body string, customPayload *pushpb.Payload, users ...*commonpb.UserId) error {
4650
if len(users) == 0 {
4751
return nil
4852
}
@@ -64,12 +68,43 @@ func (p *FCMPusher) SendBasicPushes(ctx context.Context, title, body string, use
6468

6569
tokens := extractTokens(pushTokens)
6670

71+
if customPayload == nil {
72+
customPayload = &pushpb.Payload{}
73+
}
74+
75+
err = customPayload.Validate()
76+
if err != nil {
77+
return err
78+
}
79+
80+
marshalledCustomPayload, err := proto.Marshal(customPayload)
81+
if err != nil {
82+
return err
83+
}
84+
encodedCustomPayload := base64.StdEncoding.EncodeToString(marshalledCustomPayload)
85+
86+
customData := map[string]string{
87+
"flipcash_payload": encodedCustomPayload,
88+
}
89+
if customPayload.Navigation != nil {
90+
var targetUrl string
91+
switch typed := customPayload.Navigation.Type.(type) {
92+
case *pushpb.Navigation_CurrencyInfo:
93+
targetUrl = fmt.Sprintf("https://app.flipcash.com/token/%s", base58.Encode(typed.CurrencyInfo.Value))
94+
}
95+
if len(targetUrl) > 0 {
96+
customData["target_url"] = targetUrl
97+
}
98+
}
99+
67100
message := &messaging.MulticastMessage{
68101
Tokens: tokens,
69102
Notification: &messaging.Notification{
70103
Title: title,
71104
Body: body,
72105
},
106+
107+
Data: customData,
73108
}
74109

75110
response, err := p.client.SendEachForMulticast(ctx, message)

push/pushes.go

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"golang.org/x/text/message"
99

1010
commonpb "github.com/code-payments/flipcash2-protobuf-api/generated/go/common/v1"
11+
pushpb "github.com/code-payments/flipcash2-protobuf-api/generated/go/push/v1"
1112

1213
"github.com/code-payments/flipcash2-server/localization"
1314
ocp_currency "github.com/code-payments/ocp-server/currency"
15+
ocp_common "github.com/code-payments/ocp-server/ocp/common"
1416
)
1517

1618
var (
@@ -24,34 +26,62 @@ func SendUsdfDepositedPush(ctx context.Context, pusher Pusher, user *commonpb.Us
2426
"$%.2f was added to your USD Reserves",
2527
usdMarketValue,
2628
)
27-
return pusher.SendBasicPushes(ctx, title, body, user)
29+
customPayload := &pushpb.Payload{
30+
Navigation: &pushpb.Navigation{
31+
Type: &pushpb.Navigation_CurrencyInfo{
32+
CurrencyInfo: &commonpb.PublicKey{Value: ocp_common.CoreMintAccount.PublicKey().ToBytes()},
33+
},
34+
},
35+
}
36+
return pusher.SendPushes(ctx, title, body, customPayload, user)
2837
}
2938

30-
func SendFlipcashCurrencyDepositedPush(ctx context.Context, pusher Pusher, user *commonpb.UserId, currencyName string, usdMarketValue float64) error {
39+
func SendFlipcashCurrencyDepositedPush(ctx context.Context, pusher Pusher, user *commonpb.UserId, mint *commonpb.PublicKey, currencyName string, usdMarketValue float64) error {
3140
title := fmt.Sprintf("%s Now Available", currencyName)
3241
body := amountPrinter.Sprintf(
3342
"$%.2f of %s was added to your Flipcash wallet",
3443
usdMarketValue,
3544
currencyName,
3645
)
37-
return pusher.SendBasicPushes(ctx, title, body, user)
46+
customPayload := &pushpb.Payload{
47+
Navigation: &pushpb.Navigation{
48+
Type: &pushpb.Navigation_CurrencyInfo{
49+
CurrencyInfo: mint,
50+
},
51+
},
52+
}
53+
return pusher.SendPushes(ctx, title, body, customPayload, user)
3854
}
3955

40-
func SendFlipcashCurrencyBoughtPush(ctx context.Context, pusher Pusher, user *commonpb.UserId, currencyName string, region ocp_currency.Code, nativeAmount float64) error {
56+
func SendFlipcashCurrencyBoughtPush(ctx context.Context, pusher Pusher, user *commonpb.UserId, mint *commonpb.PublicKey, currencyName string, region ocp_currency.Code, nativeAmount float64) error {
4157
title := fmt.Sprintf("%s Successfully Purchased", currencyName)
4258
body := amountPrinter.Sprintf(
4359
"%s of %s was added to your Flipcash wallet",
4460
localization.FormatFiat(defaultLocale, region, nativeAmount),
4561
currencyName,
4662
)
47-
return pusher.SendBasicPushes(ctx, title, body, user)
63+
customPayload := &pushpb.Payload{
64+
Navigation: &pushpb.Navigation{
65+
Type: &pushpb.Navigation_CurrencyInfo{
66+
CurrencyInfo: mint,
67+
},
68+
},
69+
}
70+
return pusher.SendPushes(ctx, title, body, customPayload, user)
4871
}
4972

50-
func SendFlipcashCurrencySoldPush(ctx context.Context, pusher Pusher, user *commonpb.UserId, currencyName string, region ocp_currency.Code, nativeAmount float64) error {
73+
func SendFlipcashCurrencySoldPush(ctx context.Context, pusher Pusher, user *commonpb.UserId, mint *commonpb.PublicKey, currencyName string, region ocp_currency.Code, nativeAmount float64) error {
5174
title := fmt.Sprintf("%s Successfully Sold", currencyName)
5275
body := amountPrinter.Sprintf(
5376
"%s was added to your USD Reserves",
5477
localization.FormatFiat(defaultLocale, region, nativeAmount),
5578
)
56-
return pusher.SendBasicPushes(ctx, title, body, user)
79+
customPayload := &pushpb.Payload{
80+
Navigation: &pushpb.Navigation{
81+
Type: &pushpb.Navigation_CurrencyInfo{
82+
CurrencyInfo: mint,
83+
},
84+
},
85+
}
86+
return pusher.SendPushes(ctx, title, body, customPayload, user)
5787
}

push/tests/pusher.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package tests
22

33
import (
44
"context"
5+
"encoding/base64"
56
"fmt"
67
"testing"
78

89
"firebase.google.com/go/v4/messaging"
910
"github.com/stretchr/testify/require"
1011
"go.uber.org/zap"
12+
"google.golang.org/protobuf/proto"
1113

1214
commonpb "github.com/code-payments/flipcash2-protobuf-api/generated/go/common/v1"
1315
pushpb "github.com/code-payments/flipcash2-protobuf-api/generated/go/push/v1"
@@ -58,7 +60,19 @@ func testFCMPusher_SendBasicPushes(t *testing.T, store push.TokenStore) {
5860

5961
targetUsers := users[:3]
6062

61-
require.NoError(t, pusher.SendBasicPushes(ctx, "title", "body", targetUsers...))
63+
customPayload := &pushpb.Payload{
64+
Navigation: &pushpb.Navigation{
65+
Type: &pushpb.Navigation_CurrencyInfo{
66+
CurrencyInfo: &commonpb.PublicKey{Value: make([]byte, 32)},
67+
},
68+
},
69+
}
70+
71+
marshalledCustomPayload, err := proto.Marshal(customPayload)
72+
require.NoError(t, err)
73+
expectedEncodedCustomPayload := base64.StdEncoding.EncodeToString(marshalledCustomPayload)
74+
75+
require.NoError(t, pusher.SendPushes(ctx, "title", "body", customPayload, targetUsers...))
6276

6377
require.NotNil(t, fcmClient.sentMessage)
6478

@@ -71,5 +85,7 @@ func testFCMPusher_SendBasicPushes(t *testing.T, store push.TokenStore) {
7185
"token2_1", "token2_2",
7286
}
7387
require.ElementsMatch(t, expectedTokens, fcmClient.sentMessage.Tokens)
74-
require.Empty(t, fcmClient.sentMessage.Data)
88+
require.Len(t, fcmClient.sentMessage.Data, 2)
89+
require.Equal(t, expectedEncodedCustomPayload, fcmClient.sentMessage.Data["flipcash_payload"])
90+
require.Equal(t, "https://app.flipcash.com/token/11111111111111111111111111111111", fcmClient.sentMessage.Data["target_url"])
7591
}

swap/integration.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ func NewIntegration(accounts account.Store, pusher push.Pusher) ocp_swap.Integra
2424
}
2525
}
2626

27-
func (i *Integration) OnSwapFinalized(ctx context.Context, owner *ocp_common.Account, isBuy bool, currencyName string, region ocp_currency.Code, amountReceived float64) error {
27+
func (i *Integration) OnSwapFinalized(ctx context.Context, owner *ocp_common.Account, isBuy bool, mint *ocp_common.Account, currencyName string, region ocp_currency.Code, amountReceived float64) error {
2828
userID, err := i.accounts.GetUserId(ctx, &commonpb.PublicKey{Value: owner.PublicKey().ToBytes()})
2929
if err != nil {
3030
return err
3131
}
3232

33+
protoMint := &commonpb.PublicKey{Value: mint.PublicKey().ToBytes()}
3334
if isBuy {
34-
return push.SendFlipcashCurrencyBoughtPush(ctx, i.pusher, userID, currencyName, region, amountReceived)
35+
return push.SendFlipcashCurrencyBoughtPush(ctx, i.pusher, userID, protoMint, currencyName, region, amountReceived)
3536
}
36-
return push.SendFlipcashCurrencySoldPush(ctx, i.pusher, userID, currencyName, region, amountReceived)
37+
return push.SendFlipcashCurrencySoldPush(ctx, i.pusher, userID, protoMint, currencyName, region, amountReceived)
3738
}

0 commit comments

Comments
 (0)