From 276ec31f1faa35108b8bb4314cef0d15d2621c05 Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Tue, 11 Jun 2024 21:14:01 +0300 Subject: [PATCH 1/4] feat: improve p/ownable API Signed-off-by: moul <94029+moul@users.noreply.github.com> --- examples/gno.land/p/demo/ownable/ownable.gno | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/examples/gno.land/p/demo/ownable/ownable.gno b/examples/gno.land/p/demo/ownable/ownable.gno index 7f2eac008e1..22f48530d57 100644 --- a/examples/gno.land/p/demo/ownable/ownable.gno +++ b/examples/gno.land/p/demo/ownable/ownable.gno @@ -16,6 +16,10 @@ func New() *Ownable { } } +func NewWithAddress(addr std.Address) *Ownable { + return &Ownable{owner: addr} +} + // TransferOwnership transfers ownership of the Ownable struct to a new address func (o *Ownable) TransferOwnership(newOwner std.Address) error { err := o.CallerIsOwner() @@ -45,13 +49,19 @@ func (o *Ownable) DropOwnership() error { } // CallerIsOwner checks if the caller of the function is the Realm's owner -func (o *Ownable) CallerIsOwner() error { +func (o Ownable) CallerIsOwner() error { if std.GetOrigCaller() == o.owner { return nil } return ErrUnauthorized } -func (o *Ownable) Owner() std.Address { +func (o Ownable) Owner() std.Address { return o.owner } + +func (o Ownable) AssertCallerIsOwner() { + if std.GetOrigCaller() != o.owner { + panic(ErrUnauthorized) + } +} \ No newline at end of file From eabbeb4e54ca63a45ba75d6a38ae6fd6c0671681 Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Tue, 11 Jun 2024 21:22:03 +0300 Subject: [PATCH 2/4] chore: fixup Signed-off-by: moul <94029+moul@users.noreply.github.com> --- examples/gno.land/p/demo/ownable/ownable.gno | 16 ++++++++-------- .../gno.land/p/demo/ownable/ownable_test.gno | 7 +++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/examples/gno.land/p/demo/ownable/ownable.gno b/examples/gno.land/p/demo/ownable/ownable.gno index 22f48530d57..22c0c33acc6 100644 --- a/examples/gno.land/p/demo/ownable/ownable.gno +++ b/examples/gno.land/p/demo/ownable/ownable.gno @@ -12,7 +12,7 @@ type Ownable struct { func New() *Ownable { return &Ownable{ - owner: std.GetOrigCaller(), + owner: std.PrevRealm().Addr(), } } @@ -48,20 +48,20 @@ func (o *Ownable) DropOwnership() error { return nil } +func (o Ownable) Owner() std.Address { + return o.owner +} + // CallerIsOwner checks if the caller of the function is the Realm's owner func (o Ownable) CallerIsOwner() error { - if std.GetOrigCaller() == o.owner { + if std.PrevRealm().Addr() == o.owner { return nil } return ErrUnauthorized } -func (o Ownable) Owner() std.Address { - return o.owner -} - func (o Ownable) AssertCallerIsOwner() { - if std.GetOrigCaller() != o.owner { + if std.PrevRealm().Addr() != o.owner { panic(ErrUnauthorized) } -} \ No newline at end of file +} diff --git a/examples/gno.land/p/demo/ownable/ownable_test.gno b/examples/gno.land/p/demo/ownable/ownable_test.gno index f725795fd47..618b68a2fe4 100644 --- a/examples/gno.land/p/demo/ownable/ownable_test.gno +++ b/examples/gno.land/p/demo/ownable/ownable_test.gno @@ -19,6 +19,13 @@ func TestNew(t *testing.T) { } } +func TestNewWithAddress(t *testing.T) { + result := NewWithAddress(firstCaller) + if firstCaller != result.owner { + t.Fatalf("Expected %s, got: %s\n", firstCaller, result.owner) + } +} + func TestOwner(t *testing.T) { std.TestSetOrigCaller(firstCaller) From 9d299b9e64ff1f8e5406e3c05c86717ea01c8e48 Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Tue, 11 Jun 2024 21:58:15 +0300 Subject: [PATCH 3/4] chore: fixup Signed-off-by: moul <94029+moul@users.noreply.github.com> --- .../gno.land/p/demo/ownable/ownable_test.gno | 57 +++++++++++-------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/examples/gno.land/p/demo/ownable/ownable_test.gno b/examples/gno.land/p/demo/ownable/ownable_test.gno index 618b68a2fe4..2c28f867f4a 100644 --- a/examples/gno.land/p/demo/ownable/ownable_test.gno +++ b/examples/gno.land/p/demo/ownable/ownable_test.gno @@ -11,35 +11,39 @@ var ( ) func TestNew(t *testing.T) { - std.TestSetOrigCaller(firstCaller) + std.TestSetRealm(std.NewUserRealm(firstCaller)) + std.TestSetOrigCaller(firstCaller) // TODO(bug): should not be needed - result := New() - if firstCaller != result.owner { - t.Fatalf("Expected %s, got: %s\n", firstCaller, result.owner) + o := New() + got := o.Owner() + if firstCaller != got { + t.Fatalf("Expected %s, got: %s", firstCaller, got) } } func TestNewWithAddress(t *testing.T) { - result := NewWithAddress(firstCaller) - if firstCaller != result.owner { - t.Fatalf("Expected %s, got: %s\n", firstCaller, result.owner) + o := NewWithAddress(firstCaller) + + got := o.Owner() + if firstCaller != got { + t.Fatalf("Expected %s, got: %s", firstCaller, got) } } func TestOwner(t *testing.T) { - std.TestSetOrigCaller(firstCaller) - - result := New() - resultOwner := result.Owner() + std.TestSetRealm(std.NewUserRealm(firstCaller)) + o := New() expected := firstCaller - if resultOwner != expected { - t.Fatalf("Expected %s, got: %s\n", expected, result) + got := o.Owner() + if expected != got { + t.Fatalf("Expected %s, got: %s", expected, got) } } func TestTransferOwnership(t *testing.T) { - std.TestSetOrigCaller(firstCaller) + std.TestSetRealm(std.NewUserRealm(firstCaller)) + o := New() err := o.TransferOwnership(secondCaller) @@ -47,28 +51,29 @@ func TestTransferOwnership(t *testing.T) { t.Fatalf("TransferOwnership failed, %v", err) } - result := o.Owner() - if secondCaller != result { - t.Fatalf("Expected: %s, got: %s\n", secondCaller, result) + got := o.Owner() + if secondCaller != got { + t.Fatalf("Expected: %s, got: %s", secondCaller, got) } } func TestCallerIsOwner(t *testing.T) { - std.TestSetOrigCaller(firstCaller) + std.TestSetRealm(std.NewUserRealm(firstCaller)) o := New() unauthorizedCaller := secondCaller - std.TestSetOrigCaller(unauthorizedCaller) + std.TestSetRealm(std.NewUserRealm(unauthorizedCaller)) + std.TestSetOrigCaller(unauthorizedCaller) // TODO(bug): should not be needed err := o.CallerIsOwner() if err == nil { - t.Fatalf("Expected %s to not be owner\n", unauthorizedCaller) + t.Fatalf("Expected %s to not be owner", unauthorizedCaller) } } func TestDropOwnership(t *testing.T) { - std.TestSetOrigCaller(firstCaller) + std.TestSetRealm(std.NewUserRealm(firstCaller)) o := New() @@ -79,18 +84,20 @@ func TestDropOwnership(t *testing.T) { owner := o.Owner() if owner != "" { - t.Fatalf("Expected owner to be empty, not %s\n", owner) + t.Fatalf("Expected owner to be empty, not %s", owner) } } // Errors func TestErrUnauthorized(t *testing.T) { - std.TestSetOrigCaller(firstCaller) + std.TestSetRealm(std.NewUserRealm(firstCaller)) + std.TestSetOrigCaller(firstCaller) // TODO(bug): should not be needed o := New() - std.TestSetOrigCaller(secondCaller) + std.TestSetRealm(std.NewUserRealm(secondCaller)) + std.TestSetOrigCaller(secondCaller) // TODO(bug): should not be needed err := o.TransferOwnership(firstCaller) if err != ErrUnauthorized { @@ -104,7 +111,7 @@ func TestErrUnauthorized(t *testing.T) { } func TestErrInvalidAddress(t *testing.T) { - std.TestSetOrigCaller(firstCaller) + std.TestSetRealm(std.NewUserRealm(firstCaller)) o := New() From 91dab56f33931950959288361b501db4ceb7f91c Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:55:35 +0300 Subject: [PATCH 4/4] chore: add std.Emit Signed-off-by: moul <94029+moul@users.noreply.github.com> --- examples/gno.land/p/demo/ownable/ownable.gno | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/examples/gno.land/p/demo/ownable/ownable.gno b/examples/gno.land/p/demo/ownable/ownable.gno index 22c0c33acc6..75ebcde0a28 100644 --- a/examples/gno.land/p/demo/ownable/ownable.gno +++ b/examples/gno.land/p/demo/ownable/ownable.gno @@ -4,6 +4,8 @@ import ( "std" ) +const OwnershipTransferEvent = "OwnershipTransfer" + // Ownable is meant to be used as a top-level object to make your contract ownable OR // being embedded in a Gno object to manage per-object ownership. type Ownable struct { @@ -31,7 +33,13 @@ func (o *Ownable) TransferOwnership(newOwner std.Address) error { return ErrInvalidAddress } + prevOwner := o.owner o.owner = newOwner + std.Emit( + OwnershipTransferEvent, + "from", string(prevOwner), + "to", string(newOwner), + ) return nil } @@ -44,7 +52,15 @@ func (o *Ownable) DropOwnership() error { return err } + prevOwner := o.owner o.owner = "" + + std.Emit( + OwnershipTransferEvent, + "from", string(prevOwner), + "to", "", + ) + return nil }