Search Apps Documentation Source Content File Folder Download Copy Actions Download

external_token_list.gno

2.21 Kb · 99 lines
 1package v1
 2
 3import (
 4	"chain"
 5
 6	ufmt "gno.land/p/nt/ufmt/v0"
 7	"gno.land/r/gnoswap/access"
 8	"gno.land/r/gnoswap/halt"
 9)
10
11var defaultAllowed = []string{WUGNOT_PATH, GNS_PATH}
12
13// AddToken adds a new token path to the list of allowed tokens
14// Only the admin can add a new token.
15//
16// Parameters:
17//   - tokenPath (string): The path of the token to add
18//
19// Panics:
20//   - If the caller is not the admin
21func (s *stakerV1) AddToken(_ int, rlm realm, tokenPath string) {
22	if !rlm.IsCurrent() {
23		panic(errSpoofedRealm)
24	}
25
26	halt.AssertIsNotHaltedStaker()
27
28	previousRealm := rlm.Previous()
29	caller := previousRealm.Address()
30	access.AssertIsAdminOrGovernance(caller)
31
32	if err := checkCannotAddDefaultToken(tokenPath); err != nil {
33		panic(err.Error())
34	}
35
36	if err := s.store.AddAllowedToken(0, rlm, tokenPath); err != nil {
37		panic(err.Error())
38	}
39
40	chain.Emit(
41		"AddToken",
42		"prevAddr", caller.String(),
43		"prevRealm", previousRealm.PkgPath(),
44		"tokenPath", tokenPath,
45	)
46}
47
48// RemoveToken removes a token path from the list of allowed tokens.
49// Only the admin  can remove a token.
50//
51// Default tokens cannot be removed.
52//
53// Parameters:
54//   - tokenPath (string): The path of the token to remove
55//
56// Panics:
57//   - If the caller is not the admin
58func (s *stakerV1) RemoveToken(_ int, rlm realm, tokenPath string) {
59	if !rlm.IsCurrent() {
60		panic(errSpoofedRealm)
61	}
62
63	halt.AssertIsNotHaltedStaker()
64
65	previousRealm := rlm.Previous()
66	caller := previousRealm.Address()
67	access.AssertIsAdminOrGovernance(caller)
68
69	if err := checkCannotRemoveDefaultToken(tokenPath); err != nil {
70		panic(err.Error())
71	}
72
73	if err := s.store.RemoveAllowedToken(0, rlm, tokenPath); err != nil {
74		panic(err.Error())
75	}
76
77	chain.Emit(
78		"RemoveToken",
79		"prevAddr", caller.String(),
80		"prevRealm", previousRealm.PkgPath(),
81		"tokenPath", tokenPath,
82	)
83}
84
85func checkCannotAddDefaultToken(tokenPath string) error {
86	if contains(defaultAllowed, tokenPath) {
87		return ufmt.Errorf("%v: cannot add existing token(%s)", errAddExistingToken, tokenPath)
88	}
89
90	return nil
91}
92
93func checkCannotRemoveDefaultToken(tokenPath string) error {
94	if contains(defaultAllowed, tokenPath) {
95		return ufmt.Errorf("%v: cannot remove default token(%s)", errDefaultExternalToken, tokenPath)
96	}
97
98	return nil
99}