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(examples): Ivan's registry, home realm #3354

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions examples/gno.land/r/ursulovic/home/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/ursulovic/home
90 changes: 90 additions & 0 deletions examples/gno.land/r/ursulovic/home/home.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package home

import (
"std"
"strings"

"gno.land/p/moul/md"
"gno.land/r/leon/hof"
"gno.land/p/demo/ownable"

"gno.land/r/ursulovic/registry"
)

var (
phrases [10]string
aboutMe string
pfp string
pfpCaption string
owner *ownable.Ownable
)

func init() {
owner = ownable.NewWithAddress(registry.MainAddress())

phrases[0] = "Why did the blockchain go to therapy? It had a lot of unprocessed transactions."
phrases[1] = "Why don’t blockchains ever tell secrets? They use smart contracts – always open and never closed!"
phrases[2] = "In Gnoland, we don’t worry about bugs – they’re just features waiting to be integrated!"
phrases[3] = "Why did the smart contract break up with traditional finance? It was a long-term contract – no longer sustainable."
phrases[4] = "Why do blockchains make great comedians? They always deliver the punchline at just the right time!"
phrases[5] = "Why did the blockchain apply for a job? It wanted to become the most reliable ledger around!"
phrases[6] = "Why did the blockchain file a police report? It had a case of identity theft – someone stole its private keys!"
phrases[7] = "In Gnoland, our smart contracts aren’t just written in code – they’re also written in poetry!"
phrases[8] = "Why did the blockchain join a gym? To get a better hash rate!"
phrases[9] = "Why did the blockchain break up with traditional finance? It was looking for a more transparent relationship."

aboutMe = "Hi, I’m Ivan Ursulovic, a computer engineering graduate, blockchain enthusiast, and backend developer specializing in ASP.NET. I love learning new things and taking on challenges."
pfp = "https://i.ibb.co/W28NPkw/beograd.webp"
pfpCaption = "Belgrade, the city I come from"
hof.Register()
}

//Update
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to write comments like this, I suggest you follow godoc.

If you want to separate functions that are about "updating" things, write a comment 2 lines above the section.
You can try using goland as an IDE :)

func UpdatePfp(url string, caption string) {
owner.AssertCallerIsOwner()
pfp = url
pfpCaption = caption
}

func UpdateAboutMe(abtMe string) {
owner.AssertCallerIsOwner()
aboutMe = abtMe
}

func UpdatePhrase(index int, newPhrase string) {
owner.AssertCallerIsOwner()
if index < 0 || index >= len(phrases) {
panic("index out of boundaries")
}
phrases[index] = newPhrase
}


//Rendering
func Render(path string) string {
var out strings.Builder
out.WriteString(renderAboutMe())
out.WriteString(renderPhrase())
out.WriteString(md.Image(pfpCaption, pfp))
out.WriteString(md.Italic("Belgrade, the city I come from"))
return out.String()
}

func renderAboutMe() string {
var out strings.Builder
out.WriteString(md.H3("About me"))

out.WriteString(md.Paragraph(aboutMe))

return out.String()
}


func renderPhrase() string {
var out strings.Builder
phrase := phrases[int(std.GetHeight())%len(phrases)]

out.WriteString(md.H3(`Randomly selected blockchain wisdom:`) + md.Bold(phrase))

return out.String()
}
49 changes: 49 additions & 0 deletions examples/gno.land/r/ursulovic/home/home_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package home

import (
"std"
"strings"
"testing"
)

func TestUpdatePfp(t *testing.T) {
owner := std.Address("g1d24j8fwnc0w5q427fauyey4gdd30qgu69k6n0x")
std.TestSetOrigCaller(owner)

pfp = "abc"

UpdatePfp("https://i.ibb.co/W28NPkw/beograd.webp", "Test caption")

if pfp != "https://i.ibb.co/W28NPkw/beograd.webp" {
t.Fatalf("Expected result: https://i.ibb.co/W28NPkw/beograd.webp, got %s", pfp)
}
}

func TestUpdateAboutMe(t *testing.T) {
owner := std.Address("g1d24j8fwnc0w5q427fauyey4gdd30qgu69k6n0x")
std.TestSetOrigCaller(owner)

aboutMe = ""

excpected := "Testing about me..."

UpdateAboutMe(excpected)

if aboutMe != excpected {
t.Fatalf("Excpected about me to be %s, got %s", excpected, aboutMe)
}
}

func TestUpdatePhrase(t *testing.T) {
owner := std.Address("g1d24j8fwnc0w5q427fauyey4gdd30qgu69k6n0x")
std.TestSetOrigCaller(owner)

index := 0
newPhrase := "New test phrase"

UpdatePhrase(index, newPhrase)

if phrases[index] != newPhrase {
t.Fatalf("Expected phrase at index %d to be %s, got %s", index, newPhrase, phrases[index])
}
}
1 change: 1 addition & 0 deletions examples/gno.land/r/ursulovic/registry/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/ursulovic/registry
59 changes: 59 additions & 0 deletions examples/gno.land/r/ursulovic/registry/registry.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package registry

import (
"errors"
"std"
)

var (
mainAddress std.Address
backupAddress std.Address

ErrInvalidAddr = errors.New("Ivan's registry: Invalid address")
ErrUnauthorized = errors.New("Ivan's registry: Unauthorized")
)

func init() {
mainAddress = "g1d24j8fwnc0w5q427fauyey4gdd30qgu69k6n0x"
backupAddress = "g1mw2xft3eava9kfhqw3fjj3kkf3pkammty0mtv7"
}


func MainAddress() std.Address {
return mainAddress
}

func BackupAddress() std.Address {
return backupAddress
}

func SetMainAddress(addr std.Address) error {
assertAuthorized()

if !addr.IsValid() {
return ErrInvalidAddr
}

mainAddress = addr
return nil
}

func SetBackupAddress(addr std.Address) error {
assertAuthorized()

if !addr.IsValid() {
return ErrInvalidAddr
}

backupAddress = addr
return nil
}
// It will stay here for now, might be useful later
func assertAuthorized() {
Ursulovic marked this conversation as resolved.
Show resolved Hide resolved
caller := std.PrevRealm().Addr()
isAuthorized := caller == mainAddress || caller == backupAddress

if !isAuthorized {
panic(ErrUnauthorized)
}
}
Loading