Skip to content

Commit 779fb46

Browse files
authored
Merge branch 'main' into refactor-launchpad
2 parents 953f6a3 + e138837 commit 779fb46

File tree

100 files changed

+5899
-3377
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+5899
-3377
lines changed

_deploy/r/gnoswap/common/grc20reg_helper.gno

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package common
22

33
import (
4+
"regexp"
45
"std"
6+
"strings"
57

68
"gno.land/p/demo/grc/grc20"
79
"gno.land/p/demo/ufmt"
810
"gno.land/r/demo/grc20reg"
911
)
1012

13+
var (
14+
re = regexp.MustCompile(`\[gno\.land/r/[^\]]+\]`)
15+
)
16+
1117
// GetToken returns a grc20.Token instance
1218
// if token is not registered, it will panic
1319
// token instance supports following methods:
@@ -58,6 +64,26 @@ func MustRegistered(path string) {
5864
}
5965
}
6066

67+
// ListRegisteredTokens returns the list of registered tokens
68+
// NOTE:
69+
// - Unfortunate, grc20reg doesn't support this.
70+
// - We need to parse the rendered grc20reg page to get the list of registered tokens.
71+
func ListRegisteredTokens() []string {
72+
render := grc20reg.Render("")
73+
return extractTokenPathsFromRender(render)
74+
}
75+
76+
func extractTokenPathsFromRender(render string) []string {
77+
matches := re.FindAllString(render, -1)
78+
79+
tokenPaths := make([]string, 0, len(matches))
80+
for _, match := range matches {
81+
tokenPath := strings.Trim(match, "[]") // Remove the brackets
82+
tokenPaths = append(tokenPaths, tokenPath)
83+
}
84+
return tokenPaths
85+
}
86+
6187
// TotalSupply returns the total supply of the token
6288
func TotalSupply(path string) uint64 {
6389
return GetToken(path).TotalSupply()

_deploy/r/gnoswap/common/grc20reg_helper_test.gno

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,74 @@ func TestMustRegistered(t *testing.T) {
153153
})
154154
}
155155

156+
func TestListRegisteredTokens(t *testing.T) {
157+
t.Skip("skipping tests -> can not mock grc20reg.Render() || testing extractTokenPathsFromRender() does cover this")
158+
}
159+
160+
func TestExtractTokenPathsFromRender(t *testing.T) {
161+
var (
162+
wugnotPath = "gno.land/r/demo/wugnot"
163+
gnsPath = "gno.land/r/gnoswap/v1/gns"
164+
fooPath = "gno.land/r/onbloc/foo"
165+
quxPath = "gno.land/r/onbloc/qux"
166+
)
167+
168+
// NOTE: following strings are return from grc20reg.Render()
169+
renderList := []string{
170+
// no registered token
171+
`No registered token.`,
172+
173+
// 1 token
174+
`- **wrapped GNOT** - [gno.land/r/demo/wugnot](/r/demo/wugnot) - [info](/r/demo/grc20reg:gno.land/r/demo/wugnot)`,
175+
176+
// 2 tokens
177+
`- **wrapped GNOT** - [gno.land/r/demo/wugnot](/r/demo/wugnot) - [info](/r/demo/grc20reg:gno.land/r/demo/wugnot)
178+
- **Gnoswap** - [gno.land/r/gnoswap/v1/gns](/r/gnoswap/v1/gns) - [info](/r/demo/grc20reg:gno.land/r/gnoswap/v1/gns)
179+
`,
180+
181+
// 4 tokens
182+
`- **wrapped GNOT** - [gno.land/r/demo/wugnot](/r/demo/wugnot) - [info](/r/demo/grc20reg:gno.land/r/demo/wugnot)
183+
- **Gnoswap** - [gno.land/r/gnoswap/v1/gns](/r/gnoswap/v1/gns) - [info](/r/demo/grc20reg:gno.land/r/gnoswap/v1/gns)
184+
- **Baz** - [gno.land/r/onbloc/foo](/r/onbloc/foo) - [info](/r/demo/grc20reg:gno.land/r/onbloc/foo)
185+
- **Qux** - [gno.land/r/onbloc/qux](/r/onbloc/qux) - [info](/r/demo/grc20reg:gno.land/r/onbloc/qux)
186+
`,
187+
}
188+
189+
tests := []struct {
190+
name string
191+
render string
192+
expected []string
193+
}{
194+
{
195+
name: "no registered token",
196+
render: renderList[0],
197+
expected: []string{},
198+
},
199+
{
200+
name: "1 registered token",
201+
render: renderList[1],
202+
expected: []string{wugnotPath},
203+
},
204+
{
205+
name: "2 registered tokens",
206+
render: renderList[2],
207+
expected: []string{wugnotPath, gnsPath},
208+
},
209+
{
210+
name: "4 registered tokens",
211+
render: renderList[3],
212+
expected: []string{wugnotPath, gnsPath, fooPath, quxPath},
213+
},
214+
}
215+
216+
for _, tt := range tests {
217+
t.Run(tt.name, func(t *testing.T) {
218+
extracted := extractTokenPathsFromRender(tt.render)
219+
uassert.True(t, areSlicesEqual(t, tt.expected, extracted))
220+
})
221+
}
222+
}
223+
156224
func TestTotalSupply(t *testing.T) {
157225
// result from grc2reg and (direct import/call) should be the same
158226
uassert.Equal(t, foo20.TotalSupply(), TotalSupply(tokenPath))
@@ -172,3 +240,22 @@ func TestAllowance(t *testing.T) {
172240
// result from grc2reg and (direct import/call) should be the same
173241
uassert.Equal(t, foo20.Allowance(AddrToUser(owner), AddrToUser(spender)), Allowance(tokenPath, owner, spender))
174242
}
243+
244+
// areSlicesEqual compares two slices of strings
245+
func areSlicesEqual(t *testing.T, a, b []string) bool {
246+
t.Helper()
247+
248+
// Check if lengths are different
249+
if len(a) != len(b) {
250+
return false
251+
}
252+
253+
// Compare each element
254+
for i := range a {
255+
if a[i] != b[i] {
256+
return false
257+
}
258+
}
259+
260+
return true
261+
}

_deploy/r/gnoswap/consts/consts.gno

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const (
1212

1313
TOKEN_REGISTER_NAMESPACE string = "gno.land/r/g1er355fkjksqpdtwmhf5penwa82p0rhqxkkyhk5"
1414

15-
BLOCK_GENERATION_INTERVAL int64 = 1 // seconds
15+
BLOCK_GENERATION_INTERVAL int64 = 2 // seconds
1616
)
1717

1818
// WRAP & UNWRAP

_deploy/r/gnoswap/gnft/errors.gno

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import (
77
)
88

99
var (
10-
errNoPermission = errors.New("[GNOSWAP-GNFT-001] caller has no permission")
10+
errNoPermission = errors.New("[GNOSWAP-GNFT-001] caller has no permission")
11+
errCannotSetURI = errors.New("[GNOSWAP-GNFT-002] cannot set URI")
12+
errNoTokenForCaller = errors.New("[GNOSWAP-GNFT-003] no token for caller")
13+
errInvalidAddress = errors.New("[GNOSWAP-GNFT-004] invalid addresss")
1114
)
1215

1316
func addDetailToError(err error, detail string) string {

0 commit comments

Comments
 (0)