Skip to content

Commit

Permalink
GSW-636 refactor: use custom helper instead of delete
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Dec 14, 2023
1 parent 926014d commit e7af803
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 28 deletions.
13 changes: 11 additions & 2 deletions pool/_GET_pool.gno
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,30 @@ func (pool *Pool) PoolGetTick(x int32) TickInfo {

func (pool *Pool) PoolGetTickFeeGrowthOutside0X128(x int32) bigint {
tick, exist := pool.ticks[x]
require(exist, ufmt.Sprintf("[POOL] _GET_pool.gno__PoolGetTick() || tick(%d) does not exist", x))
require(exist, ufmt.Sprintf("[POOL] _GET_pool.gno__PoolGetTickFeeGrowthOutside0X128() || tick(%d) does not exist", x))
// if !exist {
// return 0
// }

return tick.feeGrowthOutside0X128
}

func (pool *Pool) PoolGetTickFeeGrowthOutside1X128(x int32) bigint {
tick, exist := pool.ticks[x]
require(exist, ufmt.Sprintf("[POOL] _GET_pool.gno__PoolGetTick() || tick(%d) does not exist", x))
require(exist, ufmt.Sprintf("[POOL] _GET_pool.gno__PoolGetTickFeeGrowthOutside1X128() || tick(%d) does not exist", x))
// if !exist {
// return 0
// }

return tick.feeGrowthOutside1X128
}

func (pool *Pool) PoolGetTickBitmap(x int16) bigint {
tickBitmap, exist := pool.tickBitmaps[x]
require(exist, ufmt.Sprintf("[POOL] _GET_pool.gno__PoolGetTickBitmap() || tickBitmap(%d) does not exist", x))
// if !exist {
// return 0
// }

return tickBitmap
}
Expand Down
17 changes: 16 additions & 1 deletion pool/tick.gno
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (pool *Pool) tickUpdate(
}

func (pool *Pool) tickClear(tick int32) {
delete(pool.ticks, tick)
pool.ticks = deleteFromPoolTicks(pool.ticks, tick)
}

func (pool *Pool) tickCross(
Expand All @@ -114,3 +114,18 @@ func (pool *Pool) tickCross(
liquidityNet := info.liquidityNet
return liquidityNet
}

func deleteFromPoolTicks(m map[int32]TickInfo, key int32) map[int32]TickInfo {
if _, ok := m[key]; ok {
newMap := make(map[int32]TickInfo)
for k, v := range m {
if k != key {
newMap[k] = v
}
}

return newMap
}

return m
}
2 changes: 1 addition & 1 deletion position/_TEST_position_api_test.gnoa
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestApiGetPositions(t *testing.T) {
func TestApiGetPosition(t *testing.T) {
// burn 0 to collect swap fee
std.TestSetOrigCaller(test1)
DecreaseLiquidity(1, 0, 0, 0, 99999999999)
DecreaseLiquidity(1, 0, 99999999999)

gps := ApiGetPosition(uint64(1))
jsonStr := gjson.Parse(gps)
Expand Down
2 changes: 0 additions & 2 deletions position/_TEST_position_native_test.gnoa
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ func TestDecreaseLiquidity(t *testing.T) {
tAmount0, tAmount1 := DecreaseLiquidity(
bigint(1),
tTargetLiquidity,
bigint(1),
bigint(1),
bigint(1234567890),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ func TestDecreaseLiquidity(t *testing.T) {
tAmount0, tAmount1 := DecreaseLiquidity(
bigint(1),
tTargetLiquidity,
bigint(1),
bigint(1),
bigint(1234567890),
)

Expand Down Expand Up @@ -290,8 +288,6 @@ func TestNFTBurn(t *testing.T) {
DecreaseLiquidity(
tTokenId,
tPositionLiquidity, // remove all liquidity
bigint(1),
bigint(1),
bigint(1234567890),
)
// didn't collect yet => burn will panic
Expand Down
17 changes: 16 additions & 1 deletion position/position.gno
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,25 @@ func burn(tokenId uint64) {

require(position.isClear(), ufmt.Sprintf("[POSITION] position.gno__burn() || position(tokenId:%d) isn't clear(has something)", tokenId))

delete(positions, tokenId)
positions = deleteFromPositions(positions, tokenId)
gnft.Burn(tid(tokenId))
}

func isAuthorizedForToken(tokenId uint64) {
require(isApprovedOrOwner(PrevRealmAddr(), tokenId), ufmt.Sprintf("[POSITION] position.gno__isAuthorizedForToken() || caller(%s) is not approved or owner of tokenId(%d)", PrevRealmAddr(), tokenId))
}

func deleteFromPositions(m map[uint64]Position, key uint64) map[uint64]Position {
if _, ok := m[key]; ok {
newMap := make(map[uint64]Position)
for k, v := range m {
if k != key {
newMap[k] = v
}
}

return newMap
}

return m
}
64 changes: 47 additions & 17 deletions staker/staker.gno
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ var (
poolTiers map[string]uint8 = make(map[string]uint8) // poolPath -> tier
)

func init() {
// init pool tiers
// tier 1
poolTiers["gno.land/r/demo/bar:gnot:100"] = 1 // DEV

// tier 2
poolTiers["GNS/USDT_500"] = 2
poolTiers["ATOM/GNS_500"] = 2

// tier 3
poolTiers["ATOM/GNOT_500"] = 3
poolTiers["ATOM/USDT_500"] = 3
poolTiers["ATOM/WETH_500"] = 3
}
// func init() {
// // init pool tiers
// // tier 1
// poolTiers["GNS/GNOT"] = 1

// // tier 2
// poolTiers["GNS/USDT_500"] = 2
// poolTiers["ATOM/GNS_500"] = 2

// // tier 3
// poolTiers["ATOM/GNOT_500"] = 3
// poolTiers["ATOM/USDT_500"] = 3
// poolTiers["ATOM/WETH_500"] = 3
// }

func CreateExternalIncentive(
targetPoolPath string,
Expand Down Expand Up @@ -166,9 +166,9 @@ func UnstakeToken(
gns.TransferFrom(a2u(INTERNAL_REWARD_ACCOUNT), a2u(deposit.owner), uint64(internalGNS))

// unstaked status
delete(deposits, tokenId)
deposits = deleteFromDeposits(deposits, tokenId)

// transfer NFT ownership to unstaker
// transfer NFT ownership to origin owner
gnft.TransferFrom(a2u(GetOrigPkgAddr()), a2u(deposit.owner), tid(tokenId))
}

Expand All @@ -187,7 +187,7 @@ func EndExternalIncentive(refundee, targetPoolPath, rewardToken string) {

transferByRegisterCall(incentive.rewardToken, incentive.refundee, uint64(refund))

delete(incentives, incentiveId)
incentives = deleteFromIncentives(incentives, incentiveId)
for i, v := range poolIncentives[targetPoolPath] {
if v == incentiveId {
poolIncentives[targetPoolPath] = append(poolIncentives[targetPoolPath][:i], poolIncentives[targetPoolPath][i+1:]...)
Expand All @@ -204,3 +204,33 @@ func transferDeposit(tokenId uint64, to std.Address) {
// transfer NFT ownership
gnft.TransferFrom(a2u(owner), a2u(to), tid(tokenId))
}

func deleteFromDeposits(m map[uint64]Deposit, key uint64) map[uint64]Deposit {
if _, ok := m[key]; ok {
newMap := make(map[uint64]Deposit)
for k, v := range m {
if k != key {
newMap[k] = v
}
}

return newMap
}

return m
}

func deleteFromIncentives(m map[string]Incentive, key string) map[string]Incentive {
if _, ok := m[key]; ok {
newMap := make(map[string]Incentive)
for k, v := range m {
if k != key {
newMap[k] = v
}
}

return newMap
}

return m
}

0 comments on commit e7af803

Please sign in to comment.