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