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

refactor(gnovm): remove Get prefixes from GetCallerAt, GetOrigSend, GetOrigCaller and extend abbreviations #3374

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/assets/how-to-guides/creating-grc20/mytoken-1.gno
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
// init is called once at time of deployment
func init() {
// Set deployer of Realm to admin
admin = std.PrevRealm().Addr()
admin = std.PreviousRealm().Address()

// Set token name, symbol and number of decimals
banker = grc20.NewBanker("My Token", "TKN", 4)
Expand Down
4 changes: 2 additions & 2 deletions docs/assets/how-to-guides/creating-grc20/mytoken-2.gno
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func TransferFrom(from, to std.Address, amount uint64) {

// Mint mints amount of tokens to address. Callable only by admin of token
func Mint(address std.Address, amount uint64) {
assertIsAdmin(std.PrevRealm().Addr())
assertIsAdmin(std.PreviousRealm().Address())
checkErr(banker.Mint(address, amount))
}

// Burn burns amount of tokens from address. Callable only by admin of token
func Burn(address std.Address, amount uint64) {
assertIsAdmin(std.PrevRealm().Addr())
assertIsAdmin(std.PreviousRealm().Address())
checkErr(banker.Burn(address, amount))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func AuctionEnd() {

// Send the highest bid to the recipient
banker := std.GetBanker(std.BankerTypeRealmSend)
pkgAddr := std.GetOrigPkgAddr()
pkgAddr := std.GetOriginPkgAddress()

banker.SendCoins(pkgAddr, receiver, std.Coins{{"ugnot", int64(highestBid)}})
}
20 changes: 10 additions & 10 deletions docs/assets/how-to-guides/porting-solidity-to-gno/porting-13.gno
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ func TestFull(t *testing.T) {

// Send two or more types of coins
{
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
std.TestSetOriginCaller(bidder01)
std.TestSetOriginSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
shouldPanic(t, Bid)
}

// Send less than the highest bid
{
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 0}}, nil)
std.TestSetOriginCaller(bidder01)
std.TestSetOriginSend(std.Coins{{"ugnot", 0}}, nil)
shouldPanic(t, Bid)
}

// Send more than the highest bid
{
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
std.TestSetOriginCaller(bidder01)
std.TestSetOriginSend(std.Coins{{"ugnot", 1}}, nil)
shouldNoPanic(t, Bid)

shouldEqual(t, pendingReturns.Size(), 0)
Expand All @@ -42,13 +42,13 @@ func TestFull(t *testing.T) {
{

// Send less amount than the current highest bid (current: 1)
std.TestSetOrigCaller(bidder02)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
std.TestSetOriginCaller(bidder02)
std.TestSetOriginSend(std.Coins{{"ugnot", 1}}, nil)
shouldPanic(t, Bid)

// Send more amount than the current highest bid (exceeded)
std.TestSetOrigCaller(bidder02)
std.TestSetOrigSend(std.Coins{{"ugnot", 2}}, nil)
std.TestSetOriginCaller(bidder02)
std.TestSetOriginSend(std.Coins{{"ugnot", 2}}, nil)
shouldNoPanic(t, Bid)

shouldEqual(t, highestBid, 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ func Bid() {
panic("Exceeded auction end block")
}

sentCoins := std.GetOrigSend()
sentCoins := std.OriginSend()
if len(sentCoins) != 1 {
panic("Send only one type of coin")
}
Expand All @@ -23,7 +23,7 @@ func Bid() {
}

// Update the top bidder address
highestBidder = std.GetOrigCaller()
highestBidder = std.OriginCaller()
// Update the top bid amount
highestBid = sentAmount
}
Expand Down
24 changes: 12 additions & 12 deletions docs/assets/how-to-guides/porting-solidity-to-gno/porting-6.gno
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
// Bid Function Test - Send Coin
func TestBidCoins(t *testing.T) {
// Sending two types of coins
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
std.TestSetOriginCaller(bidder01)
std.TestSetOriginSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
shouldPanic(t, Bid)

// Sending lower amount than the current highest bid
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 0}}, nil)
std.TestSetOriginCaller(bidder01)
std.TestSetOriginSend(std.Coins{{"ugnot", 0}}, nil)
shouldPanic(t, Bid)

// Sending more amount than the current highest bid (exceeded)
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
std.TestSetOriginCaller(bidder01)
std.TestSetOriginSend(std.Coins{{"ugnot", 1}}, nil)
shouldNoPanic(t, Bid)
}

// Bid Function Test - Bid by two or more people
func TestBidCoins(t *testing.T) {
// bidder01 bidding with 1 coin
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
std.TestSetOriginCaller(bidder01)
std.TestSetOriginSend(std.Coins{{"ugnot", 1}}, nil)
shouldNoPanic(t, Bid)
shouldEqual(t, highestBid, 1)
shouldEqual(t, highestBidder, bidder01)
shouldEqual(t, pendingReturns.Size(), 0)

// bidder02 bidding with 1 coin
std.TestSetOrigCaller(bidder02)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
std.TestSetOriginCaller(bidder02)
std.TestSetOriginSend(std.Coins{{"ugnot", 1}}, nil)
shouldPanic(t, Bid)

// bidder02 bidding with 2 coins
std.TestSetOrigCaller(bidder02)
std.TestSetOrigSend(std.Coins{{"ugnot", 2}}, nil)
std.TestSetOriginCaller(bidder02)
std.TestSetOriginSend(std.Coins{{"ugnot", 2}}, nil)
shouldNoPanic(t, Bid)
shouldEqual(t, highestBid, 2)
shouldEqual(t, highestBidder, bidder02)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
func Withdraw() {
// Query the return amount to non-highest bidders
amount, _ := pendingReturns.Get(std.GetOrigCaller().String())
amount, _ := pendingReturns.Get(std.OriginCaller().String())

if amount > 0 {
// If there's an amount, reset the amount first,
pendingReturns.Set(std.GetOrigCaller().String(), 0)
pendingReturns.Set(std.OriginCaller().String(), 0)

// Return the exceeded amount
banker := std.GetBanker(std.BankerTypeRealmSend)
pkgAddr := std.GetOrigPkgAddr()
pkgAddr := std.GetOriginPkgAddress()

banker.SendCoins(pkgAddr, std.GetOrigCaller(), std.Coins{{"ugnot", amount.(int64)}})
banker.SendCoins(pkgAddr, std.OriginCaller(), std.Coins{{"ugnot", amount.(int64)}})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestWithdraw(t *testing.T) {
shouldEqual(t, pendingReturns.Has(returnAddr), true)

banker := std.GetBanker(std.BankerTypeRealmSend)
pkgAddr := std.GetOrigPkgAddr()
pkgAddr := std.GetOriginPkgAddress()
banker.SendCoins(pkgAddr, std.Address(returnAddr), std.Coins{{"ugnot", returnAmount}})
shouldEqual(t, banker.GetCoins(std.Address(returnAddr)).String(), "3ugnot")
}
2 changes: 1 addition & 1 deletion docs/assets/how-to-guides/write-simple-dapp/poll-2.gno
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewPoll(title, description string, deadline int64) string {
// yes - true, no - false
func Vote(id string, vote bool) string {
// get txSender
txSender := std.GetOrigCaller()
txSender := std.OriginCaller()

// get specific Poll from AVL tree
pollRaw, exists := polls.Get(id)
Expand Down
34 changes: 17 additions & 17 deletions docs/concepts/effective-gno.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ that could lead to user frustration or the need to fork the code.
import "std"

func Foobar() {
caller := std.PrevRealm().Addr()
caller := std.PreviousRealm().Address()
if caller != "g1xxxxx" {
panic("permission denied")
}
Expand Down Expand Up @@ -169,10 +169,10 @@ var (

func init() {
created = time.Now()
// std.GetOrigCaller in the context of realm initialisation is,
// std.OriginCaller in the context of realm initialisation is,
// of course, the publisher of the realm :)
// This can be better than hardcoding an admin address as a constant.
admin = std.GetOrigCaller()
admin = std.OriginCaller()
// list is already initialized, so it will already contain "foo", "bar" and
// the current time as existing items.
list = append(list, admin.String())
Expand Down Expand Up @@ -399,15 +399,15 @@ certain operations.
import "std"

func PublicMethod(nb int) {
caller := std.PrevRealm().Addr()
caller := std.PreviousRealm().Address()
privateMethod(caller, nb)
}

func privateMethod(caller std.Address, nb int) { /* ... */ }
```

In this example, `PublicMethod` is a public function that can be called by other
realms. It retrieves the caller's address using `std.PrevRealm().Addr()`, and
realms. It retrieves the caller's address using `std.PreviousRealm().Address()`, and
then passes it to `privateMethod`, which is a private function that performs the
actual logic. This way, `privateMethod` can only be called from within the
realm, and it can use the caller's address for authentication or authorization
Expand Down Expand Up @@ -440,11 +440,11 @@ import (
var owner std.Address

func init() {
owner = std.PrevRealm().Addr()
owner = std.PreviousRealm().Address()
}

func ChangeOwner(newOwner std.Address) {
caller := std.PrevRealm().Addr()
caller := std.PreviousRealm().Address()

if caller != owner {
panic("access denied")
Expand Down Expand Up @@ -494,14 +494,14 @@ whitelisted or not.

Let's deep dive into the different access control mechanisms we can use:

One strategy is to look at the caller with `std.PrevRealm()`, which could be the
One strategy is to look at the caller with `std.PreviousRealm()`, which could be the
EOA (Externally Owned Account), or the preceding realm in the call stack.

Another approach is to look specifically at the EOA. For this, you should call
`std.GetOrigCaller()`, which returns the public address of the account that
`std.OriginCaller()`, which returns the public address of the account that
signed the transaction.

TODO: explain when to use `std.GetOrigCaller`.
TODO: explain when to use `std.OriginCaller`.

Internally, this call will look at the frame stack, which is basically the stack
of callers including all the functions, anonymous functions, other realms, and
Expand All @@ -516,7 +516,7 @@ import "std"
var admin std.Address = "g1xxxxx"

func AdminOnlyFunction() {
caller := std.PrevRealm().Addr()
caller := std.PreviousRealm().Address()
if caller != admin {
panic("permission denied")
}
Expand All @@ -527,7 +527,7 @@ func AdminOnlyFunction() {
```

In this example, `AdminOnlyFunction` is a function that can only be called by
the admin. It retrieves the caller's address using `std.PrevRealm().Addr()`,
the admin. It retrieves the caller's address using `std.PreviousRealm().Address()`,
this can be either another realm contract, or the calling user if there is no
other intermediary realm. and then checks if the caller is the admin. If not, it
panics and stops the execution.
Expand All @@ -543,7 +543,7 @@ Here's an example:
import "std"

func TransferTokens(to std.Address, amount int64) {
caller := std.PrevRealm().Addr()
caller := std.PreviousRealm().Address()
if caller != admin {
panic("permission denied")
}
Expand All @@ -552,7 +552,7 @@ func TransferTokens(to std.Address, amount int64) {
```

In this example, `TransferTokens` is a function that can only be called by the
admin. It retrieves the caller's address using `std.PrevRealm().Addr()`, and
admin. It retrieves the caller's address using `std.PreviousRealm().Address()`, and
then checks if the caller is the admin. If not, the function panics and execution is stopped.

By using these access control mechanisms, you can ensure that your contract's
Expand Down Expand Up @@ -631,7 +631,7 @@ type MySafeStruct {
}

func NewSafeStruct() *MySafeStruct {
caller := std.PrevRealm().Addr()
caller := std.PreviousRealm().Address()
return &MySafeStruct{
counter: 0,
admin: caller,
Expand All @@ -640,7 +640,7 @@ func NewSafeStruct() *MySafeStruct {

func (s *MySafeStruct) Counter() int { return s.counter }
func (s *MySafeStruct) Inc() {
caller := std.PrevRealm().Addr()
caller := std.PreviousRealm().Address()
if caller != s.admin {
panic("permission denied")
}
Expand Down Expand Up @@ -704,7 +704,7 @@ import "gno.land/p/demo/grc/grc20"
var fooToken = grc20.NewBanker("Foo Token", "FOO", 4)

func MyBalance() uint64 {
caller := std.PrevRealm().Addr()
caller := std.PreviousRealm().Address()
return fooToken.BalanceOf(caller)
}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/concepts/stdlibs/banker.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The Banker module can be cast into 4 subtypes of bankers that expose different f
### Banker Types

1. `BankerTypeReadonly` - read-only access to coin balances
2. `BankerTypeOrigSend` - full access to coins sent with the transaction that called the banker
2. `BankerTypeOriginSend` - full access to coins sent with the transaction that called the banker
3. `BankerTypeRealmSend` - full access to coins that the realm itself owns, including the ones sent with the transaction
4. `BankerTypeRealmIssue` - able to issue new coins

Expand Down
8 changes: 4 additions & 4 deletions docs/how-to-guides/creating-grc20.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
// init is called once at time of deployment
func init() {
// Set deployer of Realm to admin
admin = std.PrevRealm().Addr()
admin = std.PreviousRealm().Address()

// Set token name, symbol and number of decimals
banker = grc20.NewBanker("My Token", "TKN", 4)
Expand Down Expand Up @@ -142,7 +142,7 @@ caller’s allowance.
```go
// Mint mints amount of tokens to address. Callable only by admin of token
func Mint(address std.Address, amount uint64) {
assertIsAdmin(std.PrevRealm().Addr())
assertIsAdmin(std.PreviousRealm().Address())
checkErr(banker.Mint(address, amount))
}
```
Expand All @@ -153,7 +153,7 @@ increasing the total supply.
```go
// Burn burns amount of tokens from address. Callable only by admin of token
func Burn(address std.Address, amount uint64) {
assertIsAdmin(std.PrevRealm().Addr())
assertIsAdmin(std.PreviousRealm().Address())
checkErr(banker.Burn(address, amount))
}
```
Expand All @@ -162,7 +162,7 @@ decreasing the total supply.

[embedmd]:# (../assets/how-to-guides/creating-grc20/mytoken-2.gno go /.*assertIsAdmin/ /^}/)
```go
assertIsAdmin(std.PrevRealm().Addr())
assertIsAdmin(std.PreviousRealm().Address())
checkErr(banker.Mint(address, amount))
}
```
Expand Down
Loading
Loading