2023-03-06 09:18:41 +01:00
|
|
|
package sub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
)
|
|
|
|
|
2023-04-11 12:18:06 +02:00
|
|
|
type (
|
|
|
|
ChainEvent struct {
|
|
|
|
Block uint64 `json:"block"`
|
|
|
|
From string `json:"from"`
|
|
|
|
To string `json:"to"`
|
|
|
|
ContractAddress string `json:"contractAddress"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
TxHash string `json:"transactionHash"`
|
|
|
|
TxIndex uint `json:"transactionIndex"`
|
|
|
|
Value uint64 `json:"value"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Sub) processEventHandler(ctx context.Context, msg *nats.Msg) error {
|
2023-03-06 09:18:41 +01:00
|
|
|
var (
|
2023-04-11 12:18:06 +02:00
|
|
|
chainEvent ChainEvent
|
2023-03-06 09:18:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := json.Unmarshal(msg.Data, &chainEvent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-11 12:18:06 +02:00
|
|
|
if err := s.cu.Store.UpdateDispatchStatus(
|
|
|
|
ctx,
|
|
|
|
chainEvent.Success,
|
|
|
|
chainEvent.TxHash,
|
|
|
|
chainEvent.Block,
|
|
|
|
); err != nil {
|
2023-03-06 09:18:41 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-11 12:18:06 +02:00
|
|
|
if chainEvent.Success {
|
|
|
|
switch msg.Subject {
|
|
|
|
case "CHAIN.register":
|
|
|
|
if err := s.cu.Store.ActivateAccount(ctx, chainEvent.To); err != nil {
|
2023-03-29 18:10:58 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-04-11 12:18:06 +02:00
|
|
|
|
2023-05-16 14:20:01 +02:00
|
|
|
if err := s.cu.Store.GasUnlock(ctx, chainEvent.To); err != nil {
|
2023-04-11 12:18:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case "CHAIN.gas":
|
2023-05-16 14:20:01 +02:00
|
|
|
if err := s.cu.Store.GasUnlock(ctx, chainEvent.To); err != nil {
|
2023-03-29 18:10:58 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-03-08 07:49:09 +01:00
|
|
|
}
|
2023-03-06 09:18:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|