xgns.gno
2.57 Kb ยท 114 lines
1package xgns
2
3import (
4 "chain"
5 "chain/runtime"
6 "errors"
7 "strings"
8
9 "gno.land/p/demo/tokens/grc20"
10 "gno.land/p/nt/ownable"
11 "gno.land/p/nt/ufmt"
12
13 "gno.land/r/gnoswap/access"
14 "gno.land/r/gnoswap/halt"
15
16 prbac "gno.land/p/gnoswap/rbac"
17)
18
19var (
20 admin = ownable.NewWithAddress(chain.PackageAddress(prbac.ROLE_GOV_STAKER.String()))
21 token, ledger = grc20.NewToken("XGNS", "xGNS", 6)
22)
23
24// TotalSupply returns the total supply of xGNS tokens.
25func TotalSupply() int64 {
26 return token.TotalSupply()
27}
28
29// BalanceOf returns token balance for address.
30//
31// Parameters:
32// - owner: address to check balance for
33//
34// Returns balance amount.
35func BalanceOf(owner address) int64 {
36 return token.BalanceOf(owner)
37}
38
39// Render returns a formatted representation of the token state.
40func Render(path string) string {
41 if path == "" {
42 return token.RenderHome()
43 }
44
45 parts := strings.Split(path, "/")
46 switch parts[0] {
47 case "balance":
48 if len(parts) != 2 {
49 return "404\n"
50 }
51 balance := token.BalanceOf(address(parts[1]))
52 return ufmt.Sprintf("%d\n", balance)
53 default:
54 return "404\n"
55 }
56}
57
58// Mint mints tokens to address.
59//
60// Parameters:
61// - to: recipient address
62// - amount: amount to mint
63//
64// Only callable by governance staker contract.
65func Mint(cur realm, to address, amount int64) {
66 halt.AssertIsNotHaltedXGns()
67
68 caller := runtime.PreviousRealm().Address()
69 access.AssertIsGovStaker(caller)
70
71 checkErr(ledger.Mint(to, amount))
72}
73
74// Burn burns tokens from address.
75//
76// Parameters:
77// - from: address to burn from
78// - amount: amount to burn
79//
80// Only callable by governance staker contract.
81func Burn(cur realm, from address, amount int64) {
82 halt.AssertIsNotHaltedXGns()
83
84 caller := runtime.PreviousRealm().Address()
85 access.AssertIsGovStaker(caller)
86
87 checkErr(ledger.Burn(from, amount))
88}
89
90// SupplyInfo returns supply breakdown information.
91//
92// Returns:
93// - totalIssued: total xGNS tokens issued
94// - issuedByDelegate: tokens issued through governance delegation
95// - issuedByDepositGns: tokens issued through launchpad deposit
96// - error: error if launchpad address not found
97func SupplyInfo() (totalIssued, issuedByDelegate, issuedByDepositGns int64, err error) {
98 launchpad, ok := access.GetAddress(prbac.ROLE_LAUNCHPAD.String())
99 if !ok {
100 return 0, 0, 0, errors.New("launchpad address not found")
101 }
102
103 totalIssued = token.TotalSupply()
104 issuedByDepositGns = token.BalanceOf(launchpad)
105 issuedByDelegate = totalIssued - issuedByDepositGns
106
107 return totalIssued, issuedByDelegate, issuedByDepositGns, nil
108}
109
110func checkErr(err error) {
111 if err != nil {
112 panic(err)
113 }
114}