Use storage service in events updater
This commit is contained in:
parent
fcce60e031
commit
63eb803022
@ -19,7 +19,6 @@ import (
|
||||
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/args"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/event"
|
||||
)
|
||||
|
||||
@ -158,10 +157,7 @@ func main() {
|
||||
|
||||
if fakeDir != "" {
|
||||
svc := devremote.NewDevAccountService(ctx, fakeDir).WithAutoVoucher(ctx, "FOO", 42)
|
||||
userStore := &store.UserDataStore{
|
||||
Db: userdatastore,
|
||||
}
|
||||
eu := event.NewEventsUpdater(svc, userStore, pe)
|
||||
eu := event.NewEventsUpdater(svc, menuStorageService)
|
||||
emitter := &devEmitter{
|
||||
h: eu.ToEventsHandler(),
|
||||
}
|
||||
|
4
go.mod
4
go.mod
@ -5,8 +5,8 @@ go 1.23.4
|
||||
require (
|
||||
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d
|
||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113201755-37bb46056123
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113103142-5bf0a0e85893
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113213645-7c697394b5e7
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b
|
||||
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d
|
||||
github.com/alecthomas/assert/v2 v2.2.2
|
||||
github.com/gofrs/uuid v4.4.0+incompatible
|
||||
|
8
go.sum
8
go.sum
@ -2,10 +2,10 @@ git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d h1:bPAOVZOX4frSG
|
||||
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
|
||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 h1:BenzGx6aDHKDwE23/mWIFD2InYIXyzHroZWV3MF5WUk=
|
||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60=
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113201755-37bb46056123 h1:O8+f/S0cST2EX/mSsoBM+Jja2QdIyawJSTWCy6oiBF8=
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113201755-37bb46056123/go.mod h1:e9Damfk0euyjWzn+CE8uxgg8p4ggd07wp99SOyEhGXY=
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113103142-5bf0a0e85893 h1:MyDINzwY1sjfXkIFoc+6T5lXF/1xdFV6yjHrpSNZzWM=
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113103142-5bf0a0e85893/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U=
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113213645-7c697394b5e7 h1:P+Bi5jcQbnCrLnKNC5k8XDTBsocyZTVifqDyZY6d0w8=
|
||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113213645-7c697394b5e7/go.mod h1:X9aQF93xw3vcW2QftJfzLewvbotRM0U00DRtdmFw294=
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b h1:6SieNUSEKbkjzquuwazs/lVG56zdEWF10zQQEMRJfMs=
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U=
|
||||
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d h1:q/NO1rEgK3pia32D/tCq5hXfEuJp84COZRwceFvy/MM=
|
||||
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d/go.mod h1:AH15xABcvaJr1TCGlih3oGSuwWC0E5IdbHQwuu+E1KI=
|
||||
github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk=
|
||||
|
@ -26,16 +26,20 @@ func (eh *EventsUpdater) handleCustodialRegistration(ctx context.Context, ev any
|
||||
return eh.HandleCustodialRegistration(ctx, o)
|
||||
}
|
||||
|
||||
func (eh *EventsUpdater) HandleCustodialRegistration(ctx context.Context, ev *apievent.EventCustodialRegistration) error {
|
||||
identity, err := store.IdentityFromAddress(ctx, eh.store, ev.Account)
|
||||
func (eu *EventsUpdater) HandleCustodialRegistration(ctx context.Context, ev *apievent.EventCustodialRegistration) error {
|
||||
pe, userStore, err := eu.getStore(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = eh.pe.Load(identity.SessionId)
|
||||
identity, err := store.IdentityFromAddress(ctx, userStore, ev.Account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
st := eh.pe.GetState()
|
||||
err = pe.Load(identity.SessionId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
st := pe.GetState()
|
||||
st.SetFlag(accountCreatedFlag)
|
||||
return eh.pe.Save(identity.SessionId)
|
||||
return pe.Save(identity.SessionId)
|
||||
}
|
||||
|
@ -1,29 +1,34 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.defalsify.org/vise.git/persist"
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.grassecon.net/grassrootseconomics/visedriver/storage"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-vise/store"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/remote"
|
||||
apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event"
|
||||
)
|
||||
|
||||
var (
|
||||
logg = logging.NewVanilla().WithDomain("sarafu-vise.handlers.event")
|
||||
)
|
||||
|
||||
type EventsUpdater struct {
|
||||
api remote.AccountService
|
||||
formatFunc func(string, int, any) string
|
||||
store *store.UserDataStore
|
||||
pe *persist.Persister
|
||||
store storage.StorageService
|
||||
}
|
||||
|
||||
func NewEventsUpdater(api remote.AccountService, store *store.UserDataStore, pe *persist.Persister) *EventsUpdater {
|
||||
func NewEventsUpdater(api remote.AccountService, store storage.StorageService) *EventsUpdater {
|
||||
return &EventsUpdater{
|
||||
api: api,
|
||||
formatFunc: func(tag string, i int, o any) string {
|
||||
return fmt.Sprintf("%d %v", i, o)
|
||||
},
|
||||
store: store,
|
||||
pe: pe,
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,5 +36,26 @@ func (eu *EventsUpdater) ToEventsHandler() *apievent.EventsHandler {
|
||||
eh := apievent.NewEventsHandler()
|
||||
eh = eh.WithHandler(apievent.EventTokenTransferTag, eu.handleTokenTransfer)
|
||||
eh = eh.WithHandler(apievent.EventRegistrationTag, eu.handleCustodialRegistration)
|
||||
eh = eh.WithHandler(apievent.EventTokenMintTag, eu.handleNoop)
|
||||
return eh
|
||||
}
|
||||
|
||||
func (eu *EventsUpdater) handleNoop(ctx context.Context, ev any) error {
|
||||
logg.WarnCtxf(ctx, "noop event handler")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (eu *EventsUpdater) getStore(ctx context.Context) (*persist.Persister, *store.UserDataStore, error) {
|
||||
userDb, err := eu.store.GetUserdataDb(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
userStore := &store.UserDataStore{
|
||||
Db: userDb,
|
||||
}
|
||||
pr, err := eu.store.GetPersister(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return pr, userStore, nil
|
||||
}
|
||||
|
@ -13,14 +13,14 @@ import (
|
||||
)
|
||||
|
||||
// execute all
|
||||
func (eu *EventsUpdater) updateToken(ctx context.Context, identity identity.Identity, tokenAddress string) error {
|
||||
err := eu.updateTokenList(ctx, identity)
|
||||
func (eu *EventsUpdater) updateToken(ctx context.Context, identity identity.Identity, userStore *store.UserDataStore, tokenAddress string) error {
|
||||
err := eu.updateTokenList(ctx, identity, userStore)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
eu.store.Db.SetSession(identity.SessionId)
|
||||
activeSym, err := eu.store.ReadEntry(ctx, identity.SessionId, storedb.DATA_ACTIVE_SYM)
|
||||
userStore.Db.SetSession(identity.SessionId)
|
||||
activeSym, err := userStore.ReadEntry(ctx, identity.SessionId, storedb.DATA_ACTIVE_SYM)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
@ -34,12 +34,12 @@ func (eu *EventsUpdater) updateToken(ctx context.Context, identity identity.Iden
|
||||
}
|
||||
}
|
||||
|
||||
err = eu.updateDefaultToken(ctx, identity, string(activeSym))
|
||||
err = eu.updateDefaultToken(ctx, identity, userStore, string(activeSym))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = eu.updateTokenTransferList(ctx, identity)
|
||||
err = eu.updateTokenTransferList(ctx, identity, userStore)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -49,14 +49,14 @@ func (eu *EventsUpdater) updateToken(ctx context.Context, identity identity.Iden
|
||||
|
||||
|
||||
// set default token to given symbol.
|
||||
func (eu *EventsUpdater) updateDefaultToken(ctx context.Context, identity identity.Identity, activeSym string) error {
|
||||
pfxDb := toPrefixDb(eu.store, identity.SessionId)
|
||||
func (eu *EventsUpdater) updateDefaultToken(ctx context.Context, identity identity.Identity, userStore *store.UserDataStore, activeSym string) error {
|
||||
pfxDb := toPrefixDb(userStore, identity.SessionId)
|
||||
// TODO: the activeSym input should instead be newline separated list?
|
||||
tokenData, err := store.GetVoucherData(ctx, pfxDb, activeSym)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return store.UpdateVoucherData(ctx, eu.store, identity.SessionId, tokenData)
|
||||
return store.UpdateVoucherData(ctx, userStore, identity.SessionId, tokenData)
|
||||
}
|
||||
|
||||
|
||||
@ -71,26 +71,30 @@ func (eh *EventsUpdater) handleTokenTransfer(ctx context.Context, ev any) error
|
||||
return eh.HandleTokenTransfer(ctx, o)
|
||||
}
|
||||
func (eu *EventsUpdater) HandleTokenTransfer(ctx context.Context, ev *apievent.EventTokenTransfer) error {
|
||||
identity, err := store.IdentityFromAddress(ctx, eu.store, ev.From)
|
||||
_, userStore, err := eu.getStore(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
identity, err := store.IdentityFromAddress(ctx, userStore, ev.From)
|
||||
if err != nil {
|
||||
if !db.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err = eu.updateToken(ctx, identity, ev.VoucherAddress)
|
||||
err = eu.updateToken(ctx, identity, userStore, ev.VoucherAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Compare(ev.To, ev.From) != 0 {
|
||||
identity, err = store.IdentityFromAddress(ctx, eu.store, ev.To)
|
||||
identity, err = store.IdentityFromAddress(ctx, userStore, ev.To)
|
||||
if err != nil {
|
||||
if !db.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err = eu.updateToken(ctx, identity, ev.VoucherAddress)
|
||||
err = eu.updateToken(ctx, identity, userStore, ev.VoucherAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -102,13 +106,17 @@ func (eu *EventsUpdater) HandleTokenTransfer(ctx context.Context, ev *apievent.E
|
||||
|
||||
// handle token mint.
|
||||
func (eu *EventsUpdater) HandleTokenMint(ctx context.Context, ev *apievent.EventTokenMint) error {
|
||||
identity, err := store.IdentityFromAddress(ctx, eu.store, ev.To)
|
||||
_, userStore, err := eu.getStore(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
identity, err := store.IdentityFromAddress(ctx, userStore, ev.To)
|
||||
if err != nil {
|
||||
if !db.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err = eu.updateToken(ctx, identity, ev.VoucherAddress)
|
||||
err = eu.updateToken(ctx, identity, userStore, ev.VoucherAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -117,8 +125,8 @@ func (eu *EventsUpdater) HandleTokenMint(ctx context.Context, ev *apievent.Event
|
||||
}
|
||||
|
||||
// use api to resolve address to token symbol.
|
||||
func (ev *EventsUpdater) toSym(ctx context.Context, address string) ([]byte, error) {
|
||||
voucherData, err := ev.api.VoucherData(ctx, address)
|
||||
func (eu *EventsUpdater) toSym(ctx context.Context, address string) ([]byte, error) {
|
||||
voucherData, err := eu.api.VoucherData(ctx, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -126,7 +134,7 @@ func (ev *EventsUpdater) toSym(ctx context.Context, address string) ([]byte, err
|
||||
}
|
||||
|
||||
// refresh and store token list.
|
||||
func (eu *EventsUpdater) updateTokenList(ctx context.Context, identity identity.Identity) error {
|
||||
func (eu *EventsUpdater) updateTokenList(ctx context.Context, identity identity.Identity, userStore *store.UserDataStore) error {
|
||||
holdings, err := eu.api.FetchVouchers(ctx, identity.ChecksumAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -136,7 +144,7 @@ func (eu *EventsUpdater) updateTokenList(ctx context.Context, identity identity.
|
||||
|
||||
// TODO: make sure subprefixdb is thread safe when using gdbm
|
||||
// TODO: why is address session here unless explicitly set
|
||||
pfxDb := toPrefixDb(eu.store, identity.SessionId)
|
||||
pfxDb := toPrefixDb(userStore, identity.SessionId)
|
||||
|
||||
typ := storedb.ToBytes(storedb.DATA_VOUCHER_SYMBOLS)
|
||||
err = pfxDb.Put(ctx, typ, []byte(metadata.Symbols))
|
||||
@ -166,7 +174,7 @@ func (eu *EventsUpdater) updateTokenList(ctx context.Context, identity identity.
|
||||
}
|
||||
|
||||
// refresh and store transaction history.
|
||||
func (eu *EventsUpdater) updateTokenTransferList(ctx context.Context, identity identity.Identity) error {
|
||||
func (eu *EventsUpdater) updateTokenTransferList(ctx context.Context, identity identity.Identity, userStore *store.UserDataStore) error {
|
||||
var r []string
|
||||
|
||||
txs, err := eu.api.FetchTransactions(ctx, identity.ChecksumAddress)
|
||||
@ -180,7 +188,7 @@ func (eu *EventsUpdater) updateTokenTransferList(ctx context.Context, identity i
|
||||
|
||||
s := strings.Join(r, "\n")
|
||||
|
||||
return eu.store.WriteEntry(ctx, identity.SessionId, storedb.DATA_TRANSACTIONS, []byte(s))
|
||||
return userStore.WriteEntry(ctx, identity.SessionId, storedb.DATA_TRANSACTIONS, []byte(s))
|
||||
}
|
||||
|
||||
func toPrefixDb(userStore *store.UserDataStore, sessionId string) storedb.PrefixDb {
|
||||
|
Loading…
Reference in New Issue
Block a user