-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(staker): add in-range staked liquidity tracker #438
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,8 @@ var ( | |
/* common */ | ||
// deposits stores deposit information for each tokenId | ||
deposits map[uint64]Deposit = make(map[uint64]Deposit) | ||
|
||
// depositOperations map[uint64][]DepositOperation = make(map[uint64][]DepositOperation) | ||
) | ||
|
||
const ( | ||
|
@@ -135,6 +137,8 @@ func StakeToken(tokenId uint64) (string, string, string) { | |
// after transfer, set caller(user) as position operator (to collect fee and reward) | ||
pn.SetPositionOperator(tokenId, caller) | ||
|
||
modifyStakerPoolInfo(poolPath, tokenId, liquidity) | ||
|
||
token0Amount, token1Amount := getTokenPairBalanceFromPosition(tokenId) | ||
|
||
prevAddr, prevRealm := getPrev() | ||
|
@@ -349,6 +353,8 @@ func UnstakeToken(tokenId uint64, unwrapResult bool) (string, string, string) { | |
poolPath := pn.PositionGetPositionPoolKey(tokenId) | ||
token0Amount, token1Amount := getTokenPairBalanceFromPosition(tokenId) | ||
|
||
modifyStakerPoolInfo(poolPath, tokenId, i256.Zero().Neg(liquidity)) | ||
|
||
prevAddr, prevRealm := getPrev() | ||
std.Emit( | ||
"UnstakeToken", | ||
|
@@ -744,3 +750,78 @@ func getIncentives() map[string]ExternalIncentive { | |
func getPoolIncentives() map[string][]string { | ||
return poolIncentives | ||
} | ||
|
||
|
||
// TODO: separate into different file | ||
|
||
type StakerPoolInfo struct { | ||
//totalStakedLiquidity *u256.Uint | ||
totalStakedLiquidityInRange *u256.Uint | ||
} | ||
|
||
type StakerTickInfo struct { | ||
liquidityInRangeDelta *u256.Uint | ||
} | ||
|
||
var ( | ||
stakerPoolInfo = make(map[string]StakerPoolInfo) | ||
r3v4s marked this conversation as resolved.
Show resolved
Hide resolved
|
||
stakerTickInfo = make(map[int32]StakerTickInfo) | ||
) | ||
|
||
func modifyStakerPoolInfo(poolPath string, tokenId uint64, liquidity *u256.Uint) { | ||
// update staker side pool info | ||
pool, ok := stakerPoolInfo[poolPath] | ||
if !ok { | ||
pool = StakerPoolInfo{ | ||
totalStakedLiquidity: u256.Zero(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The StakerPoolInfo type declaration is commented out. |
||
totalStakedLiquidityInRange: u256.Zero(), | ||
} | ||
} | ||
//pool.totalStakedLiquidity = liquidityMathAddDelta(pool.totalStakedLiquidity, liquidity) | ||
if pn.PositionIsInRange(tokenId) { | ||
pool.totalStakedLiquidityInRange = liquidityMathAddDelta(pool.totalStakedLiquidityInRange, liquidity) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In order to calculate the reward correctly, we need to distinguish when tokenId is inrange and when it is not. |
||
} | ||
stakerPoolInfo[poolPath] = pool | ||
|
||
// update staker side tick info | ||
lowerTick, ok := stakerTickInfo[pn.PositionGetPositionTickLower(tokenId)] | ||
if !ok { | ||
lowerTick = StakerTickInfo{ | ||
liquidityInRangeDelta: u256.Zero(), | ||
} | ||
} | ||
lowerTick.liquidityInRangeDelta = liquidityMathAddDelta(lowerTick.liquidityInRangeDelta, liquidity) | ||
stakerTickInfo[pn.PositionGetPositionTickLower(tokenId)] = lowerTick | ||
|
||
upperTick, ok := stakerTickInfo[pn.PositionGetPositionTickUpper(tokenId)] | ||
if !ok { | ||
upperTick = StakerTickInfo{ | ||
liquidityInRangeDelta: u256.Zero(), | ||
} | ||
} | ||
upperTick.liquidityInRangeDelta = liquidityMathAddDelta(upperTick.liquidityInRangeDelta, i256.Zero().Neg(liquidity)) | ||
stakerTickInfo[pn.PositionGetPositionTickUpper(tokenId)] = upperTick | ||
} | ||
|
||
func tickCrossHook(poolPath string, tickId int32, zeroForOne bool) { | ||
tick, ok := stakerTickInfo[tickId] | ||
if !ok { | ||
return | ||
} | ||
|
||
pool, ok := stakerPoolInfo[poolPath] | ||
if !ok { | ||
panic(addDetailToError( | ||
errDataNotFound, | ||
ufmt.Sprintf("staker.gno__tickCrossHook() || poolPath(%s) not found", poolPath), | ||
)) | ||
} | ||
|
||
liquidityInRangeDelta := tick.liquidityInRangeDelta | ||
if zeroForOne { | ||
liquidityInRangeDelta = i256.Zero().Neg(liquidityInRangeDelta) | ||
} | ||
pool.totalStakedLiquidityInRange = liquidityMathAddDelta(pool.totalStakedLiquidityInRange, liquidityInRangeDelta) | ||
|
||
stakerPoolInfo[poolPath] = pool | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you stack tickNext at this point, you're stacking the value before the change. If you want to stack the history of the change with tickTransition, it's better to record the updated tick in newState.tick.