term/event/token.go

258 lines
6.1 KiB
Go
Raw Normal View History

package event
import (
"context"
"fmt"
"strings"
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.defalsify.org/vise.git/db"
"git.grassecon.net/urdt/ussd/common"
"git.grassecon.net/term/lookup"
)
const (
2024-11-02 23:58:09 +01:00
evTokenTransfer = "TOKEN_TRANSFER"
2024-11-03 20:04:44 +01:00
// TODO: export from urdt storage package
2024-11-02 17:08:05 +01:00
DATATYPE_USERSUB = 64
)
2024-11-03 20:04:44 +01:00
// fields used for handling token transfer event.
type eventTokenTransfer struct {
2024-11-03 23:25:51 +01:00
To string
Value int
VoucherAddress string
TxHash string
From string
2024-11-03 23:25:51 +01:00
}
type eventTokenMint struct {
To string
2024-11-02 23:58:09 +01:00
Value int
TxHash string
2024-11-03 15:35:37 +01:00
VoucherAddress string
}
2024-11-03 23:25:51 +01:00
2024-11-03 20:04:44 +01:00
// formatter for transaction data
//
// TODO: current formatting is a placeholder.
2024-11-03 16:13:59 +01:00
func formatTransaction(idx int, tx dataserviceapi.Last10TxResponse) string {
return fmt.Sprintf("%d %s %s", idx, tx.DateBlock, tx.TxHash[:10])
}
2024-11-03 20:04:44 +01:00
// refresh and store transaction history.
2024-11-03 16:13:59 +01:00
func updateTokenTransferList(ctx context.Context, store *common.UserDataStore, identity lookup.Identity) error {
var r []string
2024-11-03 15:35:37 +01:00
txs, err := lookup.Api.FetchTransactions(ctx, identity.ChecksumAddress)
if err != nil {
return err
}
for i, tx := range(txs) {
2024-11-03 16:13:59 +01:00
r = append(r, formatTransaction(i, tx))
}
s := strings.Join(r, "\n")
2024-11-03 16:13:59 +01:00
return store.WriteEntry(ctx, identity.SessionId, common.DATA_TRANSACTIONS, []byte(s))
}
2024-11-03 20:04:44 +01:00
// refresh and store token list.
//
// TODO: when subprefixdb has been exported, can use function in ...urdt/ussd/common/ instead
2024-11-03 01:34:28 +01:00
func updateTokenList(ctx context.Context, store *common.UserDataStore, identity lookup.Identity) error {
2024-11-03 15:35:37 +01:00
holdings, err := lookup.Api.FetchVouchers(ctx, identity.ChecksumAddress)
2024-11-02 17:08:05 +01:00
if err != nil {
return err
}
metadata := common.ProcessVouchers(holdings)
_ = metadata
// TODO: make sure subprefixdb is thread safe when using gdbm
2024-11-03 15:35:37 +01:00
// TODO: why is address session here unless explicitly set
store.Db.SetSession(identity.SessionId)
2024-11-02 17:08:05 +01:00
store.Db.SetPrefix(DATATYPE_USERSUB)
k := append([]byte("vouchers"), []byte("sym")...)
err = store.Db.Put(ctx, k, []byte(metadata.Symbols))
if err != nil {
return err
}
2024-11-03 15:35:37 +01:00
logg.TraceCtxf(ctx, "processvoucher", "key", k)
2024-11-02 17:08:05 +01:00
k = append([]byte("vouchers"), []byte("bal")...)
err = store.Db.Put(ctx, k, []byte(metadata.Balances))
if err != nil {
return err
}
k = append([]byte("vouchers"), []byte("deci")...)
err = store.Db.Put(ctx, k, []byte(metadata.Decimals))
if err != nil {
return err
}
k = append([]byte("vouchers"), []byte("addr")...)
err = store.Db.Put(ctx, k, []byte(metadata.Addresses))
if err != nil {
return err
}
return nil
}
2024-11-03 20:04:44 +01:00
// set default token to given symbol.
func updateDefaultToken(ctx context.Context, store *common.UserDataStore, identity lookup.Identity, activeSym string) error {
pfxDb := common.StoreToPrefixDb(store, []byte("vouchers"))
2024-11-03 15:35:37 +01:00
// TODO: the activeSym input should instead be newline separated list?
tokenData, err := common.GetVoucherData(ctx, pfxDb, activeSym)
if err != nil {
return err
}
return common.UpdateVoucherData(ctx, store, identity.SessionId, tokenData)
}
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
// use api to resolve address to token symbol.
2024-11-03 15:35:37 +01:00
func toSym(ctx context.Context, address string) ([]byte, error) {
voucherData, err := lookup.Api.VoucherData(ctx, address)
if err != nil {
return nil, err
}
return []byte(voucherData.TokenSymbol), nil
}
2024-11-03 20:04:44 +01:00
// execute all
2024-11-03 15:35:37 +01:00
func updateToken(ctx context.Context, store *common.UserDataStore, identity lookup.Identity, tokenAddress string) error {
2024-11-03 01:34:28 +01:00
err := updateTokenList(ctx, store, identity)
if err != nil {
return err
}
2024-11-03 15:35:37 +01:00
store.Db.SetSession(identity.SessionId)
activeSym, err := store.ReadEntry(ctx, identity.SessionId, common.DATA_ACTIVE_SYM)
if err == nil {
return nil
}
if !db.IsNotFound(err) {
return err
}
2024-11-03 15:35:37 +01:00
if activeSym == nil {
activeSym, err = toSym(ctx, tokenAddress)
if err != nil {
return err
}
}
logg.Debugf("barfoo")
err = updateDefaultToken(ctx, store, identity, string(activeSym))
if err != nil {
return err
}
2024-11-03 16:13:59 +01:00
err = updateTokenTransferList(ctx, store, identity)
if err != nil {
return err
}
return nil
}
2024-11-03 20:04:44 +01:00
// attempt to coerce event as token transfer event.
2024-11-02 23:58:09 +01:00
func asTokenTransferEvent(gev *geEvent.Event) (*eventTokenTransfer, bool) {
var err error
var ok bool
var ev eventTokenTransfer
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 = common.NormalizeHex(gev.TxHash)
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
}
2024-11-03 20:04:44 +01:00
// handle token transfer.
//
// if from and to are NOT the same, handle code will be executed once for each side of the transfer.
2024-11-02 17:08:05 +01:00
func handleTokenTransfer(ctx context.Context, store *common.UserDataStore, ev *eventTokenTransfer) error {
identity, err := lookup.IdentityFromAddress(ctx, store, ev.From)
if err != nil {
if !db.IsNotFound(err) {
return err
}
} else {
2024-11-03 15:35:37 +01:00
err = updateToken(ctx, store, identity, ev.VoucherAddress)
if err != nil {
return err
}
}
2024-11-03 20:04:44 +01:00
2024-11-03 23:25:51 +01:00
if strings.Compare(ev.To, ev.From) != 0 {
2024-11-03 20:04:44 +01:00
identity, err = lookup.IdentityFromAddress(ctx, store, ev.To)
if err != nil {
2024-11-03 20:04:44 +01:00
if !db.IsNotFound(err) {
return err
}
} else {
err = updateToken(ctx, store, identity, ev.VoucherAddress)
if err != nil {
return err
}
}
}
return nil
}
2024-11-03 23:25:51 +01:00
// handle token mint.
func handleTokenMint(ctx context.Context, store *common.UserDataStore, ev *eventTokenMint) error {
identity, err := lookup.IdentityFromAddress(ctx, store, ev.To)
if err != nil {
if !db.IsNotFound(err) {
return err
}
} else {
err = updateToken(ctx, store, identity, ev.VoucherAddress)
if err != nil {
return err
}
}
return nil
}