grc20reg_helper.gno
6.87 Kb · 171 lines
1package common
2
3// IsRegistered checks whether a token key is registered.
4//
5// Parameters:
6// - tokenKey: registry key identifying the GRC20 token to inspect.
7//
8// Returns:
9// - error: nil when tokenKey is valid and registered; non-nil when the key is invalid or unregistered.
10func IsRegistered(tokenKey string) error {
11 return getImplementation().IsRegistered(tokenKey)
12}
13
14// MustRegistered checks whether all provided tokens are registered and panics if any is not registered.
15//
16// Parameters:
17// - tokenKeys: token registry keys to check; an empty list performs no checks.
18//
19// Panics if any of the provided tokens is not registered.
20func MustRegistered(tokenKeys ...string) {
21 if err := ValidateRegistered(tokenKeys...); err != nil {
22 panic(err.Error())
23 }
24}
25
26// ValidateRegistered checks whether all provided tokens are registered, returning an
27// error instead of panicking when a token is not registered.
28//
29// Parameters:
30// - tokenKeys: token registry keys to check; an empty list is valid.
31//
32// Returns:
33// - error: nil when every token is registered; otherwise the first
34// registration failure.
35func ValidateRegistered(tokenKeys ...string) error {
36 return getImplementation().ValidateRegistered(tokenKeys...)
37}
38
39// TotalSupply returns the total supply of the specified token.
40//
41// Parameters:
42// - tokenKey: registry key identifying the GRC20 token.
43//
44// Returns:
45// - totalSupply: current token supply for tokenKey, in the token's base units; panics if tokenKey is invalid or unregistered.
46func TotalSupply(tokenKey string) int64 {
47 return getImplementation().TotalSupply(tokenKey)
48}
49
50// BalanceOf returns the token balance for the specified address.
51//
52// Parameters:
53// - tokenKey: registry key identifying the GRC20 token.
54// - addr: address whose token balance is queried.
55//
56// Returns:
57// - balance: token balance held by addr, in the token's base units; panics if tokenKey is invalid or unregistered.
58func BalanceOf(tokenKey string, addr address) int64 {
59 return getImplementation().BalanceOf(tokenKey, addr)
60}
61
62// Allowance returns the token allowance from owner to spender.
63//
64// Parameters:
65// - tokenKey: registry key identifying the GRC20 token.
66// - owner: address that owns the tokens.
67// - spender: address authorized to spend owner's tokens.
68//
69// Returns:
70// - allowance: amount owner has approved spender to use, in the token's base units; panics if tokenKey is invalid or unregistered.
71func Allowance(tokenKey string, owner, spender address) int64 {
72 return getImplementation().Allowance(tokenKey, owner, spender)
73}
74
75// Transfer forwards a token transfer to the implementation bound to the current realm.
76//
77// Parameters:
78// - _: leading integer discriminator for the realm-crossing ABI; pass 0.
79// - rlm: propagated realm context used to resolve and invoke the registered token implementation.
80// - tokenKey: registry key identifying the GRC20 token to transfer.
81// - to: recipient address.
82// - amount: number of token base units to transfer.
83//
84// Returns:
85// - error: nil when the implementation completes the transfer; otherwise a token
86// validation or transfer-rule error.
87func Transfer(_ int, rlm realm, tokenKey string, to address, amount int64) error {
88 return getImplementation().Transfer(0, rlm, tokenKey, to, amount)
89}
90
91// TransferFrom forwards an allowance-backed token transfer to the implementation bound to the current realm.
92//
93// Parameters:
94// - _: leading integer discriminator for the realm-crossing ABI; pass 0.
95// - rlm: propagated realm context used to resolve and invoke the registered token implementation.
96// - tokenKey: registry key identifying the GRC20 token to transfer.
97// - from: address whose allowance and balance are debited.
98// - to: recipient address.
99// - amount: number of token base units to transfer.
100//
101// Returns:
102// - error: nil when the implementation completes the transfer; otherwise a token
103// validation, allowance, or balance-rule error.
104func TransferFrom(_ int, rlm realm, tokenKey string, from, to address, amount int64) error {
105 return getImplementation().TransferFrom(0, rlm, tokenKey, from, to, amount)
106}
107
108// Approve forwards an allowance update to the implementation bound to the current realm.
109//
110// Parameters:
111// - _: leading integer discriminator for the realm-crossing ABI; pass 0.
112// - rlm: propagated realm context used to resolve and invoke the registered token implementation.
113// - tokenKey: registry key identifying the GRC20 token.
114// - spender: address authorized to spend the caller's tokens.
115// - amount: number of token base units to authorize.
116//
117// Returns:
118// - error: nil when the implementation records the allowance; otherwise a token
119// validation or approval-rule error.
120func Approve(_ int, rlm realm, tokenKey string, spender address, amount int64) error {
121 return getImplementation().Approve(0, rlm, tokenKey, spender, amount)
122}
123
124// SafeGRC20Transfer transfers tokens as the current realm and panics if it fails.
125//
126// Parameters:
127// - _: leading integer discriminator for the realm-crossing ABI; pass 0.
128// - rlm: propagated realm context used to resolve and invoke the registered token implementation.
129// - tokenKey: registry key identifying the GRC20 token to transfer.
130// - to: recipient address.
131// - amount: number of token base units to transfer.
132//
133// Panics if the underlying transfer returns an error.
134func SafeGRC20Transfer(_ int, rlm realm, tokenKey string, to address, amount int64) {
135 if err := Transfer(0, rlm, tokenKey, to, amount); err != nil {
136 panic(err)
137 }
138}
139
140// SafeGRC20TransferFrom transfers tokens as the current realm and panics if it fails.
141//
142// Parameters:
143// - _: leading integer discriminator for the realm-crossing ABI; pass 0.
144// - rlm: propagated realm context used to resolve and invoke the registered token implementation.
145// - tokenKey: registry key identifying the GRC20 token to transfer.
146// - from: address whose allowance and balance are debited.
147// - to: recipient address.
148// - amount: number of token base units to transfer.
149//
150// Panics if the underlying transfer returns an error.
151func SafeGRC20TransferFrom(_ int, rlm realm, tokenKey string, from, to address, amount int64) {
152 if err := TransferFrom(0, rlm, tokenKey, from, to, amount); err != nil {
153 panic(err)
154 }
155}
156
157// SafeGRC20Approve approves tokens as the current realm and panics if it fails.
158//
159// Parameters:
160// - _: leading integer discriminator for the realm-crossing ABI; pass 0.
161// - rlm: propagated realm context used to resolve and invoke the registered token implementation.
162// - tokenKey: registry key identifying the GRC20 token.
163// - spender: address authorized to spend the caller's tokens.
164// - amount: number of token base units to authorize.
165//
166// Panics if the underlying approval returns an error.
167func SafeGRC20Approve(_ int, rlm realm, tokenKey string, spender address, amount int64) {
168 if err := Approve(0, rlm, tokenKey, spender, amount); err != nil {
169 panic(err)
170 }
171}