Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

assert.gno

1.96 Kb · 67 lines
 1package gnft
 2
 3import (
 4	prabc "gno.land/p/gnoswap/rbac/v1"
 5	"gno.land/p/nt/grc721/v0"
 6	"gno.land/p/nt/ufmt/v0"
 7	"gno.land/r/gnoswap/access/v1"
 8)
 9
10// assertIsValidTokenURI panics if the token already has a URI set.
11func assertIsValidTokenURI(tid grc721.TokenID) {
12	uri, _ := meta.TokenURI(tid)
13	if uri != "" {
14		panic(makeErrorWithDetails(errCannotSetURI, ufmt.Sprintf("token id (%s) has already set URI", string(tid))))
15	}
16}
17
18// assertIsNonEmptyTokenURI panics if the new URI is empty.
19func assertIsNonEmptyTokenURI(tURI string) {
20	if tURI == "" {
21		panic(makeErrorWithDetails(errCannotSetURI, "empty token URI"))
22	}
23}
24
25// assertIsValidAddress panics if the address is invalid.
26func assertIsValidAddress(addr address) {
27	if !addr.IsValid() {
28		panic(makeErrorWithDetails(errInvalidAddress, ufmt.Sprintf("address (%s)", addr.String())))
29	}
30}
31
32// assertFromIsValidAddress panics if the from address is invalid.
33func assertFromIsValidAddress(from address) {
34	if !from.IsValid() {
35		panic(makeErrorWithDetails(errInvalidAddress, ufmt.Sprintf("from address (%s)", from.String())))
36	}
37}
38
39// assertToIsValidAddress panics if the to address is invalid.
40func assertToIsValidAddress(to address) {
41	if !to.IsValid() {
42		panic(makeErrorWithDetails(errInvalidAddress, ufmt.Sprintf("to address (%s)", to.String())))
43	}
44}
45
46// assertIsAllowedTransfer enforces that NFTs held
47// by the staker (i.e. currently staked) can only be moved by the staker itself.
48// Other tokens fall through to the standard GRC721 owner/approval checks performed inside ledger.
49func assertIsAllowedTransfer(caller address, tid grc721.TokenID) {
50	owner, err := token.OwnerOf(tid)
51	if err != nil {
52		// Surface the same not-found error as the standard transfer path.
53		checkErr(err)
54	}
55
56	stakerAddr := access.MustGetAddress(prabc.ROLE_STAKER.String())
57	if owner != stakerAddr {
58		return
59	}
60
61	if caller != stakerAddr {
62		panic(makeErrorWithDetails(
63			errStakedTokenLocked,
64			ufmt.Sprintf("token (%s) is held by staker", string(tid)),
65		))
66	}
67}