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

grc20reg_helper.gno

6.12 Kb · 193 lines
  1package common
  2
  3import (
  4	"gno.land/p/demo/tokens/grc20"
  5	ufmt "gno.land/p/nt/ufmt/v0"
  6	"gno.land/r/demo/defi/grc20reg"
  7)
  8
  9// GetToken returns a grc20.Token instance for the specified tokenKey, panicking if not registered.
 10//
 11// Parameters:
 12//   - tokenKey: GRC20 registry key
 13//
 14// Returns a pointer to the grc20.Token instance.
 15//
 16// Panics if the token is not registered in grc20reg.
 17func GetToken(tokenKey string) *grc20.Token {
 18	return grc20reg.MustGet(tokenKey)
 19}
 20
 21// GetTokenTeller returns a CallerTeller for the specified tokenKey, panicking if not registered.
 22//
 23// Parameters:
 24//   - tokenKey: GRC20 registry key
 25//
 26// Returns a grc20.Teller whose write methods derive the actor from the caller
 27// realm.
 28//
 29// Panics if the token is not registered in grc20reg.
 30func GetTokenTeller(tokenKey string) grc20.Teller {
 31	return GetToken(tokenKey).CallerTeller()
 32}
 33
 34// IsRegistered checks if a token is registered in grc20reg, returning nil if registered or error if not.
 35//
 36// Parameters:
 37//   - tokenKey: GRC20 registry key
 38//
 39// Returns nil if the token is registered, or an error describing the issue.
 40func IsRegistered(tokenKey string) error {
 41	if grc20reg.Get(tokenKey) == nil {
 42		return ufmt.Errorf("token(%s) is not registered to grc20reg", tokenKey)
 43	}
 44	return nil
 45}
 46
 47// MustRegistered checks if all provided tokens are registered, panicking if any is not registered.
 48//
 49// Parameters:
 50//   - tokenKeys: variable number of GRC20 registry keys to check
 51//
 52// Panics if any of the provided tokens is not registered in grc20reg.
 53func MustRegistered(tokenKeys ...string) {
 54	for _, tokenKey := range tokenKeys {
 55		if err := IsRegistered(tokenKey); err != nil {
 56			panic(newErrorWithDetail(
 57				errNotRegistered,
 58				ufmt.Sprintf("token(%s)", tokenKey),
 59			))
 60		}
 61	}
 62}
 63
 64// TotalSupply returns the total supply of the specified token.
 65//
 66// Parameters:
 67//   - tokenKey: GRC20 registry key
 68//
 69// Returns the total supply of the token as int64.
 70func TotalSupply(tokenKey string) int64 {
 71	return GetToken(tokenKey).TotalSupply()
 72}
 73
 74// BalanceOf returns the token balance for the specified address.
 75//
 76// Parameters:
 77//   - tokenKey: GRC20 registry key
 78//   - addr: address to query the balance for
 79//
 80// Returns the token balance as int64.
 81func BalanceOf(tokenKey string, addr address) int64 {
 82	return GetToken(tokenKey).BalanceOf(addr)
 83}
 84
 85// Allowance returns the token allowance from owner to spender.
 86//
 87// Parameters:
 88//   - tokenKey: GRC20 registry key
 89//   - owner: address of the token owner
 90//   - spender: address of the spender
 91//
 92// Returns the allowance amount as int64.
 93func Allowance(tokenKey string, owner, spender address) int64 {
 94	return GetToken(tokenKey).Allowance(owner, spender)
 95}
 96
 97// Transfer crosses into common before forwarding to grc20.Teller.Transfer.
 98// CallerTeller uses cur.Previous() as the token actor, so this helper must
 99// remain a crossing adapter called with cross(cur).
100//
101// Parameters:
102//   - cur: current realm
103//   - tokenKey: GRC20 registry key
104//   - to: recipient address
105//   - amount: amount of tokens to transfer
106//
107// Returns an error if the transfer fails, nil otherwise.
108func Transfer(cur realm, tokenKey string, to address, amount int64) error {
109	return GetTokenTeller(tokenKey).Transfer(0, cur, to, amount)
110}
111
112// TransferFrom crosses into common before forwarding to grc20.Teller.TransferFrom.
113// CallerTeller uses cur.Previous() as the spender, so this helper must remain
114// a crossing adapter called with cross(cur).
115//
116// Parameters:
117//   - cur: current realm
118//   - tokenKey: GRC20 registry key
119//   - from: sender address
120//   - to: recipient address
121//   - amount: amount of tokens to transfer
122//
123// Returns an error if the transfer fails, nil otherwise.
124func TransferFrom(cur realm, tokenKey string, from, to address, amount int64) error {
125	return GetTokenTeller(tokenKey).TransferFrom(0, cur, from, to, amount)
126}
127
128// Approve crosses into common before forwarding to grc20.Teller.Approve.
129// CallerTeller uses cur.Previous() as the approver, so this helper must
130// remain a crossing adapter called with cross(cur).
131//
132// Parameters:
133//   - cur: current realm
134//   - tokenKey: GRC20 registry key
135//   - spender: address allowed to spend the tokens
136//   - amount: amount of tokens to approve
137//
138// Returns an error if the approval fails, nil otherwise.
139func Approve(cur realm, tokenKey string, spender address, amount int64) error {
140	return GetTokenTeller(tokenKey).Approve(0, cur, spender, amount)
141}
142
143// SafeGRC20Transfer crosses into common, transfers tokens, and panics if it fails.
144// CallerTeller uses cur.Previous() as the token actor, so this helper must
145// remain a crossing adapter called with cross(cur).
146//
147// Parameters:
148//   - cur: current realm
149//   - tokenKey: GRC20 registry key
150//   - to: recipient address
151//   - amount: amount of tokens to transfer
152//
153// Panics if the transfer fails.
154func SafeGRC20Transfer(cur realm, tokenKey string, to address, amount int64) {
155	if err := GetTokenTeller(tokenKey).Transfer(0, cur, to, amount); err != nil {
156		panic(err)
157	}
158}
159
160// SafeGRC20TransferFrom crosses into common, transfers tokens, and panics if it fails.
161// CallerTeller uses cur.Previous() as the spender, so this helper must remain
162// a crossing adapter called with cross(cur).
163//
164// Parameters:
165//   - cur: current realm
166//   - tokenKey: GRC20 registry key
167//   - from: sender address
168//   - to: recipient address
169//   - amount: amount of tokens to transfer
170//
171// Panics if the transfer fails.
172func SafeGRC20TransferFrom(cur realm, tokenKey string, from, to address, amount int64) {
173	if err := GetTokenTeller(tokenKey).TransferFrom(0, cur, from, to, amount); err != nil {
174		panic(err)
175	}
176}
177
178// SafeGRC20Approve crosses into common, approves tokens, and panics if it fails.
179// CallerTeller uses cur.Previous() as the approver, so this helper must remain
180// a crossing adapter called with cross(cur).
181//
182// Parameters:
183//   - cur: current realm
184//   - tokenKey: GRC20 registry key
185//   - spender: address allowed to spend the tokens
186//   - amount: amount of tokens to approve
187//
188// Panics if the approval fails.
189func SafeGRC20Approve(cur realm, tokenKey string, spender address, amount int64) {
190	if err := GetTokenTeller(tokenKey).Approve(0, cur, spender, amount); err != nil {
191		panic(err)
192	}
193}