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