From fee439121dc9b02e063c70f968fb437dd4e155ce Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 13 Jan 2025 17:26:27 +0000 Subject: [PATCH 01/14] Take event handlers from vise-event --- cmd/main.go | 21 ++++- handlers/event/custodial.go | 33 +++++++ handlers/event/event.go | 16 ++++ handlers/event/token.go | 183 ++++++++++++++++++++++++++++++++++++ store/user_store.go | 36 +++++++ 5 files changed, 288 insertions(+), 1 deletion(-) create mode 100644 handlers/event/custodial.go create mode 100644 handlers/event/event.go create mode 100644 handlers/event/token.go diff --git a/cmd/main.go b/cmd/main.go index 737cd86..c37a9c6 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -16,6 +16,7 @@ import ( httpremote "git.grassecon.net/grassrootseconomics/sarafu-api/remote/http" devremote "git.grassecon.net/grassrootseconomics/sarafu-api/dev" "git.grassecon.net/grassrootseconomics/sarafu-api/remote" + "git.grassecon.net/grassrootseconomics/sarafu-api/event" "git.grassecon.net/grassrootseconomics/sarafu-vise/args" "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers" ) @@ -26,6 +27,24 @@ var ( menuSeparator = ": " ) +// WIP: placeholder emitter, should perform same action as events +func emitter(ctx context.Context, msg event.Msg) error { + if msg.Typ == event.EventTokenTransferTag { + tx, ok := msg.Item.(devremote.Tx) + if !ok { + return fmt.Errorf("not a valid tx") + } + logg.InfoCtxf(ctx, "tx emit", "tx", tx) + } else if msg.Typ == event.EventRegistrationTag { + acc, ok := msg.Item.(devremote.Account) + if !ok { + return fmt.Errorf("not a valid tx") + } + logg.InfoCtxf(ctx, "account emit", "account", acc) + + } + return nil +} func main() { config.LoadConfig() @@ -129,7 +148,7 @@ func main() { } if fakeDir != "" { - svc := devremote.NewDevAccountService(ctx, fakeDir).WithAutoVoucher(ctx, "FOO", 42) + svc := devremote.NewDevAccountService(ctx, fakeDir).WithAutoVoucher(ctx, "FOO", 42).WithEmitter(emitter) svc.AddVoucher(ctx, "BAR") accountService = svc } else { diff --git a/handlers/event/custodial.go b/handlers/event/custodial.go new file mode 100644 index 0000000..b0c6c99 --- /dev/null +++ b/handlers/event/custodial.go @@ -0,0 +1,33 @@ +package event + +import ( + "context" + + "git.defalsify.org/vise.git/persist" + apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event" + "git.grassecon.net/grassrootseconomics/sarafu-vise/store" +) + +const ( + // TODO: consolidate with loaded flags + accountCreatedFlag = 9 +) + +// handle custodial registration. +// +// TODO: implement account created in userstore instead, so that +// the need for persister and state use here is eliminated (it +// introduces concurrency risks) +func (eh *EventsHandler) HandleCustodialRegistration(ctx context.Context, userStore *store.UserDataStore, pr *persist.Persister, ev *apievent.EventCustodialRegistration) error { + identity, err := store.IdentityFromAddress(ctx, userStore, ev.Account) + if err != nil { + return err + } + err = pr.Load(identity.SessionId) + if err != nil { + return err + } + st := pr.GetState() + st.SetFlag(accountCreatedFlag) + return pr.Save(identity.SessionId) +} diff --git a/handlers/event/event.go b/handlers/event/event.go new file mode 100644 index 0000000..86b31aa --- /dev/null +++ b/handlers/event/event.go @@ -0,0 +1,16 @@ +package event + +import ( + "git.grassecon.net/grassrootseconomics/sarafu-api/remote" +) + +type EventsHandler struct { + api remote.AccountService + formatFunc func(string, int, any) string +} + +func NewEventsHandler(api remote.AccountService) *EventsHandler { + return &EventsHandler{ + api: api, + } +} diff --git a/handlers/event/token.go b/handlers/event/token.go new file mode 100644 index 0000000..bf5075e --- /dev/null +++ b/handlers/event/token.go @@ -0,0 +1,183 @@ +package event + +import ( + "context" + "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 +func (eh *EventsHandler) updateToken(ctx context.Context, userStore *store.UserDataStore, identity identity.Identity, tokenAddress string) error { + err := eh.updateTokenList(ctx, userStore, identity) + if err != nil { + return err + } + + userStore.Db.SetSession(identity.SessionId) + activeSym, err := userStore.ReadEntry(ctx, identity.SessionId, storedb.DATA_ACTIVE_SYM) + if err == nil { + return nil + } + if !db.IsNotFound(err) { + return err + } + if activeSym == nil { + activeSym, err = eh.toSym(ctx, tokenAddress) + if err != nil { + return err + } + } + + err = updateDefaultToken(ctx, userStore, identity, string(activeSym)) + if err != nil { + return err + } + + err = eh.updateTokenTransferList(ctx, userStore, identity) + if err != nil { + return err + } + + return nil +} + + +// set default token to given symbol. +func updateDefaultToken(ctx context.Context, userStore *store.UserDataStore, identity identity.Identity, 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, userStore, identity.SessionId, tokenData) +} + + +// 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 *EventsHandler) HandleTokenTransfer(ctx context.Context, userStore *store.UserDataStore, ev *apievent.EventTokenTransfer) error { + identity, err := store.IdentityFromAddress(ctx, userStore, ev.From) + if err != nil { + if !db.IsNotFound(err) { + return err + } + } else { + err = eh.updateToken(ctx, userStore, identity, ev.VoucherAddress) + if err != nil { + return err + } + } + + if strings.Compare(ev.To, ev.From) != 0 { + identity, err = store.IdentityFromAddress(ctx, userStore, ev.To) + if err != nil { + if !db.IsNotFound(err) { + return err + } + } else { + err = eh.updateToken(ctx, userStore, identity, ev.VoucherAddress) + if err != nil { + return err + } + } + } + + return nil +} + +// handle token mint. +func (eh *EventsHandler) HandleTokenMint(ctx context.Context, userStore *store.UserDataStore, ev *apievent.EventTokenMint) error { + identity, err := store.IdentityFromAddress(ctx, userStore, ev.To) + if err != nil { + if !db.IsNotFound(err) { + return err + } + } else { + err = eh.updateToken(ctx, userStore, identity, ev.VoucherAddress) + if err != nil { + return err + } + } + return nil +} + +// use api to resolve address to token symbol. +func (ev *EventsHandler) toSym(ctx context.Context, address string) ([]byte, error) { + voucherData, err := ev.api.VoucherData(ctx, address) + if err != nil { + return nil, err + } + return []byte(voucherData.TokenSymbol), nil +} + +// refresh and store token list. +func (eh *EventsHandler) updateTokenList(ctx context.Context, userStore *store.UserDataStore, identity identity.Identity) error { + holdings, err := eh.api.FetchVouchers(ctx, identity.ChecksumAddress) + 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 + pfxDb := toPrefixDb(userStore, identity.SessionId) + + 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. +func (eh *EventsHandler) updateTokenTransferList(ctx context.Context, userStore *store.UserDataStore, identity identity.Identity) error { + var r []string + + txs, err := eh.api.FetchTransactions(ctx, identity.ChecksumAddress) + if err != nil { + return err + } + + for i, tx := range(txs) { + //r = append(r, formatTransaction(i, tx)) + r = append(r, eh.formatFunc(apievent.EventTokenTransferTag, i, tx)) + } + + s := strings.Join(r, "\n") + + return userStore.WriteEntry(ctx, identity.SessionId, storedb.DATA_TRANSACTIONS, []byte(s)) +} + +func toPrefixDb(userStore *store.UserDataStore, sessionId string) storedb.PrefixDb { + userStore.Db.SetSession(sessionId) + prefix := storedb.ToBytes(db.DATATYPE_USERDATA) + return store.StoreToPrefixDb(userStore, prefix) +} diff --git a/store/user_store.go b/store/user_store.go index 920d32e..70e2539 100644 --- a/store/user_store.go +++ b/store/user_store.go @@ -6,6 +6,8 @@ import ( visedb "git.defalsify.org/vise.git/db" storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db" "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db" + "git.grassecon.net/grassrootseconomics/common/hex" + "git.grassecon.net/grassrootseconomics/common/identity" ) // TODO: Rename interface, "datastore" is redundant naming and too general @@ -39,3 +41,37 @@ func (store *UserDataStore) WriteEntry(ctx context.Context, sessionId string, ty func StoreToPrefixDb(userStore *UserDataStore, pfx []byte) storedb.PrefixDb { return storedb.NewSubPrefixDb(userStore.Db, pfx) } + +// IdentityFromAddress fully populates and Identity object from a given +// checksum address. +// +// It is the caller's responsibility to ensure that a valid checksum address +// is passed. +func IdentityFromAddress(ctx context.Context, userStore *UserDataStore, address string) (identity.Identity, error) { + var err error + var ident identity.Identity + + ident.ChecksumAddress = address + ident.NormalAddress, err = hex.NormalizeHex(ident.ChecksumAddress) + if err != nil { + return ident, err + } + ident.SessionId, err = getSessionIdByAddress(ctx, userStore, ident.NormalAddress) + if err != nil { + return ident, err + } + return ident, nil +} + +// load matching session from address from db store. +func getSessionIdByAddress(ctx context.Context, userStore *UserDataStore, address string) (string, error) { + // TODO: replace with userdatastore when double sessionid issue fixed + //r, err := store.ReadEntry(ctx, address, common.DATA_PUBLIC_KEY_REVERSE) + userStore.Db.SetPrefix(visedb.DATATYPE_USERDATA) + userStore.Db.SetSession(address) + r, err := userStore.Db.Get(ctx, storedb.PackKey(storedb.DATA_PUBLIC_KEY_REVERSE, []byte{})) + if err != nil { + return "", err + } + return string(r), nil +} From 6b22fe134a84cd1095a1bf1021a2e8c7c7c018fe Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 13 Jan 2025 17:47:55 +0000 Subject: [PATCH 02/14] Add default formatter events handler --- handlers/event/event.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/handlers/event/event.go b/handlers/event/event.go index 86b31aa..f068f67 100644 --- a/handlers/event/event.go +++ b/handlers/event/event.go @@ -1,6 +1,8 @@ package event import ( + "fmt" + "git.grassecon.net/grassrootseconomics/sarafu-api/remote" ) @@ -12,5 +14,8 @@ type EventsHandler struct { func NewEventsHandler(api remote.AccountService) *EventsHandler { return &EventsHandler{ api: api, + formatFunc: func(tag string, i int, o any) string { + return fmt.Sprintf("%d %v", i, o) + }, } } From 0d47c096e0d66ee136df57c535a9f81ec178809c Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 13 Jan 2025 18:15:54 +0000 Subject: [PATCH 03/14] Implement event handlers in dev emitter --- cmd/main.go | 34 +++++++++++++++++++++++++++------- go.mod | 4 ++-- go.sum | 8 ++++---- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index c37a9c6..f3d5bf1 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -10,15 +10,18 @@ import ( "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/resource" + "git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/lang" "git.grassecon.net/grassrootseconomics/sarafu-vise/config" "git.grassecon.net/grassrootseconomics/visedriver/storage" httpremote "git.grassecon.net/grassrootseconomics/sarafu-api/remote/http" devremote "git.grassecon.net/grassrootseconomics/sarafu-api/dev" "git.grassecon.net/grassrootseconomics/sarafu-api/remote" - "git.grassecon.net/grassrootseconomics/sarafu-api/event" + 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" ) var ( @@ -27,23 +30,33 @@ var ( menuSeparator = ": " ) +type devEmitter struct { + h *event.EventsHandler + store *store.UserDataStore + pe *persist.Persister +} + // WIP: placeholder emitter, should perform same action as events -func emitter(ctx context.Context, msg event.Msg) error { - if msg.Typ == event.EventTokenTransferTag { +func (d *devEmitter) emit(ctx context.Context, msg apievent.Msg) error { + var err error + if msg.Typ == apievent.EventTokenTransferTag { tx, ok := msg.Item.(devremote.Tx) if !ok { return fmt.Errorf("not a valid tx") } logg.InfoCtxf(ctx, "tx emit", "tx", tx) - } else if msg.Typ == event.EventRegistrationTag { + ev := tx.ToTransferEvent() + err = d.h.HandleTokenTransfer(ctx, d.store, &ev) + } else if msg.Typ == apievent.EventRegistrationTag { acc, ok := msg.Item.(devremote.Account) if !ok { return fmt.Errorf("not a valid tx") } logg.InfoCtxf(ctx, "account emit", "account", acc) - + ev := acc.ToRegistrationEvent() + err = d.h.HandleCustodialRegistration(ctx, d.store, d.pe, &ev) } - return nil + return err } func main() { @@ -148,7 +161,14 @@ func main() { } if fakeDir != "" { - svc := devremote.NewDevAccountService(ctx, fakeDir).WithAutoVoucher(ctx, "FOO", 42).WithEmitter(emitter) + svc := devremote.NewDevAccountService(ctx, fakeDir).WithAutoVoucher(ctx, "FOO", 42) + emitter := &devEmitter{ + h: event.NewEventsHandler(svc), + store: &store.UserDataStore{ + Db: userdatastore, + }, + } + svc = svc.WithEmitter(emitter.emit) svc.AddVoucher(ctx, "BAR") accountService = svc } else { diff --git a/go.mod b/go.mod index 13226af..a04f432 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,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-20250112155828-b55686e830fd - git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113085238-f65abf625d34 + git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 + git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113181351-551195543805 git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113103142-5bf0a0e85893 git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d github.com/alecthomas/assert/v2 v2.2.2 diff --git a/go.sum b/go.sum index 2425992..32d83c5 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,9 @@ git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d h1:bPAOVZOX4frSGhfOdcj7kc555f8dc9DmMd2YAyC2AMw= git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck= -git.grassecon.net/grassrootseconomics/common v0.0.0-20250112155828-b55686e830fd h1:OG20aG/sC5yNaU2/rzoAKYp2f0u4lmGP5/Qax8UQD5c= -git.grassecon.net/grassrootseconomics/common v0.0.0-20250112155828-b55686e830fd/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60= -git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113085238-f65abf625d34 h1:Pl4OFKTeSehSTMMRUFosZUIDyUZRdXqm6CFn0+5+4qs= -git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113085238-f65abf625d34/go.mod h1:PEjJnAH5gYcjb4RRevaPCswa5VGGNqHSuyapZvIVR6w= +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-20250113181351-551195543805 h1:Okih7rAAjD8cpv7pi8QQc36mmp7SRBnOc7dc125GP/E= +git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113181351-551195543805/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/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d h1:q/NO1rEgK3pia32D/tCq5hXfEuJp84COZRwceFvy/MM= From fcce60e031881da1c42cafd6468bb70c239c591d Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 13 Jan 2025 20:19:34 +0000 Subject: [PATCH 04/14] Move out event handler encapsulation to api module --- cmd/main.go | 19 +++++------- go.mod | 2 +- go.sum | 4 +-- handlers/event/custodial.go | 32 +++++++++++-------- handlers/event/event.go | 20 ++++++++++-- handlers/event/token.go | 61 +++++++++++++++++++++---------------- 6 files changed, 82 insertions(+), 56 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index f3d5bf1..249b087 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -10,7 +10,6 @@ import ( "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/resource" - "git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/lang" "git.grassecon.net/grassrootseconomics/sarafu-vise/config" "git.grassecon.net/grassrootseconomics/visedriver/storage" @@ -31,12 +30,9 @@ var ( ) type devEmitter struct { - h *event.EventsHandler - store *store.UserDataStore - pe *persist.Persister + h *apievent.EventsHandler } -// WIP: placeholder emitter, should perform same action as events func (d *devEmitter) emit(ctx context.Context, msg apievent.Msg) error { var err error if msg.Typ == apievent.EventTokenTransferTag { @@ -46,7 +42,7 @@ func (d *devEmitter) emit(ctx context.Context, msg apievent.Msg) error { } logg.InfoCtxf(ctx, "tx emit", "tx", tx) ev := tx.ToTransferEvent() - err = d.h.HandleTokenTransfer(ctx, d.store, &ev) + err = d.h.Handle(ctx, apievent.EventTokenTransferTag, &ev) } else if msg.Typ == apievent.EventRegistrationTag { acc, ok := msg.Item.(devremote.Account) if !ok { @@ -54,7 +50,7 @@ func (d *devEmitter) emit(ctx context.Context, msg apievent.Msg) error { } logg.InfoCtxf(ctx, "account emit", "account", acc) ev := acc.ToRegistrationEvent() - err = d.h.HandleCustodialRegistration(ctx, d.store, d.pe, &ev) + err = d.h.Handle(ctx, apievent.EventRegistrationTag, &ev) } return err } @@ -162,11 +158,12 @@ func main() { if fakeDir != "" { svc := devremote.NewDevAccountService(ctx, fakeDir).WithAutoVoucher(ctx, "FOO", 42) + userStore := &store.UserDataStore{ + Db: userdatastore, + } + eu := event.NewEventsUpdater(svc, userStore, pe) emitter := &devEmitter{ - h: event.NewEventsHandler(svc), - store: &store.UserDataStore{ - Db: userdatastore, - }, + h: eu.ToEventsHandler(), } svc = svc.WithEmitter(emitter.emit) svc.AddVoucher(ctx, "BAR") diff --git a/go.mod b/go.mod index a04f432..a827c9c 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ 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-20250113181351-551195543805 + 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/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d github.com/alecthomas/assert/v2 v2.2.2 diff --git a/go.sum b/go.sum index 32d83c5..319816f 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ 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-20250113181351-551195543805 h1:Okih7rAAjD8cpv7pi8QQc36mmp7SRBnOc7dc125GP/E= -git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113181351-551195543805/go.mod h1:e9Damfk0euyjWzn+CE8uxgg8p4ggd07wp99SOyEhGXY= +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/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d h1:q/NO1rEgK3pia32D/tCq5hXfEuJp84COZRwceFvy/MM= diff --git a/handlers/event/custodial.go b/handlers/event/custodial.go index b0c6c99..9eb9f73 100644 --- a/handlers/event/custodial.go +++ b/handlers/event/custodial.go @@ -2,8 +2,8 @@ package event import ( "context" + "fmt" - "git.defalsify.org/vise.git/persist" apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event" "git.grassecon.net/grassrootseconomics/sarafu-vise/store" ) @@ -18,16 +18,24 @@ const ( // TODO: implement account created in userstore instead, so that // the need for persister and state use here is eliminated (it // introduces concurrency risks) -func (eh *EventsHandler) HandleCustodialRegistration(ctx context.Context, userStore *store.UserDataStore, pr *persist.Persister, ev *apievent.EventCustodialRegistration) error { - identity, err := store.IdentityFromAddress(ctx, userStore, ev.Account) - if err != nil { - return err +func (eh *EventsUpdater) handleCustodialRegistration(ctx context.Context, ev any) error { + o, ok := ev.(*apievent.EventCustodialRegistration) + if !ok { + fmt.Errorf("invalid event for custodial registration") } - err = pr.Load(identity.SessionId) - if err != nil { - return err - } - st := pr.GetState() - st.SetFlag(accountCreatedFlag) - return pr.Save(identity.SessionId) + 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) + if err != nil { + return err + } + err = eh.pe.Load(identity.SessionId) + if err != nil { + return err + } + st := eh.pe.GetState() + st.SetFlag(accountCreatedFlag) + return eh.pe.Save(identity.SessionId) } diff --git a/handlers/event/event.go b/handlers/event/event.go index f068f67..c9441b4 100644 --- a/handlers/event/event.go +++ b/handlers/event/event.go @@ -3,19 +3,33 @@ package event import ( "fmt" + "git.defalsify.org/vise.git/persist" + "git.grassecon.net/grassrootseconomics/sarafu-vise/store" "git.grassecon.net/grassrootseconomics/sarafu-api/remote" + apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event" ) -type EventsHandler struct { +type EventsUpdater struct { api remote.AccountService formatFunc func(string, int, any) string + store *store.UserDataStore + pe *persist.Persister } -func NewEventsHandler(api remote.AccountService) *EventsHandler { - return &EventsHandler{ +func NewEventsUpdater(api remote.AccountService, store *store.UserDataStore, pe *persist.Persister) *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, } } + +func (eu *EventsUpdater) ToEventsHandler() *apievent.EventsHandler { + eh := apievent.NewEventsHandler() + eh = eh.WithHandler(apievent.EventTokenTransferTag, eu.handleTokenTransfer) + eh = eh.WithHandler(apievent.EventRegistrationTag, eu.handleCustodialRegistration) + return eh +} diff --git a/handlers/event/token.go b/handlers/event/token.go index bf5075e..0077ac4 100644 --- a/handlers/event/token.go +++ b/handlers/event/token.go @@ -2,6 +2,7 @@ package event import ( "context" + "fmt" "strings" "git.defalsify.org/vise.git/db" @@ -12,14 +13,14 @@ import ( ) // execute all -func (eh *EventsHandler) updateToken(ctx context.Context, userStore *store.UserDataStore, identity identity.Identity, tokenAddress string) error { - err := eh.updateTokenList(ctx, userStore, identity) +func (eu *EventsUpdater) updateToken(ctx context.Context, identity identity.Identity, tokenAddress string) error { + err := eu.updateTokenList(ctx, identity) if err != nil { return err } - userStore.Db.SetSession(identity.SessionId) - activeSym, err := userStore.ReadEntry(ctx, identity.SessionId, storedb.DATA_ACTIVE_SYM) + eu.store.Db.SetSession(identity.SessionId) + activeSym, err := eu.store.ReadEntry(ctx, identity.SessionId, storedb.DATA_ACTIVE_SYM) if err == nil { return nil } @@ -27,18 +28,18 @@ func (eh *EventsHandler) updateToken(ctx context.Context, userStore *store.UserD return err } if activeSym == nil { - activeSym, err = eh.toSym(ctx, tokenAddress) + activeSym, err = eu.toSym(ctx, tokenAddress) if err != nil { return err } } - err = updateDefaultToken(ctx, userStore, identity, string(activeSym)) + err = eu.updateDefaultToken(ctx, identity, string(activeSym)) if err != nil { return err } - err = eh.updateTokenTransferList(ctx, userStore, identity) + err = eu.updateTokenTransferList(ctx, identity) if err != nil { return err } @@ -48,41 +49,48 @@ func (eh *EventsHandler) updateToken(ctx context.Context, userStore *store.UserD // set default token to given symbol. -func updateDefaultToken(ctx context.Context, userStore *store.UserDataStore, identity identity.Identity, activeSym string) error { - pfxDb := toPrefixDb(userStore, identity.SessionId) +func (eu *EventsUpdater) updateDefaultToken(ctx context.Context, identity identity.Identity, activeSym string) error { + pfxDb := toPrefixDb(eu.store, 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, userStore, identity.SessionId, tokenData) + return store.UpdateVoucherData(ctx, eu.store, identity.SessionId, tokenData) } // 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 *EventsHandler) HandleTokenTransfer(ctx context.Context, userStore *store.UserDataStore, ev *apievent.EventTokenTransfer) error { - identity, err := store.IdentityFromAddress(ctx, userStore, ev.From) +func (eh *EventsUpdater) handleTokenTransfer(ctx context.Context, ev any) error { + o, ok := ev.(*apievent.EventTokenTransfer) + if !ok { + fmt.Errorf("invalid event for custodial registration") + } + 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) if err != nil { if !db.IsNotFound(err) { return err } } else { - err = eh.updateToken(ctx, userStore, identity, ev.VoucherAddress) + err = eu.updateToken(ctx, identity, ev.VoucherAddress) if err != nil { return err } } if strings.Compare(ev.To, ev.From) != 0 { - identity, err = store.IdentityFromAddress(ctx, userStore, ev.To) + identity, err = store.IdentityFromAddress(ctx, eu.store, ev.To) if err != nil { if !db.IsNotFound(err) { return err } } else { - err = eh.updateToken(ctx, userStore, identity, ev.VoucherAddress) + err = eu.updateToken(ctx, identity, ev.VoucherAddress) if err != nil { return err } @@ -93,14 +101,14 @@ func (eh *EventsHandler) HandleTokenTransfer(ctx context.Context, userStore *sto } // handle token mint. -func (eh *EventsHandler) HandleTokenMint(ctx context.Context, userStore *store.UserDataStore, ev *apievent.EventTokenMint) error { - identity, err := store.IdentityFromAddress(ctx, userStore, ev.To) +func (eu *EventsUpdater) HandleTokenMint(ctx context.Context, ev *apievent.EventTokenMint) error { + identity, err := store.IdentityFromAddress(ctx, eu.store, ev.To) if err != nil { if !db.IsNotFound(err) { return err } } else { - err = eh.updateToken(ctx, userStore, identity, ev.VoucherAddress) + err = eu.updateToken(ctx, identity, ev.VoucherAddress) if err != nil { return err } @@ -109,7 +117,7 @@ func (eh *EventsHandler) HandleTokenMint(ctx context.Context, userStore *store.U } // use api to resolve address to token symbol. -func (ev *EventsHandler) toSym(ctx context.Context, address string) ([]byte, error) { +func (ev *EventsUpdater) toSym(ctx context.Context, address string) ([]byte, error) { voucherData, err := ev.api.VoucherData(ctx, address) if err != nil { return nil, err @@ -118,8 +126,8 @@ func (ev *EventsHandler) toSym(ctx context.Context, address string) ([]byte, err } // refresh and store token list. -func (eh *EventsHandler) updateTokenList(ctx context.Context, userStore *store.UserDataStore, identity identity.Identity) error { - holdings, err := eh.api.FetchVouchers(ctx, identity.ChecksumAddress) +func (eu *EventsUpdater) updateTokenList(ctx context.Context, identity identity.Identity) error { + holdings, err := eu.api.FetchVouchers(ctx, identity.ChecksumAddress) if err != nil { return err } @@ -128,7 +136,7 @@ func (eh *EventsHandler) updateTokenList(ctx context.Context, userStore *store.U // TODO: make sure subprefixdb is thread safe when using gdbm // TODO: why is address session here unless explicitly set - pfxDb := toPrefixDb(userStore, identity.SessionId) + pfxDb := toPrefixDb(eu.store, identity.SessionId) typ := storedb.ToBytes(storedb.DATA_VOUCHER_SYMBOLS) err = pfxDb.Put(ctx, typ, []byte(metadata.Symbols)) @@ -158,22 +166,21 @@ func (eh *EventsHandler) updateTokenList(ctx context.Context, userStore *store.U } // refresh and store transaction history. -func (eh *EventsHandler) updateTokenTransferList(ctx context.Context, userStore *store.UserDataStore, identity identity.Identity) error { +func (eu *EventsUpdater) updateTokenTransferList(ctx context.Context, identity identity.Identity) error { var r []string - txs, err := eh.api.FetchTransactions(ctx, identity.ChecksumAddress) + txs, err := eu.api.FetchTransactions(ctx, identity.ChecksumAddress) if err != nil { return err } for i, tx := range(txs) { - //r = append(r, formatTransaction(i, tx)) - r = append(r, eh.formatFunc(apievent.EventTokenTransferTag, i, tx)) + r = append(r, eu.formatFunc(apievent.EventTokenTransferTag, i, tx)) } s := strings.Join(r, "\n") - return userStore.WriteEntry(ctx, identity.SessionId, storedb.DATA_TRANSACTIONS, []byte(s)) + return eu.store.WriteEntry(ctx, identity.SessionId, storedb.DATA_TRANSACTIONS, []byte(s)) } func toPrefixDb(userStore *store.UserDataStore, sessionId string) storedb.PrefixDb { From 63eb803022afe1780bd1874c58e371e8115786c2 Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 13 Jan 2025 21:39:15 +0000 Subject: [PATCH 05/14] Use storage service in events updater --- cmd/main.go | 6 +---- go.mod | 4 +-- go.sum | 8 +++--- handlers/event/custodial.go | 14 +++++++---- handlers/event/event.go | 34 ++++++++++++++++++++++--- handlers/event/token.go | 50 +++++++++++++++++++++---------------- 6 files changed, 75 insertions(+), 41 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 249b087..dbe1508 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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(), } diff --git a/go.mod b/go.mod index a827c9c..d595c5f 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index 319816f..d36f320 100644 --- a/go.sum +++ b/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= diff --git a/handlers/event/custodial.go b/handlers/event/custodial.go index 9eb9f73..4c0d030 100644 --- a/handlers/event/custodial.go +++ b/handlers/event/custodial.go @@ -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) } diff --git a/handlers/event/event.go b/handlers/event/event.go index c9441b4..d75b5a2 100644 --- a/handlers/event/event.go +++ b/handlers/event/event.go @@ -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 +} diff --git a/handlers/event/token.go b/handlers/event/token.go index 0077ac4..8758108 100644 --- a/handlers/event/token.go +++ b/handlers/event/token.go @@ -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 { From f67edfb5411d4e986fc79b79e8637d7e38a5087a Mon Sep 17 00:00:00 2001 From: lash Date: Mon, 13 Jan 2025 22:00:20 +0000 Subject: [PATCH 06/14] Add missing return for error in custodial handler event --- handlers/event/custodial.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handlers/event/custodial.go b/handlers/event/custodial.go index 4c0d030..dd51e9b 100644 --- a/handlers/event/custodial.go +++ b/handlers/event/custodial.go @@ -21,7 +21,7 @@ const ( func (eh *EventsUpdater) handleCustodialRegistration(ctx context.Context, ev any) error { o, ok := ev.(*apievent.EventCustodialRegistration) if !ok { - fmt.Errorf("invalid event for custodial registration") + return fmt.Errorf("invalid event for custodial registration") } return eh.HandleCustodialRegistration(ctx, o) } From ce9a263b40a3f7f5932bbbd65ae56b26020e7f7c Mon Sep 17 00:00:00 2001 From: lash Date: Tue, 14 Jan 2025 07:02:42 +0000 Subject: [PATCH 07/14] Add event support for token mint --- handlers/event/event.go | 2 +- handlers/event/token.go | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/handlers/event/event.go b/handlers/event/event.go index d75b5a2..bacda01 100644 --- a/handlers/event/event.go +++ b/handlers/event/event.go @@ -34,9 +34,9 @@ func NewEventsUpdater(api remote.AccountService, store storage.StorageService) * func (eu *EventsUpdater) ToEventsHandler() *apievent.EventsHandler { eh := apievent.NewEventsHandler() + eh = eh.WithHandler(apievent.EventTokenMintTag, eu.handleTokenMint) eh = eh.WithHandler(apievent.EventTokenTransferTag, eu.handleTokenTransfer) eh = eh.WithHandler(apievent.EventRegistrationTag, eu.handleCustodialRegistration) - eh = eh.WithHandler(apievent.EventTokenMintTag, eu.handleNoop) return eh } diff --git a/handlers/event/token.go b/handlers/event/token.go index 8758108..ef85800 100644 --- a/handlers/event/token.go +++ b/handlers/event/token.go @@ -66,7 +66,7 @@ func (eu *EventsUpdater) updateDefaultToken(ctx context.Context, identity identi func (eh *EventsUpdater) handleTokenTransfer(ctx context.Context, ev any) error { o, ok := ev.(*apievent.EventTokenTransfer) if !ok { - fmt.Errorf("invalid event for custodial registration") + fmt.Errorf("invalid event for token transfer") } return eh.HandleTokenTransfer(ctx, o) } @@ -105,6 +105,13 @@ func (eu *EventsUpdater) HandleTokenTransfer(ctx context.Context, ev *apievent.E } // handle token mint. +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 { _, userStore, err := eu.getStore(ctx) if err != nil { From 55356a0c64f598e02133357fa5db38f697efa721 Mon Sep 17 00:00:00 2001 From: lash Date: Tue, 14 Jan 2025 08:39:24 +0000 Subject: [PATCH 08/14] Remove userstore change with custodial event --- handlers/event/custodial.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/handlers/event/custodial.go b/handlers/event/custodial.go index dd51e9b..bcab999 100644 --- a/handlers/event/custodial.go +++ b/handlers/event/custodial.go @@ -27,7 +27,7 @@ func (eh *EventsUpdater) handleCustodialRegistration(ctx context.Context, ev any } func (eu *EventsUpdater) HandleCustodialRegistration(ctx context.Context, ev *apievent.EventCustodialRegistration) error { - pe, userStore, err := eu.getStore(ctx) + _, userStore, err := eu.getStore(ctx) if err != nil { return err } @@ -35,11 +35,13 @@ func (eu *EventsUpdater) HandleCustodialRegistration(ctx context.Context, ev *ap if err != nil { return err } - err = pe.Load(identity.SessionId) - if err != nil { - return err - } - st := pe.GetState() - st.SetFlag(accountCreatedFlag) - return pe.Save(identity.SessionId) +// err = pe.Load(identity.SessionId) +// if err != nil { +// return err +// } +// st := pe.GetState() +// st.SetFlag(accountCreatedFlag) +// return pe.Save(identity.SessionId) + logg.DebugCtxf(ctx, "received custodial registration event", "identity", identity) + return nil } From 960a3d90757b7ff54e0218cd99d1982687f57939 Mon Sep 17 00:00:00 2001 From: lash Date: Tue, 14 Jan 2025 14:27:28 +0000 Subject: [PATCH 09/14] Implement alias dev api --- cmd/main.go | 4 +++- go.mod | 2 +- go.sum | 4 ++-- handlers/application/menuhandler.go | 2 ++ handlers/application/menuhandler_test.go | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index dbe1508..c40c337 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -156,7 +156,9 @@ func main() { } if fakeDir != "" { - svc := devremote.NewDevAccountService(ctx, fakeDir).WithAutoVoucher(ctx, "FOO", 42) + svc := devremote.NewDevAccountService() + svc = svc.WithFs(ctx, fakeDir) + svc = svc.WithAutoVoucher(ctx, "FOO", 42) eu := event.NewEventsUpdater(svc, menuStorageService) emitter := &devEmitter{ h: eu.ToEventsHandler(), diff --git a/go.mod b/go.mod index d595c5f..c00160d 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ 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-20250113213645-7c697394b5e7 + git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250114142051-9645a2af7bef 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 diff --git a/go.sum b/go.sum index d36f320..b4bb1f9 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ 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-20250113213645-7c697394b5e7 h1:P+Bi5jcQbnCrLnKNC5k8XDTBsocyZTVifqDyZY6d0w8= -git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250113213645-7c697394b5e7/go.mod h1:X9aQF93xw3vcW2QftJfzLewvbotRM0U00DRtdmFw294= +git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250114142051-9645a2af7bef h1:ukxNTuI+MF6ic2SkRlc8ZjlnsTR8g6owuHkAvnQNY8A= +git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250114142051-9645a2af7bef/go.mod h1:BRIbXv0aulFWqR2XV87haWB4oNRlVEu8KyldLQsAroQ= 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= diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index c9a1103..d82a417 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -1076,6 +1076,8 @@ func (h *MenuHandlers) ValidateBlockedNumber(ctx context.Context, sym string, in } // ValidateRecipient validates that the given input is valid. +// +// TODO: split up functino func (h *MenuHandlers) ValidateRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result store := h.userdataStore diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index eda04d6..cf5b4a5 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -1698,7 +1698,7 @@ func TestValidateRecipient(t *testing.T) { accountService: mockAccountService, } - aliasResponse := &dataserviceapi.AliasAddress{ + aliasResponse := &models.AliasAddress{ Address: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9", } From 26b32a8b93ce41f78ed2bdc20146c4b8ceaf82e1 Mon Sep 17 00:00:00 2001 From: lash Date: Tue, 14 Jan 2025 15:34:17 +0000 Subject: [PATCH 10/14] Introduce initial language code setting --- handlers/application/menuhandler.go | 19 ++++++++++++++++++- store/db/db.go | 2 ++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index d82a417..fa0c532 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -2187,6 +2187,23 @@ func (h *MenuHandlers) resetIncorrectPINAttempts(ctx context.Context, sessionId return nil } +func (h *MenuHandlers) persistInitialLanguageCode(ctx context.Context, sessionId string, code string) error { + store := h.userdataStore + _, err := store.ReadEntry(ctx, sessionId, storedb.DATA_INITIAL_LANGUAGE_CODE) + if err == nil { + return nil + } + if !db.IsNotFound(err) { + return err + } + err = store.WriteEntry(ctx, sessionId, storedb.DATA_SELECTED_LANGUAGE_CODE, []byte(code)) + if err != nil { + logg.ErrorCtxf(ctx, "failed to persist initial language code", "key", storedb.DATA_INITIAL_LANGUAGE_CODE, "value", code, "error", err) + return err + } + return nil +} + // persistLanguageCode persists the selected ISO 639 language code func (h *MenuHandlers) persistLanguageCode(ctx context.Context, code string) error { store := h.userdataStore @@ -2199,5 +2216,5 @@ func (h *MenuHandlers) persistLanguageCode(ctx context.Context, code string) err logg.ErrorCtxf(ctx, "failed to persist language code", "key", storedb.DATA_SELECTED_LANGUAGE_CODE, "value", code, "error", err) return err } - return nil + return h.persistInitialLanguageCode(ctx, sessionId, code) } diff --git a/store/db/db.go b/store/db/db.go index b70cc5e..2ccd9e6 100644 --- a/store/db/db.go +++ b/store/db/db.go @@ -59,6 +59,8 @@ const ( DATA_INCORRECT_PIN_ATTEMPTS //ISO 639 code for the selected language. DATA_SELECTED_LANGUAGE_CODE + //ISO 639 code for the language initially selected. + DATA_INITIAL_LANGUAGE_CODE ) const ( From 604f3103c7f5dd40d6c35f3b2c9f526e5cc0aa3f Mon Sep 17 00:00:00 2001 From: lash Date: Tue, 14 Jan 2025 15:44:14 +0000 Subject: [PATCH 11/14] Fix wrong key for initial language write, add test --- handlers/application/menuhandler.go | 2 +- handlers/application/menuhandler_test.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index fa0c532..28f6854 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -2196,7 +2196,7 @@ func (h *MenuHandlers) persistInitialLanguageCode(ctx context.Context, sessionId if !db.IsNotFound(err) { return err } - err = store.WriteEntry(ctx, sessionId, storedb.DATA_SELECTED_LANGUAGE_CODE, []byte(code)) + err = store.WriteEntry(ctx, sessionId, storedb.DATA_INITIAL_LANGUAGE_CODE, []byte(code)) if err != nil { logg.ErrorCtxf(ctx, "failed to persist initial language code", "key", storedb.DATA_INITIAL_LANGUAGE_CODE, "value", code, "error", err) return err diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index cf5b4a5..3244ce4 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -825,6 +825,17 @@ func TestSetLanguage(t *testing.T) { // Assert that the Result FlagSet has the required flags after language switch assert.Equal(t, res, tt.expectedResult, "Result should match expected result") + code, err := store.ReadEntry(ctx, sessionId, storedb.DATA_SELECTED_LANGUAGE_CODE) + if err != nil { + t.Error(err) + } + + assert.Equal(t, string(code), tt.expectedResult.Content) + code, err = store.ReadEntry(ctx, sessionId, storedb.DATA_INITIAL_LANGUAGE_CODE) + if err != nil { + t.Error(err) + } + assert.Equal(t, string(code), "eng") }) } } From ac00e4619ef8c38317177e058bfbdfd335515499 Mon Sep 17 00:00:00 2001 From: lash Date: Wed, 15 Jan 2025 00:16:03 +0000 Subject: [PATCH 12/14] Toggle services with build tags, cmd now runs with dev api --- cmd/main.go | 50 ++-------------------------------------------- go.mod | 6 +++--- go.sum | 12 +++++------ services/local.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++ services/remote.go | 15 ++++++++++++++ 5 files changed, 76 insertions(+), 57 deletions(-) create mode 100644 services/local.go create mode 100644 services/remote.go diff --git a/cmd/main.go b/cmd/main.go index c40c337..81aad6f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -13,13 +13,9 @@ import ( "git.defalsify.org/vise.git/lang" "git.grassecon.net/grassrootseconomics/sarafu-vise/config" "git.grassecon.net/grassrootseconomics/visedriver/storage" - httpremote "git.grassecon.net/grassrootseconomics/sarafu-api/remote/http" - devremote "git.grassecon.net/grassrootseconomics/sarafu-api/dev" - "git.grassecon.net/grassrootseconomics/sarafu-api/remote" - apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event" + "git.grassecon.net/grassrootseconomics/sarafu-vise/services" "git.grassecon.net/grassrootseconomics/sarafu-vise/args" "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers" - "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/event" ) var ( @@ -28,37 +24,9 @@ var ( menuSeparator = ": " ) -type devEmitter struct { - h *apievent.EventsHandler -} - -func (d *devEmitter) emit(ctx context.Context, msg apievent.Msg) error { - var err error - if msg.Typ == apievent.EventTokenTransferTag { - tx, ok := msg.Item.(devremote.Tx) - if !ok { - return fmt.Errorf("not a valid tx") - } - logg.InfoCtxf(ctx, "tx emit", "tx", tx) - ev := tx.ToTransferEvent() - err = d.h.Handle(ctx, apievent.EventTokenTransferTag, &ev) - } else if msg.Typ == apievent.EventRegistrationTag { - acc, ok := msg.Item.(devremote.Account) - if !ok { - return fmt.Errorf("not a valid tx") - } - logg.InfoCtxf(ctx, "account emit", "account", acc) - ev := acc.ToRegistrationEvent() - err = d.h.Handle(ctx, apievent.EventRegistrationTag, &ev) - } - return err -} - func main() { config.LoadConfig() - var accountService remote.AccountService - var fakeDir string var connStr string var size uint var sessionId string @@ -71,7 +39,6 @@ func main() { flag.StringVar(&resourceDir, "resourcedir", scriptDir, "resource dir") flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&connStr, "c", "", "connection string") - flag.StringVar(&fakeDir, "fakedir", "", "if valid path, enables fake api with fsdb backend") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&gettextDir, "gettext", "", "use gettext translations from given directory") @@ -155,20 +122,7 @@ func main() { os.Exit(1) } - if fakeDir != "" { - svc := devremote.NewDevAccountService() - svc = svc.WithFs(ctx, fakeDir) - svc = svc.WithAutoVoucher(ctx, "FOO", 42) - eu := event.NewEventsUpdater(svc, menuStorageService) - emitter := &devEmitter{ - h: eu.ToEventsHandler(), - } - svc = svc.WithEmitter(emitter.emit) - svc.AddVoucher(ctx, "BAR") - accountService = svc - } else { - accountService = &httpremote.HTTPAccountService{} - } + accountService := services.New(ctx, menuStorageService, connData) hl, err := lhs.GetHandler(accountService) if err != nil { fmt.Fprintf(os.Stderr, "get accounts service handler: %v\n", err) diff --git a/go.mod b/go.mod index c00160d..bf2af0f 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,10 @@ module git.grassecon.net/grassrootseconomics/sarafu-vise go 1.23.4 require ( - git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d + git.defalsify.org/vise.git v0.2.3-0.20250115000535-e2d329b3f739 git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 - git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250114142051-9645a2af7bef - git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b + git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250115001238-7001d1f828aa + git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115001111-24e729d27566 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 diff --git a/go.sum b/go.sum index b4bb1f9..2380f5d 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,11 @@ -git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d h1:bPAOVZOX4frSGhfOdcj7kc555f8dc9DmMd2YAyC2AMw= -git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck= +git.defalsify.org/vise.git v0.2.3-0.20250115000535-e2d329b3f739 h1:w7pj1oh7jXrfajahVYU7m7AfHst4C6jNVzDVoaqJ7e8= +git.defalsify.org/vise.git v0.2.3-0.20250115000535-e2d329b3f739/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-20250114142051-9645a2af7bef h1:ukxNTuI+MF6ic2SkRlc8ZjlnsTR8g6owuHkAvnQNY8A= -git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250114142051-9645a2af7bef/go.mod h1:BRIbXv0aulFWqR2XV87haWB4oNRlVEu8KyldLQsAroQ= -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/sarafu-api v0.0.0-20250115001238-7001d1f828aa h1:V1K4CqA2lYJVKv5uPIZ8zy0pIWUM78+M8xQnum2C5aM= +git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250115001238-7001d1f828aa/go.mod h1:yT/NvEiicBoWWRmOvBp1nT5YED3akTVdeZ2Ntq4hOFg= +git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115001111-24e729d27566 h1:+Mrd6e7KnWpnzMTjwmPyXgF4+enAelmDbAqRzhq1aAM= +git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115001111-24e729d27566/go.mod h1:Syw9TZyigPAM7t9FvicOm36iUnidt45f0SxzT2JniQ8= 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= diff --git a/services/local.go b/services/local.go new file mode 100644 index 0000000..e79c8fb --- /dev/null +++ b/services/local.go @@ -0,0 +1,50 @@ +// +build !online + +package services + +import ( + "fmt" + "context" + + "git.grassecon.net/grassrootseconomics/visedriver/storage" + devremote "git.grassecon.net/grassrootseconomics/sarafu-api/dev" + "git.grassecon.net/grassrootseconomics/sarafu-api/remote" + apievent "git.grassecon.net/grassrootseconomics/sarafu-api/event" + "git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/event" +) + +type localEmitter struct { + h *apievent.EventsHandler +} + +func (d *localEmitter) emit(ctx context.Context, msg apievent.Msg) error { + var err error + if msg.Typ == apievent.EventTokenTransferTag { + tx, ok := msg.Item.(devremote.Tx) + if !ok { + return fmt.Errorf("not a valid tx") + } + ev := tx.ToTransferEvent() + err = d.h.Handle(ctx, apievent.EventTokenTransferTag, &ev) + } else if msg.Typ == apievent.EventRegistrationTag { + acc, ok := msg.Item.(devremote.Account) + if !ok { + return fmt.Errorf("not a valid tx") + } + ev := acc.ToRegistrationEvent() + err = d.h.Handle(ctx, apievent.EventRegistrationTag, &ev) + } + return err +} + +func New(ctx context.Context, storageService storage.StorageService, conn storage.ConnData) remote.AccountService { + svc := devremote.NewDevAccountService(ctx, storageService) + svc = svc.WithAutoVoucher(ctx, "FOO", 42) + eu := event.NewEventsUpdater(svc, storageService) + emitter := &localEmitter{ + h: eu.ToEventsHandler(), + } + svc = svc.WithEmitter(emitter.emit) + svc.AddVoucher(ctx, "BAR") + return svc +} diff --git a/services/remote.go b/services/remote.go new file mode 100644 index 0000000..2d31217 --- /dev/null +++ b/services/remote.go @@ -0,0 +1,15 @@ +// +build online + +package services + +import ( + "context" + + "git.grassecon.net/grassrootseconomics/visedriver/storage" + "git.grassecon.net/grassrootseconomics/sarafu-api/remote" + httpremote "git.grassecon.net/grassrootseconomics/sarafu-api/remote/http" +) + +func New(ctx context.Context, storageService storage.StorageService, conn storage.ConnData) remote.AccountService { + return &httpremote.HTTPAccountService{} +} From ac492e8403cfe33e01b418fb377906d3abcf196c Mon Sep 17 00:00:00 2001 From: lash Date: Wed, 15 Jan 2025 00:41:04 +0000 Subject: [PATCH 13/14] update deps --- go.mod | 4 ++-- go.sum | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index bf2af0f..4edb4ea 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.23.4 require ( git.defalsify.org/vise.git v0.2.3-0.20250115000535-e2d329b3f739 git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 - git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250115001238-7001d1f828aa - git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115001111-24e729d27566 + git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250115003359-c128678f4a6d + git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115003256-c0534ede1b63 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 diff --git a/go.sum b/go.sum index 2380f5d..f70ba59 100644 --- a/go.sum +++ b/go.sum @@ -2,10 +2,10 @@ git.defalsify.org/vise.git v0.2.3-0.20250115000535-e2d329b3f739 h1:w7pj1oh7jXrfa git.defalsify.org/vise.git v0.2.3-0.20250115000535-e2d329b3f739/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-20250115001238-7001d1f828aa h1:V1K4CqA2lYJVKv5uPIZ8zy0pIWUM78+M8xQnum2C5aM= -git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250115001238-7001d1f828aa/go.mod h1:yT/NvEiicBoWWRmOvBp1nT5YED3akTVdeZ2Ntq4hOFg= -git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115001111-24e729d27566 h1:+Mrd6e7KnWpnzMTjwmPyXgF4+enAelmDbAqRzhq1aAM= -git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115001111-24e729d27566/go.mod h1:Syw9TZyigPAM7t9FvicOm36iUnidt45f0SxzT2JniQ8= +git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250115003359-c128678f4a6d h1:lKzJH3rSEfhpoC87lC9wqHQtJpN7LII3Z4g9U81OIyc= +git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250115003359-c128678f4a6d/go.mod h1:tbA4whUGMUIDgQVdIW0sxWPuuXOvZRSny5zeku5hX4k= +git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115003256-c0534ede1b63 h1:bX7klKZpX+ZZu1LKbtOXDAhV/KK0YwExehiIi0jusAM= +git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115003256-c0534ede1b63/go.mod h1:Syw9TZyigPAM7t9FvicOm36iUnidt45f0SxzT2JniQ8= 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= From f8b38dd2102b3f0999427786afce1069e8dd78f2 Mon Sep 17 00:00:00 2001 From: lash Date: Wed, 15 Jan 2025 07:26:34 +0000 Subject: [PATCH 14/14] Upgrade deps --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 4edb4ea..9148b5f 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.23.4 require ( git.defalsify.org/vise.git v0.2.3-0.20250115000535-e2d329b3f739 git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 - git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250115003359-c128678f4a6d + git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250115072214-bca7c5de969f git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115003256-c0534ede1b63 git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d github.com/alecthomas/assert/v2 v2.2.2 diff --git a/go.sum b/go.sum index f70ba59..c450543 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ git.defalsify.org/vise.git v0.2.3-0.20250115000535-e2d329b3f739 h1:w7pj1oh7jXrfa git.defalsify.org/vise.git v0.2.3-0.20250115000535-e2d329b3f739/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-20250115003359-c128678f4a6d h1:lKzJH3rSEfhpoC87lC9wqHQtJpN7LII3Z4g9U81OIyc= -git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250115003359-c128678f4a6d/go.mod h1:tbA4whUGMUIDgQVdIW0sxWPuuXOvZRSny5zeku5hX4k= +git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250115072214-bca7c5de969f h1:FgccQi8vipX6dUt+GRiRDYHMR3UqC+plz73vw7y3fyU= +git.grassecon.net/grassrootseconomics/sarafu-api v0.0.0-20250115072214-bca7c5de969f/go.mod h1:tbA4whUGMUIDgQVdIW0sxWPuuXOvZRSny5zeku5hX4k= git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115003256-c0534ede1b63 h1:bX7klKZpX+ZZu1LKbtOXDAhV/KK0YwExehiIi0jusAM= git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115003256-c0534ede1b63/go.mod h1:Syw9TZyigPAM7t9FvicOm36iUnidt45f0SxzT2JniQ8= git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250113103030-f0b2056fd87d h1:q/NO1rEgK3pia32D/tCq5hXfEuJp84COZRwceFvy/MM=