-
Notifications
You must be signed in to change notification settings - Fork 385
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
Ursulovic
wants to merge
13
commits into
gnolang:master
Choose a base branch
from
Ursulovic:home-page
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
241eb1d
Created registry, home real in progress
452ced8
I have now added my home realm files, still need to add functionaliti…
0e75031
Merge branch 'master' of github.com:Ursulovic/gno into home-page
f81e4eb
Merge branch 'master' into home-page
Ursulovic 65d4777
Finished all home realm functionalities, added tests aswell
5308289
Merge branch 'gnolang:master' into home-page
Ursulovic 7e8de8c
Merge branch 'gnolang:master' into home-page
Ursulovic 2f45a55
Minor updates to renderAboutMe function
7533f05
Merge branch 'gnolang:master' into home-page
Ursulovic b3ceda8
Fixed tests
92cfaea
Updated bio and fixed tests
1cf5070
Merge branch 'gnolang:master' into home-page
Ursulovic c907258
Merge branch 'gnolang:master' into home-page
Ursulovic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module gno.land/r/ursulovic/home |
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,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 | ||
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() | ||
} |
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,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]) | ||
} | ||
} |
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 @@ | ||
module gno.land/r/ursulovic/registry |
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,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) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 :)