Add voucherdata resolve

This commit is contained in:
lash 2024-11-03 14:35:37 +00:00
parent 1e14395739
commit a4443cbe4c
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
6 changed files with 47 additions and 15 deletions

View File

@ -13,7 +13,7 @@ import (
"git.grassecon.net/urdt/ussd/common" "git.grassecon.net/urdt/ussd/common"
"git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/models" "git.grassecon.net/urdt/ussd/models"
"git.grassecon.net/term/event" "git.grassecon.net/term/lookup"
) )
func init() { func init() {
@ -107,13 +107,22 @@ func(m mockApi) FetchTransactions(ctx context.Context, publicKey string) ([]data
return nil, nil 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) { func TestHandleMsg(t *testing.T) {
err := config.LoadConfig() err := config.LoadConfig()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
event.Api = mockApi{} lookup.Api = mockApi{}
ctx := context.Background() ctx := context.Background()
userDb := memdb.NewMemDb() userDb := memdb.NewMemDb()

View File

@ -28,12 +28,13 @@ type eventTokenTransfer struct {
To string To string
Value int Value int
TxHash string TxHash string
VoucherAddress string
} }
func updateTokenTransferList(ctx context.Context, store common.UserDataStore, identity lookup.Identity) error { func updateTokenTransferList(ctx context.Context, store common.UserDataStore, identity lookup.Identity) error {
var r []string var r []string
txs, err := Api.FetchTransactions(ctx, identity.ChecksumAddress) txs, err := lookup.Api.FetchTransactions(ctx, identity.ChecksumAddress)
if err != nil { if err != nil {
return err 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 { 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 { if err != nil {
return err return err
} }
@ -56,6 +57,8 @@ func updateTokenList(ctx context.Context, store *common.UserDataStore, identity
// TODO: export subprefixdb and use that instead // TODO: export subprefixdb and use that instead
// TODO: make sure subprefixdb is thread safe when using gdbm // 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) store.Db.SetPrefix(DATATYPE_USERSUB)
k := append([]byte("vouchers"), []byte("sym")...) k := append([]byte("vouchers"), []byte("sym")...)
@ -63,7 +66,7 @@ func updateTokenList(ctx context.Context, store *common.UserDataStore, identity
if err != nil { if err != nil {
return err return err
} }
logg.TraceCtxf(ctx, "processvoucher", "key", k)
k = append([]byte("vouchers"), []byte("bal")...) k = append([]byte("vouchers"), []byte("bal")...)
err = store.Db.Put(ctx, k, []byte(metadata.Balances)) err = store.Db.Put(ctx, k, []byte(metadata.Balances))
if err != nil { 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 { func updateDefaultToken(ctx context.Context, store *common.UserDataStore, identity lookup.Identity, activeSym string) error {
pfxDb := common.StoreToPrefixDb(store, []byte("vouchers")) pfxDb := common.StoreToPrefixDb(store, []byte("vouchers"))
// TODO: the activeSym input should instead be newline separated list?
tokenData, err := common.GetVoucherData(ctx, pfxDb, activeSym) tokenData, err := common.GetVoucherData(ctx, pfxDb, activeSym)
if err != nil { if err != nil {
return err return err
} }
logg.TraceCtxf(ctx, "tokendaa", "d", tokenData)
return common.UpdateVoucherData(ctx, store, identity.SessionId, tokenData) return common.UpdateVoucherData(ctx, store, identity.SessionId, tokenData)
} }
@ -98,19 +103,35 @@ func updateWait(ctx context.Context) error {
return nil 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) err := updateTokenList(ctx, store, identity)
if err != nil { if err != nil {
return err 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 { if err == nil {
return nil return nil
} }
if !db.IsNotFound(err) { if !db.IsNotFound(err) {
return 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)) err = updateDefaultToken(ctx, store, identity, string(activeSym))
if err != nil { if err != nil {
@ -154,6 +175,9 @@ func asTokenTransferEvent(gev *geEvent.Event) (*eventTokenTransfer, bool) {
logg.Errorf("could not decode value", "value", value, "err", err) logg.Errorf("could not decode value", "value", value, "err", err)
return nil, false return nil, false
} }
ev.VoucherAddress = gev.ContractAddress
return &ev, true return &ev, true
} }
@ -164,7 +188,7 @@ func handleTokenTransfer(ctx context.Context, store *common.UserDataStore, ev *e
return err return err
} }
} else { } else {
err = updateToken(ctx, store, identity) err = updateToken(ctx, store, identity, ev.VoucherAddress)
if err != nil { if err != nil {
return err return err
} }
@ -175,7 +199,7 @@ func handleTokenTransfer(ctx context.Context, store *common.UserDataStore, ev *e
return err return err
} }
} else { } else {
err = updateToken(ctx, store, identity) err = updateToken(ctx, store, identity, ev.VoucherAddress)
if err != nil { if err != nil {
return err return err
} }

4
go.mod
View File

@ -4,8 +4,9 @@ go 1.23.2
require ( require (
git.defalsify.org/vise.git v0.2.1-0.20241031204035-b588301738ed 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/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 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/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c // indirect
github.com/fxamacker/cbor/v2 v2.4.0 // indirect github.com/fxamacker/cbor/v2 v2.4.0 // indirect
github.com/grassrootseconomics/eth-custodial v1.3.0-beta // 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/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect

4
go.sum
View File

@ -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 h1:4TrsfbK7NKgsa7KjMPlnV/tjYTkAAXP5PWAZzUfzCdI=
git.defalsify.org/vise.git v0.2.1-0.20241031204035-b588301738ed/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck= 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-20241103143426-0506a8c452f1 h1:uTscFuyKCqWshcN+pgoJiE0jIVzRrUrgBfI/RsiM7qE=
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/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 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk=
github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ= github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk= github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=

View File

@ -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) { func getSessionIdByAddress(ctx context.Context, store *common.UserDataStore, address string) (string, error) {
logg.Debugf("fooar")
// TODO: replace with userdatastore when double sessionid issue fixed // TODO: replace with userdatastore when double sessionid issue fixed
//r, err := store.ReadEntry(ctx, address, common.DATA_PUBLIC_KEY_REVERSE) //r, err := store.ReadEntry(ctx, address, common.DATA_PUBLIC_KEY_REVERSE)
store.Db.SetPrefix(db.DATATYPE_USERDATA) store.Db.SetPrefix(db.DATATYPE_USERDATA)

View File

@ -1,4 +1,4 @@
package event package lookup
import ( import (
"git.grassecon.net/urdt/ussd/remote" "git.grassecon.net/urdt/ussd/remote"