Skip to content

Commit

Permalink
feat(stdlibs/std)!: remove Get prefixes, extend abbreviations (#3374)
Browse files Browse the repository at this point in the history
Linked to #1475

- GetCallerAt =>CallerAt
- GetOrigSend => OriginSend
- origSend => originSend
- GetOrigCaller => OriginCaller
- origCaller => originCaller
- Orig => Origin
- Addr => Address
- PrevRealm => PreviousRealm
- GetChainID => ChainID
- GetBanker => NewBanker
- GetChainDomain => ChainDomain
- GetHeight => Height


BREAKING CHANGE: changes many of the existing standard library
functions.

---------

Co-authored-by: hieu.ha <[email protected]>
Co-authored-by: Leon Hudak <[email protected]>
Co-authored-by: Morgan Bazalgette <[email protected]>
  • Loading branch information
4 people authored Feb 19, 2025
1 parent ec696d4 commit 0a239e1
Show file tree
Hide file tree
Showing 242 changed files with 1,195 additions and 1,194 deletions.
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
@@ -1,5 +1,5 @@
func AuctionEnd() {
if std.GetHeight() < auctionEndBlock {
if std.ChainHeight() < auctionEndBlock {
panic("Auction hasn't ended")
}

Expand All @@ -10,8 +10,8 @@ func AuctionEnd() {
ended = true

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

banker.SendCoins(pkgAddr, receiver, std.Coins{{"ugnot", int64(highestBid)}})
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func TestAuctionEnd(t *testing.T) {
shouldNoPanic(t, AuctionEnd)
shouldEqual(t, ended, true)

banker := std.GetBanker(std.BankerTypeRealmSend)
banker := std.NewBanker(std.BankerTypeRealmSend)
shouldEqual(t, banker.GetCoins(receiver).String(), "3ugnot")

// Auction has already ended
Expand Down
22 changes: 11 additions & 11 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 All @@ -68,7 +68,7 @@ func TestFull(t *testing.T) {
shouldNoPanic(t, AuctionEnd)
shouldEqual(t, ended, true)

banker := std.GetBanker(std.BankerTypeRealmSend)
banker := std.NewBanker(std.BankerTypeRealmSend)
shouldEqual(t, banker.GetCoins(receiver).String(), "2ugnot")
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var (
receiver = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
auctionEndBlock = std.GetHeight() + uint(300) // in blocks
auctionEndBlock = std.ChainHeight() + uint(300) // in blocks
highestBidder std.Address
highestBid = uint(0)
pendingReturns avl.Tree
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
func Bid() {
if std.GetHeight() > auctionEndBlock {
if std.ChainHeight() > auctionEndBlock {
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
10 changes: 5 additions & 5 deletions docs/assets/how-to-guides/porting-solidity-to-gno/porting-8.gno
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()
banker := std.NewBanker(std.BankerTypeRealmSend)
pkgAddr := std.OriginPkgAddress()

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 @@ -10,8 +10,8 @@ func TestWithdraw(t *testing.T) {
shouldEqual(t, pendingReturns.Size(), 1)
shouldEqual(t, pendingReturns.Has(returnAddr), true)

banker := std.GetBanker(std.BankerTypeRealmSend)
pkgAddr := std.GetOrigPkgAddr()
banker := std.NewBanker(std.BankerTypeRealmSend)
pkgAddr := std.OriginPkgAddress()
banker.SendCoins(pkgAddr, std.Address(returnAddr), std.Coins{{"ugnot", returnAmount}})
shouldEqual(t, banker.GetCoins(std.Address(returnAddr)).String(), "3ugnot")
}
4 changes: 2 additions & 2 deletions docs/assets/how-to-guides/simple-library/tapas.gno
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ var listOfTapas = []string{
func GetTapaSuggestion(userInput string) string {

// Create a random number depending on the block height.
// We get the block height using std.GetHeight(), which is from an imported Gno library, "std"
// We get the block height using std.ChainHeight(), which is from an imported Gno library, "std"
// Note: this value is not fully random and is easily guessable
randomNumber := int(std.GetHeight()) % len(listOfTapas)
randomNumber := int(std.ChainHeight()) % len(listOfTapas)

// Return the random suggestion
return listOfTapas[randomNumber]
Expand Down
6 changes: 3 additions & 3 deletions docs/assets/how-to-guides/write-simple-dapp/poll-2.gno
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func init() {
// NewPoll - Creates a new Poll instance
func NewPoll(title, description string, deadline int64) string {
// get block height
if deadline <= std.GetHeight() {
if deadline <= std.ChainHeight() {
panic("deadline has to be in the future")
}

Expand All @@ -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 All @@ -57,7 +57,7 @@ func Vote(id string, vote bool) string {
panic("you've already voted!")
}

if poll.Deadline() <= std.GetHeight() {
if poll.Deadline() <= std.ChainHeight() {
panic("voting for this poll is closed")
}

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 @@ -449,15 +449,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 @@ -490,11 +490,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 @@ -544,14 +544,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 @@ -566,7 +566,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 @@ -577,7 +577,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 @@ -593,7 +593,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 @@ -602,7 +602,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 @@ -681,7 +681,7 @@ type MySafeStruct {
}

func NewSafeStruct() *MySafeStruct {
caller := std.PrevRealm().Addr()
caller := std.PreviousRealm().Address()
return &MySafeStruct{
counter: 0,
admin: caller,
Expand All @@ -690,7 +690,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 @@ -754,7 +754,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
Loading

0 comments on commit 0a239e1

Please sign in to comment.