Search Apps Documentation Source Content File Folder Download Copy Actions Download

position.gno

3.62 Kb · 143 lines
  1package position
  2
  3import bptree "gno.land/p/nt/bptree/v0"
  4
  5// Position represents a liquidity position in a pool.
  6// Each position tracks the amount of liquidity, fee growth, and tokens owed to the position owner.
  7// All uint256 fields are stored as decimal strings to reduce realm object overhead.
  8type Position struct {
  9	operator  address // address that is approved for spending this token
 10	poolKey   string  // poolPath of the pool which this has lp token
 11	tickLower int32   // the lower tick of the position, bounds are included
 12	tickUpper int32   // the upper tick of the position
 13	liquidity string  // liquidity of the position
 14
 15	// fee growth of the aggregate position as of the last action on the individual position
 16	feeGrowthInside0LastX128 string
 17	feeGrowthInside1LastX128 string
 18
 19	// how many uncollected tokens are owed to the position, as of the last computation
 20	tokensOwed0 int64
 21	tokensOwed1 int64
 22
 23	burned bool // whether the position has been burned (we don't burn the NFT, just mark as burned)
 24}
 25
 26func (p *Position) PoolKey() string {
 27	return p.poolKey
 28}
 29
 30func (p *Position) SetPoolKey(poolKey string) {
 31	p.poolKey = poolKey
 32}
 33
 34func (p *Position) Liquidity() string {
 35	return p.liquidity
 36}
 37
 38func (p *Position) SetLiquidity(liquidity string) {
 39	p.liquidity = liquidity
 40}
 41
 42func (p *Position) TickLower() int32 {
 43	return p.tickLower
 44}
 45
 46func (p *Position) SetTickLower(tickLower int32) {
 47	p.tickLower = tickLower
 48}
 49
 50func (p *Position) TickUpper() int32 {
 51	return p.tickUpper
 52}
 53
 54func (p *Position) SetTickUpper(tickUpper int32) {
 55	p.tickUpper = tickUpper
 56}
 57
 58func (p *Position) TokensOwed0() int64 {
 59	return p.tokensOwed0
 60}
 61
 62func (p *Position) SetTokensOwed0(tokensOwed0 int64) {
 63	p.tokensOwed0 = tokensOwed0
 64}
 65
 66func (p *Position) TokensOwed1() int64 {
 67	return p.tokensOwed1
 68}
 69
 70func (p *Position) SetTokensOwed1(tokensOwed1 int64) {
 71	p.tokensOwed1 = tokensOwed1
 72}
 73
 74func (p *Position) FeeGrowthInside0LastX128() string {
 75	return p.feeGrowthInside0LastX128
 76}
 77
 78func (p *Position) SetFeeGrowthInside0LastX128(feeGrowthInside0LastX128 string) {
 79	p.feeGrowthInside0LastX128 = feeGrowthInside0LastX128
 80}
 81
 82func (p *Position) FeeGrowthInside1LastX128() string {
 83	return p.feeGrowthInside1LastX128
 84}
 85
 86func (p *Position) SetFeeGrowthInside1LastX128(feeGrowthInside1LastX128 string) {
 87	p.feeGrowthInside1LastX128 = feeGrowthInside1LastX128
 88}
 89
 90func (p *Position) Burned() bool {
 91	return p.burned
 92}
 93
 94func (p *Position) SetBurned(burned bool) {
 95	p.burned = burned
 96}
 97
 98func (p *Position) Operator() address {
 99	return p.operator
100}
101
102func (p *Position) SetOperator(operator address) {
103	p.operator = operator
104}
105
106// isClear reports whether the position is empty
107func (p *Position) IsClear() bool {
108	return isZeroStr(p.liquidity) && p.tokensOwed0 == 0 && p.tokensOwed1 == 0
109}
110
111func isZeroStr(s string) bool {
112	return s == "" || s == "0"
113}
114
115func NewPosition(
116	poolKey string,
117	tickLower int32,
118	tickUpper int32,
119	liquidity string,
120	feeGrowthInside0LastX128, feeGrowthInside1LastX128 string,
121	tokensOwed0, tokensOwed1 int64,
122	burned bool,
123	operator address,
124) *Position {
125	return &Position{
126		poolKey:                  poolKey,
127		tickLower:                tickLower,
128		tickUpper:                tickUpper,
129		liquidity:                liquidity,
130		feeGrowthInside0LastX128: feeGrowthInside0LastX128,
131		feeGrowthInside1LastX128: feeGrowthInside1LastX128,
132		tokensOwed0:              tokensOwed0,
133		tokensOwed1:              tokensOwed1,
134		burned:                   burned,
135		operator:                 operator,
136	}
137}
138
139// NewPositionsTree allocates the positions BP-tree
140// under /r/gnoswap/position's realm context (the realm that declares Position).
141func NewPositionsTree() *bptree.BPTree {
142	return bptree.NewBPTreeN(16)
143}