diff --git a/examples/gno.land/p/demo/teritori/worx/gno.mod b/examples/gno.land/p/demo/teritori/worx/gno.mod new file mode 100644 index 00000000000..2c58c902520 --- /dev/null +++ b/examples/gno.land/p/demo/teritori/worx/gno.mod @@ -0,0 +1 @@ +module gno.land/p/demo/teritori/worx diff --git a/examples/gno.land/p/demo/teritori/worx/worx.gno b/examples/gno.land/p/demo/teritori/worx/worx.gno new file mode 100644 index 00000000000..82c2017204f --- /dev/null +++ b/examples/gno.land/p/demo/teritori/worx/worx.gno @@ -0,0 +1,22 @@ +package worx + +import "std" + + +type Worx struct { + Hours int + Metadata string + Address std.Address + Points int + Timestamp int64 +} + +func NewWorx(hours int, metadata string, addr std.Address, points int, timestamp int64) *Worx{ + return &Worx{ + Hours: hours, + Metadata: metadata, + Address: addr, + Points: points, + Timestamp: timestamp, + } +} \ No newline at end of file diff --git a/examples/gno.land/p/demo/teritori/worx/worxs.gno b/examples/gno.land/p/demo/teritori/worx/worxs.gno new file mode 100644 index 00000000000..359f3bba311 --- /dev/null +++ b/examples/gno.land/p/demo/teritori/worx/worxs.gno @@ -0,0 +1,13 @@ +package worx + +type WorxKeeper struct { + worxs []*Worx +} + +func (keeper *WorxKeeper) Store(worx *Worx) { + keeper.worxs = append(keeper.worxs, worx) +} + +func (keeper *WorxKeeper) Get() []*Worx { + return keeper.worxs +} \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/providers/gno.mod b/examples/gno.land/r/demo/teritori/providers/gno.mod new file mode 100644 index 00000000000..abc0c720f8e --- /dev/null +++ b/examples/gno.land/r/demo/teritori/providers/gno.mod @@ -0,0 +1,6 @@ +module gno.land/r/demo/teritori/social_follow + +require ( + gno.land/p/demo/avl v0.0.0-latest + gno.land/p/demo/testutils v0.0.0-latest +) \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profile.gno b/examples/gno.land/r/demo/teritori/providers/profile.gno similarity index 100% rename from examples/gno.land/r/demo/teritori/worx_aggregator/providers/profile.gno rename to examples/gno.land/r/demo/teritori/providers/profile.gno diff --git a/examples/gno.land/r/demo/teritori/providers/profiles.gno b/examples/gno.land/r/demo/teritori/providers/profiles.gno new file mode 100644 index 00000000000..17cf28cd0aa --- /dev/null +++ b/examples/gno.land/r/demo/teritori/providers/profiles.gno @@ -0,0 +1,66 @@ +package provider + +import ( + "std" + "gno.land/p/demo/avl" + "gno.land/p/demo/ufmt" + "gno.land/r/demo/teritori/registry" +) + +var profiles avl.Tree + +func init() { + registry.Register("profiles", RegisterHandler) +} + + +func Get(dataName string, addr std.Address) interface{} { + if dataName != "profile"{ + panic("invalid dataname") + } + profile:=getProfile(addr) + + return profile.ToString() +} + +func SupportedTypes() interface{}{ + return []interface{}{"profile"} +} + +func UpsertProfile(field string, value string){ + caller := std.GetOrigCaller() + profile := getProfile(caller) + profile.SetField(field, value) + profiles.Set(caller.String(), profile) +} + + +func getProfile(addr std.Address ) *Profile { + profile, found:=profiles.Get(addr.String()) + if !found{ + return &Profile{} + } + + return profile.(*Profile) +} + +func RegisterHandler(functionName string, args ...interface{}) interface{} { + switch functionName { + case "get": + if len(args) != 2{ + panic("invalid number of arguments") + } + dataname := args[0].(string) + address := args[1].(std.Address) + return Get(dataname,address) + case "supportedTypes": + if len(args) != 0{ + panic("invalid number of arguments") + } + dataname := args[0].(string) + address := args[1].(std.Address) + return SupportedTypes() + default: + panic("invalid function name") + } +} \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles_test.gno b/examples/gno.land/r/demo/teritori/providers/profiles_test.gno similarity index 62% rename from examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles_test.gno rename to examples/gno.land/r/demo/teritori/providers/profiles_test.gno index b3c02e9f1ec..b4e44e5f5c6 100644 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles_test.gno +++ b/examples/gno.land/r/demo/teritori/providers/profiles_test.gno @@ -20,5 +20,14 @@ func TestGetProfile(t *testing.T) { if !strings.Contains(result,"name: Gopher"){ t.Error("Bad") } +} + +func TestRegisterHandler(t *testing.T) { + user1 := testutils.TestAddress("user1") + std.TestSetOrigCaller(user1) + supportedTypes := RegisterHandler("supportedTypes") + if len(supportedTypes) != 1{ + t.Error("Supported types is wrong") + } +} -} \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/providers/worx_dao.gno b/examples/gno.land/r/demo/teritori/providers/worx_dao.gno new file mode 100644 index 00000000000..612374f74dc --- /dev/null +++ b/examples/gno.land/r/demo/teritori/providers/worx_dao.gno @@ -0,0 +1,18 @@ +package provider + +import ( + "std" + "gno.land/r/demo/teritori/worx_aggregator" + "gno.land/p/demo/rand" +) + +var admin std.Address + +func init() { + admin = std.GetOrigCaller() +} + +func RandWorx(addr std.Address){ + r := rand.New() + worx_aggregator.Push(r.Intn(25), "", addr, r.Intn(100), std.GetHeight()) +} \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/registry/registry.gno b/examples/gno.land/r/demo/teritori/registry/registry.gno new file mode 100644 index 00000000000..faa6175fd9a --- /dev/null +++ b/examples/gno.land/r/demo/teritori/registry/registry.gno @@ -0,0 +1,27 @@ +package registry + +import ( + "gno.land/p/demo/avl" + "std" +) + +var callbacks avl.Tree +type FunctionCB func(functionName string, args ...interface{}) interface{} + +func Register(id string, callback FunctionCB){ + _,exists:=callbacks.Get(id) + if exists{ + panic("A callback already exists for the id") + } + + callbacks.Set(id, callback) +} + +func Exec(id string, functionName string, args ...interface{}) interface{} { + cb, ok:= callbacks.Get(id) + if !ok{ + panic("Callback not found") + } + function := cb.(FunctionCB) + return function(functionName, args) +} \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/registry/registry_test.gno b/examples/gno.land/r/demo/teritori/registry/registry_test.gno new file mode 100644 index 00000000000..bb167cd1752 --- /dev/null +++ b/examples/gno.land/r/demo/teritori/registry/registry_test.gno @@ -0,0 +1,22 @@ +package registry + +import ( + "std" + "testing" + + "gno.land/p/demo/avl" + "gno.land/p/demo/testutils" + "strings" +) + +func TestGetProfile(t *testing.T) { + user1 := testutils.TestAddress("user1") + std.TestSetOrigCaller(user1) + functionID:="SOMEID" + var cb functionCB = func(args ...interface{})[]interface{}{ + //t.Errorf("dataType",dataType) + return nil + } + Register(functionID, cb) + Exec(functionID) +} \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles.gno deleted file mode 100644 index aef780696d1..00000000000 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/providers/profiles.gno +++ /dev/null @@ -1,39 +0,0 @@ -package provider - -import ( - "std" - "gno.land/p/demo/avl" - "gno.land/p/demo/ufmt" -) - -var profiles avl.Tree - -func Get(dataName string, addr std.Address) string { - if dataName != "profile"{ - panic("invalid dataname") - } - profile:=getProfile(addr) - - return profile.ToString() -} - -func SupportedTypes() []string{ - return []string{"profile"} -} - -func UpsertProfile(field string, value string){ - caller := std.GetOrigCaller() - profile := getProfile(caller) - profile.SetField(field, value) - profiles.Set(caller.String(), profile) -} - - -func getProfile(addr std.Address ) *Profile { - profile, found:=profiles.Get(addr.String()) - if !found{ - return &Profile{} - } - - return profile.(*Profile) -} \ No newline at end of file diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno index 2a0f39e756a..3b853bc6d11 100644 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno @@ -2,38 +2,49 @@ package worx_aggregator import( "gno.land/p/demo/avl" + "gno.land/p/demo/teritori/worx" + "std" ) - -//interface WorxDataProvider { -// Get(dataName string, addr std.Address) any -// SupportedTypes() []string -//} - var admin std.Address -var dataProviders []WorxDataProvider -var dataTypeToDataProvider avl.Tree - - -func Get(dataType string, addr std.Address) []any { - all := []any{} - dataProviders := dataTypeToDataProvider.Get(dataType) - if len(dataProviders) == 0 { - panic("there is not dataprovider configured for that datatype") +var registeredProviders avl.Tree +var worxByAddress avl.Tree + +func Push(hours int, metadata string, addr std.Address, points int, timestamp int64) { + prevRealm := std.PrevRealm().Addr() + dataProviders, ok := registeredProviders.Get(string(prevRealm)) + if !ok { + panic("caller realm is not registered as provider") } - for d := range dataProviders { - all = append(all, d.Get(dataType, addr)...) + keeper := getKeeper(addr) + keeper.Store(worx.NewWorx(hours, metadata, addr, points, timestamp)) + worxByAddress.Set(string(addr), keeper) + + std.Emit("worx_added", + "addr",string(addr), + "metadata", metadata, + ) +} + +func getKeeper(addr std.Address) *worx.WorxKeeper{ + data, ok := worxByAddress.Get(string(addr)) + if ok { + return data.(*worx.WorxKeeper) } - return all + return &worx.WorxKeeper{} +} + +func Get(dataType string, addr std.Address) []*worx.Worx { + return getKeeper(addr).Get() } -func RegisterDataProvider(dp WorxDataProvider) { +func RegisterDataProvider(addr std.Address) { assertAdmin() - for supp := range dp.SupportedTypes() { - providers := dataTypeToDataProvider.Get(supp) - providers = append(providers, dp) - dataTypeToDataProvider.set(supp, providers) + _, ok :=registeredProviders.Get(string(addr)) + if !ok { + panic("Data provider already registered") } + registeredProviders.Set(string(addr), 0) } func assertAdmin(){ diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/worx_test.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/worx_test.gno new file mode 100644 index 00000000000..21ce9b75639 --- /dev/null +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/worx_test.gno @@ -0,0 +1,16 @@ +package worx_aggregator + +import ( + "testing" + "gno.land/p/demo/testutils" +) + +func TestPushCallerNotRegistered(t * testing.T){ + user1 := testutils.TestAddress("user1") + defer func() { + if v := recover(); v == nil { + t.Fatalf("expected panic got no error: ") + } + }() + Push(1, "", user1, 3, 10) +}