store.gno
2.14 Kb · 90 lines
1package router
2
3import (
4 "errors"
5
6 "gno.land/p/gnoswap/store"
7 ufmt "gno.land/p/nt/ufmt/v0"
8)
9
10var errSpoofedRealm = errors.New("rlm does not match the current crossing frame")
11
12type StoreKey string
13
14func (s StoreKey) String() string {
15 return string(s)
16}
17
18const (
19 StoreKeySwapFee StoreKey = "swapFee" // Swap fee in basis points
20 StoreKeyPendingProtocolFees StoreKey = "pendingProtocolFees" // tokenPath -> amount held locally for protocol_fee
21)
22
23type routerStore struct {
24 kvStore store.KVStore
25}
26
27func (s *routerStore) HasSwapFeeKey() bool {
28 return s.kvStore.Has(StoreKeySwapFee.String())
29}
30
31// GetSwapFee retrieves the current swap fee.
32// If not set, returns the default swap fee.
33func (s *routerStore) GetSwapFee() uint64 {
34 result, err := s.kvStore.Get(StoreKeySwapFee.String())
35 if err != nil {
36 panic(err)
37 }
38
39 swapFee, ok := result.(uint64)
40 if !ok {
41 panic(ufmt.Sprintf("failed to cast result to uint64: %T", result))
42 }
43
44 return swapFee
45}
46
47// SetSwapFee stores the swap fee.
48func (s *routerStore) SetSwapFee(_ int, rlm realm, fee uint64) error {
49 if !rlm.IsCurrent() {
50 return errSpoofedRealm
51 }
52
53 return s.kvStore.Set(0, rlm, StoreKeySwapFee.String(), fee)
54}
55
56func (s *routerStore) HasPendingProtocolFeesKey() bool {
57 return s.kvStore.Has(StoreKeyPendingProtocolFees.String())
58}
59
60func (s *routerStore) GetPendingProtocolFees() map[string]int64 {
61 result, err := s.kvStore.Get(StoreKeyPendingProtocolFees.String())
62 if err != nil {
63 panic(err)
64 }
65
66 pendingProtocolFees, ok := result.(map[string]int64)
67 if !ok {
68 panic(ufmt.Sprintf("failed to cast result to map[string]int64: %T", result))
69 }
70
71 return pendingProtocolFees
72}
73
74func (s *routerStore) SetPendingProtocolFees(_ int, rlm realm, fees map[string]int64) error {
75 if !rlm.IsCurrent() {
76 return errSpoofedRealm
77 }
78
79 return s.kvStore.Set(0, rlm, StoreKeyPendingProtocolFees.String(), fees)
80}
81
82// NewRouterStore creates a new router store instance with the provided KV store.
83// This function is used by the upgrade system to create storage instances for each implementation.
84func NewRouterStore(kvStore store.KVStore) IRouterStore {
85 rs := &routerStore{
86 kvStore: kvStore,
87 }
88
89 return rs
90}