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

xgns.gno

2.66 Kb · 121 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
 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.
 32func TotalSupply() int64 {
 33	return token.TotalSupply()
 34}
 35
 36// BalanceOf returns token balance for address.
 37//
 38// Parameters:
 39//   - owner: address to check balance for
 40//
 41// Returns balance amount.
 42func BalanceOf(owner address) int64 {
 43	return token.BalanceOf(owner)
 44}
 45
 46// Render returns a formatted representation of the token state.
 47func Render(path string) string {
 48	if path == "" {
 49		return token.RenderHome()
 50	}
 51
 52	parts := strings.Split(path, "/")
 53	switch parts[0] {
 54	case "balance":
 55		if len(parts) != 2 {
 56			return "404\n"
 57		}
 58		balance := token.BalanceOf(address(parts[1]))
 59		return ufmt.Sprintf("%d\n", balance)
 60	default:
 61		return "404\n"
 62	}
 63}
 64
 65// Mint mints tokens to address.
 66//
 67// Parameters:
 68//   - to: recipient address
 69//   - amount: amount to mint
 70//
 71// Only callable by governance staker contract.
 72func Mint(cur realm, to address, amount int64) {
 73	halt.AssertIsNotHaltedXGns()
 74
 75	caller := cur.Previous().Address()
 76	access.AssertIsGovStaker(caller)
 77
 78	checkErr(ledger.Mint(to, amount))
 79}
 80
 81// Burn burns tokens from address.
 82//
 83// Parameters:
 84//   - from: address to burn from
 85//   - amount: amount to burn
 86//
 87// Only callable by governance staker contract.
 88func Burn(cur realm, from address, amount int64) {
 89	halt.AssertIsNotHaltedWithdraw()
 90
 91	caller := cur.Previous().Address()
 92	access.AssertIsGovStaker(caller)
 93
 94	checkErr(ledger.Burn(from, amount))
 95}
 96
 97// SupplyInfo returns supply breakdown information.
 98//
 99// Returns:
100//   - totalIssued: total xGNS tokens issued
101//   - issuedByDelegate: tokens issued through governance delegation
102//   - issuedByDepositGns: tokens issued through launchpad deposit
103//   - error: error if launchpad address not found
104func SupplyInfo() (totalIssued, issuedByDelegate, issuedByDepositGns int64, err error) {
105	launchpad, ok := access.GetAddress(prbac.ROLE_LAUNCHPAD.String())
106	if !ok {
107		return 0, 0, 0, errors.New("launchpad address not found")
108	}
109
110	totalIssued = token.TotalSupply()
111	issuedByDepositGns = token.BalanceOf(launchpad)
112	issuedByDelegate = totalIssued - issuedByDepositGns
113
114	return totalIssued, issuedByDelegate, issuedByDepositGns, nil
115}
116
117func checkErr(err error) {
118	if err != nil {
119		panic(err)
120	}
121}