forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
73 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
examples/gno.land/r/demo/teritori/worx_aggregator/aggregator.gno
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package worx_aggregator | ||
|
||
import ( | ||
"std" | ||
|
||
"gno.land/p/demo/teritori/worx" | ||
) | ||
|
||
func GetWorx(addr std.Address) []*worx.Worx { | ||
keeper := getKeeper(addr) | ||
return keeper.Get() | ||
} | ||
|
||
func GetWorxFromDate(addr std.Address, date int64) []*worx.Worx { | ||
keeper := getKeeper(addr) | ||
return keeper.GetFromDate(date) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module gno.land/r/demo/teritori/worx_aggregator | ||
|
||
require ( | ||
gno.land/p/demo/teritori/worx v0.0.0-latest | ||
) |
84 changes: 44 additions & 40 deletions
84
examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,58 @@ | ||
package worx_aggregator | ||
|
||
import( | ||
import ( | ||
"std" | ||
|
||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/teritori/worx" | ||
"std" | ||
"gno.land/p/demo/teritori/worx" | ||
) | ||
|
||
var ( | ||
admin std.Address | ||
registeredProviders avl.Tree | ||
worxByAddress avl.Tree | ||
) | ||
var admin std.Address | ||
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") | ||
} | ||
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, | ||
) | ||
} | ||
assertRegistered(addr) | ||
|
||
func getKeeper(addr std.Address) *worx.WorxKeeper{ | ||
data, ok := worxByAddress.Get(string(addr)) | ||
if ok { | ||
return data.(*worx.WorxKeeper) | ||
} | ||
return &worx.WorxKeeper{} | ||
} | ||
keeper := getKeeper(addr) | ||
keeper.Store(worx.NewWorx(hours, metadata, addr, points, timestamp)) | ||
worxByAddress.Set(string(addr), keeper) | ||
|
||
func Get(dataType string, addr std.Address) []*worx.Worx { | ||
return getKeeper(addr).Get() | ||
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 &worx.WorxKeeper{} | ||
} | ||
|
||
func RegisterDataProvider(addr std.Address) { | ||
assertAdmin() | ||
_, ok :=registeredProviders.Get(string(addr)) | ||
if !ok { | ||
panic("Data provider already registered") | ||
} | ||
registeredProviders.Set(string(addr), 0) | ||
assertAdmin() | ||
_, ok := registeredProviders.Get(string(addr)) | ||
if !ok { | ||
panic("Data provider already registered") | ||
} | ||
registeredProviders.Set(string(addr), 0) | ||
} | ||
|
||
func assertRegistered(addr std.Address) { | ||
prevRealm := std.PrevRealm().Addr() | ||
dataProviders, ok := registeredProviders.Get(string(prevRealm)) | ||
if !ok { | ||
panic("caller realm is not registered as provider") | ||
} | ||
} | ||
|
||
func assertAdmin(){ | ||
if (std.PrevRealm().Addr() != admin) { | ||
panic("unathorized") | ||
} | ||
} | ||
func assertAdmin() { | ||
if std.PrevRealm().Addr() != admin { | ||
panic("unathorized") | ||
} | ||
} |