Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/main worx aggregator #17

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/gno.land/r/demo/teritori/providers/profile.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package provider

import (
"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"
)

type Profile struct{
data avl.Tree
}

func(p *Profile) ToString() string{
var data = ""
p.data.Iterate("", "", func(key string, value interface{}) bool {
data += ufmt.Sprintf("%s: %s\n",key, value)
return false
})
return data
}

func(p *Profile) SetField(fieldName string, value string) {
p.data.Set(fieldName, value)
}
66 changes: 66 additions & 0 deletions examples/gno.land/r/demo/teritori/providers/profiles.gno
Original file line number Diff line number Diff line change
@@ -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")
}
}
33 changes: 33 additions & 0 deletions examples/gno.land/r/demo/teritori/providers/profiles_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package provider

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)
UpsertProfile("firstname", "John")
UpsertProfile("name", "Gopher")

result:=Get("profile",user1)
t.Log(result)
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")
}
}

27 changes: 27 additions & 0 deletions examples/gno.land/r/demo/teritori/registry/registry.gno
Original file line number Diff line number Diff line change
@@ -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)
}
22 changes: 22 additions & 0 deletions examples/gno.land/r/demo/teritori/registry/registry_test.gno
Original file line number Diff line number Diff line change
@@ -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)
}
48 changes: 48 additions & 0 deletions examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package worx_aggregator

import(
"gno.land/p/demo/avl"
"gno.land/r/demo/teritori/registry"
"std"
)
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")
}
for registerID := range dataProviders {
all = append(all, getFromRealm(registerID,dataType, addr)...)
}
return all
}


func RegisterDataProvider(registerRootID string) {
assertAdmin()
supportedTypes := registry.Exec(registerRootID, "supportedTypes")

supportedTypesSlice := supportedTypes.[[]interface{}]
for supp := range supportedTypesSlice {
suppStr:= supp.(string)
providers := dataTypeToDataProvider.Get(supp)
providers = append(providers, registerRootID)
dataTypeToDataProvider.set(supp, providers)
}
}

func assertAdmin(){
if (std.PrevRealm().Addr() != admin) {
panic("unathorized")
}
}

func getFromRealm(registerRootID string,dataType string, address std.Address) []any {
getRegisterID := registerRootID
return registry.Exec(getRegisterID, "get", dataType, address)
}
Loading