Add identity struct, WIP implement token event
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
|
||||
|
||||
"git.grassecon.net/urdt/ussd/common"
|
||||
"git.grassecon.net/term/lookup"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -9,7 +14,7 @@ const (
|
||||
)
|
||||
|
||||
type eventCustodialRegistration struct {
|
||||
account string
|
||||
Account string
|
||||
}
|
||||
|
||||
func asCustodialRegistrationEvent(gev *geEvent.Event) (*eventCustodialRegistration, bool) {
|
||||
@@ -19,7 +24,7 @@ func asCustodialRegistrationEvent(gev *geEvent.Event) (*eventCustodialRegistrati
|
||||
return nil, false
|
||||
}
|
||||
pl := gev.Payload
|
||||
ev.account, ok = pl["account"].(string)
|
||||
ev.Account, ok = pl["account"].(string)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
@@ -27,6 +32,11 @@ func asCustodialRegistrationEvent(gev *geEvent.Event) (*eventCustodialRegistrati
|
||||
return &ev, true
|
||||
}
|
||||
|
||||
func handleCustodialRegistration(ev *eventCustodialRegistration) error {
|
||||
func handleCustodialRegistration(ctx context.Context, store common.UserDataStore, ev *eventCustodialRegistration) error {
|
||||
identity, err := lookup.IdentityFromAddress(ctx, store, ev.Account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = identity
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ import (
|
||||
|
||||
nats "github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
|
||||
|
||||
"git.defalsify.org/vise.git/db"
|
||||
"git.grassecon.net/urdt/ussd/common"
|
||||
"git.grassecon.net/term/event"
|
||||
)
|
||||
|
||||
@@ -27,8 +27,14 @@ type NatsSubscription struct {
|
||||
cctx jetstream.ConsumeContext
|
||||
}
|
||||
|
||||
func NewNatsSubscription() *NatsSubscription {
|
||||
return &NatsSubscription{}
|
||||
func NewNatsSubscription(store db.Db) *NatsSubscription {
|
||||
return &NatsSubscription{
|
||||
Router: event.Router{
|
||||
Store: common.UserDataStore{
|
||||
Db: store,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func(n *NatsSubscription) Connect(ctx context.Context, connStr string) error {
|
||||
@@ -86,7 +92,7 @@ func(n *NatsSubscription) handleEvent(m jetstream.Msg) {
|
||||
logg.Error("nats msg deserialize fail", "err", err)
|
||||
//fail(m)
|
||||
} else {
|
||||
err = n.Route(&ev)
|
||||
err = n.Route(n.ctx, &ev)
|
||||
if err != nil {
|
||||
logg.Error("handler route fail", "err", err)
|
||||
//fail(m)
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"git.grassecon.net/urdt/ussd/common"
|
||||
|
||||
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
|
||||
)
|
||||
|
||||
@@ -18,13 +21,14 @@ var (
|
||||
)
|
||||
|
||||
type Router struct {
|
||||
Store common.UserDataStore
|
||||
}
|
||||
|
||||
func(r *Router) Route(gev *geEvent.Event) error {
|
||||
func(r *Router) Route(ctx context.Context, gev *geEvent.Event) error {
|
||||
logg.Debug("have event", "ev", gev)
|
||||
evCC, ok := asCustodialRegistrationEvent(gev)
|
||||
if ok {
|
||||
return handleCustodialRegistration(evCC)
|
||||
return handleCustodialRegistration(ctx, r.Store, evCC)
|
||||
}
|
||||
return fmt.Errorf("unexpected message")
|
||||
}
|
||||
|
||||
101
event/token.go
Normal file
101
event/token.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.defalsify.org/vise.git/db"
|
||||
"git.grassecon.net/urdt/ussd/common"
|
||||
"git.grassecon.net/urdt/ussd/remote"
|
||||
"git.grassecon.net/term/lookup"
|
||||
)
|
||||
|
||||
const (
|
||||
evTransfer = "TOKEN_TRANSFER"
|
||||
)
|
||||
|
||||
type eventTokenTransfer struct {
|
||||
From string
|
||||
To string
|
||||
Value string
|
||||
}
|
||||
|
||||
//func updateTokenTransferList(ctx context.Context, api remote.AccountServiceInterface, store common.UserDataStore, sessionId string) error {
|
||||
// return nil
|
||||
//}
|
||||
|
||||
func updateTokenList(ctx context.Context, api remote.AccountServiceInterface, store common.UserDataStore, identity lookup.Identity) error {
|
||||
api.FetchVouchers(ctx, identity.ChecksumAddress)
|
||||
return nil
|
||||
}
|
||||
|
||||
//func updateTokenBalance(ctx context.Context, api remote.AccountServiceInterface, store common.UserDataStore, sessionId string) error {
|
||||
// r, err := api.CheckBalance(ctx, sessionId)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// //store.WriteEntry()
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func updateDefaultToken(ctx context.Context, store common.UserDataStore, sessionId string, activeSym string) {
|
||||
//
|
||||
//}
|
||||
|
||||
func updateToken(ctx context.Context, store common.UserDataStore, identity lookup.Identity) error {
|
||||
var api remote.AccountService
|
||||
|
||||
err := updateTokenList(ctx, &api, store, identity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// activeSym, err := store.ReadEntry(common.DATA_ACTIVE_ADDRESS)
|
||||
// if err == nil {
|
||||
// return nil
|
||||
// }
|
||||
// if !db.IsNotFound(err) {
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
// err = updateDefaultToken(ctx, store, sessionId, string(activeSym))
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// err = updateTokenBalance(ctx, &api, store, sessionId)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// err = updateTokenTransferList(ctx, &api, store, sessionId)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleTokenTransfer(ctx context.Context, store common.UserDataStore, ev *eventTokenTransfer) error {
|
||||
identity, err := lookup.IdentityFromAddress(ctx, store, ev.From)
|
||||
if err != nil {
|
||||
if !db.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err = updateToken(ctx, store, identity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
identity, err = lookup.IdentityFromAddress(ctx, store, ev.To)
|
||||
if err != nil {
|
||||
if !db.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err = updateToken(ctx, store, identity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user