gnolang / gno

Gno: An interpreted, stack-based Go virtual machine to build succinct and composable apps + Gno.land: a blockchain for timeless code and fair open-source.
https://gno.land/
Other
883 stars 367 forks source link

Problem with import 'users' realm #1756

Closed kazai777 closed 6 months ago

kazai777 commented 6 months ago

[name AddressOrName not declared]

Description

Hi, I have an error with the import of the realm 'users' in local environment. This problem does not occur when I use the 'gno playground'.

Your environment

Actual behaviour

When I use the AddressOrName() that I use with the realm import gno.land/r/demo/users I get the error below. But if I import the gno.land/p/demo/users package, I no longer get this error. The problem is that if my realm uses functions from both the realm and the users package I'm stuck, because I can't import both, and importing only the users realm doesn't seem to give access to the users package functions. I don't get this error on the 'gno playground' using the same code.

Logs

Screenshot 2024-03-11 at 20 31 15

My code

Screenshot 2024-03-11 at 20 39 07

My gno.mod

Screenshot 2024-03-11 at 20 29 46

deelawn commented 6 months ago

Hi @kazai777. Resolve is a function in the users realm, not a method on the struct from the users package. https://github.com/gnolang/gno/blob/master/examples/gno.land/r/demo/users/users.gno#L238

Also, importing both the users realm and package should be fine -- you'll just need to alias one of the names to avoid a conflict. I think something like this is what you're looking for.

package ethtoken

import (
    "gno.land/p/demo/grc/grc20"
    "gno.land/p/demo/ufmt"
    "gno.land/p/demo/users"
    usersRealm "gno.land/r/demo/users"
    "std"
)

var (
    ethtoken *grc20.AdminToken
    admin std.Address
)

func init() {
    ethtoken = grc20.NewAdminToken("Ethereum", "ETH", 4)
    ethtoken.Mint(std.GetOrigCaller(), 1000000000)
}

func TotalSupply() uint64 {
    return ethtoken.TotalSupply()
}

func BalanceOf(owner users.AddressOrName) uint64 {
    balance, err := ethtoken.BalanceOf(usersRealm.Resolve(owner))
    if err != nil {
        panic(err)
    }

    return balance
}
kazai777 commented 6 months ago

Thank you @deelawn for your answer.