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

init.gno

2.96 Kb · 126 lines
  1package pool
  2
  3import (
  4	"errors"
  5	"gno.land/r/gnoswap/pool"
  6)
  7
  8const (
  9	// slot0FeeProtocol represents the protocol fee percentage (0-10).
 10	// This parameter can be modified through governance.
 11	defaultSlot0FeeProtocol = uint8(0)
 12
 13	// poolCreationFee is the fee that is charged when a user creates a pool.
 14	// The fee is denominated in GNS tokens.
 15	// This parameter can be modified through governance.
 16	defaultPoolCreationFee = int64(100_000_000) // 100_GNS
 17
 18	// withdrawalFeeBPS is the fee that is charged when a user withdraws their collected fees
 19	// The fee is denominated in BPS (Basis Points)
 20	// Example: 100 BPS = 1%
 21	// This parameter can be modified through governance.
 22	defaultWithdrawalFeeBPS = uint64(100)
 23
 24	defaultUnlocked = true
 25)
 26
 27func init(cur realm) {
 28	registerPoolV1(cur)
 29}
 30
 31func registerPoolV1(cur realm) {
 32	pool.RegisterInitializer(cross(cur), func(_ int, rlm realm, poolStore pool.IPoolStore) pool.IPool {
 33		if !rlm.IsCurrent() {
 34			panic(errors.New(errSpoofedRealm))
 35		}
 36
 37		err := initStoreData(0, rlm, poolStore)
 38		if err != nil {
 39			panic(err)
 40		}
 41
 42		return NewPoolV1(poolStore)
 43	})
 44}
 45
 46func initStoreData(_ int, rlm realm, poolStore pool.IPoolStore) error {
 47	if !poolStore.HasPools() {
 48		err := poolStore.SetPools(0, rlm, pool.NewPoolsTree())
 49		if err != nil {
 50			return err
 51		}
 52	}
 53
 54	if !poolStore.HasFeeAmountTickSpacing() {
 55		err := poolStore.SetFeeAmountTickSpacing(0, rlm, pool.NewDefaultFeeAmountTickSpacing())
 56		if err != nil {
 57			return err
 58		}
 59	}
 60
 61	if !poolStore.HasPoolCreationFee() {
 62		err := poolStore.SetPoolCreationFee(0, rlm, defaultPoolCreationFee)
 63		if err != nil {
 64			return err
 65		}
 66	}
 67
 68	if !poolStore.HasPendingProtocolFees() {
 69		err := poolStore.SetPendingProtocolFees(0, rlm, make(map[string]int64))
 70		if err != nil {
 71			return err
 72		}
 73	}
 74
 75	if !poolStore.HasSlot0FeeProtocol() {
 76		err := poolStore.SetSlot0FeeProtocol(0, rlm, defaultSlot0FeeProtocol)
 77		if err != nil {
 78			return err
 79		}
 80	}
 81
 82	if !poolStore.HasWithdrawalFeeBPS() {
 83		err := poolStore.SetWithdrawalFeeBPS(0, rlm, defaultWithdrawalFeeBPS)
 84		if err != nil {
 85			return err
 86		}
 87	}
 88
 89	if !poolStore.HasUnlocked() {
 90		err := poolStore.SetUnlocked(0, rlm, defaultUnlocked)
 91		if err != nil {
 92			return err
 93		}
 94	}
 95
 96	// Initialize swap start hook with no-op function
 97	if !poolStore.HasSwapStartHook() {
 98		noopSwapStart := func(cur realm, poolPath string, timestamp int64) {}
 99		err := poolStore.SetSwapStartHook(0, rlm, noopSwapStart)
100		if err != nil {
101			return err
102		}
103	}
104
105	// Initialize swap end hook with no-op function
106	if !poolStore.HasSwapEndHook() {
107		noopSwapEnd := func(cur realm, poolPath string) error {
108			return nil
109		}
110		err := poolStore.SetSwapEndHook(0, rlm, noopSwapEnd)
111		if err != nil {
112			return err
113		}
114	}
115
116	// Initialize tick cross hook with no-op function
117	if !poolStore.HasTickCrossHook() {
118		noopTickCross := func(cur realm, poolPath string, tickId int32, zeroForOne bool, timestamp int64) {}
119		err := poolStore.SetTickCrossHook(0, rlm, noopTickCross)
120		if err != nil {
121			return err
122		}
123	}
124
125	return nil
126}