sarafu-vise-events/event/token.go

83 lines
1.9 KiB
Go
Raw Normal View History

package event
import (
"context"
"fmt"
2024-11-02 23:58:09 +01:00
"strconv"
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
2024-11-03 16:13:59 +01:00
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
"git.grassecon.net/grassrootseconomics/common/hex"
2025-01-13 18:47:21 +01:00
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
)
const (
2025-01-13 18:47:21 +01:00
evTokenTransfer = apievent.EventTokenTransferTag
// TODO: export from visedriver storage package
2025-01-13 18:47:21 +01:00
//DATATYPE_USERSUB = 64
)
2024-11-03 20:04:44 +01:00
// formatter for transaction data
//
// TODO: current formatting is a placeholder.
2025-01-13 18:47:21 +01:00
func formatTransaction(tag string, idx int, item any) string {
if tag == apievent.EventTokenTransferTag {
tx, ok := item.(dataserviceapi.Last10TxResponse)
if !ok {
logg.Errorf("invalid formatting object", "tag", tag)
return ""
}
return fmt.Sprintf("%d %s %s", idx, tx.DateBlock, tx.TxHash[:10])
2024-11-02 17:08:05 +01:00
}
2025-01-13 18:47:21 +01:00
logg.Warnf("unhandled formatting object", "tag", tag)
return ""
}
2024-11-03 20:04:44 +01:00
// waiter to check whether object is available on dependency endpoints.
2024-11-03 01:34:28 +01:00
func updateWait(ctx context.Context) error {
2024-11-02 23:58:09 +01:00
return nil
}
2024-11-03 20:04:44 +01:00
// attempt to coerce event as token transfer event.
2025-01-13 18:47:21 +01:00
func asTokenTransferEvent(gev *geEvent.Event) (*apievent.EventTokenTransfer, bool) {
2024-11-02 23:58:09 +01:00
var err error
var ok bool
2025-01-13 18:47:21 +01:00
var ev apievent.EventTokenTransfer
2024-11-02 23:58:09 +01:00
if gev.TxType != evTokenTransfer {
return nil, false
}
pl := gev.Payload
2024-11-03 20:04:44 +01:00
// we are assuming from and to are checksum addresses
2024-11-02 23:58:09 +01:00
ev.From, ok = pl["from"].(string)
if !ok {
return nil, false
}
ev.To, ok = pl["to"].(string)
if !ok {
return nil, false
}
ev.TxHash, err = hex.NormalizeHex(gev.TxHash)
2024-11-02 23:58:09 +01:00
if err != nil {
2024-11-03 01:34:28 +01:00
logg.Errorf("could not decode tx hash", "tx", gev.TxHash, "err", err)
2024-11-02 23:58:09 +01:00
return nil, false
}
value, ok := pl["value"].(string)
if !ok {
return nil, false
}
ev.Value, err = strconv.Atoi(value)
if err != nil {
2024-11-03 01:34:28 +01:00
logg.Errorf("could not decode value", "value", value, "err", err)
2024-11-02 23:58:09 +01:00
return nil, false
}
2024-11-03 15:35:37 +01:00
ev.VoucherAddress = gev.ContractAddress
2024-11-02 23:58:09 +01:00
return &ev, true
}