accessor.gno
5.42 Kb · 178 lines
1package staker
2
3import (
4 "errors"
5 "gno.land/p/gnoswap/deps/tokens/grc721"
6 "gno.land/r/gnoswap/emission"
7 "gno.land/r/gnoswap/gnft"
8 "gno.land/r/gnoswap/pool"
9)
10
11type PoolAccessor interface {
12 ExistsPoolPath(poolPath string) bool
13 GetSlot0Tick(poolPath string) int32
14 GetSlot0SqrtPriceX96(poolPath string) string
15
16 SetTickCrossHook(_ int, rlm realm, hook func(_ int, rlm realm, poolPath string, tickId int32, zeroForOne bool, timestamp int64))
17 SetSwapStartHook(_ int, rlm realm, hook func(_ int, rlm realm, poolPath string, timestamp int64))
18 SetSwapEndHook(_ int, rlm realm, hook func(_ int, rlm realm, poolPath string) error)
19}
20
21type poolAccessor struct{}
22
23func (p *poolAccessor) ExistsPoolPath(poolPath string) bool {
24 return pool.ExistsPoolPath(poolPath)
25}
26
27func (p *poolAccessor) GetSlot0Tick(poolPath string) int32 {
28 return pool.GetSlot0Tick(poolPath)
29}
30
31func (p *poolAccessor) GetSlot0SqrtPriceX96(poolPath string) string {
32 return pool.GetSlot0SqrtPriceX96(poolPath)
33}
34
35func (p *poolAccessor) SetTickCrossHook(_ int, rlm realm, hook func(_ int, rlm realm, poolPath string, tickId int32, zeroForOne bool, timestamp int64)) {
36 if !rlm.IsCurrent() {
37 panic(errors.New(ErrSpoofedRealm))
38 }
39
40 pool.SetTickCrossHook(cross(rlm), func(cur realm, poolPath string, tickId int32, zeroForOne bool, timestamp int64) {
41 hook(0, cur, poolPath, tickId, zeroForOne, timestamp)
42 })
43}
44
45func (p *poolAccessor) SetSwapStartHook(_ int, rlm realm, hook func(_ int, rlm realm, poolPath string, timestamp int64)) {
46 if !rlm.IsCurrent() {
47 panic(errors.New(ErrSpoofedRealm))
48 }
49
50 pool.SetSwapStartHook(cross(rlm), func(cur realm, poolPath string, timestamp int64) {
51 hook(0, cur, poolPath, timestamp)
52 })
53}
54
55func (p *poolAccessor) SetSwapEndHook(_ int, rlm realm, hook func(_ int, rlm realm, poolPath string) error) {
56 if !rlm.IsCurrent() {
57 panic(errors.New(ErrSpoofedRealm))
58 }
59
60 pool.SetSwapEndHook(cross(rlm), func(cur realm, poolPath string) error {
61 return hook(0, cur, poolPath)
62 })
63}
64
65func newPoolAccessor() PoolAccessor {
66 return &poolAccessor{}
67}
68
69type EmissionAccessor interface {
70 MintAndDistributeGns(_ int, rlm realm) (int64, bool)
71 GetStakerEmissionAmountPerSecond() int64
72 GetStakerEmissionAmountPerSecondInRange(start, end int64) ([]int64, []int64)
73 SetOnDistributionPctChangeCallback(_ int, rlm realm, callback func(_ int, rlm realm, emissionAmountPerSecond int64))
74}
75
76type emissionAccessor struct{}
77
78func (e *emissionAccessor) MintAndDistributeGns(_ int, rlm realm) (int64, bool) {
79 if !rlm.IsCurrent() {
80 panic(errors.New(ErrSpoofedRealm))
81 }
82
83 return emission.MintAndDistributeGns(cross(rlm))
84}
85
86func (e *emissionAccessor) GetStakerEmissionAmountPerSecond() int64 {
87 return emission.GetStakerEmissionAmountPerSecond()
88}
89
90func (e *emissionAccessor) GetStakerEmissionAmountPerSecondInRange(start, end int64) ([]int64, []int64) {
91 return emission.GetStakerEmissionAmountPerSecondInRange(start, end)
92}
93
94func (e *emissionAccessor) SetOnDistributionPctChangeCallback(_ int, rlm realm, callback func(_ int, rlm realm, emissionAmountPerSecond int64)) {
95 if !rlm.IsCurrent() {
96 panic(errors.New(ErrSpoofedRealm))
97 }
98
99 // Wrap the caller-provided callback in an adapter constructed HERE, inside
100 // the /r/gnoswap/staker domain package. By borrow rule #3 the wrapper
101 // closure is owned by /r/gnoswap/staker (its construction realm), not by the
102 // v1 implementation that passed `callback` in. This mirrors the swap/tick
103 // hook accessors above and lets emission persist the callback into its
104 // package-level var without hitting "cannot persist realm value" (which
105 // fired when a v1-constructed closure was stored there directly).
106 emission.SetOnDistributionPctChangeCallback(cross(rlm), func(cur realm, emissionAmountPerSecond int64) {
107 callback(0, cur, emissionAmountPerSecond)
108 })
109}
110
111func newEmissionAccessor() EmissionAccessor {
112 return &emissionAccessor{}
113}
114
115type NFTAccessor interface {
116 Approve(_ int, rlm realm, approved address, tid grc721.TokenID) error
117 Mint(_ int, rlm realm, to address, tid grc721.TokenID) grc721.TokenID
118 Burn(_ int, rlm realm, tid grc721.TokenID)
119 TransferFrom(_ int, rlm realm, from, to address, tid grc721.TokenID) error
120 TotalSupply() int64
121 Exists(tid grc721.TokenID) bool
122 MustOwnerOf(tid grc721.TokenID) address
123 OwnerOf(tid grc721.TokenID) (address, error)
124}
125
126type gnftAccessor struct{}
127
128func (n *gnftAccessor) Approve(_ int, rlm realm, approved address, tid grc721.TokenID) error {
129 if !rlm.IsCurrent() {
130 return errors.New(ErrSpoofedRealm)
131 }
132
133 return gnft.Approve(cross(rlm), approved, tid)
134}
135
136func (n *gnftAccessor) Mint(_ int, rlm realm, to address, tid grc721.TokenID) grc721.TokenID {
137 if !rlm.IsCurrent() {
138 panic(errors.New(ErrSpoofedRealm))
139 }
140
141 return gnft.Mint(cross(rlm), to, tid)
142}
143
144func (n *gnftAccessor) Burn(_ int, rlm realm, tid grc721.TokenID) {
145 if !rlm.IsCurrent() {
146 panic(errors.New(ErrSpoofedRealm))
147 }
148
149 gnft.Burn(cross(rlm), tid)
150}
151
152func (n *gnftAccessor) TransferFrom(_ int, rlm realm, from, to address, tid grc721.TokenID) error {
153 if !rlm.IsCurrent() {
154 return errors.New(ErrSpoofedRealm)
155 }
156
157 return gnft.TransferFrom(cross(rlm), from, to, tid)
158}
159
160func (n *gnftAccessor) TotalSupply() int64 {
161 return gnft.TotalSupply()
162}
163
164func (n *gnftAccessor) Exists(tid grc721.TokenID) bool {
165 return gnft.Exists(tid)
166}
167
168func (n *gnftAccessor) MustOwnerOf(tid grc721.TokenID) address {
169 return gnft.MustOwnerOf(tid)
170}
171
172func (n *gnftAccessor) OwnerOf(tid grc721.TokenID) (address, error) {
173 return gnft.OwnerOf(tid)
174}
175
176func newNFTAccessor() NFTAccessor {
177 return &gnftAccessor{}
178}