grc20reg_helper.gno
5.34 Kb ยท 181 lines
1package common
2
3import (
4 "gno.land/p/demo/tokens/grc20"
5 "gno.land/p/nt/ufmt"
6 "gno.land/r/demo/defi/grc20reg"
7)
8
9// GetToken returns a grc20.Token instance for the specified path, panicking if not registered.
10//
11// Parameters:
12// - path: GRC20 token path
13//
14// Returns a pointer to the grc20.Token instance.
15//
16// Panics if the token is not registered in grc20reg.
17func GetToken(path string) *grc20.Token {
18 return grc20reg.MustGet(path)
19}
20
21// GetTokenTeller returns a grc20.Teller instance for the specified path, panicking if not registered.
22//
23// Parameters:
24// - path: GRC20 token path
25//
26// Returns a grc20.Teller instance for making token operations.
27//
28// Panics if the token is not registered in grc20reg.
29func GetTokenTeller(path string) grc20.Teller {
30 return GetToken(path).CallerTeller()
31}
32
33// IsRegistered checks if a token is registered in grc20reg, returning nil if registered or error if not.
34//
35// Parameters:
36// - path: GRC20 token path
37//
38// Returns nil if the token is registered, or an error describing the issue.
39func IsRegistered(path string) error {
40 getter := grc20reg.Get(path)
41 if getter == nil {
42 return ufmt.Errorf("token(%s) is not registered to grc20reg", path)
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// - paths: variable number of GRC20 token paths to check
51//
52// Panics if any of the provided tokens is not registered in grc20reg.
53func MustRegistered(paths ...string) {
54 for _, path := range paths {
55 if err := IsRegistered(path); err != nil {
56 panic(newErrorWithDetail(
57 errNotRegistered,
58 ufmt.Sprintf("token(%s)", path),
59 ))
60 }
61 }
62}
63
64// TotalSupply returns the total supply of the specified token.
65//
66// Parameters:
67// - path: GRC20 token path
68//
69// Returns the total supply of the token as int64.
70func TotalSupply(path string) int64 {
71 return GetToken(path).TotalSupply()
72}
73
74// BalanceOf returns the token balance for the specified address.
75//
76// Parameters:
77// - path: GRC20 token path
78// - addr: address to query the balance for
79//
80// Returns the token balance as int64.
81func BalanceOf(path string, addr address) int64 {
82 return GetToken(path).BalanceOf(addr)
83}
84
85// Allowance returns the token allowance from owner to spender.
86//
87// Parameters:
88// - path: GRC20 token path
89// - owner: address of the token owner
90// - spender: address of the spender
91//
92// Returns the allowance amount as int64.
93func Allowance(path string, owner, spender address) int64 {
94 return GetToken(path).Allowance(owner, spender)
95}
96
97// Transfer transfers tokens to the specified address using grc20.Teller.Transfer.
98//
99// Parameters:
100// - cur: current realm (unused, reserved for future use)
101// - path: GRC20 token path
102// - to: recipient address
103// - amount: amount of tokens to transfer
104//
105// Returns an error if the transfer fails, nil otherwise.
106func Transfer(cur realm, path string, to address, amount int64) error {
107 return GetTokenTeller(path).Transfer(to, amount)
108}
109
110// TransferFrom transfers tokens from one address to another using grc20.Teller.TransferFrom.
111//
112// Parameters:
113// - cur: current realm (unused, reserved for future use)
114// - path: GRC20 token path
115// - from: sender address
116// - to: recipient address
117// - amount: amount of tokens to transfer
118//
119// Returns an error if the transfer fails, nil otherwise.
120func TransferFrom(cur realm, path string, from, to address, amount int64) error {
121 return GetTokenTeller(path).TransferFrom(from, to, amount)
122}
123
124// Approve approves tokens for the specified spender using grc20.Teller.Approve.
125//
126// Parameters:
127// - cur: current realm (unused, reserved for future use)
128// - path: GRC20 token path
129// - spender: address allowed to spend the tokens
130// - amount: amount of tokens to approve
131//
132// Returns an error if the approval fails, nil otherwise.
133func Approve(cur realm, path string, spender address, amount int64) error {
134 return GetTokenTeller(path).Approve(spender, amount)
135}
136
137// SafeGRC20Transfer performs a token transfer and panics if it fails.
138//
139// Parameters:
140// - cur: current realm (unused, reserved for future use)
141// - path: GRC20 token path
142// - to: recipient address
143// - amount: amount of tokens to transfer
144//
145// Panics if the transfer fails.
146func SafeGRC20Transfer(cur realm, path string, to address, amount int64) {
147 if err := GetTokenTeller(path).Transfer(to, amount); err != nil {
148 panic(err)
149 }
150}
151
152// SafeGRC20TransferFrom performs a token transfer from one address to another and panics if it fails.
153//
154// Parameters:
155// - cur: current realm (unused, reserved for future use)
156// - path: GRC20 token path
157// - from: sender address
158// - to: recipient address
159// - amount: amount of tokens to transfer
160//
161// Panics if the transfer fails.
162func SafeGRC20TransferFrom(cur realm, path string, from, to address, amount int64) {
163 if err := GetTokenTeller(path).TransferFrom(from, to, amount); err != nil {
164 panic(err)
165 }
166}
167
168// SafeGRC20Approve performs a token approval and panics if it fails.
169//
170// Parameters:
171// - cur: current realm (unused, reserved for future use)
172// - path: GRC20 token path
173// - spender: address allowed to spend the tokens
174// - amount: amount of tokens to approve
175//
176// Panics if the approval fails.
177func SafeGRC20Approve(cur realm, path string, spender address, amount int64) {
178 if err := GetTokenTeller(path).Approve(spender, amount); err != nil {
179 panic(err)
180 }
181}