2023-03-06 09:18:41 +01:00
|
|
|
package sub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Sub) handler(ctx context.Context, msg *nats.Msg) error {
|
|
|
|
var (
|
|
|
|
chainEvent store.MinimalTxInfo
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := json.Unmarshal(msg.Data, &chainEvent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.cu.PgStore.UpdateOtxStatusFromChainEvent(ctx, chainEvent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch msg.Subject {
|
2023-03-29 18:10:58 +02:00
|
|
|
case "CHAIN.register":
|
|
|
|
if chainEvent.Success {
|
|
|
|
if err := s.cu.PgStore.ActivateAccount(ctx, chainEvent.To); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-03-06 09:18:41 +01:00
|
|
|
case "CHAIN.gas":
|
2023-03-29 18:10:58 +02:00
|
|
|
if chainEvent.Success {
|
|
|
|
if err := s.cu.PgStore.ResetGasQuota(ctx, chainEvent.To); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-08 07:49:09 +01:00
|
|
|
}
|
2023-03-06 09:18:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|