Search Apps Documentation Source Content File Folder Download Copy Actions Download

assert.gno

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