Skip to content
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

Handle problems of sending coins to the reserve account #364

Closed
2 of 4 tasks
hallazzang opened this issue May 27, 2021 · 4 comments · Fixed by #369
Closed
2 of 4 tasks

Handle problems of sending coins to the reserve account #364

hallazzang opened this issue May 27, 2021 · 4 comments · Fixed by #369
Assignees
Labels
bug Something isn't working

Comments

@hallazzang
Copy link
Contributor

Summary of Bug

When someone sends coins to the reserve account of a pool, it can cause some problems including:

  • A pool with zero amount balance in one side of the reserve coin, leading to incorrect pool price calculation
    • By withdrawing all coins from the pool and then sending only one reserve coin directly to the reserve account
  • A panic in deposit logic, when calculating the reserve coin ratio(divide by zero error with a pool described above)
  • ...

Version

https://github.com/tendermint/liquidity/releases/tag/v1.2.5

Steps to Reproduce

Running this test causes a panic:

func TestSwapHalfZeroReserve(t *testing.T) {
	simapp, ctx := createTestInput()
	params := simapp.LiquidityKeeper.GetParams(ctx)
	pool, addr, err := createPool(simapp, ctx, sdk.NewInt(1000000), sdk.NewInt(1000000), DenomX, DenomY)
	require.NoError(t, err)
	pc := simapp.BankKeeper.GetBalance(ctx, addr, pool.PoolCoinDenom)
	_, err = simapp.LiquidityKeeper.WithdrawLiquidityPoolToBatch(ctx, types.NewMsgWithdrawWithinBatch(addr, pool.Id, pc))
	require.NoError(t, err)
	liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper)
	liquidity.EndBlocker(ctx, simapp.LiquidityKeeper)
	require.True(t, simapp.BankKeeper.GetBalance(ctx, pool.GetReserveAccount(), DenomX).IsZero())
	require.True(t, simapp.BankKeeper.GetBalance(ctx, pool.GetReserveAccount(), DenomY).IsZero())
	err = simapp.BankKeeper.SendCoins(ctx, addr, pool.GetReserveAccount(), sdk.NewCoins(sdk.NewInt64Coin(DenomX, 10000)))
	require.NoError(t, err)
	_, err = simapp.LiquidityKeeper.SwapLiquidityPoolToBatch(ctx,
		types.NewMsgSwapWithinBatch(
			addr, pool.Id, types.DefaultSwapTypeId, sdk.NewInt64Coin(DenomX, 1000), DenomY, sdk.MustNewDecFromStr("1.0"), params.SwapFeeRate), 0)
	require.NoError(t, err)
	liquidity.BeginBlocker(ctx, simapp.LiquidityKeeper)
	liquidity.EndBlocker(ctx, simapp.LiquidityKeeper)
}

For Admin Use

  • Not duplicate issue
  • Appropriate labels applied
  • Appropriate contributors tagged
  • Contributor assigned/self-assigned
@hallazzang hallazzang added bug Something isn't working help wanted Extra attention is needed labels May 27, 2021
@hallazzang
Copy link
Contributor Author

hallazzang commented May 27, 2021

If we define a depleted pool as a pool with zero pool coin supply, not a pool with zero reserve, we can prevent chain to panic in the mentioned situations.

I'll work on it.

@migueldingli1997
Copy link

Something that can be considered is to make use of module accounts that are blacklisted (via the bank module) from receiving tokens, but this is a greater change compared to the above solution.

@hallazzang hallazzang removed the help wanted Extra attention is needed label May 27, 2021
@jaybxyz
Copy link
Contributor

jaybxyz commented May 27, 2021

@migueldingli1997 We thought about that approach at first, but since ReserveAcc is created dynamically when there is new pool, we think implementation requires a greater change to blockedAddrs. We're still exploring this approach to see if there is workaround.

@migueldingli1997
Copy link

Yep that's correct. A workaround to that then is to use just one module account (created at genesis and with the appropriate restrictions) but then you will have to keep track of the reserve balances for each pool in the liquidity module, rather than leaving it up to the bank module. I understand that this might not be desirable however.

hallazzang added a commit to b-harvest/liquidity that referenced this issue May 27, 2021
also change Mul/QuoTruncate to Mul/Quo.

fixes tendermint#364
dongsam pushed a commit that referenced this issue Jun 3, 2021
* keep zero reserve coins from GetReserveCoins

* treat pool with zero pool coin supply as depleted

also change Mul/QuoTruncate to Mul/Quo.

fixes #364

* rollback change of Quo/MulTruncate to Quo/Mul

this should be done in #380
dongsam pushed a commit that referenced this issue Jun 8, 2021
* keep zero reserve coins from GetReserveCoins

* treat pool with zero pool coin supply as depleted

also change Mul/QuoTruncate to Mul/Quo.

fixes #364

* rollback change of Quo/MulTruncate to Quo/Mul

this should be done in #380

* treat a pool with zero reserve coin as depleted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants