package.gno

2.25 Kb ยท 100 lines
  1// meme is a GRC20 token contract where all the grc20.Teller methods are
  2// proxified with top-level functions. see also gno.land/r/demo/bar20.
  3package doge
  4
  5import (
  6	"std"
  7	"strings"
  8
  9	"gno.land/p/demo/grc/grc20"
 10	"gno.land/p/demo/ownable"
 11	"gno.land/p/demo/ufmt"
 12	"gno.land/r/demo/grc20reg"
 13)
 14
 15var (
 16	Token, privateLedger = grc20.NewToken("doge", "doge", 4)
 17	UserTeller           = Token.CallerTeller()
 18	Ownable              = ownable.NewWithAddress("g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d")
 19)
 20
 21func init() {
 22	privateLedger.Mint(Ownable.Owner(), 1_000_000*1_000_000) // @privateLedgeristrator (1M)
 23	grc20reg.Register(Token.Getter(), "")
 24}
 25
 26func GetName() string {
 27	return UserTeller.GetName()
 28}
 29
 30func GetSymbol() string {
 31	return UserTeller.GetSymbol()
 32}
 33
 34func GetDecimals() uint {
 35	return UserTeller.GetDecimals()
 36}
 37
 38func TotalSupply() uint64 {
 39	return UserTeller.TotalSupply()
 40}
 41
 42func BalanceOf(owner std.Address) uint64 {
 43	return UserTeller.BalanceOf(owner)
 44}
 45
 46func Allowance(owner, spender std.Address) uint64 {
 47	return UserTeller.Allowance(owner, spender)
 48}
 49
 50func Transfer(to std.Address, amount uint64) {
 51	checkErr(UserTeller.Transfer(to, amount))
 52}
 53
 54func Approve(spender std.Address, amount uint64) {
 55	checkErr(UserTeller.Approve(spender, amount))
 56}
 57
 58func TransferFrom(from, to std.Address, amount uint64) {
 59	checkErr(UserTeller.TransferFrom(from, to, amount))
 60}
 61
 62// Faucet is distributing meme tokens without restriction (unsafe).
 63// For a real token faucet, you should take care of setting limits are asking payment.
 64func Faucet() {
 65	caller := std.PreviousRealm().Address()
 66	amount := uint64(1_000 * 10_000) // 1k
 67	checkErr(privateLedger.Mint(caller, amount))
 68}
 69
 70func Mint(to std.Address, amount uint64) {
 71	Ownable.AssertCallerIsOwner()
 72	checkErr(privateLedger.Mint(to, amount))
 73}
 74
 75func Burn(from std.Address, amount uint64) {
 76	Ownable.AssertCallerIsOwner()
 77	checkErr(privateLedger.Burn(from, amount))
 78}
 79
 80func Render(path string) string {
 81	parts := strings.Split(path, "/")
 82	c := len(parts)
 83
 84	switch {
 85	case path == "":
 86		return Token.RenderHome()
 87	case c == 2 && parts[0] == "balance":
 88		owner := std.Address(parts[1])
 89		balance := UserTeller.BalanceOf(owner)
 90		return ufmt.Sprintf("%d\n", balance)
 91	default:
 92		return "404\n"
 93	}
 94}
 95
 96func checkErr(err error) {
 97	if err != nil {
 98		panic(err)
 99	}
100}