Skip to content

Commit

Permalink
[Pusher] split Transaction Notificaitons onto old and new versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Bozhytskyi committed Jun 18, 2021
1 parent a4bc9b2 commit faf662c
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 1 deletion.
4 changes: 3 additions & 1 deletion types/subscription.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import "strconv"
import (
"strconv"
)

type (
Subscriptions map[string][]string
Expand Down
59 changes: 59 additions & 0 deletions types/v1/subscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package types

import (
"strconv"
)

type (
Subscriptions map[string][]string

SubscriptionOperation string

SubscriptionEvent struct {
Subscriptions Subscriptions `json:"subscriptions"`
Operation SubscriptionOperation `json:"operation"`
}

Subscription struct {
Coin uint `json:"coin"`
Address string `json:"address"`
}

TransactionNotification struct {
Action TransactionType `json:"action"`
Result Tx `json:"result"`
}
)

type Subscriber string

const (
Notifications Subscriber = "notifications"
AddSubscription SubscriptionOperation = "AddSubscription"
DeleteSubscription SubscriptionOperation = "DeleteSubscription"
)

func (v *Subscription) AddressID() string {
return GetAddressID(strconv.Itoa(int(v.Coin)), v.Address)
}

func GetAddressID(coin, address string) string {
return coin + "_" + address
}

func (e *SubscriptionEvent) ParseSubscriptions(s Subscriptions) []Subscription {
subs := make([]Subscription, 0)
for coinStr, perCoin := range s {
coin, err := strconv.Atoi(coinStr)
if err != nil {
continue
}
for _, addr := range perCoin {
subs = append(subs, Subscription{
Coin: uint(coin),
Address: addr,
})
}
}
return subs
}
70 changes: 70 additions & 0 deletions types/v1/subscription_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package types

import (
"sort"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_parseSubscriptions(t *testing.T) {
tests := []struct {
name string
subscriptions SubscriptionEvent
wantSubs []Subscription
}{
{
name: "guid with 1 coin",
subscriptions: SubscriptionEvent{
Subscriptions: Subscriptions{
"0": {"xpub6BpYi6J1GZzfY3yY7DbhLLccF3efQa18nQngM3jaehgtNSoEgk6UtPULpC3oK5oA3trczY8Ld34LFw1USMPfGHwTEizdD5QyGcMyuh2UoBA", "xpub6CYwPfnPJLPquufPkb98coSb3mdy1CgaZrWUtYWGJTJ4VWZUbzH9HLGy7nHpP7DG4UdTkYYpirkTWQSP7pWHsrk24Nos5oYNHpfr4BgPVTL"},
},
},
wantSubs: []Subscription{
{
Coin: 0,
Address: "xpub6BpYi6J1GZzfY3yY7DbhLLccF3efQa18nQngM3jaehgtNSoEgk6UtPULpC3oK5oA3trczY8Ld34LFw1USMPfGHwTEizdD5QyGcMyuh2UoBA",
},
{
Coin: 0,
Address: "xpub6CYwPfnPJLPquufPkb98coSb3mdy1CgaZrWUtYWGJTJ4VWZUbzH9HLGy7nHpP7DG4UdTkYYpirkTWQSP7pWHsrk24Nos5oYNHpfr4BgPVTL",
},
},
},
{
name: "guid with 2 coins",
subscriptions: SubscriptionEvent{
Subscriptions: Subscriptions{
"2": {"zpub6rH4MwgyTmuexAX6HAraks5cKv5BbtmwdLirvnU5845ovUJb4abgjt9DtXK4ZEaToRrNj8dQznuLC6Nka4eMviGMinCVMUxKLpuyddcG9Vc"},
"0": {"xpub6BpYi6J1GZzfY3yY7DbhLLccF3efQa18nQngM3jaehgtNSoEgk6UtPULpC3oK5oA3trczY8Ld34LFw1USMPfGHwTEizdD5QyGcMyuh2UoBA", "xpub6CYwPfnPJLPquufPkb98coSb3mdy1CgaZrWUtYWGJTJ4VWZUbzH9HLGy7nHpP7DG4UdTkYYpirkTWQSP7pWHsrk24Nos5oYNHpfr4BgPVTL"},
},
},
wantSubs: []Subscription{
{
Coin: 2,
Address: "zpub6rH4MwgyTmuexAX6HAraks5cKv5BbtmwdLirvnU5845ovUJb4abgjt9DtXK4ZEaToRrNj8dQznuLC6Nka4eMviGMinCVMUxKLpuyddcG9Vc",
},
{
Coin: 0,
Address: "xpub6BpYi6J1GZzfY3yY7DbhLLccF3efQa18nQngM3jaehgtNSoEgk6UtPULpC3oK5oA3trczY8Ld34LFw1USMPfGHwTEizdD5QyGcMyuh2UoBA",
},
{
Coin: 0,
Address: "xpub6CYwPfnPJLPquufPkb98coSb3mdy1CgaZrWUtYWGJTJ4VWZUbzH9HLGy7nHpP7DG4UdTkYYpirkTWQSP7pWHsrk24Nos5oYNHpfr4BgPVTL",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotSubs := tt.subscriptions.ParseSubscriptions(tt.subscriptions.Subscriptions)
sort.Slice(gotSubs, func(i, j int) bool {
return gotSubs[i].Coin > gotSubs[j].Coin
})
sort.Slice(tt.wantSubs, func(i, j int) bool {
return tt.wantSubs[i].Coin > tt.wantSubs[j].Coin
})
assert.EqualValues(t, tt.wantSubs, gotSubs)
})
}
}
41 changes: 41 additions & 0 deletions types/v1/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package types

import (
"encoding/json"
"fmt"
"math/big"
"strings"
)

type HexNumber big.Int

func (i HexNumber) MarshalJSON() ([]byte, error) {
hexNumber := fmt.Sprintf("\"0x%x\"", (*big.Int)(&i).Uint64())

return []byte(hexNumber), nil
}

func (i *HexNumber) UnmarshalJSON(data []byte) error {
var resultStr string
err := json.Unmarshal(data, &resultStr)
if err != nil {
return err
}

var value *big.Int
if resultStr == "0x" {
value = new(big.Int)
} else {
hex := strings.Replace(resultStr, "0x", "", 1)

var ok bool
value, ok = new(big.Int).SetString(hex, 16)
if !ok {
return fmt.Errorf("could not parse hex value %v", resultStr)
}
}

*i = HexNumber(*value)

return nil
}

0 comments on commit faf662c

Please sign in to comment.