Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

xgns.gno

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