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