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(stdlibs/std)!: remove Get prefixes, extend abbreviations #3374

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
c57797d
chore: remove Get prefixes from GetCallerAt, GetOrigSend, GetOrigCaller
hthieu1110 Dec 19, 2024
6156309
Merge remote-tracking branch 'remote/master' into chore/remove-get-pr…
hthieu1110 Dec 19, 2024
c58ae72
chore: expanding abbreviations
hthieu1110 Dec 19, 2024
6879236
chore: remove prefix GetCallerAt => CallerAt
hthieu1110 Dec 19, 2024
d41482e
Merge branch 'master' into chore/remove-get-prefix-1475
Dec 20, 2024
fbc982d
refactor: PrevRealm => PreviousRealm
Dec 20, 2024
0f79638
refactor: Orig => Origin
Dec 20, 2024
627d000
refactor: orig => origin
Dec 20, 2024
8489ec4
refactor: Addr => Address
Dec 20, 2024
e2223ff
refactor: refactor missing Addr => Address for Realm
Dec 20, 2024
eb965f1
fix: fix test crossrealm
Dec 20, 2024
ad57bfc
refactor: address feedbacks
hthieu1110 Dec 20, 2024
afd55ca
Merge remote-tracking branch 'remote/master' into chore/remove-get-pr…
hthieu1110 Dec 20, 2024
df80fe8
Merge remote-tracking branch 'remote/master' into chore/remove-get-pr…
hthieu1110 Dec 21, 2024
2a4eb54
refactor: refactor new merged code
hthieu1110 Dec 21, 2024
90b9a33
Merge branch 'master' into chore/remove-get-prefix-1475
leohhhn Dec 27, 2024
87d6705
Merge remote-tracking branch 'remote/master' into chore/remove-get-pr…
Dec 30, 2024
93eec42
refactor: refactor new added code
Dec 30, 2024
daf1574
refactor: refactor new added code
Dec 30, 2024
ec5c4f1
refactor: refactor new added code
Dec 30, 2024
8d7571c
Merge remote-tracking branch 'remote/master' into chore/remove-get-pr…
Jan 2, 2025
29c3282
Merge remote-tracking branch 'remote/master' into chore/remove-get-pr…
hthieu1110 Jan 8, 2025
e9a799e
chore: merge with master
Jan 13, 2025
c63186b
refactor: refactor new merged code
Jan 13, 2025
da38008
refactor: remove get prefix
Jan 13, 2025
48a7606
fix: remove removed code
Jan 13, 2025
6752cd8
Merge remote-tracking branch 'remote/master' into chore/remove-get-pr…
hthieu1110 Jan 13, 2025
c31f410
chore: merge with master
Jan 15, 2025
5c6abaf
refactor: GetBanker => NewBanker, GetChainDomain ==> ChainDomain, Get…
Jan 15, 2025
1cc833c
refactor: refactor missing code
Jan 15, 2025
55a6ed3
Merge branch 'master' into chore/remove-get-prefix-1475
hthieu1110 Jan 15, 2025
bbbd553
refactor: refactor missing code
Jan 15, 2025
9f96395
Merge remote-tracking branch 'remote/master' into chore/remove-get-pr…
Jan 15, 2025
cfb7233
chore: revert ChainID, ChainDomain, Height
hthieu1110 Jan 15, 2025
a2b63b0
chore: revert ChainID refactor
hthieu1110 Jan 15, 2025
f14548c
Merge remote-tracking branch 'remote/master' into chore/remove-get-pr…
Jan 17, 2025
b994dde
refactor: GetHeight => ChainHeight, GetChainID => ChainID, GetChainDo…
Jan 17, 2025
9e78817
refactor: refactor missing code
Jan 17, 2025
19ee3d9
Merge remote-tracking branch 'remote/master' into chore/remove-get-pr…
Jan 20, 2025
7b233fe
fix: fix refactoring
Jan 20, 2025
1fc101c
merged with master
Jan 21, 2025
8f6ed21
chore: revert test5 genesis
Jan 21, 2025
9fdc06a
refactor: refactor new merged code
Jan 21, 2025
ae600c9
refactor: refactor new merged code
Jan 21, 2025
943628e
fix: fix tests
Jan 21, 2025
a9cfec4
Merge branch 'master' of github.com:gnolang/gno into chore/remove-get…
thehowl Jan 21, 2025
174f1d0
Merge remote-tracking branch 'remote/master' into chore/remove-get-pr…
hthieu1110 Jan 21, 2025
4a47c55
Merge branch 'master' into chore/remove-get-prefix-1475
hthieu1110 Jan 22, 2025
1ec0604
merge with master + resolve conflict
hthieu1110 Jan 27, 2025
ce557b8
Merge branch 'chore/remove-get-prefix-1475' of https://github.com/hth…
hthieu1110 Jan 27, 2025
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