Search Apps Documentation Source Content File Folder Download Copy Actions Download

mytoken.gno

1.01 Kb · 55 lines
 1package mytoken
 2
 3import (
 4	"strconv"
 5
 6	"gno.land/p/nt/avl/v0"
 7
 8	"chain/runtime/unsafe"
 9)
10
11var (
12	balances    avl.Tree
13	totalSupply int64
14	admin       address
15)
16
17func init() {
18	admin = unsafe.OriginCaller()
19	totalSupply = 1000000
20	balances.Set(admin.String(), totalSupply)
21}
22
23func Transfer(cur realm, to address, amount int64) {
24	caller := cur.Previous().Address()
25
26	fromRaw, exists := balances.Get(caller.String())
27	if !exists {
28		panic("caller has no balance")
29	}
30	fromBalance := fromRaw.(int64)
31	if fromBalance < amount {
32		panic("insufficient balance")
33	}
34	balances.Set(caller.String(), fromBalance-amount)
35
36	toRaw, exists := balances.Get(to.String())
37	var toBalance int64
38	if exists {
39		toBalance = toRaw.(int64)
40	}
41	balances.Set(to.String(), toBalance+amount)
42}
43
44func BalanceOf(addr address) int64 {
45	raw, exists := balances.Get(addr.String())
46	if !exists {
47		return 0
48	}
49	return raw.(int64)
50}
51
52func Render(path string) string {
53	return "Total supply: " + strconv.FormatInt(totalSupply, 10) +
54		"\nAdmin: " + admin.String()
55}