instance.gno
5.72 Kb · 168 lines
1package position
2
3import (
4 "errors"
5
6 "gno.land/p/nt/grc721/v0"
7 "gno.land/r/gnoswap/access/v1"
8 "gno.land/r/gnoswap/gnft"
9 "gno.land/r/gnoswap/position"
10)
11
12type positionV1 struct {
13 store position.IPositionStore
14 nftAccessor NFTAccessor
15}
16
17// NewPositionV1 constructs a position implementation backed by the supplied
18// position store and NFT accessor.
19//
20// Parameters:
21// - positionStore: storage interface used to read and update position records
22// - accessor: NFT interface used for position token minting, burning, approval, and ownership
23//
24// Returns:
25// - position: IPosition implementation connected to positionStore and accessor
26func NewPositionV1(positionStore position.IPositionStore, accessor NFTAccessor) position.IPosition {
27 return &positionV1{
28 store: positionStore,
29 nftAccessor: accessor,
30 }
31}
32
33type NFTAccessor interface {
34 // Approve forwards an operator approval for a position NFT.
35 //
36 // Parameters:
37 // - _: leading realm-call discriminator for the forwarded NFT operation; callers pass 0
38 // - rlm: propagated realm context validated by the concrete accessor before crossing into the NFT realm
39 // - approved: address to approve for the specified position NFT; the empty address revokes approval
40 // - tid: position NFT token ID whose operator approval is changed
41 //
42 // Returns:
43 // - err: nil when approval succeeds; otherwise the NFT accessor or realm-validation error
44 Approve(_ int, rlm realm, approved address, tid grc721.TokenID) error
45 // Mint creates a position NFT for an address and token ID.
46 //
47 // Parameters:
48 // - _: leading realm-call discriminator for the forwarded NFT operation; callers pass 0
49 // - rlm: propagated realm context used to cross into the NFT realm
50 // - to: address that receives the newly minted position NFT
51 // - tid: requested position NFT token ID
52 //
53 // Returns:
54 // - mintedTid: token ID returned by the NFT realm after minting
55 Mint(_ int, rlm realm, to address, tid grc721.TokenID) grc721.TokenID
56 // Burn removes a position NFT from the NFT ledger.
57 //
58 // Parameters:
59 // - _: leading realm-call discriminator for the forwarded NFT operation; callers pass 0
60 // - rlm: propagated realm context used to cross into the NFT realm
61 // - tid: position NFT token ID to burn
62 Burn(_ int, rlm realm, tid grc721.TokenID)
63 // TotalSupply returns the number of NFTs currently recorded by the ledger.
64 //
65 // Returns:
66 // - totalSupply: current number of position NFTs in the ledger
67 TotalSupply() int64
68 // Exists reports whether the NFT ledger contains a token ID.
69 //
70 // Parameters:
71 // - tid: position NFT token ID to test
72 //
73 // Returns:
74 // - exists: true when the NFT ledger contains tid
75 Exists(tid grc721.TokenID) bool
76 // OwnerOf returns the current owner of a position NFT.
77 //
78 // Parameters:
79 // - tid: position NFT token ID whose owner is requested
80 //
81 // Returns:
82 // - owner: address recorded as the token owner
83 // - err: nil when the token exists; otherwise the NFT ledger's ownership lookup error
84 OwnerOf(tid grc721.TokenID) (address, error)
85}
86
87type gnftAccessor struct{}
88
89// Approve forwards an operator approval to the GNFT realm.
90//
91// Parameters:
92// - _: leading realm-call discriminator for the forwarded NFT operation; callers pass 0
93// - rlm: propagated realm context that must be current before crossing into GNFT
94// - approved: address to approve for the specified position NFT; the empty address revokes approval
95// - tid: position NFT token ID whose operator approval is changed
96//
97// Returns:
98// - err: nil when GNFT accepts the approval; errSpoofedRealm when rlm is not current, otherwise the underlying GNFT error
99func (n *gnftAccessor) Approve(_ int, rlm realm, approved address, tid grc721.TokenID) error {
100 if !rlm.IsCurrent() {
101 return errors.New(errSpoofedRealm)
102 }
103
104 return gnft.Approve(cross(rlm), approved, tid)
105}
106
107// Mint forwards position NFT creation to the GNFT realm.
108//
109// Parameters:
110// - _: leading realm-call discriminator for the forwarded NFT operation; callers pass 0
111// - rlm: propagated realm context asserted current before crossing into GNFT
112// - to: address that receives the newly minted position NFT
113// - tid: requested position NFT token ID
114//
115// Returns:
116// - mintedTid: token ID returned by GNFT after minting
117func (n *gnftAccessor) Mint(_ int, rlm realm, to address, tid grc721.TokenID) grc721.TokenID {
118 access.AssertIsRlmCurrent(0, rlm)
119
120 return gnft.Mint(cross(rlm), to, tid)
121}
122
123// Burn forwards position NFT destruction to the GNFT realm.
124//
125// Parameters:
126// - _: leading realm-call discriminator for the forwarded NFT operation; callers pass 0
127// - rlm: propagated realm context asserted current before crossing into GNFT
128// - tid: position NFT token ID to burn
129func (n *gnftAccessor) Burn(_ int, rlm realm, tid grc721.TokenID) {
130 access.AssertIsRlmCurrent(0, rlm)
131
132 gnft.Burn(cross(rlm), tid)
133}
134
135// TotalSupply returns the GNFT ledger's current token count.
136//
137// Returns:
138// - totalSupply: number of NFTs currently recorded by GNFT
139func (n *gnftAccessor) TotalSupply() int64 {
140 return gnft.TotalSupply()
141}
142
143// Exists reports whether GNFT contains a position NFT token ID.
144//
145// Parameters:
146// - tid: position NFT token ID to test
147//
148// Returns:
149// - exists: true when GNFT contains tid
150func (n *gnftAccessor) Exists(tid grc721.TokenID) bool {
151 return gnft.Exists(tid)
152}
153
154// OwnerOf returns the owner recorded by GNFT for a position NFT.
155//
156// Parameters:
157// - tid: position NFT token ID whose owner is requested
158//
159// Returns:
160// - owner: address recorded as the token owner
161// - err: nil when GNFT finds the token; otherwise GNFT's ownership lookup error
162func (n *gnftAccessor) OwnerOf(tid grc721.TokenID) (address, error) {
163 return gnft.OwnerOf(tid)
164}
165
166func newGNFTAccessor() NFTAccessor {
167 return &gnftAccessor{}
168}