github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/r/demo/foo20/foo20.gno (about)

     1  package foo20
     2  
     3  import (
     4  	"std"
     5  	"strings"
     6  
     7  	"gno.land/p/demo/grc/grc20"
     8  	"gno.land/p/demo/ufmt"
     9  	"gno.land/r/demo/users"
    10  
    11  	pusers "gno.land/p/demo/users"
    12  )
    13  
    14  var (
    15  	foo   *grc20.AdminToken
    16  	admin std.Address = "g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj" // TODO: helper to change admin
    17  )
    18  
    19  func init() {
    20  	foo = grc20.NewAdminToken("Foo", "FOO", 4)
    21  	foo.Mint(admin, 1000000*10000)                                    // @administrator (1M)
    22  	foo.Mint("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq", 10000*10000) // @manfred (10k)
    23  }
    24  
    25  // method proxies as public functions.
    26  //
    27  
    28  // getters.
    29  
    30  func TotalSupply() uint64 {
    31  	return foo.TotalSupply()
    32  }
    33  
    34  func BalanceOf(owner pusers.AddressOrName) uint64 {
    35  	balance, err := foo.BalanceOf(users.Resolve(owner))
    36  	if err != nil {
    37  		panic(err)
    38  	}
    39  	return balance
    40  }
    41  
    42  func Allowance(owner, spender pusers.AddressOrName) uint64 {
    43  	allowance, err := foo.Allowance(users.Resolve(owner), users.Resolve(spender))
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  	return allowance
    48  }
    49  
    50  // setters.
    51  
    52  func Transfer(to pusers.AddressOrName, amount uint64) {
    53  	caller := std.PrevRealm().Addr()
    54  	err := foo.Transfer(caller, users.Resolve(to), amount)
    55  	if err != nil {
    56  		panic(err)
    57  	}
    58  }
    59  
    60  func Approve(spender pusers.AddressOrName, amount uint64) {
    61  	caller := std.PrevRealm().Addr()
    62  	err := foo.Approve(caller, users.Resolve(spender), amount)
    63  	if err != nil {
    64  		panic(err)
    65  	}
    66  }
    67  
    68  func TransferFrom(from, to pusers.AddressOrName, amount uint64) {
    69  	caller := std.PrevRealm().Addr()
    70  	err := foo.TransferFrom(caller, users.Resolve(from), users.Resolve(to), amount)
    71  	if err != nil {
    72  		panic(err)
    73  	}
    74  }
    75  
    76  // faucet.
    77  
    78  func Faucet() {
    79  	// FIXME: add limits?
    80  	// FIXME: add payment in gnot?
    81  	caller := std.PrevRealm().Addr()
    82  	err := foo.Mint(caller, 1000*10000) // 1k
    83  	if err != nil {
    84  		panic(err)
    85  	}
    86  }
    87  
    88  // administration.
    89  
    90  func Mint(address pusers.AddressOrName, amount uint64) {
    91  	caller := std.PrevRealm().Addr()
    92  	assertIsAdmin(caller)
    93  	err := foo.Mint(users.Resolve(address), amount)
    94  	if err != nil {
    95  		panic(err)
    96  	}
    97  }
    98  
    99  func Burn(address pusers.AddressOrName, amount uint64) {
   100  	caller := std.PrevRealm().Addr()
   101  	assertIsAdmin(caller)
   102  	err := foo.Burn(users.Resolve(address), amount)
   103  	if err != nil {
   104  		panic(err)
   105  	}
   106  }
   107  
   108  // render.
   109  //
   110  
   111  func Render(path string) string {
   112  	parts := strings.Split(path, "/")
   113  	c := len(parts)
   114  
   115  	switch {
   116  	case path == "":
   117  		return foo.RenderHome()
   118  	case c == 2 && parts[0] == "balance":
   119  		owner := pusers.AddressOrName(parts[1])
   120  		balance, _ := foo.BalanceOf(users.Resolve(owner))
   121  		return ufmt.Sprintf("%d\n", balance)
   122  	default:
   123  		return "404\n"
   124  	}
   125  }
   126  
   127  func assertIsAdmin(address std.Address) {
   128  	if address != admin {
   129  		panic("restricted access")
   130  	}
   131  }