sarafu-vise/handlers/event/token.go

206 lines
5.4 KiB
Go
Raw Normal View History

2025-01-13 18:26:27 +01:00
package event
import (
"context"
"fmt"
2025-01-13 18:26:27 +01:00
"strings"
"git.defalsify.org/vise.git/db"
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
"git.grassecon.net/grassrootseconomics/common/identity"
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
)
// execute all
2025-01-13 22:39:15 +01:00
func (eu *EventsUpdater) updateToken(ctx context.Context, identity identity.Identity, userStore *store.UserDataStore, tokenAddress string) error {
err := eu.updateTokenList(ctx, identity, userStore)
2025-01-13 18:26:27 +01:00
if err != nil {
return err
}
2025-01-13 22:39:15 +01:00
userStore.Db.SetSession(identity.SessionId)
activeSym, err := userStore.ReadEntry(ctx, identity.SessionId, storedb.DATA_ACTIVE_SYM)
2025-01-13 18:26:27 +01:00
if err == nil {
return nil
}
if !db.IsNotFound(err) {
return err
}
if activeSym == nil {
activeSym, err = eu.toSym(ctx, tokenAddress)
2025-01-13 18:26:27 +01:00
if err != nil {
return err
}
}
2025-01-13 22:39:15 +01:00
err = eu.updateDefaultToken(ctx, identity, userStore, string(activeSym))
2025-01-13 18:26:27 +01:00
if err != nil {
return err
}
2025-01-13 22:39:15 +01:00
err = eu.updateTokenTransferList(ctx, identity, userStore)
2025-01-13 18:26:27 +01:00
if err != nil {
return err
}
return nil
}
// set default token to given symbol.
2025-01-13 22:39:15 +01:00
func (eu *EventsUpdater) updateDefaultToken(ctx context.Context, identity identity.Identity, userStore *store.UserDataStore, activeSym string) error {
pfxDb := toPrefixDb(userStore, identity.SessionId)
2025-01-13 18:26:27 +01:00
// TODO: the activeSym input should instead be newline separated list?
tokenData, err := store.GetVoucherData(ctx, pfxDb, activeSym)
if err != nil {
return err
}
2025-01-13 22:39:15 +01:00
return store.UpdateVoucherData(ctx, userStore, identity.SessionId, tokenData)
2025-01-13 18:26:27 +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.
func (eh *EventsUpdater) handleTokenTransfer(ctx context.Context, ev any) error {
o, ok := ev.(*apievent.EventTokenTransfer)
if !ok {
2025-01-14 08:02:42 +01:00
fmt.Errorf("invalid event for token transfer")
}
return eh.HandleTokenTransfer(ctx, o)
}
func (eu *EventsUpdater) HandleTokenTransfer(ctx context.Context, ev *apievent.EventTokenTransfer) error {
2025-01-13 22:39:15 +01:00
_, userStore, err := eu.getStore(ctx)
if err != nil {
return err
}
identity, err := store.IdentityFromAddress(ctx, userStore, ev.From)
2025-01-13 18:26:27 +01:00
if err != nil {
if !db.IsNotFound(err) {
return err
}
} else {
2025-01-13 22:39:15 +01:00
err = eu.updateToken(ctx, identity, userStore, ev.VoucherAddress)
2025-01-13 18:26:27 +01:00
if err != nil {
return err
}
}
if strings.Compare(ev.To, ev.From) != 0 {
2025-01-13 22:39:15 +01:00
identity, err = store.IdentityFromAddress(ctx, userStore, ev.To)
2025-01-13 18:26:27 +01:00
if err != nil {
if !db.IsNotFound(err) {
return err
}
} else {
2025-01-13 22:39:15 +01:00
err = eu.updateToken(ctx, identity, userStore, ev.VoucherAddress)
2025-01-13 18:26:27 +01:00
if err != nil {
return err
}
}
}
return nil
}
// handle token mint.
2025-01-14 08:02:42 +01:00
func (eh *EventsUpdater) handleTokenMint(ctx context.Context, ev any) error {
o, ok := ev.(*apievent.EventTokenMint)
if !ok {
fmt.Errorf("invalid event for token mint")
}
return eh.HandleTokenMint(ctx, o)
}
func (eu *EventsUpdater) HandleTokenMint(ctx context.Context, ev *apievent.EventTokenMint) error {
2025-01-13 22:39:15 +01:00
_, userStore, err := eu.getStore(ctx)
if err != nil {
return err
}
identity, err := store.IdentityFromAddress(ctx, userStore, ev.To)
2025-01-13 18:26:27 +01:00
if err != nil {
if !db.IsNotFound(err) {
return err
}
} else {
2025-01-13 22:39:15 +01:00
err = eu.updateToken(ctx, identity, userStore, ev.VoucherAddress)
2025-01-13 18:26:27 +01:00
if err != nil {
return err
}
}
return nil
}
// use api to resolve address to token symbol.
2025-01-13 22:39:15 +01:00
func (eu *EventsUpdater) toSym(ctx context.Context, address string) ([]byte, error) {
voucherData, err := eu.api.VoucherData(ctx, address)
2025-01-13 18:26:27 +01:00
if err != nil {
return nil, err
}
return []byte(voucherData.TokenSymbol), nil
}
// refresh and store token list.
2025-01-13 22:39:15 +01:00
func (eu *EventsUpdater) updateTokenList(ctx context.Context, identity identity.Identity, userStore *store.UserDataStore) error {
holdings, err := eu.api.FetchVouchers(ctx, identity.ChecksumAddress)
2025-01-13 18:26:27 +01:00
if err != nil {
return err
}
metadata := store.ProcessVouchers(holdings)
_ = metadata
// TODO: make sure subprefixdb is thread safe when using gdbm
// TODO: why is address session here unless explicitly set
2025-01-13 22:39:15 +01:00
pfxDb := toPrefixDb(userStore, identity.SessionId)
2025-01-13 18:26:27 +01:00
typ := storedb.ToBytes(storedb.DATA_VOUCHER_SYMBOLS)
err = pfxDb.Put(ctx, typ, []byte(metadata.Symbols))
if err != nil {
return err
}
typ = storedb.ToBytes(storedb.DATA_VOUCHER_BALANCES)
err = pfxDb.Put(ctx, typ, []byte(metadata.Balances))
if err != nil {
return err
}
typ = storedb.ToBytes(storedb.DATA_VOUCHER_DECIMALS)
err = pfxDb.Put(ctx, typ, []byte(metadata.Decimals))
if err != nil {
return err
}
typ = storedb.ToBytes(storedb.DATA_VOUCHER_ADDRESSES)
err = pfxDb.Put(ctx, typ, []byte(metadata.Addresses))
if err != nil {
return err
}
return nil
}
// refresh and store transaction history.
2025-01-13 22:39:15 +01:00
func (eu *EventsUpdater) updateTokenTransferList(ctx context.Context, identity identity.Identity, userStore *store.UserDataStore) error {
2025-01-13 18:26:27 +01:00
var r []string
txs, err := eu.api.FetchTransactions(ctx, identity.ChecksumAddress)
2025-01-13 18:26:27 +01:00
if err != nil {
return err
}
for i, tx := range(txs) {
r = append(r, eu.formatFunc(apievent.EventTokenTransferTag, i, tx))
2025-01-13 18:26:27 +01:00
}
s := strings.Join(r, "\n")
2025-01-13 22:39:15 +01:00
return userStore.WriteEntry(ctx, identity.SessionId, storedb.DATA_TRANSACTIONS, []byte(s))
2025-01-13 18:26:27 +01:00
}
func toPrefixDb(userStore *store.UserDataStore, sessionId string) storedb.PrefixDb {
userStore.Db.SetSession(sessionId)
prefix := storedb.ToBytes(db.DATATYPE_USERDATA)
return store.StoreToPrefixDb(userStore, prefix)
}