Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

store.gno

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