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