diff --git a/event/nats/nats_test.go b/event/nats/nats_test.go index b5adcef..c9ace83 100644 --- a/event/nats/nats_test.go +++ b/event/nats/nats_test.go @@ -13,7 +13,7 @@ import ( "git.grassecon.net/urdt/ussd/common" "git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/models" - "git.grassecon.net/term/event" + "git.grassecon.net/term/lookup" ) func init() { @@ -107,13 +107,22 @@ func(m mockApi) FetchTransactions(ctx context.Context, publicKey string) ([]data return nil, nil } +func(m mockApi) VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error) { + return &models.VoucherDataResult{ + TokenSymbol: "FOO", + TokenName: "Foo Token", + TokenDecimals: "6", + SinkAddress: "0xb42C5920014eE152F2225285219407938469BBfA", + }, nil +} + func TestHandleMsg(t *testing.T) { err := config.LoadConfig() if err != nil { t.Fatal(err) } - event.Api = mockApi{} + lookup.Api = mockApi{} ctx := context.Background() userDb := memdb.NewMemDb() diff --git a/event/token.go b/event/token.go index 3383921..084faff 100644 --- a/event/token.go +++ b/event/token.go @@ -28,12 +28,13 @@ type eventTokenTransfer struct { To string Value int TxHash string + VoucherAddress string } func updateTokenTransferList(ctx context.Context, store common.UserDataStore, identity lookup.Identity) error { var r []string - txs, err := Api.FetchTransactions(ctx, identity.ChecksumAddress) + txs, err := lookup.Api.FetchTransactions(ctx, identity.ChecksumAddress) if err != nil { return err } @@ -47,7 +48,7 @@ func updateTokenTransferList(ctx context.Context, store common.UserDataStore, id } func updateTokenList(ctx context.Context, store *common.UserDataStore, identity lookup.Identity) error { - holdings, err := Api.FetchVouchers(ctx, identity.ChecksumAddress) + holdings, err := lookup.Api.FetchVouchers(ctx, identity.ChecksumAddress) if err != nil { return err } @@ -56,6 +57,8 @@ func updateTokenList(ctx context.Context, store *common.UserDataStore, identity // TODO: export subprefixdb and use that instead // TODO: make sure subprefixdb is thread safe when using gdbm + // TODO: why is address session here unless explicitly set + store.Db.SetSession(identity.SessionId) store.Db.SetPrefix(DATATYPE_USERSUB) k := append([]byte("vouchers"), []byte("sym")...) @@ -63,7 +66,7 @@ func updateTokenList(ctx context.Context, store *common.UserDataStore, identity if err != nil { return err } - + logg.TraceCtxf(ctx, "processvoucher", "key", k) k = append([]byte("vouchers"), []byte("bal")...) err = store.Db.Put(ctx, k, []byte(metadata.Balances)) if err != nil { @@ -87,10 +90,12 @@ func updateTokenList(ctx context.Context, store *common.UserDataStore, identity func updateDefaultToken(ctx context.Context, store *common.UserDataStore, identity lookup.Identity, activeSym string) error { pfxDb := common.StoreToPrefixDb(store, []byte("vouchers")) + // TODO: the activeSym input should instead be newline separated list? tokenData, err := common.GetVoucherData(ctx, pfxDb, activeSym) if err != nil { return err } + logg.TraceCtxf(ctx, "tokendaa", "d", tokenData) return common.UpdateVoucherData(ctx, store, identity.SessionId, tokenData) } @@ -98,19 +103,35 @@ func updateWait(ctx context.Context) error { return nil } -func updateToken(ctx context.Context, store *common.UserDataStore, identity lookup.Identity) error { +func toSym(ctx context.Context, address string) ([]byte, error) { + voucherData, err := lookup.Api.VoucherData(ctx, address) + if err != nil { + return nil, err + } + return []byte(voucherData.TokenSymbol), nil +} + +func updateToken(ctx context.Context, store *common.UserDataStore, identity lookup.Identity, tokenAddress string) error { err := updateTokenList(ctx, store, identity) if err != nil { return err } - activeSym, err := store.ReadEntry(ctx, identity.SessionId, common.DATA_ACTIVE_ADDRESS) + store.Db.SetSession(identity.SessionId) + activeSym, err := store.ReadEntry(ctx, identity.SessionId, common.DATA_ACTIVE_SYM) if err == nil { return nil } if !db.IsNotFound(err) { return err } + if activeSym == nil { + activeSym, err = toSym(ctx, tokenAddress) + if err != nil { + return err + } + } + logg.Debugf("barfoo") err = updateDefaultToken(ctx, store, identity, string(activeSym)) if err != nil { @@ -154,6 +175,9 @@ func asTokenTransferEvent(gev *geEvent.Event) (*eventTokenTransfer, bool) { logg.Errorf("could not decode value", "value", value, "err", err) return nil, false } + + ev.VoucherAddress = gev.ContractAddress + return &ev, true } @@ -164,7 +188,7 @@ func handleTokenTransfer(ctx context.Context, store *common.UserDataStore, ev *e return err } } else { - err = updateToken(ctx, store, identity) + err = updateToken(ctx, store, identity, ev.VoucherAddress) if err != nil { return err } @@ -175,7 +199,7 @@ func handleTokenTransfer(ctx context.Context, store *common.UserDataStore, ev *e return err } } else { - err = updateToken(ctx, store, identity) + err = updateToken(ctx, store, identity, ev.VoucherAddress) if err != nil { return err } diff --git a/go.mod b/go.mod index 7026705..8b24fb8 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,9 @@ go 1.23.2 require ( git.defalsify.org/vise.git v0.2.1-0.20241031204035-b588301738ed - git.grassecon.net/urdt/ussd v0.0.0-20241103014457-a237b615f2ee + git.grassecon.net/urdt/ussd v0.0.0-20241103143426-0506a8c452f1 github.com/grassrootseconomics/eth-tracker v1.3.0-rc + github.com/grassrootseconomics/ussd-data-service v0.0.0-20241003123429-4904b4438a3a github.com/nats-io/nats.go v1.37.0 ) @@ -13,7 +14,6 @@ require ( github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c // indirect github.com/fxamacker/cbor/v2 v2.4.0 // indirect github.com/grassrootseconomics/eth-custodial v1.3.0-beta // indirect - github.com/grassrootseconomics/ussd-data-service v0.0.0-20241003123429-4904b4438a3a // indirect github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect diff --git a/go.sum b/go.sum index 2f92062..871205a 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ git.defalsify.org/vise.git v0.2.1-0.20241031204035-b588301738ed h1:4TrsfbK7NKgsa7KjMPlnV/tjYTkAAXP5PWAZzUfzCdI= git.defalsify.org/vise.git v0.2.1-0.20241031204035-b588301738ed/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck= -git.grassecon.net/urdt/ussd v0.0.0-20241103014457-a237b615f2ee h1:DJI0cfGxMKMoEPeO9IRaBKQzPBJWS00GeQ5derjE+GY= -git.grassecon.net/urdt/ussd v0.0.0-20241103014457-a237b615f2ee/go.mod h1:ADB/wpwvI6umvYzGqpJGm/GYj8msxYGiczzWCCdXegs= +git.grassecon.net/urdt/ussd v0.0.0-20241103143426-0506a8c452f1 h1:uTscFuyKCqWshcN+pgoJiE0jIVzRrUrgBfI/RsiM7qE= +git.grassecon.net/urdt/ussd v0.0.0-20241103143426-0506a8c452f1/go.mod h1:ADB/wpwvI6umvYzGqpJGm/GYj8msxYGiczzWCCdXegs= github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk= github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ= github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk= diff --git a/lookup/db.go b/lookup/db.go index 96b5872..9ddf34f 100644 --- a/lookup/db.go +++ b/lookup/db.go @@ -35,7 +35,6 @@ func IdentityFromAddress(ctx context.Context, store *common.UserDataStore, addre } func getSessionIdByAddress(ctx context.Context, store *common.UserDataStore, address string) (string, error) { - logg.Debugf("fooar") // TODO: replace with userdatastore when double sessionid issue fixed //r, err := store.ReadEntry(ctx, address, common.DATA_PUBLIC_KEY_REVERSE) store.Db.SetPrefix(db.DATATYPE_USERDATA) diff --git a/event/api.go b/lookup/token.go similarity index 89% rename from event/api.go rename to lookup/token.go index 2e2c3a8..f4b26d4 100644 --- a/event/api.go +++ b/lookup/token.go @@ -1,4 +1,4 @@ -package event +package lookup import ( "git.grassecon.net/urdt/ussd/remote"