github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/docs/assets/how-to-guides/creating-grc20/mytoken-2.gno (about) 1 // TotalSupply returns the total supply of mytoken 2 func TotalSupply() uint64 { 3 return mytoken.TotalSupply() 4 } 5 6 // Decimals returns the number of decimals of mytoken 7 func Decimals() uint { 8 return mytoken.GetDecimals() 9 } 10 11 // BalanceOf returns the balance mytoken for `account` 12 func BalanceOf(account std.Address) uint64 { 13 balance, err := mytoken.BalanceOf(account) 14 if err != nil { 15 panic(err) 16 } 17 18 return balance 19 } 20 21 // Allowance returns the allowance of spender on owner's balance 22 func Allowance(owner, spender std.Address) uint64 { 23 allowance, err := mytoken.Allowance(owner, spender) 24 if err != nil { 25 panic(err) 26 } 27 28 return allowance 29 } 30 31 // Transfer transfers amount from caller to recipient 32 func Transfer(recipient std.Address, amount uint64) { 33 caller := std.PrevRealm().Addr() 34 if err := mytoken.Transfer(caller, recipient, amount); err != nil { 35 panic(err) 36 } 37 } 38 39 // Approve approves amount of caller's tokens to be spent by spender 40 func Approve(spender std.Address, amount uint64) { 41 caller := std.PrevRealm().Addr() 42 if err := mytoken.Approve(caller, spender, amount); err != nil { 43 panic(err) 44 } 45 } 46 47 // TransferFrom transfers `amount` of tokens from `from` to `to` 48 func TransferFrom(from, to std.Address, amount uint64) { 49 caller := std.PrevRealm().Addr() 50 51 if amount <= 0 { 52 panic("transfer amount must be greater than zero") 53 } 54 55 if err := mytoken.TransferFrom(caller, from, to, amount); err != nil { 56 panic(err) 57 } 58 } 59 60 // Mint mints amount of tokens to address. Callable only by admin of token 61 func Mint(address std.Address, amount uint64) { 62 assertIsAdmin(std.PrevRealm().Addr()) 63 64 if amount <= 0 { 65 panic("mint amount must be greater than zero") 66 } 67 68 if err := mytoken.Mint(address, amount); err != nil { 69 panic(err) 70 } 71 } 72 73 // Burn burns amount of tokens from address. Callable only by admin of token 74 func Burn(address std.Address, amount uint64) { 75 assertIsAdmin(std.PrevRealm().Addr()) 76 77 if amount <= 0 { 78 panic("burn amount must be greater than zero") 79 } 80 81 if err := mytoken.Burn(address, amount); err != nil { 82 panic(err) 83 } 84 } 85 86 // assertIsAdmin asserts the address is the admin of token 87 func assertIsAdmin(address std.Address) { 88 if address != admin { 89 panic("restricted access") 90 } 91 } 92 93 // Render renders the state of the realm 94 func Render(path string) string { 95 parts := strings.Split(path, "/") 96 c := len(parts) 97 98 switch { 99 case path == "": 100 // Default GRC20 render 101 return mytoken.RenderHome() 102 case c == 2 && parts[0] == "balance": 103 // Render balance of specific address 104 owner := std.Address(parts[1]) 105 balance, _ := mytoken.BalanceOf(owner) 106 return ufmt.Sprintf("%d\n", balance) 107 default: 108 return "404\n" 109 } 110 }