Skip to content

Commit

Permalink
Add ZeroAddress const, create ZeroAccount during InitChainer
Browse files Browse the repository at this point in the history
  • Loading branch information
thinhnx-var committed Jun 20, 2024
1 parent d2d34eb commit 5911266
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions gno.land/pkg/gnoland/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ func InitChainer(baseApp *sdk.BaseApp, acctKpr auth.AccountKeeperI, bankKpr bank
// Get genesis state
genState := req.AppState.(GnoGenesisState)

// Init ZeroAccount
zeroAcc := acctKpr.NewAccountWithAddress(ctx, std.ZeroAddress())
acctKpr.SetAccount(ctx, zeroAcc)

// Parse and set genesis state balances
for _, bal := range genState.Balances {
acc := acctKpr.NewAccountWithAddress(ctx, bal.Address)
Expand Down
6 changes: 6 additions & 0 deletions gno.land/pkg/sdk/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,10 @@ func (bnk *SDKBanker) RemoveCoin(b32addr crypto.Bech32Address, denom string, amo
if err != nil {
panic(err)
}
// TODO: once TotalCoin is implemented, we need to deduct the TotalCoins beside send coin to ZeroAddress (?)
zeroAddr := std.ZeroAddress()
_, err = bnk.vmk.bank.AddCoins(bnk.ctx, zeroAddr, std.Coins{std.Coin{denom, amount}})
if err != nil {
panic(err)
}
}
4 changes: 4 additions & 0 deletions gnovm/stdlibs/std/native.gno
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package std

const (
ZeroAddress Address = "g100000000000000000000000000000000dnmcnx"
)

// AssertOriginCall panics if [IsOriginCall] returns false.
func AssertOriginCall() // injected

Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/zrealm_natbind0_stdlibs.gno
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "native.gno",
// "Line": "13",
// "Line": "17",
// "Nonce": "0",
// "PkgPath": "std"
// }
Expand Down
15 changes: 15 additions & 0 deletions tm2/pkg/std/zero_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package std

import "github.com/gnolang/gno/tm2/pkg/crypto"

const (
zAddressBech32 = string("g100000000000000000000000000000000dnmcnx")
)

func ZeroAddress() crypto.Address {
ZAddress, err := crypto.AddressFromBech32(zAddressBech32)
if err != nil {
panic(err)

Check warning on line 12 in tm2/pkg/std/zero_address.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/std/zero_address.go#L12

Added line #L12 was not covered by tests
}
return ZAddress
}
12 changes: 12 additions & 0 deletions tm2/pkg/std/zero_address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package std

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_ZAccount(t *testing.T) {
zA := ZeroAddress()
assert.Exactly(t, zA.String(), "g100000000000000000000000000000000dnmcnx")
}

0 comments on commit 5911266

Please sign in to comment.