Skip to content

Commit

Permalink
Change Pull -> Push
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed Apr 20, 2024
1 parent be6f420 commit 5c24dd8
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 63 deletions.
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/teritori/worx/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/teritori/worx
22 changes: 22 additions & 0 deletions examples/gno.land/p/demo/teritori/worx/worx.gno
Original file line number Diff line number Diff line change
@@ -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,
}
}
13 changes: 13 additions & 0 deletions examples/gno.land/p/demo/teritori/worx/worxs.gno
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions examples/gno.land/r/demo/teritori/providers/gno.mod
Original file line number Diff line number Diff line change
@@ -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
)
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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

}
18 changes: 18 additions & 0 deletions examples/gno.land/r/demo/teritori/providers/worx_dao.gno
Original file line number Diff line number Diff line change
@@ -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())
}
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)
}

This file was deleted.

57 changes: 34 additions & 23 deletions examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down
16 changes: 16 additions & 0 deletions examples/gno.land/r/demo/teritori/worx_aggregator/worx_test.gno
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 5c24dd8

Please sign in to comment.