mirror of
https://github.com/grassrootseconomics/cic-custodial.git
synced 2026-05-26 21:26:19 +02:00
refactor: remove unecessary publisher component
* clients should now rely on the CHAIn.* subjects
This commit is contained in:
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/grassrootseconomics/celoutils"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/keystore"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/nonce"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/pub"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/w3-celo-patch"
|
||||
@@ -27,7 +26,6 @@ type (
|
||||
LockProvider *redislock.Client
|
||||
Noncestore nonce.Noncestore
|
||||
PgStore store.Store
|
||||
Pub *pub.Pub
|
||||
RedisClient *redis.Client
|
||||
RegistryAddress string
|
||||
SystemPrivateKey string
|
||||
@@ -42,7 +40,6 @@ type (
|
||||
LockProvider *redislock.Client
|
||||
Noncestore nonce.Noncestore
|
||||
PgStore store.Store
|
||||
Pub *pub.Pub
|
||||
RedisClient *redis.Client
|
||||
RegistryMap map[string]common.Address
|
||||
SystemPrivateKey *ecdsa.PrivateKey
|
||||
@@ -93,7 +90,6 @@ func NewCustodial(o Opts) (*Custodial, error) {
|
||||
LockProvider: o.LockProvider,
|
||||
Noncestore: o.Noncestore,
|
||||
PgStore: o.PgStore,
|
||||
Pub: o.Pub,
|
||||
RedisClient: o.RedisClient,
|
||||
RegistryMap: registryMap,
|
||||
SystemPrivateKey: privateKey,
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
package pub
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
const (
|
||||
streamName string = "CUSTODIAL"
|
||||
streamSubjects string = "CUSTODIAL.*"
|
||||
AccountActivated string = "CUSTODIAL.accountActivated"
|
||||
GasRefilled string = "CUSTODIAL.gasRefilled"
|
||||
)
|
||||
|
||||
type (
|
||||
PubOpts struct {
|
||||
DedupDuration time.Duration
|
||||
JsCtx nats.JetStreamContext
|
||||
PersistDuration time.Duration
|
||||
}
|
||||
|
||||
Pub struct {
|
||||
jsCtx nats.JetStreamContext
|
||||
}
|
||||
|
||||
EventPayload struct {
|
||||
TxHash string `json:"txHash"`
|
||||
}
|
||||
)
|
||||
|
||||
func NewPub(o PubOpts) (*Pub, error) {
|
||||
stream, _ := o.JsCtx.StreamInfo(streamName)
|
||||
if stream == nil {
|
||||
_, err := o.JsCtx.AddStream(&nats.StreamConfig{
|
||||
Name: streamName,
|
||||
MaxAge: o.PersistDuration,
|
||||
Storage: nats.FileStorage,
|
||||
Subjects: []string{streamSubjects},
|
||||
Duplicates: o.DedupDuration,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &Pub{
|
||||
jsCtx: o.JsCtx,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *Pub) Publish(subject string, dedupId string, eventPayload interface{}) error {
|
||||
jsonData, err := json.Marshal(eventPayload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = p.jsCtx.Publish(subject, jsonData, nats.MsgId(dedupId))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/pub"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
||||
"github.com/nats-io/nats.go"
|
||||
)
|
||||
@@ -28,36 +27,12 @@ func (s *Sub) handler(ctx context.Context, msg *nats.Msg) error {
|
||||
if err := s.cu.PgStore.ActivateAccount(ctx, chainEvent.To); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
eventPayload := &pub.EventPayload{
|
||||
TxHash: chainEvent.TxHash,
|
||||
}
|
||||
|
||||
if err := s.cu.Pub.Publish(
|
||||
pub.AccountActivated,
|
||||
chainEvent.TxHash,
|
||||
eventPayload,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
case "CHAIN.gas":
|
||||
if chainEvent.Success {
|
||||
if err := s.cu.PgStore.ResetGasQuota(ctx, chainEvent.To); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
eventPayload := &pub.EventPayload{
|
||||
TxHash: chainEvent.TxHash,
|
||||
}
|
||||
|
||||
if err := s.cu.Pub.Publish(
|
||||
pub.GasRefilled,
|
||||
chainEvent.TxHash,
|
||||
eventPayload,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user