-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
GSW-807 protocol fees
- Loading branch information
Showing
21 changed files
with
564 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ import ( | |
|
||
"gno.land/r/demo/consts" | ||
|
||
gns "gno.land/r/demo/gns" | ||
"gno.land/r/demo/gns" | ||
) | ||
|
||
var ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package pool | ||
|
||
import ( | ||
"gno.land/p/demo/common" | ||
) | ||
|
||
func (pool *Pool) modifyPosition(params ModifyPositionParams) (PositionInfo, bigint, bigint) { | ||
position := pool.updatePosition( | ||
params.owner, | ||
params.tickLower, | ||
params.tickUpper, | ||
params.liquidityDelta, | ||
pool.slot0.tick, | ||
) | ||
|
||
var amount0, amount1 bigint | ||
|
||
if params.liquidityDelta != 0 { | ||
if pool.slot0.tick < params.tickLower { | ||
amount0 = sqrtPriceMathGetAmount0Delta( | ||
common.TickMathGetSqrtRatioAtTick(params.tickLower), | ||
common.TickMathGetSqrtRatioAtTick(params.tickUpper), | ||
params.liquidityDelta, | ||
) | ||
} else if pool.slot0.tick < params.tickUpper { | ||
liquidityBefore := pool.liquidity | ||
|
||
amount0 = sqrtPriceMathGetAmount0Delta( | ||
pool.slot0.sqrtPriceX96, | ||
common.TickMathGetSqrtRatioAtTick(params.tickUpper), | ||
params.liquidityDelta, | ||
) | ||
|
||
amount1 = sqrtPriceMathGetAmount1Delta( | ||
common.TickMathGetSqrtRatioAtTick(params.tickLower), | ||
pool.slot0.sqrtPriceX96, | ||
params.liquidityDelta, | ||
) | ||
|
||
pool.liquidity = liquidityMathAddDelta(liquidityBefore, params.liquidityDelta) | ||
|
||
} else { | ||
amount1 = sqrtPriceMathGetAmount1Delta( | ||
common.TickMathGetSqrtRatioAtTick(params.tickLower), | ||
common.TickMathGetSqrtRatioAtTick(params.tickUpper), | ||
params.liquidityDelta, | ||
) | ||
} | ||
} | ||
|
||
return position, amount0, amount1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package pool | ||
|
||
import ( | ||
"std" | ||
) | ||
|
||
func (pool *Pool) updatePosition( | ||
owner std.Address, | ||
tickLower int32, | ||
tickUpper int32, | ||
liquidityDelta bigint, | ||
tick int32, | ||
) PositionInfo { | ||
var _feeGrowthGlobal0X128 bigint = pool.feeGrowthGlobal0X128 | ||
var _feeGrowthGlobal1X128 bigint = pool.feeGrowthGlobal1X128 | ||
|
||
var flippedLower, flippedUpper bool | ||
if liquidityDelta != 0 { | ||
flippedLower = pool.tickUpdate( | ||
tickLower, | ||
tick, | ||
liquidityDelta, | ||
_feeGrowthGlobal0X128, | ||
_feeGrowthGlobal1X128, | ||
false, | ||
pool.maxLiquidityPerTick, | ||
) | ||
|
||
flippedUpper = pool.tickUpdate( | ||
tickUpper, | ||
tick, | ||
liquidityDelta, | ||
_feeGrowthGlobal0X128, | ||
_feeGrowthGlobal1X128, | ||
true, | ||
pool.maxLiquidityPerTick, | ||
) | ||
|
||
if flippedLower { | ||
pool.tickBitmapFlipTick(tickLower, pool.tickSpacing) | ||
} | ||
|
||
if flippedUpper { | ||
pool.tickBitmapFlipTick(tickUpper, pool.tickSpacing) | ||
} | ||
} | ||
|
||
// NO LIQ, ONLY BURN 0 | ||
|
||
feeGrowthInside0X128, feeGrowthInside1X128 := pool.tickGetFeeGrowthInside( | ||
tickLower, | ||
tickUpper, | ||
tick, | ||
_feeGrowthGlobal0X128, | ||
_feeGrowthGlobal1X128, | ||
) | ||
|
||
positionKey := positionGetKey(owner, tickLower, tickUpper) | ||
|
||
position := pool.positionUpdateWithKey( | ||
positionKey, | ||
liquidityDelta, | ||
feeGrowthInside0X128, | ||
feeGrowthInside1X128, | ||
) | ||
|
||
if liquidityDelta < 0 { | ||
if flippedLower { | ||
pool.tickClear(tickLower) | ||
} | ||
|
||
if flippedUpper { | ||
pool.tickClear(tickUpper) | ||
} | ||
} | ||
return position | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package pool | ||
|
||
import ( | ||
"std" | ||
|
||
"gno.land/r/demo/consts" | ||
|
||
"gno.land/p/demo/ufmt" | ||
) | ||
|
||
var ( | ||
withdrawalFee = bigint(1) | ||
) | ||
|
||
func HandleWithdrawalFee( | ||
tokenId uint64, | ||
token0Path string, | ||
amount0 bigint, | ||
token1Path string, | ||
amount1 bigint, | ||
) (bigint, bigint) { | ||
requirePrevRealmPath(consts.POSITION_PATH, ufmt.Sprintf("[POOL] withdrawal_fee.gno__HandleWithdrawFee() || expected PrevRealmPath(%s), got %s", consts.POSITION_PATH, PrevRealmPath())) | ||
|
||
if withdrawalFee == 0 { | ||
return amount0, amount1 | ||
} | ||
|
||
feeAmount0 := amount0 * withdrawalFee / bigint(100) | ||
feeAmount1 := amount1 * withdrawalFee / bigint(100) | ||
|
||
ok := transferFromByRegisterCall(token0Path, GetOrigCaller(), consts.FEE_COLLECTOR, uint64(feeAmount0)) | ||
require(ok, ufmt.Sprintf("[POOL] withdrawal_fee.gno__HandleWithdrawFee() || expected transferFromByRegisterCall(%s, %s, %s, %s) == true", token0Path, GetOrigCaller(), consts.FEE_COLLECTOR, feeAmount0)) | ||
|
||
ok = transferFromByRegisterCall(token1Path, GetOrigCaller(), consts.FEE_COLLECTOR, uint64(feeAmount1)) | ||
require(ok, ufmt.Sprintf("[POOL] withdrawal_fee.gno__HandleWithdrawFee() || expected transferFromByRegisterCall(%s, %s, %s, %s) == true", token1Path, GetOrigCaller(), consts.FEE_COLLECTOR, feeAmount1)) | ||
|
||
return amount0 - feeAmount0, amount1 - feeAmount1 | ||
} | ||
|
||
func SetWithdrawalFee(fee bigint) { | ||
// MUST BE ORIGIN CALL | ||
std.AssertOriginCall() | ||
|
||
// MUST BE ADMIN | ||
require(isAdmin(GetOrigCaller()), ufmt.Sprintf("[POOL] withdrawal_fee.gno__SetFeeProtocol() || caller(%s) must be admin", GetOrigCaller())) | ||
|
||
require(fee >= 0, ufmt.Sprintf("[POOL] withdrawal_fee.gno__SetFeeProtocol() || fee(%d) must be >= 0", fee)) | ||
|
||
withdrawalFee = fee | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.